xref: /freebsd/sys/fs/msdosfs/msdosfs_vfsops.c (revision 369a2542caa9252ce2e89b0360f85a1c28a908e0)
1 /*	$NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8  * All rights reserved.
9  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by TooLs GmbH.
22  * 4. The name of TooLs GmbH may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 /*-
37  * Written by Paul Popelka (paulp@uts.amdahl.com)
38  *
39  * You can do anything you want with this software, just don't say you wrote
40  * it, and don't remove this notice.
41  *
42  * This software is provided "as is".
43  *
44  * The author supplies this software to be publicly redistributed on the
45  * understanding that the author is not responsible for the correct
46  * functioning of this software in any circumstances and is not liable for
47  * any damages caused by this software.
48  *
49  * October 1992
50  */
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/buf.h>
55 #include <sys/bufobj.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/rwlock.h>
68 #include <sys/stat.h>
69 #include <sys/taskqueue.h>
70 #include <sys/vnode.h>
71 
72 #include <geom/geom.h>
73 #include <geom/geom_vfs.h>
74 
75 #include <fs/msdosfs/bootsect.h>
76 #include <fs/msdosfs/bpb.h>
77 #include <fs/msdosfs/direntry.h>
78 #include <fs/msdosfs/denode.h>
79 #include <fs/msdosfs/fat.h>
80 #include <fs/msdosfs/msdosfsmount.h>
81 
82 #ifdef MSDOSFS_DEBUG
83 #include <sys/rwlock.h>
84 #endif
85 
86 static const char msdosfs_lock_msg[] = "fatlk";
87 
88 /* Mount options that we support. */
89 static const char *msdosfs_opts[] = {
90 	"async", "noatime", "noclusterr", "noclusterw",
91 	"export", "force", "from", "sync",
92 	"cs_dos", "cs_local", "cs_win", "dirmask",
93 	"gid", "kiconv", "longname",
94 	"longnames", "mask", "shortname", "shortnames",
95 	"uid", "win95", "nowin95",
96 	NULL
97 };
98 
99 #if 1 /*def PC98*/
100 /*
101  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
102  *       garbage or a random value :-{
103  *       If you want to use that broken-signatured media, define the
104  *       following symbol even though PC/AT.
105  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
106  */
107 #define	MSDOSFS_NOCHECKSIG
108 #endif
109 
110 MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure");
111 static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table");
112 
113 struct iconv_functions *msdosfs_iconv;
114 
115 static int	update_mp(struct mount *mp, struct thread *td);
116 static int	mountmsdosfs(struct vnode *devvp, struct mount *mp);
117 static void	msdosfs_remount_ro(void *arg, int pending);
118 static vfs_fhtovp_t	msdosfs_fhtovp;
119 static vfs_mount_t	msdosfs_mount;
120 static vfs_root_t	msdosfs_root;
121 static vfs_statfs_t	msdosfs_statfs;
122 static vfs_sync_t	msdosfs_sync;
123 static vfs_unmount_t	msdosfs_unmount;
124 
125 /* Maximum length of a character set name (arbitrary). */
126 #define	MAXCSLEN	64
127 
128 static int
update_mp(struct mount * mp,struct thread * td)129 update_mp(struct mount *mp, struct thread *td)
130 {
131 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
132 	void *dos, *win, *local;
133 	int error, v;
134 
135 	if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
136 		if (msdosfs_iconv != NULL) {
137 			error = vfs_getopt(mp->mnt_optnew,
138 			    "cs_win", &win, NULL);
139 			if (!error)
140 				error = vfs_getopt(mp->mnt_optnew,
141 				    "cs_local", &local, NULL);
142 			if (!error)
143 				error = vfs_getopt(mp->mnt_optnew,
144 				    "cs_dos", &dos, NULL);
145 			if (!error) {
146 				msdosfs_iconv->open(win, local, &pmp->pm_u2w);
147 				msdosfs_iconv->open(local, win, &pmp->pm_w2u);
148 				msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
149 				msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
150 			}
151 			if (error != 0)
152 				return (error);
153 		} else {
154 			pmp->pm_w2u = NULL;
155 			pmp->pm_u2w = NULL;
156 			pmp->pm_d2u = NULL;
157 			pmp->pm_u2d = NULL;
158 		}
159 	}
160 
161 	if (vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v) == 1)
162 		pmp->pm_gid = v;
163 	if (vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v) == 1)
164 		pmp->pm_uid = v;
165 	if (vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v) == 1)
166 		pmp->pm_mask = v & ALLPERMS;
167 	if (vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v) == 1)
168 		pmp->pm_dirmask = v & ALLPERMS;
169 	vfs_flagopt(mp->mnt_optnew, "shortname",
170 	    &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
171 	vfs_flagopt(mp->mnt_optnew, "shortnames",
172 	    &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
173 	vfs_flagopt(mp->mnt_optnew, "longname",
174 	    &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
175 	vfs_flagopt(mp->mnt_optnew, "longnames",
176 	    &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
177 	vfs_flagopt(mp->mnt_optnew, "kiconv",
178 	    &pmp->pm_flags, MSDOSFSMNT_KICONV);
179 
180 	if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
181 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
182 	else
183 		pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
184 
185 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
186 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
187 	else
188 		pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
189 	return 0;
190 }
191 
192 static int
msdosfs_cmount(struct mntarg * ma,void * data,uint64_t flags)193 msdosfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
194 {
195 	struct msdosfs_args args;
196 	int error;
197 
198 	if (data == NULL)
199 		return (EINVAL);
200 	error = copyin(data, &args, sizeof args);
201 	if (error)
202 		return (error);
203 
204 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
205 	ma = mount_arg(ma, "export", &args.export, sizeof(args.export));
206 	ma = mount_argf(ma, "uid", "%d", args.uid);
207 	ma = mount_argf(ma, "gid", "%d", args.gid);
208 	ma = mount_argf(ma, "mask", "%d", args.mask);
209 	ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
210 
211 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
212 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
213 	ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
214 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
215 
216 	ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
217 	ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
218 	ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
219 
220 	error = kernel_mount(ma, flags);
221 
222 	return (error);
223 }
224 
225 /*
226  * mp - path - addr in user space of mount point (ie /usr or whatever)
227  * data - addr in user space of mount params including the name of the block
228  * special file to treat as a filesystem.
229  */
230 static int
msdosfs_mount(struct mount * mp)231 msdosfs_mount(struct mount *mp)
232 {
233 	struct vnode *devvp, *odevvp;	  /* vnode for blk device to mount */
234 	struct thread *td;
235 	/* msdosfs specific mount control block */
236 	struct msdosfsmount *pmp = NULL;
237 	struct nameidata ndp;
238 	int error, flags;
239 	accmode_t accmode;
240 	char *from;
241 
242 	td = curthread;
243 	if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
244 		return (EINVAL);
245 
246 	/*
247 	 * If updating, check whether changing from read-only to
248 	 * read/write; if there is no device name, that's all we do.
249 	 */
250 	if (mp->mnt_flag & MNT_UPDATE) {
251 		pmp = VFSTOMSDOSFS(mp);
252 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
253 		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
254 			if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
255 				return (error);
256 			error = vfs_write_suspend_umnt(mp);
257 			if (error != 0)
258 				return (error);
259 
260 			flags = WRITECLOSE;
261 			if (mp->mnt_flag & MNT_FORCE)
262 				flags |= FORCECLOSE;
263 			error = vflush(mp, 0, flags, td);
264 			if (error != 0) {
265 				vfs_write_resume(mp, 0);
266 				return (error);
267 			}
268 
269 			/*
270 			 * Now the volume is clean.  Mark it so while the
271 			 * device is still rw.
272 			 */
273 			error = markvoldirty(pmp, 0);
274 			if (error != 0) {
275 				vfs_write_resume(mp, 0);
276 				(void)markvoldirty(pmp, 1);
277 				return (error);
278 			}
279 
280 			/* Downgrade the device from rw to ro. */
281 			g_topology_lock();
282 			error = g_access(pmp->pm_cp, 0, -1, 0);
283 			g_topology_unlock();
284 			if (error) {
285 				vfs_write_resume(mp, 0);
286 				(void)markvoldirty(pmp, 1);
287 				return (error);
288 			}
289 
290 			/*
291 			 * Backing out after an error was painful in the
292 			 * above.  Now we are committed to succeeding.
293 			 */
294 			pmp->pm_fmod = 0;
295 			pmp->pm_flags |= MSDOSFSMNT_RONLY;
296 			MNT_ILOCK(mp);
297 			mp->mnt_flag |= MNT_RDONLY;
298 			MNT_IUNLOCK(mp);
299 			vfs_write_resume(mp, 0);
300 		} else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
301 		    !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
302 			/*
303 			 * If upgrade to read-write by non-root, then verify
304 			 * that user has necessary permissions on the device.
305 			 */
306 			odevvp = pmp->pm_odevvp;
307 			vn_lock(odevvp, LK_EXCLUSIVE | LK_RETRY);
308 			error = VOP_ACCESS(odevvp, VREAD | VWRITE,
309 			    td->td_ucred, td);
310 			if (error)
311 				error = priv_check(td, PRIV_VFS_MOUNT_PERM);
312 			if (error) {
313 				VOP_UNLOCK(odevvp);
314 				return (error);
315 			}
316 			VOP_UNLOCK(odevvp);
317 			g_topology_lock();
318 			error = g_access(pmp->pm_cp, 0, 1, 0);
319 			g_topology_unlock();
320 			if (error)
321 				return (error);
322 
323 			/* Now that the volume is modifiable, mark it dirty. */
324 			error = markvoldirty_upgrade(pmp, true, true);
325 			if (error) {
326 				/*
327 				 * If dirtying the superblock failed, drop GEOM
328 				 * 'w' refs (we're still RO).
329 				 */
330 				g_topology_lock();
331 				(void)g_access(pmp->pm_cp, 0, -1, 0);
332 				g_topology_unlock();
333 
334 				return (error);
335 			}
336 
337 			pmp->pm_fmod = 1;
338 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
339 			MNT_ILOCK(mp);
340 			mp->mnt_flag &= ~MNT_RDONLY;
341 			MNT_IUNLOCK(mp);
342 		}
343 
344 		/*
345 		 * Avoid namei() below.  The "from" option is not set.
346 		 * Update of the devvp is pointless for this case.
347 		 */
348 		if ((pmp->pm_flags & MSDOSFS_ERR_RO) != 0)
349 			return (0);
350 	}
351 	/*
352 	 * Not an update, or updating the name: look up the name
353 	 * and verify that it refers to a sensible disk device.
354 	 */
355 	if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
356 		return (EINVAL);
357 	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from);
358 	error = namei(&ndp);
359 	if (error)
360 		return (error);
361 	devvp = ndp.ni_vp;
362 	NDFREE_PNBUF(&ndp);
363 
364 	if (!vn_isdisk_error(devvp, &error)) {
365 		vput(devvp);
366 		return (error);
367 	}
368 	/*
369 	 * If mount by non-root, then verify that user has necessary
370 	 * permissions on the device.
371 	 */
372 	accmode = VREAD;
373 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
374 		accmode |= VWRITE;
375 	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
376 	if (error)
377 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
378 	if (error) {
379 		vput(devvp);
380 		return (error);
381 	}
382 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
383 		error = mountmsdosfs(devvp, mp);
384 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
385 		pmp = VFSTOMSDOSFS(mp);
386 #endif
387 	} else {
388 		vput(devvp);
389 		if (devvp != pmp->pm_odevvp)
390 			return (EINVAL);	/* XXX needs translation */
391 	}
392 	if (error) {
393 		vrele(devvp);
394 		return (error);
395 	}
396 
397 	error = update_mp(mp, td);
398 	if (error) {
399 		if ((mp->mnt_flag & MNT_UPDATE) == 0)
400 			msdosfs_unmount(mp, MNT_FORCE);
401 		return error;
402 	}
403 
404 	vfs_mountedfrom(mp, from);
405 #ifdef MSDOSFS_DEBUG
406 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
407 #endif
408 	return (0);
409 }
410 
411 /*
412  * The FAT12 and FAT16 file systems use a limited size root directory that
413  * can be created with 1 to 65535 entries for files, directories, or a disk
414  * label (but DOS or Windows creates at most 512 root directory entries).
415  * This function calculates the number of free root directory entries by
416  * counting the non-deleted entries (not starting with 0xE5) and by adding
417  * the amount of never used entries (with the position indicated by an
418  * entry that starts with 0x00).
419  */
420 static int
rootdir_free(struct msdosfsmount * pmp)421 rootdir_free(struct msdosfsmount* pmp)
422 {
423 	struct buf *bp;
424 	struct direntry *dep;
425 	u_long readsize;
426 	int dirclu;
427 	int diridx;
428 	int dirmax;
429 	int dirleft;
430 	int ffree;
431 
432 	dirclu = pmp->pm_rootdirblk;
433 
434 	/*
435 	 * The msdosfs code ignores pm_RootDirEnts and uses pm_rootdirsize
436 	 * (measured in DEV_BSIZE) to prevent excess root dir allocations.
437 	 */
438 	dirleft = howmany(pmp->pm_rootdirsize * DEV_BSIZE,
439 			  sizeof(struct direntry));
440 
441 	/* Read in chunks of default maximum root directory size */
442 	readsize = 512 * sizeof(struct direntry);
443 
444 #ifdef MSDOSFS_DEBUG
445 	printf("rootdir_free: blkpersec=%lu fatblksize=%lu dirsize=%lu "
446 	    "firstclu=%lu dirclu=%d entries=%d rootdirsize=%lu "
447 	    "bytespersector=%hu bytepercluster=%lu\n",
448 	    pmp->pm_BlkPerSec, pmp->pm_fatblocksize, readsize,
449 	    pmp->pm_firstcluster, dirclu, dirleft, pmp->pm_rootdirsize,
450 	    pmp->pm_BytesPerSec, pmp->pm_bpcluster);
451 #endif
452 	ffree = dirleft;
453 	while (dirleft > 0 && ffree > 0) {
454 		if (readsize > dirleft * sizeof(struct direntry))
455 			readsize = dirleft * sizeof(struct direntry);
456 #ifdef MSDOSFS_DEBUG
457 		printf("rootdir_free: dirclu=%d dirleft=%d readsize=%lu\n",
458 		       dirclu, dirleft, readsize);
459 #endif
460 		if (bread(pmp->pm_devvp, dirclu, readsize, NOCRED, &bp) != 0) {
461 			printf("rootdir_free: read error\n");
462 			if (bp != NULL)
463 				brelse(bp);
464 			return (-1);
465 		}
466 		dirmax = readsize / sizeof(struct direntry);
467 		for (diridx = 0; diridx < dirmax && dirleft > 0;
468 		     diridx++, dirleft--) {
469 			dep = (struct direntry*)bp->b_data + diridx;
470 #ifdef MSDOSFS_DEBUG
471 			if (dep->deName[0] == SLOT_DELETED)
472 				printf("rootdir_free: idx=%d <deleted>\n",
473 				    diridx);
474 			else if (dep->deName[0] == SLOT_EMPTY)
475 				printf("rootdir_free: idx=%d <end marker>\n",
476 				    diridx);
477 			else if (dep->deAttributes == ATTR_WIN95)
478 				printf("rootdir_free: idx=%d <LFN part %d>\n",
479 				    diridx, (dep->deName[0] & 0x1f) + 1);
480 			else if (dep->deAttributes & ATTR_VOLUME)
481 				printf("rootdir_free: idx=%d label='%11.11s'\n",
482 				    diridx, dep->deName);
483 			else if (dep->deAttributes & ATTR_DIRECTORY)
484 				printf("rootdir_free: idx=%d dir='%11.11s'\n",
485 				    diridx, dep->deName);
486 			else
487 				printf("rootdir_free: idx=%d file='%11.11s'\n",
488 				    diridx, dep->deName);
489 #endif
490 			if (dep->deName[0] == SLOT_EMPTY)
491 				dirleft = 0;
492 			else if (dep->deName[0] != SLOT_DELETED)
493 				ffree--;
494 		}
495 		brelse(bp);
496 		bp = NULL;
497 		dirclu += readsize / DEV_BSIZE;
498 	}
499 	return (ffree);
500 }
501 
502 static int
mountmsdosfs(struct vnode * odevvp,struct mount * mp)503 mountmsdosfs(struct vnode *odevvp, struct mount *mp)
504 {
505 	struct msdosfsmount *pmp;
506 	struct buf *bp;
507 	struct cdev *dev;
508 	struct vnode *devvp;
509 	union bootsector *bsp;
510 	struct byte_bpb33 *b33;
511 	struct byte_bpb50 *b50;
512 	struct byte_bpb710 *b710;
513 	uint8_t SecPerClust;
514 	u_long clusters;
515 	int ronly, error;
516 	struct g_consumer *cp;
517 	struct bufobj *bo;
518 
519 	bp = NULL;		/* This and pmp both used in error_exit. */
520 	pmp = NULL;
521 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
522 
523 	devvp = mntfs_allocvp(mp, odevvp);
524 	dev = devvp->v_rdev;
525 	if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
526 	    (uintptr_t)mp) == 0) {
527 		mntfs_freevp(devvp);
528 		return (EBUSY);
529 	}
530 	g_topology_lock();
531 	error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
532 	g_topology_unlock();
533 	if (error != 0) {
534 		atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
535 		mntfs_freevp(devvp);
536 		return (error);
537 	}
538 	dev_ref(dev);
539 	bo = &devvp->v_bufobj;
540 	BO_LOCK(&odevvp->v_bufobj);
541 	odevvp->v_bufobj.bo_flag |= BO_NOBUFS;
542 	BO_UNLOCK(&odevvp->v_bufobj);
543 	VOP_UNLOCK(devvp);
544 	if (dev->si_iosize_max != 0)
545 		mp->mnt_iosize_max = dev->si_iosize_max;
546 	if (mp->mnt_iosize_max > maxphys)
547 		mp->mnt_iosize_max = maxphys;
548 
549 	/*
550 	 * Read the boot sector of the filesystem, and then check the
551 	 * boot signature.  If not a dos boot sector then error out.
552 	 *
553 	 * NOTE: 8192 is a magic size that works for ffs.
554 	 */
555 	error = bread(devvp, 0, 8192, NOCRED, &bp);
556 	if (error)
557 		goto error_exit;
558 	bp->b_flags |= B_AGE;
559 	bsp = (union bootsector *)bp->b_data;
560 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
561 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
562 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
563 
564 #ifndef MSDOSFS_NOCHECKSIG
565 	if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 ||
566 	    bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
567 		error = EINVAL;
568 		goto error_exit;
569 	}
570 #endif
571 
572 	pmp = malloc(sizeof(*pmp), M_MSDOSFSMNT, M_WAITOK | M_ZERO);
573 	pmp->pm_mountp = mp;
574 	pmp->pm_cp = cp;
575 	pmp->pm_bo = bo;
576 
577 	lockinit(&pmp->pm_fatlock, 0, msdosfs_lock_msg, 0, 0);
578 
579 	TASK_INIT(&pmp->pm_rw2ro_task, 0, msdosfs_remount_ro, pmp);
580 
581 	/*
582 	 * Initialize ownerships and permissions, since nothing else will
583 	 * initialize them iff we are mounting root.
584 	 */
585 	pmp->pm_uid = UID_ROOT;
586 	pmp->pm_gid = GID_WHEEL;
587 	pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
588 	    S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
589 
590 	/*
591 	 * Compute several useful quantities from the bpb in the
592 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
593 	 * the fields that are different between dos 5 and dos 3.3.
594 	 */
595 	SecPerClust = b50->bpbSecPerClust;
596 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
597 	if (pmp->pm_BytesPerSec < DEV_BSIZE) {
598 		error = EINVAL;
599 		goto error_exit;
600 	}
601 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
602 	pmp->pm_FATs = b50->bpbFATs;
603 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
604 	pmp->pm_Sectors = getushort(b50->bpbSectors);
605 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
606 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
607 	pmp->pm_Heads = getushort(b50->bpbHeads);
608 	pmp->pm_Media = b50->bpbMedia;
609 
610 	/* calculate the ratio of sector size to DEV_BSIZE */
611 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
612 
613 	/*
614 	 * We don't check pm_Heads nor pm_SecPerTrack, because
615 	 * these may not be set for EFI file systems. We don't
616 	 * use these anyway, so we're unaffected if they are
617 	 * invalid.
618 	 */
619 	if (pmp->pm_BytesPerSec == 0 || SecPerClust == 0) {
620 		error = EINVAL;
621 		goto error_exit;
622 	}
623 
624 	if (pmp->pm_Sectors == 0) {
625 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
626 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
627 	} else {
628 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
629 		pmp->pm_HugeSectors = pmp->pm_Sectors;
630 	}
631 
632 	if (pmp->pm_RootDirEnts == 0) {
633 		if (pmp->pm_FATsecs != 0 || getushort(b710->bpbFSVers) != 0) {
634 			error = EINVAL;
635 #ifdef MSDOSFS_DEBUG
636 			printf("mountmsdosfs(): bad FAT32 filesystem\n");
637 #endif
638 			goto error_exit;
639 		}
640 		pmp->pm_fatmask = FAT32_MASK;
641 		pmp->pm_fatmult = 4;
642 		pmp->pm_fatdiv = 1;
643 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
644 		if ((getushort(b710->bpbExtFlags) & FATMIRROR) != 0)
645 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
646 		else
647 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
648 	} else
649 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
650 
651 	/*
652 	 * Check a few values (could do some more):
653 	 * - logical sector size: power of 2, >= block size
654 	 * - sectors per cluster: power of 2, >= 1
655 	 * - number of sectors:   >= 1, <= size of partition
656 	 * - number of FAT sectors: >= 1
657 	 */
658 	if (SecPerClust == 0 || (SecPerClust & (SecPerClust - 1)) != 0 ||
659 	    pmp->pm_BytesPerSec < DEV_BSIZE ||
660 	    (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) != 0 ||
661 	    pmp->pm_HugeSectors == 0 || pmp->pm_FATsecs == 0 ||
662 	    SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE) {
663 		error = EINVAL;
664 		goto error_exit;
665 	}
666 
667 	if ((off_t)pmp->pm_HugeSectors * pmp->pm_BytesPerSec <
668 	    pmp->pm_HugeSectors /* overflow */ ||
669 	    (off_t)pmp->pm_HugeSectors * pmp->pm_BytesPerSec >
670 	    cp->provider->mediasize /* past end of vol */) {
671 		error = EINVAL;
672 		goto error_exit;
673 	}
674 
675 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
676 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec;	/* XXX not used? */
677 	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
678 	SecPerClust         *= pmp->pm_BlkPerSec;
679 
680 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
681 
682 	if (FAT32(pmp)) {
683 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
684 		pmp->pm_firstcluster = pmp->pm_fatblk +
685 		    pmp->pm_FATs * pmp->pm_FATsecs;
686 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
687 	} else {
688 		pmp->pm_rootdirblk = pmp->pm_fatblk +
689 		    pmp->pm_FATs * pmp->pm_FATsecs;
690 		pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts *
691 		    sizeof(struct direntry), DEV_BSIZE); /* in blocks */
692 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
693 	}
694 
695 	if (pmp->pm_HugeSectors <= pmp->pm_firstcluster) {
696 		error = EINVAL;
697 		goto error_exit;
698 	}
699 	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
700 	    SecPerClust + 1;
701 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE;
702 
703 	if (pmp->pm_fatmask == 0) {
704 		/*
705 		 * The last 10 (or 16?) clusters are reserved and must not
706 		 * be allocated for data.
707 		 */
708 		if (pmp->pm_maxcluster < (CLUST_RSRVD & FAT12_MASK)) {
709 			/*
710 			 * This will usually be a floppy disk. This size makes
711 			 * sure that one FAT entry will not be split across
712 			 * multiple blocks.
713 			 */
714 			pmp->pm_fatmask = FAT12_MASK;
715 			pmp->pm_fatmult = 3;
716 			pmp->pm_fatdiv = 2;
717 		} else {
718 			pmp->pm_fatmask = FAT16_MASK;
719 			pmp->pm_fatmult = 2;
720 			pmp->pm_fatdiv = 1;
721 		}
722 	}
723 
724 	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
725 	if (clusters >= (CLUST_RSRVD & pmp->pm_fatmask))
726 		clusters = CLUST_RSRVD & pmp->pm_fatmask;
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 		free(pmp->pm_inusemap, M_MSDOSFSFAT);
874 		free(pmp, M_MSDOSFSMNT);
875 		mp->mnt_data = NULL;
876 	}
877 	BO_LOCK(&odevvp->v_bufobj);
878 	odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
879 	BO_UNLOCK(&odevvp->v_bufobj);
880 	atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
881 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
882 	mntfs_freevp(devvp);
883 	dev_rel(dev);
884 	return (error);
885 }
886 
887 /*
888  * Unmount the filesystem described by mp.
889  */
890 static int
msdosfs_unmount(struct mount * mp,int mntflags)891 msdosfs_unmount(struct mount *mp, int mntflags)
892 {
893 	struct msdosfsmount *pmp;
894 	int error, flags;
895 	bool susp;
896 
897 	error = flags = 0;
898 	pmp = VFSTOMSDOSFS(mp);
899 	susp = (pmp->pm_flags & MSDOSFSMNT_RONLY) == 0;
900 
901 	if (susp) {
902 		error = vfs_write_suspend_umnt(mp);
903 		if (error != 0)
904 			return (error);
905 	}
906 
907 	if ((mntflags & MNT_FORCE) != 0)
908 		flags |= FORCECLOSE;
909 	error = vflush(mp, 0, flags, curthread);
910 	if (error != 0 && error != ENXIO) {
911 		if (susp)
912 			vfs_write_resume(mp, VR_START_WRITE);
913 		return (error);
914 	}
915 	if (susp) {
916 		error = markvoldirty(pmp, 0);
917 		if (error != 0 && error != ENXIO) {
918 			if (susp)
919 				vfs_write_resume(mp, VR_START_WRITE);
920 			(void)markvoldirty(pmp, 1);
921 			return (error);
922 		}
923 	}
924 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
925 		if (pmp->pm_w2u)
926 			msdosfs_iconv->close(pmp->pm_w2u);
927 		if (pmp->pm_u2w)
928 			msdosfs_iconv->close(pmp->pm_u2w);
929 		if (pmp->pm_d2u)
930 			msdosfs_iconv->close(pmp->pm_d2u);
931 		if (pmp->pm_u2d)
932 			msdosfs_iconv->close(pmp->pm_u2d);
933 	}
934 
935 #ifdef MSDOSFS_DEBUG
936 	{
937 		struct vnode *vp = pmp->pm_devvp;
938 		struct bufobj *bo;
939 
940 		bo = &vp->v_bufobj;
941 		BO_LOCK(bo);
942 		VI_LOCK(vp);
943 		vn_printf(vp,
944 		    "msdosfs_umount(): just before calling VOP_CLOSE()\n");
945 		printf("freef %p, freeb %p, mount %p\n",
946 		    TAILQ_NEXT(vp, v_vnodelist), vp->v_vnodelist.tqe_prev,
947 		    vp->v_mount);
948 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
949 		    TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
950 		    TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
951 		    vp->v_bufobj.bo_numoutput, vp->v_type);
952 		VI_UNLOCK(vp);
953 		BO_UNLOCK(bo);
954 	}
955 #endif
956 	if (susp)
957 		vfs_write_resume(mp, VR_START_WRITE);
958 
959 	vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
960 	g_topology_lock();
961 	g_vfs_close(pmp->pm_cp);
962 	g_topology_unlock();
963 	BO_LOCK(&pmp->pm_odevvp->v_bufobj);
964 	pmp->pm_odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
965 	BO_UNLOCK(&pmp->pm_odevvp->v_bufobj);
966 	atomic_store_rel_ptr((uintptr_t *)&pmp->pm_dev->si_mountpt, 0);
967 	mntfs_freevp(pmp->pm_devvp);
968 	vrele(pmp->pm_odevvp);
969 	dev_rel(pmp->pm_dev);
970 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
971 	lockdestroy(&pmp->pm_fatlock);
972 	free(pmp, M_MSDOSFSMNT);
973 	mp->mnt_data = NULL;
974 	return (error);
975 }
976 
977 static void
msdosfs_remount_ro(void * arg,int pending)978 msdosfs_remount_ro(void *arg, int pending)
979 {
980 	struct msdosfsmount *pmp;
981 	int error;
982 
983 	pmp = arg;
984 
985 	MSDOSFS_LOCK_MP(pmp);
986 	if ((pmp->pm_flags & MSDOSFS_ERR_RO) != 0) {
987 		while ((pmp->pm_flags & MSDOSFS_ERR_RO) != 0)
988 			msleep(&pmp->pm_flags, &pmp->pm_fatlock, PVFS,
989 			    "msdoserrro", hz);
990 	} else if ((pmp->pm_mountp->mnt_flag & MNT_RDONLY) == 0) {
991 		pmp->pm_flags |= MSDOSFS_ERR_RO;
992 		MSDOSFS_UNLOCK_MP(pmp);
993 		printf("%s: remounting read-only due to corruption\n",
994 		    pmp->pm_mountp->mnt_stat.f_mntfromname);
995 		error = vfs_remount_ro(pmp->pm_mountp);
996 		if (error != 0)
997 			printf("%s: remounting read-only failed: error %d\n",
998 			    pmp->pm_mountp->mnt_stat.f_mntfromname, error);
999 		else
1000 			printf("remounted %s read-only\n",
1001 			    pmp->pm_mountp->mnt_stat.f_mntfromname);
1002 		MSDOSFS_LOCK_MP(pmp);
1003 		pmp->pm_flags &= ~MSDOSFS_ERR_RO;
1004 		wakeup(&pmp->pm_flags);
1005 	}
1006 	MSDOSFS_UNLOCK_MP(pmp);
1007 
1008 	while (--pending >= 0)
1009 		vfs_unbusy(pmp->pm_mountp);
1010 }
1011 
1012 void
msdosfs_integrity_error(struct msdosfsmount * pmp)1013 msdosfs_integrity_error(struct msdosfsmount *pmp)
1014 {
1015 	int error;
1016 
1017 	error = vfs_busy(pmp->pm_mountp, MBF_NOWAIT);
1018 	if (error == 0) {
1019 		error = taskqueue_enqueue(taskqueue_thread,
1020 		    &pmp->pm_rw2ro_task);
1021 		if (error != 0) {
1022 			printf("%s: integrity error scheduling failed, "
1023 			    "error %d\n",
1024 			    pmp->pm_mountp->mnt_stat.f_mntfromname, error);
1025 			vfs_unbusy(pmp->pm_mountp);
1026 		}
1027 	} else {
1028 		printf("%s: integrity error busying failed, error %d\n",
1029 		    pmp->pm_mountp->mnt_stat.f_mntfromname, error);
1030 	}
1031 }
1032 
1033 static int
msdosfs_root(struct mount * mp,int flags,struct vnode ** vpp)1034 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp)
1035 {
1036 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1037 	struct denode *ndep;
1038 	int error;
1039 
1040 #ifdef MSDOSFS_DEBUG
1041 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
1042 #endif
1043 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, LK_EXCLUSIVE, &ndep);
1044 	if (error)
1045 		return (error);
1046 	*vpp = DETOV(ndep);
1047 	return (0);
1048 }
1049 
1050 static int
msdosfs_statfs(struct mount * mp,struct statfs * sbp)1051 msdosfs_statfs(struct mount *mp, struct statfs *sbp)
1052 {
1053 	struct msdosfsmount *pmp;
1054 
1055 	pmp = VFSTOMSDOSFS(mp);
1056 	sbp->f_bsize = pmp->pm_bpcluster;
1057 	sbp->f_iosize = pmp->pm_bpcluster;
1058 	sbp->f_blocks = pmp->pm_maxcluster - CLUST_FIRST + 1;
1059 	sbp->f_bfree = pmp->pm_freeclustercount;
1060 	sbp->f_bavail = pmp->pm_freeclustercount;
1061 	sbp->f_files =	howmany(pmp->pm_rootdirsize * DEV_BSIZE,
1062 				sizeof(struct direntry));
1063 	sbp->f_ffree = pmp->pm_rootdirfree;
1064 	return (0);
1065 }
1066 
1067 /*
1068  * If we have an FSInfo block, update it.
1069  */
1070 static int
msdosfs_fsiflush(struct msdosfsmount * pmp,int waitfor)1071 msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
1072 {
1073 	struct fsinfo *fp;
1074 	struct buf *bp;
1075 	int error;
1076 
1077 	MSDOSFS_LOCK_MP(pmp);
1078 	if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
1079 		error = 0;
1080 		goto unlock;
1081 	}
1082 	error = bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
1083 	    NOCRED, &bp);
1084 	if (error != 0) {
1085 		goto unlock;
1086 	}
1087 	fp = (struct fsinfo *)bp->b_data;
1088 	putulong(fp->fsinfree, pmp->pm_freeclustercount);
1089 	putulong(fp->fsinxtfree, pmp->pm_nxtfree);
1090 	pmp->pm_flags &= ~MSDOSFS_FSIMOD;
1091 	if (waitfor == MNT_WAIT)
1092 		error = bwrite(bp);
1093 	else
1094 		bawrite(bp);
1095 unlock:
1096 	MSDOSFS_UNLOCK_MP(pmp);
1097 	return (error);
1098 }
1099 
1100 static int
msdosfs_sync(struct mount * mp,int waitfor)1101 msdosfs_sync(struct mount *mp, int waitfor)
1102 {
1103 	struct vnode *vp, *nvp;
1104 	struct thread *td;
1105 	struct denode *dep;
1106 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1107 	int error, allerror = 0;
1108 
1109 	td = curthread;
1110 
1111 	/*
1112 	 * If we ever switch to not updating all of the FATs all the time,
1113 	 * this would be the place to update them from the first one.
1114 	 */
1115 	if (pmp->pm_fmod != 0) {
1116 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1117 			panic("msdosfs_sync: rofs mod");
1118 		else {
1119 			/* update FATs here */
1120 		}
1121 	}
1122 	/*
1123 	 * Write back each (modified) denode.
1124 	 */
1125 loop:
1126 	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
1127 		if (vp->v_type == VNON) {
1128 			VI_UNLOCK(vp);
1129 			continue;
1130 		}
1131 		dep = VTODE(vp);
1132 		if ((dep->de_flag &
1133 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
1134 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
1135 		    waitfor == MNT_LAZY)) {
1136 			VI_UNLOCK(vp);
1137 			continue;
1138 		}
1139 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1140 		if (error) {
1141 			if (error == ENOENT) {
1142 				MNT_VNODE_FOREACH_ALL_ABORT(mp, nvp);
1143 				goto loop;
1144 			}
1145 			continue;
1146 		}
1147 		error = VOP_FSYNC(vp, waitfor, td);
1148 		if (error)
1149 			allerror = error;
1150 		vput(vp);
1151 	}
1152 
1153 	/*
1154 	 * Flush filesystem control info.
1155 	 */
1156 	if (waitfor != MNT_LAZY) {
1157 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
1158 		error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
1159 		if (error)
1160 			allerror = error;
1161 		VOP_UNLOCK(pmp->pm_devvp);
1162 	}
1163 
1164 	error = msdosfs_fsiflush(pmp, waitfor);
1165 	if (error != 0)
1166 		allerror = error;
1167 
1168 	if (allerror == 0 && waitfor == MNT_SUSPEND) {
1169 		MNT_ILOCK(mp);
1170 		mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1171 		MNT_IUNLOCK(mp);
1172 	}
1173 	return (allerror);
1174 }
1175 
1176 static int
msdosfs_fhtovp(struct mount * mp,struct fid * fhp,int flags,struct vnode ** vpp)1177 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1178 {
1179 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1180 	struct defid *defhp = (struct defid *) fhp;
1181 	struct denode *dep;
1182 	int error;
1183 
1184 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs,
1185 	    LK_EXCLUSIVE, &dep);
1186 	if (error) {
1187 		*vpp = NULLVP;
1188 		return (error);
1189 	}
1190 	*vpp = DETOV(dep);
1191 	vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
1192 	return (0);
1193 }
1194 
1195 static struct vfsops msdosfs_vfsops = {
1196 	.vfs_fhtovp =		msdosfs_fhtovp,
1197 	.vfs_mount =		msdosfs_mount,
1198 	.vfs_cmount =		msdosfs_cmount,
1199 	.vfs_root =		msdosfs_root,
1200 	.vfs_statfs =		msdosfs_statfs,
1201 	.vfs_sync =		msdosfs_sync,
1202 	.vfs_unmount =		msdosfs_unmount,
1203 };
1204 
1205 VFS_SET(msdosfs_vfsops, msdosfs, 0);
1206 MODULE_VERSION(msdosfs, 1);
1207