xref: /freebsd/sys/fs/msdosfs/msdosfs_vfsops.c (revision 027bebe8e1be3f30c262cde5075f91613a246e63)
1c3aac50fSPeter Wemm /* $FreeBSD$ */
2952a6212SJordan K. Hubbard /*	$NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $	*/
327a0bc89SDoug Rabson 
427a0bc89SDoug Rabson /*-
5952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6952a6212SJordan K. Hubbard  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
727a0bc89SDoug Rabson  * All rights reserved.
827a0bc89SDoug Rabson  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
927a0bc89SDoug Rabson  *
1027a0bc89SDoug Rabson  * Redistribution and use in source and binary forms, with or without
1127a0bc89SDoug Rabson  * modification, are permitted provided that the following conditions
1227a0bc89SDoug Rabson  * are met:
1327a0bc89SDoug Rabson  * 1. Redistributions of source code must retain the above copyright
1427a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer.
1527a0bc89SDoug Rabson  * 2. Redistributions in binary form must reproduce the above copyright
1627a0bc89SDoug Rabson  *    notice, this list of conditions and the following disclaimer in the
1727a0bc89SDoug Rabson  *    documentation and/or other materials provided with the distribution.
1827a0bc89SDoug Rabson  * 3. All advertising materials mentioning features or use of this software
1927a0bc89SDoug Rabson  *    must display the following acknowledgement:
2027a0bc89SDoug Rabson  *	This product includes software developed by TooLs GmbH.
2127a0bc89SDoug Rabson  * 4. The name of TooLs GmbH may not be used to endorse or promote products
2227a0bc89SDoug Rabson  *    derived from this software without specific prior written permission.
2327a0bc89SDoug Rabson  *
2427a0bc89SDoug Rabson  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
2527a0bc89SDoug Rabson  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2627a0bc89SDoug Rabson  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2727a0bc89SDoug Rabson  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2827a0bc89SDoug Rabson  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2927a0bc89SDoug Rabson  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
3027a0bc89SDoug Rabson  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
3127a0bc89SDoug Rabson  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
3227a0bc89SDoug Rabson  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
3327a0bc89SDoug Rabson  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3427a0bc89SDoug Rabson  */
35d167cf6fSWarner Losh /*-
3627a0bc89SDoug Rabson  * Written by Paul Popelka (paulp@uts.amdahl.com)
3727a0bc89SDoug Rabson  *
3827a0bc89SDoug Rabson  * You can do anything you want with this software, just don't say you wrote
3927a0bc89SDoug Rabson  * it, and don't remove this notice.
4027a0bc89SDoug Rabson  *
4127a0bc89SDoug Rabson  * This software is provided "as is".
4227a0bc89SDoug Rabson  *
4327a0bc89SDoug Rabson  * The author supplies this software to be publicly redistributed on the
4427a0bc89SDoug Rabson  * understanding that the author is not responsible for the correct
4527a0bc89SDoug Rabson  * functioning of this software in any circumstances and is not liable for
4627a0bc89SDoug Rabson  * any damages caused by this software.
4727a0bc89SDoug Rabson  *
4827a0bc89SDoug Rabson  * October 1992
4927a0bc89SDoug Rabson  */
5027a0bc89SDoug Rabson 
5127a0bc89SDoug Rabson #include <sys/param.h>
5227a0bc89SDoug Rabson #include <sys/systm.h>
536becd1c8SBruce Evans #include <sys/buf.h>
5465baf8f0SBruce Evans #include <sys/conf.h>
5557b4252eSKonstantin Belousov #include <sys/fcntl.h>
566becd1c8SBruce Evans #include <sys/iconv.h>
576becd1c8SBruce Evans #include <sys/kernel.h>
581103771dSBruce Evans #include <sys/lock.h>
596becd1c8SBruce Evans #include <sys/malloc.h>
606becd1c8SBruce Evans #include <sys/mount.h>
611103771dSBruce Evans #include <sys/mutex.h>
6227a0bc89SDoug Rabson #include <sys/namei.h>
63acd3428bSRobert Watson #include <sys/priv.h>
6427a0bc89SDoug Rabson #include <sys/proc.h>
656becd1c8SBruce Evans #include <sys/stat.h>
6627a0bc89SDoug Rabson #include <sys/vnode.h>
6727a0bc89SDoug Rabson 
689a135592SPoul-Henning Kamp #include <geom/geom.h>
699a135592SPoul-Henning Kamp #include <geom/geom_vfs.h>
709a135592SPoul-Henning Kamp 
716becd1c8SBruce Evans #include <fs/msdosfs/bootsect.h>
726becd1c8SBruce Evans #include <fs/msdosfs/bpb.h>
736becd1c8SBruce Evans #include <fs/msdosfs/direntry.h>
746becd1c8SBruce Evans #include <fs/msdosfs/denode.h>
756becd1c8SBruce Evans #include <fs/msdosfs/fat.h>
766becd1c8SBruce Evans #include <fs/msdosfs/msdosfsmount.h>
776becd1c8SBruce Evans 
78*027bebe8SEd Maste #ifdef MSDOSFS_DEBUG
79*027bebe8SEd Maste #include <sys/rwlock.h>
80*027bebe8SEd Maste #endif
81*027bebe8SEd Maste 
8223b6c230SKonstantin Belousov static const char msdosfs_lock_msg[] = "fatlk";
8323b6c230SKonstantin Belousov 
847c3fc9deSBruce Evans /* Mount options that we support. */
856a4b48f4SPoul-Henning Kamp static const char *msdosfs_opts[] = {
86cb65c1eeSBruce Evans 	"async", "noatime", "noclusterr", "noclusterw",
877c3fc9deSBruce Evans 	"export", "force", "from", "sync",
887c3fc9deSBruce Evans 	"cs_dos", "cs_local", "cs_win", "dirmask",
8940373cf5SKonstantin Belousov 	"gid", "kiconv", "longname",
907c3fc9deSBruce Evans 	"longnames", "mask", "shortname", "shortnames",
917c3fc9deSBruce Evans 	"uid", "win95", "nowin95",
926a4b48f4SPoul-Henning Kamp 	NULL
936a4b48f4SPoul-Henning Kamp };
946a4b48f4SPoul-Henning Kamp 
9501f6cfbaSYoshihiro Takahashi #if 1 /*def PC98*/
9601f6cfbaSYoshihiro Takahashi /*
9701f6cfbaSYoshihiro Takahashi  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
9801f6cfbaSYoshihiro Takahashi  *       garbage or a random value :-{
9901f6cfbaSYoshihiro Takahashi  *       If you want to use that broken-signatured media, define the
10001f6cfbaSYoshihiro Takahashi  *       following symbol even though PC/AT.
10101f6cfbaSYoshihiro Takahashi  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
10201f6cfbaSYoshihiro Takahashi  */
10301f6cfbaSYoshihiro Takahashi #define	MSDOSFS_NOCHECKSIG
10401f6cfbaSYoshihiro Takahashi #endif
10501f6cfbaSYoshihiro Takahashi 
1065bb84bc8SRobert Watson MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure");
1075bb84bc8SRobert Watson static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table");
10855166637SPoul-Henning Kamp 
1092d7c6b27SBruce Evans struct iconv_functions *msdosfs_iconv;
110c4f02a89SMax Khon 
1116a4b48f4SPoul-Henning Kamp static int	update_mp(struct mount *mp, struct thread *td);
1120d7935fdSAttilio Rao static int	mountmsdosfs(struct vnode *devvp, struct mount *mp);
1131be5bc74STom Rhodes static vfs_fhtovp_t	msdosfs_fhtovp;
1146a4b48f4SPoul-Henning Kamp static vfs_mount_t	msdosfs_mount;
1159bf1a756SPoul-Henning Kamp static vfs_root_t	msdosfs_root;
1169bf1a756SPoul-Henning Kamp static vfs_statfs_t	msdosfs_statfs;
1179bf1a756SPoul-Henning Kamp static vfs_sync_t	msdosfs_sync;
1189bf1a756SPoul-Henning Kamp static vfs_unmount_t	msdosfs_unmount;
119af482601SBruce Evans 
1201a9415afSTim J. Robbins /* Maximum length of a character set name (arbitrary). */
1211a9415afSTim J. Robbins #define	MAXCSLEN	64
1221a9415afSTim J. Robbins 
123952a6212SJordan K. Hubbard static int
124dc9a617aSCraig Rodrigues update_mp(struct mount *mp, struct thread *td)
125952a6212SJordan K. Hubbard {
126952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1276a4b48f4SPoul-Henning Kamp 	void *dos, *win, *local;
1286a4b48f4SPoul-Henning Kamp 	int error, v;
129952a6212SJordan K. Hubbard 
1306a4b48f4SPoul-Henning Kamp 	if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
1316a4b48f4SPoul-Henning Kamp 		if (msdosfs_iconv != NULL) {
1326a4b48f4SPoul-Henning Kamp 			error = vfs_getopt(mp->mnt_optnew,
1336a4b48f4SPoul-Henning Kamp 			    "cs_win", &win, NULL);
1346a4b48f4SPoul-Henning Kamp 			if (!error)
1356a4b48f4SPoul-Henning Kamp 				error = vfs_getopt(mp->mnt_optnew,
1366a4b48f4SPoul-Henning Kamp 				    "cs_local", &local, NULL);
1376a4b48f4SPoul-Henning Kamp 			if (!error)
1386a4b48f4SPoul-Henning Kamp 				error = vfs_getopt(mp->mnt_optnew,
1396a4b48f4SPoul-Henning Kamp 				    "cs_dos", &dos, NULL);
1406a4b48f4SPoul-Henning Kamp 			if (!error) {
1411a9415afSTim J. Robbins 				msdosfs_iconv->open(win, local, &pmp->pm_u2w);
1421a9415afSTim J. Robbins 				msdosfs_iconv->open(local, win, &pmp->pm_w2u);
1431a9415afSTim J. Robbins 				msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
1441a9415afSTim J. Robbins 				msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
1456a4b48f4SPoul-Henning Kamp 			}
1461a9415afSTim J. Robbins 			if (error != 0)
1471a9415afSTim J. Robbins 				return (error);
148c4f02a89SMax Khon 		} else {
149c4f02a89SMax Khon 			pmp->pm_w2u = NULL;
150c4f02a89SMax Khon 			pmp->pm_u2w = NULL;
151c4f02a89SMax Khon 			pmp->pm_d2u = NULL;
152c4f02a89SMax Khon 			pmp->pm_u2d = NULL;
1537391f611SAndrey A. Chernov 		}
1541a9415afSTim J. Robbins 	}
1551a9415afSTim J. Robbins 
156dd104b33SKevin Lo 	if (vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v) == 1)
1576a4b48f4SPoul-Henning Kamp 		pmp->pm_gid = v;
158dd104b33SKevin Lo 	if (vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v) == 1)
1596a4b48f4SPoul-Henning Kamp 		pmp->pm_uid = v;
160dd104b33SKevin Lo 	if (vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v) == 1)
1616a4b48f4SPoul-Henning Kamp 		pmp->pm_mask = v & ALLPERMS;
162dd104b33SKevin Lo 	if (vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v) == 1)
1636a4b48f4SPoul-Henning Kamp 		pmp->pm_dirmask = v & ALLPERMS;
1646a4b48f4SPoul-Henning Kamp 	vfs_flagopt(mp->mnt_optnew, "shortname",
1656a4b48f4SPoul-Henning Kamp 	    &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
1664ab12573SCraig Rodrigues 	vfs_flagopt(mp->mnt_optnew, "shortnames",
1674ab12573SCraig Rodrigues 	    &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
1686a4b48f4SPoul-Henning Kamp 	vfs_flagopt(mp->mnt_optnew, "longname",
1696a4b48f4SPoul-Henning Kamp 	    &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
1704ab12573SCraig Rodrigues 	vfs_flagopt(mp->mnt_optnew, "longnames",
1714ab12573SCraig Rodrigues 	    &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
1726a4b48f4SPoul-Henning Kamp 	vfs_flagopt(mp->mnt_optnew, "kiconv",
1736a4b48f4SPoul-Henning Kamp 	    &pmp->pm_flags, MSDOSFSMNT_KICONV);
1746a4b48f4SPoul-Henning Kamp 
175d75b2048SCraig Rodrigues 	if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
1766a4b48f4SPoul-Henning Kamp 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
177d75b2048SCraig Rodrigues 	else
178d75b2048SCraig Rodrigues 		pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
179952a6212SJordan K. Hubbard 
180952a6212SJordan K. Hubbard 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
181952a6212SJordan K. Hubbard 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
1820696afbeSAlan Somers 	else
183952a6212SJordan K. Hubbard 		pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
184952a6212SJordan K. Hubbard 	return 0;
185952a6212SJordan K. Hubbard }
186952a6212SJordan K. Hubbard 
1876a4b48f4SPoul-Henning Kamp static int
188cc672d35SKirk McKusick msdosfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
1896a4b48f4SPoul-Henning Kamp {
1906a4b48f4SPoul-Henning Kamp 	struct msdosfs_args args;
191d0cc54f3SKonstantin Belousov 	struct export_args exp;
1926a4b48f4SPoul-Henning Kamp 	int error;
1936a4b48f4SPoul-Henning Kamp 
1946a4b48f4SPoul-Henning Kamp 	if (data == NULL)
1956a4b48f4SPoul-Henning Kamp 		return (EINVAL);
1966a4b48f4SPoul-Henning Kamp 	error = copyin(data, &args, sizeof args);
1976a4b48f4SPoul-Henning Kamp 	if (error)
1986a4b48f4SPoul-Henning Kamp 		return (error);
199d0cc54f3SKonstantin Belousov 	vfs_oexport_conv(&args.export, &exp);
2006a4b48f4SPoul-Henning Kamp 
2016a4b48f4SPoul-Henning Kamp 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
202d0cc54f3SKonstantin Belousov 	ma = mount_arg(ma, "export", &exp, sizeof(exp));
2036a4b48f4SPoul-Henning Kamp 	ma = mount_argf(ma, "uid", "%d", args.uid);
2046a4b48f4SPoul-Henning Kamp 	ma = mount_argf(ma, "gid", "%d", args.gid);
2056a4b48f4SPoul-Henning Kamp 	ma = mount_argf(ma, "mask", "%d", args.mask);
2066a4b48f4SPoul-Henning Kamp 	ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
2076a4b48f4SPoul-Henning Kamp 
2086a4b48f4SPoul-Henning Kamp 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
2096a4b48f4SPoul-Henning Kamp 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
2106a4b48f4SPoul-Henning Kamp 	ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
2116a4b48f4SPoul-Henning Kamp 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
2126a4b48f4SPoul-Henning Kamp 
2136a4b48f4SPoul-Henning Kamp 	ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
2146a4b48f4SPoul-Henning Kamp 	ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
2156a4b48f4SPoul-Henning Kamp 	ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
2166a4b48f4SPoul-Henning Kamp 
2176a4b48f4SPoul-Henning Kamp 	error = kernel_mount(ma, flags);
2186a4b48f4SPoul-Henning Kamp 
2196a4b48f4SPoul-Henning Kamp 	return (error);
2206a4b48f4SPoul-Henning Kamp }
2216a4b48f4SPoul-Henning Kamp 
22227a0bc89SDoug Rabson /*
22327a0bc89SDoug Rabson  * mp - path - addr in user space of mount point (ie /usr or whatever)
22427a0bc89SDoug Rabson  * data - addr in user space of mount params including the name of the block
22527a0bc89SDoug Rabson  * special file to treat as a filesystem.
22627a0bc89SDoug Rabson  */
2277fefffeeSPoul-Henning Kamp static int
228dfd233edSAttilio Rao msdosfs_mount(struct mount *mp)
22927a0bc89SDoug Rabson {
23027a0bc89SDoug Rabson 	struct vnode *devvp;	  /* vnode for blk device to mount */
231dfd233edSAttilio Rao 	struct thread *td;
232952a6212SJordan K. Hubbard 	/* msdosfs specific mount control block */
233952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = NULL;
2345e8c582aSPoul-Henning Kamp 	struct nameidata ndp;
235269c902fSPoul-Henning Kamp 	int error, flags;
23615bc6b2bSEdward Tomasz Napierala 	accmode_t accmode;
2376a4b48f4SPoul-Henning Kamp 	char *from;
23827a0bc89SDoug Rabson 
239dfd233edSAttilio Rao 	td = curthread;
2406a4b48f4SPoul-Henning Kamp 	if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
2416a4b48f4SPoul-Henning Kamp 		return (EINVAL);
2426a4b48f4SPoul-Henning Kamp 
24327a0bc89SDoug Rabson 	/*
244952a6212SJordan K. Hubbard 	 * If updating, check whether changing from read-only to
245952a6212SJordan K. Hubbard 	 * read/write; if there is no device name, that's all we do.
24627a0bc89SDoug Rabson 	 */
24727a0bc89SDoug Rabson 	if (mp->mnt_flag & MNT_UPDATE) {
2482d7c6b27SBruce Evans 		pmp = VFSTOMSDOSFS(mp);
2499a135592SPoul-Henning Kamp 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
2506a4b48f4SPoul-Henning Kamp 		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
251dfd233edSAttilio Rao 			error = VFS_SYNC(mp, MNT_WAIT);
252e83f1423SPoul-Henning Kamp 			if (error)
253e83f1423SPoul-Henning Kamp 				return (error);
25427a0bc89SDoug Rabson 			flags = WRITECLOSE;
25527a0bc89SDoug Rabson 			if (mp->mnt_flag & MNT_FORCE)
25627a0bc89SDoug Rabson 				flags |= FORCECLOSE;
257f257b7a5SAlfred Perlstein 			error = vflush(mp, 0, flags, td);
2586a4b48f4SPoul-Henning Kamp 			if (error)
2596a4b48f4SPoul-Henning Kamp 				return (error);
2603247c9ddSXin LI 
2613247c9ddSXin LI 			/*
2623247c9ddSXin LI 			 * Now the volume is clean.  Mark it so while the
2633247c9ddSXin LI 			 * device is still rw.
2643247c9ddSXin LI 			 */
2653247c9ddSXin LI 			error = markvoldirty(pmp, 0);
2663247c9ddSXin LI 			if (error) {
2673247c9ddSXin LI 				(void)markvoldirty(pmp, 1);
2683247c9ddSXin LI 				return (error);
2693247c9ddSXin LI 			}
2703247c9ddSXin LI 
2713247c9ddSXin LI 			/* Downgrade the device from rw to ro. */
2729a135592SPoul-Henning Kamp 			g_topology_lock();
2734eb3abf0SBruce Evans 			error = g_access(pmp->pm_cp, 0, -1, 0);
2749a135592SPoul-Henning Kamp 			g_topology_unlock();
2753247c9ddSXin LI 			if (error) {
2763247c9ddSXin LI 				(void)markvoldirty(pmp, 1);
2774eb3abf0SBruce Evans 				return (error);
2783247c9ddSXin LI 			}
2794eb3abf0SBruce Evans 
2803247c9ddSXin LI 			/*
2813247c9ddSXin LI 			 * Backing out after an error was painful in the
2823247c9ddSXin LI 			 * above.  Now we are committed to succeeding.
2833247c9ddSXin LI 			 */
2843247c9ddSXin LI 			pmp->pm_fmod = 0;
2853247c9ddSXin LI 			pmp->pm_flags |= MSDOSFSMNT_RONLY;
2863247c9ddSXin LI 			MNT_ILOCK(mp);
2873247c9ddSXin LI 			mp->mnt_flag |= MNT_RDONLY;
2883247c9ddSXin LI 			MNT_IUNLOCK(mp);
2896a4b48f4SPoul-Henning Kamp 		} else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
2906a4b48f4SPoul-Henning Kamp 		    !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
291952a6212SJordan K. Hubbard 			/*
292952a6212SJordan K. Hubbard 			 * If upgrade to read-write by non-root, then verify
293952a6212SJordan K. Hubbard 			 * that user has necessary permissions on the device.
294952a6212SJordan K. Hubbard 			 */
295952a6212SJordan K. Hubbard 			devvp = pmp->pm_devvp;
296cb05b60aSAttilio Rao 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
297952a6212SJordan K. Hubbard 			error = VOP_ACCESS(devvp, VREAD | VWRITE,
298a854ed98SJohn Baldwin 			    td->td_ucred, td);
299acd3428bSRobert Watson 			if (error)
300acd3428bSRobert Watson 				error = priv_check(td, PRIV_VFS_MOUNT_PERM);
301952a6212SJordan K. Hubbard 			if (error) {
30222db15c0SAttilio Rao 				VOP_UNLOCK(devvp, 0);
303952a6212SJordan K. Hubbard 				return (error);
304952a6212SJordan K. Hubbard 			}
30522db15c0SAttilio Rao 			VOP_UNLOCK(devvp, 0);
3069a135592SPoul-Henning Kamp 			g_topology_lock();
3079a135592SPoul-Henning Kamp 			error = g_access(pmp->pm_cp, 0, 1, 0);
3089a135592SPoul-Henning Kamp 			g_topology_unlock();
3099a135592SPoul-Henning Kamp 			if (error)
3109a135592SPoul-Henning Kamp 				return (error);
311cede1f56STom Rhodes 
3123247c9ddSXin LI 			pmp->pm_fmod = 1;
3133247c9ddSXin LI 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
3143247c9ddSXin LI 			MNT_ILOCK(mp);
3153247c9ddSXin LI 			mp->mnt_flag &= ~MNT_RDONLY;
3163247c9ddSXin LI 			MNT_IUNLOCK(mp);
31782c59ec6SCraig Rodrigues 
31882c59ec6SCraig Rodrigues 			/* Now that the volume is modifiable, mark it dirty. */
31982c59ec6SCraig Rodrigues 			error = markvoldirty(pmp, 1);
32082c59ec6SCraig Rodrigues 			if (error)
32182c59ec6SCraig Rodrigues 				return (error);
32282c59ec6SCraig Rodrigues 		}
323952a6212SJordan K. Hubbard 	}
32427a0bc89SDoug Rabson 	/*
325952a6212SJordan K. Hubbard 	 * Not an update, or updating the name: look up the name
326e9827c6dSBruce Evans 	 * and verify that it refers to a sensible disk device.
32727a0bc89SDoug Rabson 	 */
3286a4b48f4SPoul-Henning Kamp 	if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
3296a4b48f4SPoul-Henning Kamp 		return (EINVAL);
33075d7ba93SSuleiman Souhlal 	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
3315e8c582aSPoul-Henning Kamp 	error = namei(&ndp);
332952a6212SJordan K. Hubbard 	if (error)
333952a6212SJordan K. Hubbard 		return (error);
3345e8c582aSPoul-Henning Kamp 	devvp = ndp.ni_vp;
3355e8c582aSPoul-Henning Kamp 	NDFREE(&ndp, NDF_ONLY_PNBUF);
336952a6212SJordan K. Hubbard 
337ba4ad1fcSPoul-Henning Kamp 	if (!vn_isdisk(devvp, &error)) {
33875d7ba93SSuleiman Souhlal 		vput(devvp);
339ba4ad1fcSPoul-Henning Kamp 		return (error);
34027a0bc89SDoug Rabson 	}
34127a0bc89SDoug Rabson 	/*
342952a6212SJordan K. Hubbard 	 * If mount by non-root, then verify that user has necessary
343952a6212SJordan K. Hubbard 	 * permissions on the device.
34427a0bc89SDoug Rabson 	 */
34515bc6b2bSEdward Tomasz Napierala 	accmode = VREAD;
346952a6212SJordan K. Hubbard 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
34715bc6b2bSEdward Tomasz Napierala 		accmode |= VWRITE;
34815bc6b2bSEdward Tomasz Napierala 	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
349acd3428bSRobert Watson 	if (error)
350acd3428bSRobert Watson 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
351952a6212SJordan K. Hubbard 	if (error) {
352952a6212SJordan K. Hubbard 		vput(devvp);
353952a6212SJordan K. Hubbard 		return (error);
354952a6212SJordan K. Hubbard 	}
355952a6212SJordan K. Hubbard 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
3560d7935fdSAttilio Rao 		error = mountmsdosfs(devvp, mp);
357952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
358952a6212SJordan K. Hubbard 		pmp = VFSTOMSDOSFS(mp);
359952a6212SJordan K. Hubbard #endif
360952a6212SJordan K. Hubbard 	} else {
36175d7ba93SSuleiman Souhlal 		vput(devvp);
3626c0358ccSKonstantin Belousov 		if (devvp != pmp->pm_devvp)
3636c0358ccSKonstantin Belousov 			return (EINVAL);	/* XXX needs translation */
36427a0bc89SDoug Rabson 	}
36527a0bc89SDoug Rabson 	if (error) {
36627a0bc89SDoug Rabson 		vrele(devvp);
367952a6212SJordan K. Hubbard 		return (error);
368952a6212SJordan K. Hubbard 	}
369952a6212SJordan K. Hubbard 
3706a4b48f4SPoul-Henning Kamp 	error = update_mp(mp, td);
371952a6212SJordan K. Hubbard 	if (error) {
3721a9415afSTim J. Robbins 		if ((mp->mnt_flag & MNT_UPDATE) == 0)
373dfd233edSAttilio Rao 			msdosfs_unmount(mp, MNT_FORCE);
37427a0bc89SDoug Rabson 		return error;
37527a0bc89SDoug Rabson 	}
3766a4b48f4SPoul-Henning Kamp 
3776a4b48f4SPoul-Henning Kamp 	vfs_mountedfrom(mp, from);
37827a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
3796a4b48f4SPoul-Henning Kamp 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
38027a0bc89SDoug Rabson #endif
381952a6212SJordan K. Hubbard 	return (0);
38227a0bc89SDoug Rabson }
38327a0bc89SDoug Rabson 
3847fefffeeSPoul-Henning Kamp static int
3850d7935fdSAttilio Rao mountmsdosfs(struct vnode *devvp, struct mount *mp)
38627a0bc89SDoug Rabson {
387952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp;
388952a6212SJordan K. Hubbard 	struct buf *bp;
389c72ae142SJohn Baldwin 	struct cdev *dev;
39027a0bc89SDoug Rabson 	union bootsector *bsp;
39127a0bc89SDoug Rabson 	struct byte_bpb33 *b33;
39227a0bc89SDoug Rabson 	struct byte_bpb50 *b50;
393952a6212SJordan K. Hubbard 	struct byte_bpb710 *b710;
39423c53312SEd Maste 	uint8_t SecPerClust;
395499d3ffaSBoris Popov 	u_long clusters;
396952a6212SJordan K. Hubbard 	int ronly, error;
3979a135592SPoul-Henning Kamp 	struct g_consumer *cp;
3989a135592SPoul-Henning Kamp 	struct bufobj *bo;
39927a0bc89SDoug Rabson 
400c72ae142SJohn Baldwin 	bp = NULL;		/* This and pmp both used in error_exit. */
401c72ae142SJohn Baldwin 	pmp = NULL;
4024eb3abf0SBruce Evans 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
403c72ae142SJohn Baldwin 
404c72ae142SJohn Baldwin 	dev = devvp->v_rdev;
405bb8297e6SKonstantin Belousov 	if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
406bb8297e6SKonstantin Belousov 	    (uintptr_t)mp) == 0) {
407bb8297e6SKonstantin Belousov 		VOP_UNLOCK(devvp, 0);
408bb8297e6SKonstantin Belousov 		return (EBUSY);
409bb8297e6SKonstantin Belousov 	}
4109a135592SPoul-Henning Kamp 	g_topology_lock();
4114eb3abf0SBruce Evans 	error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
4129a135592SPoul-Henning Kamp 	g_topology_unlock();
413bb8297e6SKonstantin Belousov 	if (error != 0) {
414bb8297e6SKonstantin Belousov 		atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
41522db15c0SAttilio Rao 		VOP_UNLOCK(devvp, 0);
416bb8297e6SKonstantin Belousov 		return (error);
417bb8297e6SKonstantin Belousov 	}
418bb8297e6SKonstantin Belousov 	dev_ref(dev);
419bb8297e6SKonstantin Belousov 	VOP_UNLOCK(devvp, 0);
420952a6212SJordan K. Hubbard 
4219a135592SPoul-Henning Kamp 	bo = &devvp->v_bufobj;
422952a6212SJordan K. Hubbard 
42327a0bc89SDoug Rabson 	/*
424952a6212SJordan K. Hubbard 	 * Read the boot sector of the filesystem, and then check the
425952a6212SJordan K. Hubbard 	 * boot signature.  If not a dos boot sector then error out.
42601f6cfbaSYoshihiro Takahashi 	 *
42793fe42b6SBruce Evans 	 * NOTE: 8192 is a magic size that works for ffs.
42827a0bc89SDoug Rabson 	 */
42993fe42b6SBruce Evans 	error = bread(devvp, 0, 8192, NOCRED, &bp);
430c3c6d51eSPoul-Henning Kamp 	if (error)
43127a0bc89SDoug Rabson 		goto error_exit;
432952a6212SJordan K. Hubbard 	bp->b_flags |= B_AGE;
433952a6212SJordan K. Hubbard 	bsp = (union bootsector *)bp->b_data;
43427a0bc89SDoug Rabson 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
43527a0bc89SDoug Rabson 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
43601a4d019SJohn Baldwin 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
437952a6212SJordan K. Hubbard 
43801f6cfbaSYoshihiro Takahashi #ifndef MSDOSFS_NOCHECKSIG
439952a6212SJordan K. Hubbard 	if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
440952a6212SJordan K. Hubbard 	    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
44127a0bc89SDoug Rabson 		error = EINVAL;
44227a0bc89SDoug Rabson 		goto error_exit;
44327a0bc89SDoug Rabson 	}
44401f6cfbaSYoshihiro Takahashi #endif
44527a0bc89SDoug Rabson 
446a163d034SWarner Losh 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
44727a0bc89SDoug Rabson 	pmp->pm_mountp = mp;
4489a135592SPoul-Henning Kamp 	pmp->pm_cp = cp;
4499a135592SPoul-Henning Kamp 	pmp->pm_bo = bo;
45027a0bc89SDoug Rabson 
45123b6c230SKonstantin Belousov 	lockinit(&pmp->pm_fatlock, 0, msdosfs_lock_msg, 0, 0);
45223b6c230SKonstantin Belousov 
45327a0bc89SDoug Rabson 	/*
4544eb3abf0SBruce Evans 	 * Initialize ownerships and permissions, since nothing else will
45523c1e989SMaxim Konovalov 	 * initialize them iff we are mounting root.
4564eb3abf0SBruce Evans 	 */
4574eb3abf0SBruce Evans 	pmp->pm_uid = UID_ROOT;
4584eb3abf0SBruce Evans 	pmp->pm_gid = GID_WHEEL;
4594eb3abf0SBruce Evans 	pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
4604eb3abf0SBruce Evans 	    S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
4614eb3abf0SBruce Evans 
4624eb3abf0SBruce Evans 	/*
46327a0bc89SDoug Rabson 	 * Compute several useful quantities from the bpb in the
46427a0bc89SDoug Rabson 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
46527a0bc89SDoug Rabson 	 * the fields that are different between dos 5 and dos 3.3.
46627a0bc89SDoug Rabson 	 */
467952a6212SJordan K. Hubbard 	SecPerClust = b50->bpbSecPerClust;
46827a0bc89SDoug Rabson 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
469696f22f0SJohn Baldwin 	if (pmp->pm_BytesPerSec < DEV_BSIZE) {
470696f22f0SJohn Baldwin 		error = EINVAL;
471696f22f0SJohn Baldwin 		goto error_exit;
472696f22f0SJohn Baldwin 	}
47327a0bc89SDoug Rabson 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
47427a0bc89SDoug Rabson 	pmp->pm_FATs = b50->bpbFATs;
47527a0bc89SDoug Rabson 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
47627a0bc89SDoug Rabson 	pmp->pm_Sectors = getushort(b50->bpbSectors);
47727a0bc89SDoug Rabson 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
47827a0bc89SDoug Rabson 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
47927a0bc89SDoug Rabson 	pmp->pm_Heads = getushort(b50->bpbHeads);
480952a6212SJordan K. Hubbard 	pmp->pm_Media = b50->bpbMedia;
48127a0bc89SDoug Rabson 
48201f6cfbaSYoshihiro Takahashi 	/* calculate the ratio of sector size to DEV_BSIZE */
48301f6cfbaSYoshihiro Takahashi 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
48401f6cfbaSYoshihiro Takahashi 
485043ec583SMarcel Moolenaar 	/*
486043ec583SMarcel Moolenaar 	 * We don't check pm_Heads nor pm_SecPerTrack, because
487043ec583SMarcel Moolenaar 	 * these may not be set for EFI file systems. We don't
488043ec583SMarcel Moolenaar 	 * use these anyway, so we're unaffected if they are
489043ec583SMarcel Moolenaar 	 * invalid.
490043ec583SMarcel Moolenaar 	 */
491043ec583SMarcel Moolenaar 	if (!pmp->pm_BytesPerSec || !SecPerClust) {
49227a0bc89SDoug Rabson 		error = EINVAL;
49327a0bc89SDoug Rabson 		goto error_exit;
49427a0bc89SDoug Rabson 	}
49527a0bc89SDoug Rabson 
49627a0bc89SDoug Rabson 	if (pmp->pm_Sectors == 0) {
49727a0bc89SDoug Rabson 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
49827a0bc89SDoug Rabson 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
49927a0bc89SDoug Rabson 	} else {
50027a0bc89SDoug Rabson 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
50127a0bc89SDoug Rabson 		pmp->pm_HugeSectors = pmp->pm_Sectors;
50227a0bc89SDoug Rabson 	}
503952a6212SJordan K. Hubbard 
504952a6212SJordan K. Hubbard 	if (pmp->pm_RootDirEnts == 0) {
50548efa33bSKonstantin Belousov 		if (pmp->pm_FATsecs
506952a6212SJordan K. Hubbard 		    || getushort(b710->bpbFSVers)) {
507952a6212SJordan K. Hubbard 			error = EINVAL;
50854cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
509aaf0bb19SAndrey A. Chernov 			printf("mountmsdosfs(): bad FAT32 filesystem\n");
51054cf9198SKonstantin Belousov #endif
511952a6212SJordan K. Hubbard 			goto error_exit;
512952a6212SJordan K. Hubbard 		}
513952a6212SJordan K. Hubbard 		pmp->pm_fatmask = FAT32_MASK;
514952a6212SJordan K. Hubbard 		pmp->pm_fatmult = 4;
515952a6212SJordan K. Hubbard 		pmp->pm_fatdiv = 1;
516952a6212SJordan K. Hubbard 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
517952a6212SJordan K. Hubbard 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
518952a6212SJordan K. Hubbard 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
519952a6212SJordan K. Hubbard 		else
520952a6212SJordan K. Hubbard 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
521952a6212SJordan K. Hubbard 	} else
522952a6212SJordan K. Hubbard 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
523952a6212SJordan K. Hubbard 
524952a6212SJordan K. Hubbard 	/*
525952a6212SJordan K. Hubbard 	 * Check a few values (could do some more):
526952a6212SJordan K. Hubbard 	 * - logical sector size: power of 2, >= block size
527952a6212SJordan K. Hubbard 	 * - sectors per cluster: power of 2, >= 1
528952a6212SJordan K. Hubbard 	 * - number of sectors:   >= 1, <= size of partition
529696f22f0SJohn Baldwin 	 * - number of FAT sectors: >= 1
530952a6212SJordan K. Hubbard 	 */
531952a6212SJordan K. Hubbard 	if ( (SecPerClust == 0)
532952a6212SJordan K. Hubbard 	  || (SecPerClust & (SecPerClust - 1))
53301f6cfbaSYoshihiro Takahashi 	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
534952a6212SJordan K. Hubbard 	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
535952a6212SJordan K. Hubbard 	  || (pmp->pm_HugeSectors == 0)
536696f22f0SJohn Baldwin 	  || (pmp->pm_FATsecs == 0)
537423b0fb7SAndriy Gapon 	  || (SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE)
538952a6212SJordan K. Hubbard 	) {
539952a6212SJordan K. Hubbard 		error = EINVAL;
540952a6212SJordan K. Hubbard 		goto error_exit;
541952a6212SJordan K. Hubbard 	}
54201f6cfbaSYoshihiro Takahashi 
54301f6cfbaSYoshihiro Takahashi 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
54401f6cfbaSYoshihiro Takahashi 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec;	/* XXX not used? */
54501f6cfbaSYoshihiro Takahashi 	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
54601f6cfbaSYoshihiro Takahashi 	SecPerClust         *= pmp->pm_BlkPerSec;
54701f6cfbaSYoshihiro Takahashi 
54801f6cfbaSYoshihiro Takahashi 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
54901f6cfbaSYoshihiro Takahashi 
550952a6212SJordan K. Hubbard 	if (FAT32(pmp)) {
551952a6212SJordan K. Hubbard 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
552952a6212SJordan K. Hubbard 		pmp->pm_firstcluster = pmp->pm_fatblk
553952a6212SJordan K. Hubbard 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
55401f6cfbaSYoshihiro Takahashi 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
555952a6212SJordan K. Hubbard 	} else {
55627a0bc89SDoug Rabson 		pmp->pm_rootdirblk = pmp->pm_fatblk +
55727a0bc89SDoug Rabson 			(pmp->pm_FATs * pmp->pm_FATsecs);
55855e0987aSPedro F. Giffuni 		pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts *
55955e0987aSPedro F. Giffuni 			sizeof(struct direntry), DEV_BSIZE); /* in blocks */
56027a0bc89SDoug Rabson 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
561952a6212SJordan K. Hubbard 	}
562952a6212SJordan K. Hubbard 
563499d3ffaSBoris Popov 	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
564499d3ffaSBoris Popov 	    SecPerClust + 1;
56501f6cfbaSYoshihiro Takahashi 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE;	/* XXX not used? */
566952a6212SJordan K. Hubbard 
567952a6212SJordan K. Hubbard 	if (pmp->pm_fatmask == 0) {
568952a6212SJordan K. Hubbard 		if (pmp->pm_maxcluster
569952a6212SJordan K. Hubbard 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
57027a0bc89SDoug Rabson 			/*
571952a6212SJordan K. Hubbard 			 * This will usually be a floppy disk. This size makes
5729287dbaaSEd Maste 			 * sure that one FAT entry will not be split across
573952a6212SJordan K. Hubbard 			 * multiple blocks.
57427a0bc89SDoug Rabson 			 */
575952a6212SJordan K. Hubbard 			pmp->pm_fatmask = FAT12_MASK;
576952a6212SJordan K. Hubbard 			pmp->pm_fatmult = 3;
577952a6212SJordan K. Hubbard 			pmp->pm_fatdiv = 2;
578952a6212SJordan K. Hubbard 		} else {
579952a6212SJordan K. Hubbard 			pmp->pm_fatmask = FAT16_MASK;
580952a6212SJordan K. Hubbard 			pmp->pm_fatmult = 2;
581952a6212SJordan K. Hubbard 			pmp->pm_fatdiv = 1;
582952a6212SJordan K. Hubbard 		}
583952a6212SJordan K. Hubbard 	}
584499d3ffaSBoris Popov 
585499d3ffaSBoris Popov 	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
586499d3ffaSBoris Popov 	if (pmp->pm_maxcluster >= clusters) {
58754cf9198SKonstantin Belousov #ifdef MSDOSFS_DEBUG
588499d3ffaSBoris Popov 		printf("Warning: number of clusters (%ld) exceeds FAT "
589499d3ffaSBoris Popov 		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
59054cf9198SKonstantin Belousov #endif
591499d3ffaSBoris Popov 		pmp->pm_maxcluster = clusters - 1;
592499d3ffaSBoris Popov 	}
593499d3ffaSBoris Popov 
594952a6212SJordan K. Hubbard 	if (FAT12(pmp))
59593fe42b6SBruce Evans 		pmp->pm_fatblocksize = 3 * 512;
59627a0bc89SDoug Rabson 	else
59793fe42b6SBruce Evans 		pmp->pm_fatblocksize = PAGE_SIZE;
59893fe42b6SBruce Evans 	pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
59993fe42b6SBruce Evans 	    pmp->pm_BytesPerSec);
60001f6cfbaSYoshihiro Takahashi 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
60101f6cfbaSYoshihiro Takahashi 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
60227a0bc89SDoug Rabson 
60327a0bc89SDoug Rabson 	/*
60427a0bc89SDoug Rabson 	 * Compute mask and shift value for isolating cluster relative byte
60527a0bc89SDoug Rabson 	 * offsets and cluster numbers from a file offset.
60627a0bc89SDoug Rabson 	 */
60701f6cfbaSYoshihiro Takahashi 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
608952a6212SJordan K. Hubbard 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
609952a6212SJordan K. Hubbard 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
61027a0bc89SDoug Rabson 
611952a6212SJordan K. Hubbard 	/*
612952a6212SJordan K. Hubbard 	 * Check for valid cluster size
613952a6212SJordan K. Hubbard 	 * must be a power of 2
614952a6212SJordan K. Hubbard 	 */
615952a6212SJordan K. Hubbard 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
616952a6212SJordan K. Hubbard 		error = EINVAL;
617952a6212SJordan K. Hubbard 		goto error_exit;
618ad63a118SSatoshi Asami 	}
61927a0bc89SDoug Rabson 
62027a0bc89SDoug Rabson 	/*
62127a0bc89SDoug Rabson 	 * Release the bootsector buffer.
62227a0bc89SDoug Rabson 	 */
623952a6212SJordan K. Hubbard 	brelse(bp);
624952a6212SJordan K. Hubbard 	bp = NULL;
625952a6212SJordan K. Hubbard 
626952a6212SJordan K. Hubbard 	/*
6278d61a735SBruce Evans 	 * Check the fsinfo sector if we have one.  Silently fix up our
6288d61a735SBruce Evans 	 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
6298d61a735SBruce Evans 	 * or too large.  Ignore fp->fsinfree for now, since we need to
6308d61a735SBruce Evans 	 * read the entire FAT anyway to fill the inuse map.
631952a6212SJordan K. Hubbard 	 */
632952a6212SJordan K. Hubbard 	if (pmp->pm_fsinfo) {
633952a6212SJordan K. Hubbard 		struct fsinfo *fp;
634952a6212SJordan K. Hubbard 
63537269429SBruce Evans 		if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
63601f6cfbaSYoshihiro Takahashi 		    NOCRED, &bp)) != 0)
637952a6212SJordan K. Hubbard 			goto error_exit;
638952a6212SJordan K. Hubbard 		fp = (struct fsinfo *)bp->b_data;
639952a6212SJordan K. Hubbard 		if (!bcmp(fp->fsisig1, "RRaA", 4)
640952a6212SJordan K. Hubbard 		    && !bcmp(fp->fsisig2, "rrAa", 4)
641fd7c4230SBruce Evans 		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
642952a6212SJordan K. Hubbard 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
6438d61a735SBruce Evans 			if (pmp->pm_nxtfree > pmp->pm_maxcluster)
6448bb386f2STim J. Robbins 				pmp->pm_nxtfree = CLUST_FIRST;
6458bb386f2STim J. Robbins 		} else
646952a6212SJordan K. Hubbard 			pmp->pm_fsinfo = 0;
647952a6212SJordan K. Hubbard 		brelse(bp);
648952a6212SJordan K. Hubbard 		bp = NULL;
649952a6212SJordan K. Hubbard 	}
650952a6212SJordan K. Hubbard 
651952a6212SJordan K. Hubbard 	/*
6528d61a735SBruce Evans 	 * Finish initializing pmp->pm_nxtfree (just in case the first few
6538d61a735SBruce Evans 	 * sectors aren't properly reserved in the FAT).  This completes
6548d61a735SBruce Evans 	 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
6558d61a735SBruce Evans 	 * value if there is no fsinfo.  We will use pmp->pm_nxtfree
6568d61a735SBruce Evans 	 * internally even if there is no fsinfo.
657952a6212SJordan K. Hubbard 	 */
6588d61a735SBruce Evans 	if (pmp->pm_nxtfree < CLUST_FIRST)
6598d61a735SBruce Evans 		pmp->pm_nxtfree = CLUST_FIRST;
66027a0bc89SDoug Rabson 
66127a0bc89SDoug Rabson 	/*
66227a0bc89SDoug Rabson 	 * Allocate memory for the bitmap of allocated clusters, and then
66327a0bc89SDoug Rabson 	 * fill it in.
66427a0bc89SDoug Rabson 	 */
6650ef0dd6fSBruce Evans 	pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
66627a0bc89SDoug Rabson 				  * sizeof(*pmp->pm_inusemap),
667a163d034SWarner Losh 				  M_MSDOSFSFAT, M_WAITOK);
66827a0bc89SDoug Rabson 
66927a0bc89SDoug Rabson 	/*
67027a0bc89SDoug Rabson 	 * fillinusemap() needs pm_devvp.
67127a0bc89SDoug Rabson 	 */
67227a0bc89SDoug Rabson 	pmp->pm_devvp = devvp;
673c72ae142SJohn Baldwin 	pmp->pm_dev = dev;
67427a0bc89SDoug Rabson 
67527a0bc89SDoug Rabson 	/*
67627a0bc89SDoug Rabson 	 * Have the inuse map filled in.
67727a0bc89SDoug Rabson 	 */
6786be1a4ccSKonstantin Belousov 	MSDOSFS_LOCK_MP(pmp);
6796be1a4ccSKonstantin Belousov 	error = fillinusemap(pmp);
6806be1a4ccSKonstantin Belousov 	MSDOSFS_UNLOCK_MP(pmp);
6816be1a4ccSKonstantin Belousov 	if (error != 0)
68227a0bc89SDoug Rabson 		goto error_exit;
68327a0bc89SDoug Rabson 
68427a0bc89SDoug Rabson 	/*
6859287dbaaSEd Maste 	 * If they want FAT updates to be synchronous then let them suffer
68627a0bc89SDoug Rabson 	 * the performance degradation in exchange for the on disk copy of
6879287dbaaSEd Maste 	 * the FAT being correct just about all the time.  I suppose this
68827a0bc89SDoug Rabson 	 * would be a good thing to turn on if the kernel is still flakey.
68927a0bc89SDoug Rabson 	 */
690952a6212SJordan K. Hubbard 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
691952a6212SJordan K. Hubbard 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
69227a0bc89SDoug Rabson 
69327a0bc89SDoug Rabson 	/*
69427a0bc89SDoug Rabson 	 * Finish up.
69527a0bc89SDoug Rabson 	 */
696952a6212SJordan K. Hubbard 	if (ronly)
697952a6212SJordan K. Hubbard 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
698cede1f56STom Rhodes 	else {
6993247c9ddSXin LI 		if ((error = markvoldirty(pmp, 1)) != 0) {
7003247c9ddSXin LI 			(void)markvoldirty(pmp, 0);
701cede1f56STom Rhodes 			goto error_exit;
7023247c9ddSXin LI 		}
70327a0bc89SDoug Rabson 		pmp->pm_fmod = 1;
704cede1f56STom Rhodes 	}
70577465d93SAlfred Perlstein 	mp->mnt_data =  pmp;
706939cb752SBruce Evans 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
707af3f60d5SBruce Evans 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
7085da56ddbSTor Egge 	MNT_ILOCK(mp);
709cc9d8990SPeter Wemm 	mp->mnt_flag |= MNT_LOCAL;
7102aa39445SKonstantin Belousov 	mp->mnt_kern_flag |= MNTK_USES_BCACHE | MNTK_NO_IOPF;
7115da56ddbSTor Egge 	MNT_IUNLOCK(mp);
71227a0bc89SDoug Rabson 
71340373cf5SKonstantin Belousov 	return (0);
71427a0bc89SDoug Rabson 
715952a6212SJordan K. Hubbard error_exit:
716952a6212SJordan K. Hubbard 	if (bp)
717952a6212SJordan K. Hubbard 		brelse(bp);
7189a135592SPoul-Henning Kamp 	if (cp != NULL) {
7199a135592SPoul-Henning Kamp 		g_topology_lock();
7200d7935fdSAttilio Rao 		g_vfs_close(cp);
7219a135592SPoul-Henning Kamp 		g_topology_unlock();
7229a135592SPoul-Henning Kamp 	}
72327a0bc89SDoug Rabson 	if (pmp) {
724a84ec05fSKonstantin Belousov 		lockdestroy(&pmp->pm_fatlock);
725952a6212SJordan K. Hubbard 		free(pmp->pm_inusemap, M_MSDOSFSFAT);
726952a6212SJordan K. Hubbard 		free(pmp, M_MSDOSFSMNT);
72777465d93SAlfred Perlstein 		mp->mnt_data = NULL;
72827a0bc89SDoug Rabson 	}
729bb8297e6SKonstantin Belousov 	atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
730c72ae142SJohn Baldwin 	dev_rel(dev);
731952a6212SJordan K. Hubbard 	return (error);
73227a0bc89SDoug Rabson }
73327a0bc89SDoug Rabson 
73427a0bc89SDoug Rabson /*
73527a0bc89SDoug Rabson  * Unmount the filesystem described by mp.
73627a0bc89SDoug Rabson  */
7377fefffeeSPoul-Henning Kamp static int
738dfd233edSAttilio Rao msdosfs_unmount(struct mount *mp, int mntflags)
73927a0bc89SDoug Rabson {
740952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp;
741952a6212SJordan K. Hubbard 	int error, flags;
74227a0bc89SDoug Rabson 
7432359e2dcSKonstantin Belousov 	error = flags = 0;
7442359e2dcSKonstantin Belousov 	pmp = VFSTOMSDOSFS(mp);
7452359e2dcSKonstantin Belousov 	if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0)
746b2344ab5SKonstantin Belousov 		error = msdosfs_sync(mp, MNT_WAIT);
7472359e2dcSKonstantin Belousov 	if ((mntflags & MNT_FORCE) != 0)
74827a0bc89SDoug Rabson 		flags |= FORCECLOSE;
7492359e2dcSKonstantin Belousov 	else if (error != 0)
750b2344ab5SKonstantin Belousov 		return (error);
751dfd233edSAttilio Rao 	error = vflush(mp, 0, flags, curthread);
752b2344ab5SKonstantin Belousov 	if (error != 0 && error != ENXIO)
753b2344ab5SKonstantin Belousov 		return (error);
7543247c9ddSXin LI 	if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
7553247c9ddSXin LI 		error = markvoldirty(pmp, 0);
7564f560d75SEdward Tomasz Napierala 		if (error && error != ENXIO) {
7573247c9ddSXin LI 			(void)markvoldirty(pmp, 1);
7583247c9ddSXin LI 			return (error);
7593247c9ddSXin LI 		}
7603247c9ddSXin LI 	}
761c4f02a89SMax Khon 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
762c4f02a89SMax Khon 		if (pmp->pm_w2u)
763c4f02a89SMax Khon 			msdosfs_iconv->close(pmp->pm_w2u);
764c4f02a89SMax Khon 		if (pmp->pm_u2w)
765c4f02a89SMax Khon 			msdosfs_iconv->close(pmp->pm_u2w);
766c4f02a89SMax Khon 		if (pmp->pm_d2u)
767c4f02a89SMax Khon 			msdosfs_iconv->close(pmp->pm_d2u);
768c4f02a89SMax Khon 		if (pmp->pm_u2d)
769c4f02a89SMax Khon 			msdosfs_iconv->close(pmp->pm_u2d);
770c4f02a89SMax Khon 	}
771cede1f56STom Rhodes 
772952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG
773952a6212SJordan K. Hubbard 	{
774952a6212SJordan K. Hubbard 		struct vnode *vp = pmp->pm_devvp;
775698b1a66SJeff Roberson 		struct bufobj *bo;
776952a6212SJordan K. Hubbard 
777698b1a66SJeff Roberson 		bo = &vp->v_bufobj;
778698b1a66SJeff Roberson 		BO_LOCK(bo);
7794d93c0beSJeff Roberson 		VI_LOCK(vp);
780f69d42a1SPoul-Henning Kamp 		vn_printf(vp,
781f69d42a1SPoul-Henning Kamp 		    "msdosfs_umount(): just before calling VOP_CLOSE()\n");
782952a6212SJordan K. Hubbard 		printf("freef %p, freeb %p, mount %p\n",
783f257ebbbSKirk McKusick 		    TAILQ_NEXT(vp, v_actfreelist), vp->v_actfreelist.tqe_prev,
784952a6212SJordan K. Hubbard 		    vp->v_mount);
785952a6212SJordan K. Hubbard 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
786156cb265SPoul-Henning Kamp 		    TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
787156cb265SPoul-Henning Kamp 		    TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
788156cb265SPoul-Henning Kamp 		    vp->v_bufobj.bo_numoutput, vp->v_type);
7894d93c0beSJeff Roberson 		VI_UNLOCK(vp);
790698b1a66SJeff Roberson 		BO_UNLOCK(bo);
791952a6212SJordan K. Hubbard 	}
792952a6212SJordan K. Hubbard #endif
7939a135592SPoul-Henning Kamp 	g_topology_lock();
7940d7935fdSAttilio Rao 	g_vfs_close(pmp->pm_cp);
7959a135592SPoul-Henning Kamp 	g_topology_unlock();
796bb8297e6SKonstantin Belousov 	atomic_store_rel_ptr((uintptr_t *)&pmp->pm_dev->si_mountpt, 0);
79727a0bc89SDoug Rabson 	vrele(pmp->pm_devvp);
798c72ae142SJohn Baldwin 	dev_rel(pmp->pm_dev);
799952a6212SJordan K. Hubbard 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
80023b6c230SKonstantin Belousov 	lockdestroy(&pmp->pm_fatlock);
801952a6212SJordan K. Hubbard 	free(pmp, M_MSDOSFSMNT);
80277465d93SAlfred Perlstein 	mp->mnt_data = NULL;
8035da56ddbSTor Egge 	MNT_ILOCK(mp);
804cc9d8990SPeter Wemm 	mp->mnt_flag &= ~MNT_LOCAL;
8055da56ddbSTor Egge 	MNT_IUNLOCK(mp);
8064f560d75SEdward Tomasz Napierala 	return (error);
80727a0bc89SDoug Rabson }
80827a0bc89SDoug Rabson 
8097fefffeeSPoul-Henning Kamp static int
810dfd233edSAttilio Rao msdosfs_root(struct mount *mp, int flags, struct vnode **vpp)
81127a0bc89SDoug Rabson {
812952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
81327a0bc89SDoug Rabson 	struct denode *ndep;
81427a0bc89SDoug Rabson 	int error;
81527a0bc89SDoug Rabson 
81627a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
817952a6212SJordan K. Hubbard 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
81827a0bc89SDoug Rabson #endif
819952a6212SJordan K. Hubbard 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
820952a6212SJordan K. Hubbard 	if (error)
821952a6212SJordan K. Hubbard 		return (error);
82227a0bc89SDoug Rabson 	*vpp = DETOV(ndep);
823952a6212SJordan K. Hubbard 	return (0);
82427a0bc89SDoug Rabson }
82527a0bc89SDoug Rabson 
8267fefffeeSPoul-Henning Kamp static int
827dfd233edSAttilio Rao msdosfs_statfs(struct mount *mp, struct statfs *sbp)
82827a0bc89SDoug Rabson {
829952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp;
83027a0bc89SDoug Rabson 
831952a6212SJordan K. Hubbard 	pmp = VFSTOMSDOSFS(mp);
83227a0bc89SDoug Rabson 	sbp->f_bsize = pmp->pm_bpcluster;
83327a0bc89SDoug Rabson 	sbp->f_iosize = pmp->pm_bpcluster;
834499d3ffaSBoris Popov 	sbp->f_blocks = pmp->pm_maxcluster + 1;
83527a0bc89SDoug Rabson 	sbp->f_bfree = pmp->pm_freeclustercount;
83627a0bc89SDoug Rabson 	sbp->f_bavail = pmp->pm_freeclustercount;
83727a0bc89SDoug Rabson 	sbp->f_files = pmp->pm_RootDirEnts;	/* XXX */
83827a0bc89SDoug Rabson 	sbp->f_ffree = 0;	/* what to put in here? */
839952a6212SJordan K. Hubbard 	return (0);
84027a0bc89SDoug Rabson }
84127a0bc89SDoug Rabson 
842bb7ca822SKonstantin Belousov /*
843bb7ca822SKonstantin Belousov  * If we have an FSInfo block, update it.
844bb7ca822SKonstantin Belousov  */
845bb7ca822SKonstantin Belousov static int
846bb7ca822SKonstantin Belousov msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
847bb7ca822SKonstantin Belousov {
848bb7ca822SKonstantin Belousov 	struct fsinfo *fp;
849bb7ca822SKonstantin Belousov 	struct buf *bp;
850bb7ca822SKonstantin Belousov 	int error;
851bb7ca822SKonstantin Belousov 
852bb7ca822SKonstantin Belousov 	MSDOSFS_LOCK_MP(pmp);
853bb7ca822SKonstantin Belousov 	if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
854bb7ca822SKonstantin Belousov 		error = 0;
855bb7ca822SKonstantin Belousov 		goto unlock;
856bb7ca822SKonstantin Belousov 	}
857bb7ca822SKonstantin Belousov 	error = bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
858bb7ca822SKonstantin Belousov 	    NOCRED, &bp);
859bb7ca822SKonstantin Belousov 	if (error != 0) {
860bb7ca822SKonstantin Belousov 		brelse(bp);
861bb7ca822SKonstantin Belousov 		goto unlock;
862bb7ca822SKonstantin Belousov 	}
863bb7ca822SKonstantin Belousov 	fp = (struct fsinfo *)bp->b_data;
864bb7ca822SKonstantin Belousov 	putulong(fp->fsinfree, pmp->pm_freeclustercount);
865bb7ca822SKonstantin Belousov 	putulong(fp->fsinxtfree, pmp->pm_nxtfree);
866bb7ca822SKonstantin Belousov 	pmp->pm_flags &= ~MSDOSFS_FSIMOD;
867bb7ca822SKonstantin Belousov 	if (waitfor == MNT_WAIT)
868bb7ca822SKonstantin Belousov 		error = bwrite(bp);
869bb7ca822SKonstantin Belousov 	else
870bb7ca822SKonstantin Belousov 		bawrite(bp);
871bb7ca822SKonstantin Belousov unlock:
872bb7ca822SKonstantin Belousov 	MSDOSFS_UNLOCK_MP(pmp);
873bb7ca822SKonstantin Belousov 	return (error);
874bb7ca822SKonstantin Belousov }
875bb7ca822SKonstantin Belousov 
8767fefffeeSPoul-Henning Kamp static int
877dfd233edSAttilio Rao msdosfs_sync(struct mount *mp, int waitfor)
87827a0bc89SDoug Rabson {
879952a6212SJordan K. Hubbard 	struct vnode *vp, *nvp;
880dfd233edSAttilio Rao 	struct thread *td;
88127a0bc89SDoug Rabson 	struct denode *dep;
882952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
883952a6212SJordan K. Hubbard 	int error, allerror = 0;
88427a0bc89SDoug Rabson 
885dfd233edSAttilio Rao 	td = curthread;
886dfd233edSAttilio Rao 
88727a0bc89SDoug Rabson 	/*
8889287dbaaSEd Maste 	 * If we ever switch to not updating all of the FATs all the time,
88927a0bc89SDoug Rabson 	 * this would be the place to update them from the first one.
89027a0bc89SDoug Rabson 	 */
891dfd5dee1SPeter Wemm 	if (pmp->pm_fmod != 0) {
892952a6212SJordan K. Hubbard 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
89327a0bc89SDoug Rabson 			panic("msdosfs_sync: rofs mod");
89427a0bc89SDoug Rabson 		else {
8959287dbaaSEd Maste 			/* update FATs here */
89627a0bc89SDoug Rabson 		}
897dfd5dee1SPeter Wemm 	}
89827a0bc89SDoug Rabson 	/*
899952a6212SJordan K. Hubbard 	 * Write back each (modified) denode.
90027a0bc89SDoug Rabson 	 */
90127a0bc89SDoug Rabson loop:
90271469bb3SKirk McKusick 	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
90371469bb3SKirk McKusick 		if (vp->v_type == VNON) {
9044ab2c8bdSJeff Roberson 			VI_UNLOCK(vp);
9054ab2c8bdSJeff Roberson 			continue;
9064ab2c8bdSJeff Roberson 		}
90727a0bc89SDoug Rabson 		dep = VTODE(vp);
908f00f5d71SPoul-Henning Kamp 		if ((dep->de_flag &
909c681be37SDmitrij Tejblum 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
910156cb265SPoul-Henning Kamp 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
911f00f5d71SPoul-Henning Kamp 		    waitfor == MNT_LAZY)) {
9124d93c0beSJeff Roberson 			VI_UNLOCK(vp);
91327a0bc89SDoug Rabson 			continue;
914af3f60d5SBruce Evans 		}
915b40ce416SJulian Elischer 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
916af3f60d5SBruce Evans 		if (error) {
917af3f60d5SBruce Evans 			if (error == ENOENT)
91827a0bc89SDoug Rabson 				goto loop;
919af3f60d5SBruce Evans 			continue;
920af3f60d5SBruce Evans 		}
9218df6bac4SPoul-Henning Kamp 		error = VOP_FSYNC(vp, waitfor, td);
922c3c6d51eSPoul-Henning Kamp 		if (error)
92327a0bc89SDoug Rabson 			allerror = error;
92422db15c0SAttilio Rao 		VOP_UNLOCK(vp, 0);
925cb9ddc80SAlexander Kabaev 		vrele(vp);
92627a0bc89SDoug Rabson 	}
92727a0bc89SDoug Rabson 
92827a0bc89SDoug Rabson 	/*
92927a0bc89SDoug Rabson 	 * Flush filesystem control info.
93027a0bc89SDoug Rabson 	 */
931c681be37SDmitrij Tejblum 	if (waitfor != MNT_LAZY) {
932cb05b60aSAttilio Rao 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
9338df6bac4SPoul-Henning Kamp 		error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
934c3c6d51eSPoul-Henning Kamp 		if (error)
93527a0bc89SDoug Rabson 			allerror = error;
93622db15c0SAttilio Rao 		VOP_UNLOCK(pmp->pm_devvp, 0);
937c681be37SDmitrij Tejblum 	}
938bb7ca822SKonstantin Belousov 
939bb7ca822SKonstantin Belousov 	error = msdosfs_fsiflush(pmp, waitfor);
940bb7ca822SKonstantin Belousov 	if (error != 0)
941bb7ca822SKonstantin Belousov 		allerror = error;
942952a6212SJordan K. Hubbard 	return (allerror);
94327a0bc89SDoug Rabson }
94427a0bc89SDoug Rabson 
9451be5bc74STom Rhodes static int
946694a586aSRick Macklem msdosfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
9471be5bc74STom Rhodes {
9481be5bc74STom Rhodes 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
9491be5bc74STom Rhodes 	struct defid *defhp = (struct defid *) fhp;
9501be5bc74STom Rhodes 	struct denode *dep;
9511be5bc74STom Rhodes 	int error;
9521be5bc74STom Rhodes 
9531be5bc74STom Rhodes 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
9541be5bc74STom Rhodes 	if (error) {
9551be5bc74STom Rhodes 		*vpp = NULLVP;
9561be5bc74STom Rhodes 		return (error);
9571be5bc74STom Rhodes 	}
9581be5bc74STom Rhodes 	*vpp = DETOV(dep);
9591be5bc74STom Rhodes 	vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
9601be5bc74STom Rhodes 	return (0);
9611be5bc74STom Rhodes }
9621be5bc74STom Rhodes 
96330ffadf3SPoul-Henning Kamp static struct vfsops msdosfs_vfsops = {
9641be5bc74STom Rhodes 	.vfs_fhtovp =		msdosfs_fhtovp,
9656a4b48f4SPoul-Henning Kamp 	.vfs_mount =		msdosfs_mount,
9666a4b48f4SPoul-Henning Kamp 	.vfs_cmount =		msdosfs_cmount,
9677652131bSPoul-Henning Kamp 	.vfs_root =		msdosfs_root,
9687652131bSPoul-Henning Kamp 	.vfs_statfs =		msdosfs_statfs,
9697652131bSPoul-Henning Kamp 	.vfs_sync =		msdosfs_sync,
9707652131bSPoul-Henning Kamp 	.vfs_unmount =		msdosfs_unmount,
97127a0bc89SDoug Rabson };
972c901836cSGarrett Wollman 
9734ccd7546SRuslan Ermilov VFS_SET(msdosfs_vfsops, msdosfs, 0);
974c4f02a89SMax Khon MODULE_VERSION(msdosfs, 1);
975