xref: /freebsd/sys/fs/msdosfs/msdosfs_vfsops.c (revision 2b833162)
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;	/* XXX not used? */
704 
705 	if (pmp->pm_fatmask == 0) {
706 		if (pmp->pm_maxcluster <= ((CLUST_RSRVD - CLUST_FIRST) &
707 		    FAT12_MASK)) {
708 			/*
709 			 * This will usually be a floppy disk. This size makes
710 			 * sure that one FAT entry will not be split across
711 			 * multiple blocks.
712 			 */
713 			pmp->pm_fatmask = FAT12_MASK;
714 			pmp->pm_fatmult = 3;
715 			pmp->pm_fatdiv = 2;
716 		} else {
717 			pmp->pm_fatmask = FAT16_MASK;
718 			pmp->pm_fatmult = 2;
719 			pmp->pm_fatdiv = 1;
720 		}
721 	}
722 
723 	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
724 	if (pmp->pm_maxcluster >= clusters) {
725 #ifdef MSDOSFS_DEBUG
726 		printf("Warning: number of clusters (%ld) exceeds FAT "
727 		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
728 #endif
729 		pmp->pm_maxcluster = clusters - 1;
730 	}
731 
732 	if (FAT12(pmp))
733 		pmp->pm_fatblocksize = 3 * 512;
734 	else
735 		pmp->pm_fatblocksize = PAGE_SIZE;
736 	pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
737 	    pmp->pm_BytesPerSec);
738 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
739 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
740 
741 	/*
742 	 * Compute mask and shift value for isolating cluster relative byte
743 	 * offsets and cluster numbers from a file offset.
744 	 */
745 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
746 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
747 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
748 
749 	/*
750 	 * Check for valid cluster size
751 	 * must be a power of 2
752 	 */
753 	if ((pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) != 0) {
754 		error = EINVAL;
755 		goto error_exit;
756 	}
757 
758 	/*
759 	 * Release the bootsector buffer.
760 	 */
761 	brelse(bp);
762 	bp = NULL;
763 
764 	/*
765 	 * Check the fsinfo sector if we have one.  Silently fix up our
766 	 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
767 	 * or too large.  Ignore fp->fsinfree for now, since we need to
768 	 * read the entire FAT anyway to fill the inuse map.
769 	 */
770 	if (pmp->pm_fsinfo) {
771 		struct fsinfo *fp;
772 
773 		if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
774 		    NOCRED, &bp)) != 0)
775 			goto error_exit;
776 		fp = (struct fsinfo *)bp->b_data;
777 		if (!bcmp(fp->fsisig1, "RRaA", 4) &&
778 		    !bcmp(fp->fsisig2, "rrAa", 4) &&
779 		    !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
780 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
781 			if (pmp->pm_nxtfree > pmp->pm_maxcluster)
782 				pmp->pm_nxtfree = CLUST_FIRST;
783 		} else
784 			pmp->pm_fsinfo = 0;
785 		brelse(bp);
786 		bp = NULL;
787 	}
788 
789 	/*
790 	 * Finish initializing pmp->pm_nxtfree (just in case the first few
791 	 * sectors aren't properly reserved in the FAT).  This completes
792 	 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
793 	 * value if there is no fsinfo.  We will use pmp->pm_nxtfree
794 	 * internally even if there is no fsinfo.
795 	 */
796 	if (pmp->pm_nxtfree < CLUST_FIRST)
797 		pmp->pm_nxtfree = CLUST_FIRST;
798 
799 	/*
800 	 * Allocate memory for the bitmap of allocated clusters, and then
801 	 * fill it in.
802 	 */
803 	pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1,
804 	    N_INUSEBITS) * sizeof(*pmp->pm_inusemap), M_MSDOSFSFAT, M_WAITOK);
805 
806 	/*
807 	 * fillinusemap() needs pm_devvp.
808 	 */
809 	pmp->pm_devvp = devvp;
810 	pmp->pm_odevvp = odevvp;
811 	pmp->pm_dev = dev;
812 
813 	/*
814 	 * Have the inuse map filled in.
815 	 */
816 	MSDOSFS_LOCK_MP(pmp);
817 	error = fillinusemap(pmp);
818 	MSDOSFS_UNLOCK_MP(pmp);
819 	if (error != 0)
820 		goto error_exit;
821 
822 	/*
823 	 * If they want FAT updates to be synchronous then let them suffer
824 	 * the performance degradation in exchange for the on disk copy of
825 	 * the FAT being correct just about all the time.  I suppose this
826 	 * would be a good thing to turn on if the kernel is still flakey.
827 	 */
828 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
829 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
830 
831 	/*
832 	 * Finish up.
833 	 */
834 	if (ronly)
835 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
836 	else {
837 		if ((error = markvoldirty(pmp, 1)) != 0)
838 			goto error_exit;
839 		pmp->pm_fmod = 1;
840 	}
841 
842 	if (FAT32(pmp)) {
843 		pmp->pm_rootdirfree = 0;
844 	} else {
845 		pmp->pm_rootdirfree = rootdir_free(pmp);
846 		if (pmp->pm_rootdirfree < 0)
847 			goto error_exit;
848 	}
849 
850 	mp->mnt_data =  pmp;
851 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
852 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
853 	MNT_ILOCK(mp);
854 	mp->mnt_flag |= MNT_LOCAL;
855 	mp->mnt_kern_flag |= MNTK_USES_BCACHE | MNTK_NO_IOPF;
856 	MNT_IUNLOCK(mp);
857 
858 	return (0);
859 
860 error_exit:
861 	if (bp != NULL)
862 		brelse(bp);
863 	if (cp != NULL) {
864 		g_topology_lock();
865 		g_vfs_close(cp);
866 		g_topology_unlock();
867 	}
868 	if (pmp != NULL) {
869 		lockdestroy(&pmp->pm_fatlock);
870 		lockdestroy(&pmp->pm_checkpath_lock);
871 		free(pmp->pm_inusemap, M_MSDOSFSFAT);
872 		free(pmp, M_MSDOSFSMNT);
873 		mp->mnt_data = NULL;
874 	}
875 	BO_LOCK(&odevvp->v_bufobj);
876 	odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
877 	BO_UNLOCK(&odevvp->v_bufobj);
878 	atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
879 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
880 	mntfs_freevp(devvp);
881 	dev_rel(dev);
882 	return (error);
883 }
884 
885 /*
886  * Unmount the filesystem described by mp.
887  */
888 static int
889 msdosfs_unmount(struct mount *mp, int mntflags)
890 {
891 	struct msdosfsmount *pmp;
892 	int error, flags;
893 	bool susp;
894 
895 	error = flags = 0;
896 	pmp = VFSTOMSDOSFS(mp);
897 	susp = (pmp->pm_flags & MSDOSFSMNT_RONLY) == 0;
898 
899 	if (susp) {
900 		error = vfs_write_suspend_umnt(mp);
901 		if (error != 0)
902 			return (error);
903 	}
904 
905 	if ((mntflags & MNT_FORCE) != 0)
906 		flags |= FORCECLOSE;
907 	error = vflush(mp, 0, flags, curthread);
908 	if (error != 0 && error != ENXIO) {
909 		if (susp)
910 			vfs_write_resume(mp, VR_START_WRITE);
911 		return (error);
912 	}
913 	if (susp) {
914 		error = markvoldirty(pmp, 0);
915 		if (error != 0 && error != ENXIO) {
916 			if (susp)
917 				vfs_write_resume(mp, VR_START_WRITE);
918 			(void)markvoldirty(pmp, 1);
919 			return (error);
920 		}
921 	}
922 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
923 		if (pmp->pm_w2u)
924 			msdosfs_iconv->close(pmp->pm_w2u);
925 		if (pmp->pm_u2w)
926 			msdosfs_iconv->close(pmp->pm_u2w);
927 		if (pmp->pm_d2u)
928 			msdosfs_iconv->close(pmp->pm_d2u);
929 		if (pmp->pm_u2d)
930 			msdosfs_iconv->close(pmp->pm_u2d);
931 	}
932 
933 #ifdef MSDOSFS_DEBUG
934 	{
935 		struct vnode *vp = pmp->pm_devvp;
936 		struct bufobj *bo;
937 
938 		bo = &vp->v_bufobj;
939 		BO_LOCK(bo);
940 		VI_LOCK(vp);
941 		vn_printf(vp,
942 		    "msdosfs_umount(): just before calling VOP_CLOSE()\n");
943 		printf("freef %p, freeb %p, mount %p\n",
944 		    TAILQ_NEXT(vp, v_vnodelist), vp->v_vnodelist.tqe_prev,
945 		    vp->v_mount);
946 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
947 		    TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
948 		    TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
949 		    vp->v_bufobj.bo_numoutput, vp->v_type);
950 		VI_UNLOCK(vp);
951 		BO_UNLOCK(bo);
952 	}
953 #endif
954 	if (susp)
955 		vfs_write_resume(mp, VR_START_WRITE);
956 
957 	vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
958 	g_topology_lock();
959 	g_vfs_close(pmp->pm_cp);
960 	g_topology_unlock();
961 	BO_LOCK(&pmp->pm_odevvp->v_bufobj);
962 	pmp->pm_odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
963 	BO_UNLOCK(&pmp->pm_odevvp->v_bufobj);
964 	atomic_store_rel_ptr((uintptr_t *)&pmp->pm_dev->si_mountpt, 0);
965 	mntfs_freevp(pmp->pm_devvp);
966 	vrele(pmp->pm_odevvp);
967 	dev_rel(pmp->pm_dev);
968 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
969 	lockdestroy(&pmp->pm_fatlock);
970 	lockdestroy(&pmp->pm_checkpath_lock);
971 	free(pmp, M_MSDOSFSMNT);
972 	mp->mnt_data = NULL;
973 	return (error);
974 }
975 
976 static void
977 msdosfs_remount_ro(void *arg, int pending)
978 {
979 	struct msdosfsmount *pmp;
980 	int error;
981 
982 	pmp = arg;
983 
984 	MSDOSFS_LOCK_MP(pmp);
985 	if ((pmp->pm_flags & MSDOSFS_ERR_RO) != 0) {
986 		while ((pmp->pm_flags & MSDOSFS_ERR_RO) != 0)
987 			msleep(&pmp->pm_flags, &pmp->pm_fatlock, PVFS,
988 			    "msdoserrro", hz);
989 	} else if ((pmp->pm_mountp->mnt_flag & MNT_RDONLY) == 0) {
990 		pmp->pm_flags |= MSDOSFS_ERR_RO;
991 		MSDOSFS_UNLOCK_MP(pmp);
992 		printf("%s: remounting read-only due to corruption\n",
993 		    pmp->pm_mountp->mnt_stat.f_mntfromname);
994 		error = vfs_remount_ro(pmp->pm_mountp);
995 		if (error != 0)
996 			printf("%s: remounting read-only failed: error %d\n",
997 			    pmp->pm_mountp->mnt_stat.f_mntfromname, error);
998 		else
999 			printf("remounted %s read-only\n",
1000 			    pmp->pm_mountp->mnt_stat.f_mntfromname);
1001 		MSDOSFS_LOCK_MP(pmp);
1002 		pmp->pm_flags &= ~MSDOSFS_ERR_RO;
1003 		wakeup(&pmp->pm_flags);
1004 	}
1005 	MSDOSFS_UNLOCK_MP(pmp);
1006 
1007 	vfs_unbusy(pmp->pm_mountp);
1008 }
1009 
1010 void
1011 msdosfs_integrity_error(struct msdosfsmount *pmp)
1012 {
1013 	int error;
1014 
1015 	error = vfs_busy(pmp->pm_mountp, MBF_NOWAIT);
1016 	if (error == 0)
1017 		taskqueue_enqueue(taskqueue_thread, &pmp->pm_rw2ro_task);
1018 	else
1019 		printf("%s: integrity error busying failed, error %d\n",
1020 		    pmp->pm_mountp->mnt_stat.f_mntfromname, error);
1021 }
1022 
1023 static int
1024 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp)
1025 {
1026 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1027 	struct denode *ndep;
1028 	int error;
1029 
1030 #ifdef MSDOSFS_DEBUG
1031 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
1032 #endif
1033 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, LK_EXCLUSIVE, &ndep);
1034 	if (error)
1035 		return (error);
1036 	*vpp = DETOV(ndep);
1037 	return (0);
1038 }
1039 
1040 static int
1041 msdosfs_statfs(struct mount *mp, struct statfs *sbp)
1042 {
1043 	struct msdosfsmount *pmp;
1044 
1045 	pmp = VFSTOMSDOSFS(mp);
1046 	sbp->f_bsize = pmp->pm_bpcluster;
1047 	sbp->f_iosize = pmp->pm_bpcluster;
1048 	sbp->f_blocks = pmp->pm_maxcluster + 1;
1049 	sbp->f_bfree = pmp->pm_freeclustercount;
1050 	sbp->f_bavail = pmp->pm_freeclustercount;
1051 	sbp->f_files =	howmany(pmp->pm_rootdirsize * DEV_BSIZE,
1052 				sizeof(struct direntry));
1053 	sbp->f_ffree = pmp->pm_rootdirfree;
1054 	return (0);
1055 }
1056 
1057 /*
1058  * If we have an FSInfo block, update it.
1059  */
1060 static int
1061 msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
1062 {
1063 	struct fsinfo *fp;
1064 	struct buf *bp;
1065 	int error;
1066 
1067 	MSDOSFS_LOCK_MP(pmp);
1068 	if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
1069 		error = 0;
1070 		goto unlock;
1071 	}
1072 	error = bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
1073 	    NOCRED, &bp);
1074 	if (error != 0) {
1075 		goto unlock;
1076 	}
1077 	fp = (struct fsinfo *)bp->b_data;
1078 	putulong(fp->fsinfree, pmp->pm_freeclustercount);
1079 	putulong(fp->fsinxtfree, pmp->pm_nxtfree);
1080 	pmp->pm_flags &= ~MSDOSFS_FSIMOD;
1081 	if (waitfor == MNT_WAIT)
1082 		error = bwrite(bp);
1083 	else
1084 		bawrite(bp);
1085 unlock:
1086 	MSDOSFS_UNLOCK_MP(pmp);
1087 	return (error);
1088 }
1089 
1090 static int
1091 msdosfs_sync(struct mount *mp, int waitfor)
1092 {
1093 	struct vnode *vp, *nvp;
1094 	struct thread *td;
1095 	struct denode *dep;
1096 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1097 	int error, allerror = 0;
1098 
1099 	td = curthread;
1100 
1101 	/*
1102 	 * If we ever switch to not updating all of the FATs all the time,
1103 	 * this would be the place to update them from the first one.
1104 	 */
1105 	if (pmp->pm_fmod != 0) {
1106 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1107 			panic("msdosfs_sync: rofs mod");
1108 		else {
1109 			/* update FATs here */
1110 		}
1111 	}
1112 	/*
1113 	 * Write back each (modified) denode.
1114 	 */
1115 loop:
1116 	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
1117 		if (vp->v_type == VNON) {
1118 			VI_UNLOCK(vp);
1119 			continue;
1120 		}
1121 		dep = VTODE(vp);
1122 		if ((dep->de_flag &
1123 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
1124 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
1125 		    waitfor == MNT_LAZY)) {
1126 			VI_UNLOCK(vp);
1127 			continue;
1128 		}
1129 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1130 		if (error) {
1131 			if (error == ENOENT) {
1132 				MNT_VNODE_FOREACH_ALL_ABORT(mp, nvp);
1133 				goto loop;
1134 			}
1135 			continue;
1136 		}
1137 		error = VOP_FSYNC(vp, waitfor, td);
1138 		if (error)
1139 			allerror = error;
1140 		vput(vp);
1141 	}
1142 
1143 	/*
1144 	 * Flush filesystem control info.
1145 	 */
1146 	if (waitfor != MNT_LAZY) {
1147 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
1148 		error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
1149 		if (error)
1150 			allerror = error;
1151 		VOP_UNLOCK(pmp->pm_devvp);
1152 	}
1153 
1154 	error = msdosfs_fsiflush(pmp, waitfor);
1155 	if (error != 0)
1156 		allerror = error;
1157 
1158 	if (allerror == 0 && waitfor == MNT_SUSPEND) {
1159 		MNT_ILOCK(mp);
1160 		mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1161 		MNT_IUNLOCK(mp);
1162 	}
1163 	return (allerror);
1164 }
1165 
1166 static int
1167 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1168 {
1169 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1170 	struct defid *defhp = (struct defid *) fhp;
1171 	struct denode *dep;
1172 	int error;
1173 
1174 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs,
1175 	    LK_EXCLUSIVE, &dep);
1176 	if (error) {
1177 		*vpp = NULLVP;
1178 		return (error);
1179 	}
1180 	*vpp = DETOV(dep);
1181 	vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
1182 	return (0);
1183 }
1184 
1185 static struct vfsops msdosfs_vfsops = {
1186 	.vfs_fhtovp =		msdosfs_fhtovp,
1187 	.vfs_mount =		msdosfs_mount,
1188 	.vfs_cmount =		msdosfs_cmount,
1189 	.vfs_root =		msdosfs_root,
1190 	.vfs_statfs =		msdosfs_statfs,
1191 	.vfs_sync =		msdosfs_sync,
1192 	.vfs_unmount =		msdosfs_unmount,
1193 };
1194 
1195 VFS_SET(msdosfs_vfsops, msdosfs, 0);
1196 MODULE_VERSION(msdosfs, 1);
1197