xref: /freebsd/sys/fs/msdosfs/msdosfs_vfsops.c (revision e6e370a7fe930e04cec38bcc2e06be127ed7ee02)
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  */
3527a0bc89SDoug Rabson /*
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>
5365baf8f0SBruce Evans #include <sys/conf.h>
5427a0bc89SDoug Rabson #include <sys/namei.h>
5527a0bc89SDoug Rabson #include <sys/proc.h>
5627a0bc89SDoug Rabson #include <sys/kernel.h>
5727a0bc89SDoug Rabson #include <sys/vnode.h>
5827a0bc89SDoug Rabson #include <sys/mount.h>
599626b608SPoul-Henning Kamp #include <sys/bio.h>
6027a0bc89SDoug Rabson #include <sys/buf.h>
613ac4d1efSBruce Evans #include <sys/fcntl.h>
6227a0bc89SDoug Rabson #include <sys/malloc.h>
63952a6212SJordan K. Hubbard #include <sys/stat.h> 				/* defines ALLPERMS */
641b367556SJason Evans #include <sys/mutex.h>
65a18b1f1dSJason Evans 
661166fb51SRuslan Ermilov #include <fs/msdosfs/bpb.h>
671166fb51SRuslan Ermilov #include <fs/msdosfs/bootsect.h>
681166fb51SRuslan Ermilov #include <fs/msdosfs/direntry.h>
691166fb51SRuslan Ermilov #include <fs/msdosfs/denode.h>
701166fb51SRuslan Ermilov #include <fs/msdosfs/msdosfsmount.h>
711166fb51SRuslan Ermilov #include <fs/msdosfs/fat.h>
7227a0bc89SDoug Rabson 
737c58e473SMatthew Dillon #define MSDOSFS_DFLTBSIZE       4096
747c58e473SMatthew Dillon 
7501f6cfbaSYoshihiro Takahashi #if 1 /*def PC98*/
7601f6cfbaSYoshihiro Takahashi /*
7701f6cfbaSYoshihiro Takahashi  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
7801f6cfbaSYoshihiro Takahashi  *       garbage or a random value :-{
7901f6cfbaSYoshihiro Takahashi  *       If you want to use that broken-signatured media, define the
8001f6cfbaSYoshihiro Takahashi  *       following symbol even though PC/AT.
8101f6cfbaSYoshihiro Takahashi  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
8201f6cfbaSYoshihiro Takahashi  */
8301f6cfbaSYoshihiro Takahashi #define	MSDOSFS_NOCHECKSIG
8401f6cfbaSYoshihiro Takahashi #endif
8501f6cfbaSYoshihiro Takahashi 
86a1c995b6SPoul-Henning Kamp MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
87a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
8855166637SPoul-Henning Kamp 
8911caded3SAlfred Perlstein static int	update_mp(struct mount *mp, struct msdosfs_args *argp);
9011caded3SAlfred Perlstein static int	mountmsdosfs(struct vnode *devvp, struct mount *mp,
9111caded3SAlfred Perlstein 		    struct thread *td, struct msdosfs_args *argp);
9211caded3SAlfred Perlstein static int	msdosfs_fhtovp(struct mount *, struct fid *, struct vnode **);
9311caded3SAlfred Perlstein static int	msdosfs_mount(struct mount *, char *, caddr_t,
9411caded3SAlfred Perlstein 		    struct nameidata *, struct thread *);
9511caded3SAlfred Perlstein static int	msdosfs_root(struct mount *, struct vnode **);
9611caded3SAlfred Perlstein static int	msdosfs_statfs(struct mount *, struct statfs *,
9711caded3SAlfred Perlstein 		    struct thread *);
9811caded3SAlfred Perlstein static int	msdosfs_sync(struct mount *, int, struct ucred *,
9911caded3SAlfred Perlstein 		    struct thread *);
10011caded3SAlfred Perlstein static int	msdosfs_unmount(struct mount *, int, struct thread *);
10111caded3SAlfred Perlstein static int	msdosfs_vptofh(struct vnode *, struct fid *);
102af482601SBruce Evans 
103952a6212SJordan K. Hubbard static int
104952a6212SJordan K. Hubbard update_mp(mp, argp)
105952a6212SJordan K. Hubbard 	struct mount *mp;
106952a6212SJordan K. Hubbard 	struct msdosfs_args *argp;
107952a6212SJordan K. Hubbard {
108952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
109952a6212SJordan K. Hubbard 	int error;
110952a6212SJordan K. Hubbard 
111952a6212SJordan K. Hubbard 	pmp->pm_gid = argp->gid;
112952a6212SJordan K. Hubbard 	pmp->pm_uid = argp->uid;
113952a6212SJordan K. Hubbard 	pmp->pm_mask = argp->mask & ALLPERMS;
114952a6212SJordan K. Hubbard 	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
1157391f611SAndrey A. Chernov 	if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
11613df76f2SAndrey A. Chernov 		bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
1177391f611SAndrey A. Chernov 		bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
1187391f611SAndrey A. Chernov 		bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
1197391f611SAndrey A. Chernov 	}
1207391f611SAndrey A. Chernov 	if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
12164fb806cSAndrey A. Chernov 		bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
1227391f611SAndrey A. Chernov 		bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
1237391f611SAndrey A. Chernov 	}
124952a6212SJordan K. Hubbard 
125952a6212SJordan K. Hubbard 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
126952a6212SJordan K. Hubbard 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
127952a6212SJordan K. Hubbard 	else if (!(pmp->pm_flags &
128952a6212SJordan K. Hubbard 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
129952a6212SJordan K. Hubbard 		struct vnode *rootvp;
130952a6212SJordan K. Hubbard 
131952a6212SJordan K. Hubbard 		/*
132952a6212SJordan K. Hubbard 		 * Try to divine whether to support Win'95 long filenames
133952a6212SJordan K. Hubbard 		 */
134952a6212SJordan K. Hubbard 		if (FAT32(pmp))
135952a6212SJordan K. Hubbard 			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
136952a6212SJordan K. Hubbard 		else {
137952a6212SJordan K. Hubbard 			if ((error = msdosfs_root(mp, &rootvp)) != 0)
138952a6212SJordan K. Hubbard 				return error;
139952a6212SJordan K. Hubbard 			pmp->pm_flags |= findwin95(VTODE(rootvp))
140952a6212SJordan K. Hubbard 				? MSDOSFSMNT_LONGNAME
141952a6212SJordan K. Hubbard 					: MSDOSFSMNT_SHORTNAME;
142952a6212SJordan K. Hubbard 			vput(rootvp);
143952a6212SJordan K. Hubbard 		}
144952a6212SJordan K. Hubbard 	}
145952a6212SJordan K. Hubbard 	return 0;
146952a6212SJordan K. Hubbard }
147952a6212SJordan K. Hubbard 
14827a0bc89SDoug Rabson /*
14927a0bc89SDoug Rabson  * mp - path - addr in user space of mount point (ie /usr or whatever)
15027a0bc89SDoug Rabson  * data - addr in user space of mount params including the name of the block
15127a0bc89SDoug Rabson  * special file to treat as a filesystem.
15227a0bc89SDoug Rabson  */
1537fefffeeSPoul-Henning Kamp static int
154b40ce416SJulian Elischer msdosfs_mount(mp, path, data, ndp, td)
15527a0bc89SDoug Rabson 	struct mount *mp;
15627a0bc89SDoug Rabson 	char *path;
15727a0bc89SDoug Rabson 	caddr_t data;
15827a0bc89SDoug Rabson 	struct nameidata *ndp;
159b40ce416SJulian Elischer 	struct thread *td;
16027a0bc89SDoug Rabson {
16127a0bc89SDoug Rabson 	struct vnode *devvp;	  /* vnode for blk device to mount */
16227a0bc89SDoug Rabson 	struct msdosfs_args args; /* will hold data from mount request */
163952a6212SJordan K. Hubbard 	/* msdosfs specific mount control block */
164952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = NULL;
165952a6212SJordan K. Hubbard 	size_t size;
16627a0bc89SDoug Rabson 	int error, flags;
167952a6212SJordan K. Hubbard 	mode_t accessmode;
16827a0bc89SDoug Rabson 
169c3c6d51eSPoul-Henning Kamp 	error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
170c3c6d51eSPoul-Henning Kamp 	if (error)
171952a6212SJordan K. Hubbard 		return (error);
172206faeeeSDmitrij Tejblum 	if (args.magic != MSDOSFS_ARGSMAGIC)
173952a6212SJordan K. Hubbard 		args.flags = 0;
17427a0bc89SDoug Rabson 	/*
175952a6212SJordan K. Hubbard 	 * If updating, check whether changing from read-only to
176952a6212SJordan K. Hubbard 	 * read/write; if there is no device name, that's all we do.
17727a0bc89SDoug Rabson 	 */
17827a0bc89SDoug Rabson 	if (mp->mnt_flag & MNT_UPDATE) {
179952a6212SJordan K. Hubbard 		pmp = VFSTOMSDOSFS(mp);
18027a0bc89SDoug Rabson 		error = 0;
181952a6212SJordan K. Hubbard 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
18227a0bc89SDoug Rabson 			flags = WRITECLOSE;
18327a0bc89SDoug Rabson 			if (mp->mnt_flag & MNT_FORCE)
18427a0bc89SDoug Rabson 				flags |= FORCECLOSE;
1850864ef1eSIan Dowse 			error = vflush(mp, 0, flags);
18627a0bc89SDoug Rabson 		}
18727a0bc89SDoug Rabson 		if (!error && (mp->mnt_flag & MNT_RELOAD))
18827a0bc89SDoug Rabson 			/* not yet implemented */
189952a6212SJordan K. Hubbard 			error = EOPNOTSUPP;
19027a0bc89SDoug Rabson 		if (error)
191952a6212SJordan K. Hubbard 			return (error);
192952a6212SJordan K. Hubbard 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
193952a6212SJordan K. Hubbard 			/*
194952a6212SJordan K. Hubbard 			 * If upgrade to read-write by non-root, then verify
195952a6212SJordan K. Hubbard 			 * that user has necessary permissions on the device.
196952a6212SJordan K. Hubbard 			 */
19744731cabSJohn Baldwin 			if (suser(td)) {
198952a6212SJordan K. Hubbard 				devvp = pmp->pm_devvp;
199b40ce416SJulian Elischer 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
200952a6212SJordan K. Hubbard 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
201a854ed98SJohn Baldwin 						   td->td_ucred, td);
202952a6212SJordan K. Hubbard 				if (error) {
203b40ce416SJulian Elischer 					VOP_UNLOCK(devvp, 0, td);
204952a6212SJordan K. Hubbard 					return (error);
205952a6212SJordan K. Hubbard 				}
206b40ce416SJulian Elischer 				VOP_UNLOCK(devvp, 0, td);
207952a6212SJordan K. Hubbard 			}
208952a6212SJordan K. Hubbard 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
209952a6212SJordan K. Hubbard 		}
21027a0bc89SDoug Rabson 		if (args.fspec == 0) {
211952a6212SJordan K. Hubbard #ifdef	__notyet__	/* doesn't work correctly with current mountd	XXX */
212952a6212SJordan K. Hubbard 			if (args.flags & MSDOSFSMNT_MNTOPT) {
213952a6212SJordan K. Hubbard 				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
214952a6212SJordan K. Hubbard 				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
215952a6212SJordan K. Hubbard 				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
216952a6212SJordan K. Hubbard 					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
217952a6212SJordan K. Hubbard 			}
218952a6212SJordan K. Hubbard #endif
21927a0bc89SDoug Rabson 			/*
22027a0bc89SDoug Rabson 			 * Process export requests.
22127a0bc89SDoug Rabson 			 */
222a13234bbSPoul-Henning Kamp 			return (vfs_export(mp, &args.export));
22327a0bc89SDoug Rabson 		}
224952a6212SJordan K. Hubbard 	}
22527a0bc89SDoug Rabson 	/*
226952a6212SJordan K. Hubbard 	 * Not an update, or updating the name: look up the name
227952a6212SJordan K. Hubbard 	 * and verify that it refers to a sensible block device.
22827a0bc89SDoug Rabson 	 */
229b40ce416SJulian Elischer 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
23027a0bc89SDoug Rabson 	error = namei(ndp);
231952a6212SJordan K. Hubbard 	if (error)
232952a6212SJordan K. Hubbard 		return (error);
23327a0bc89SDoug Rabson 	devvp = ndp->ni_vp;
234762e6b85SEivind Eklund 	NDFREE(ndp, NDF_ONLY_PNBUF);
235952a6212SJordan K. Hubbard 
236ba4ad1fcSPoul-Henning Kamp 	if (!vn_isdisk(devvp, &error)) {
23727a0bc89SDoug Rabson 		vrele(devvp);
238ba4ad1fcSPoul-Henning Kamp 		return (error);
23927a0bc89SDoug Rabson 	}
24027a0bc89SDoug Rabson 	/*
241952a6212SJordan K. Hubbard 	 * If mount by non-root, then verify that user has necessary
242952a6212SJordan K. Hubbard 	 * permissions on the device.
24327a0bc89SDoug Rabson 	 */
24444731cabSJohn Baldwin 	if (suser(td)) {
245952a6212SJordan K. Hubbard 		accessmode = VREAD;
246952a6212SJordan K. Hubbard 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
247952a6212SJordan K. Hubbard 			accessmode |= VWRITE;
248b40ce416SJulian Elischer 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
249a854ed98SJohn Baldwin 		error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
250952a6212SJordan K. Hubbard 		if (error) {
251952a6212SJordan K. Hubbard 			vput(devvp);
252952a6212SJordan K. Hubbard 			return (error);
253952a6212SJordan K. Hubbard 		}
254b40ce416SJulian Elischer 		VOP_UNLOCK(devvp, 0, td);
255952a6212SJordan K. Hubbard 	}
256952a6212SJordan K. Hubbard 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
257b40ce416SJulian Elischer 		error = mountmsdosfs(devvp, mp, td, &args);
258952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
259952a6212SJordan K. Hubbard 		pmp = VFSTOMSDOSFS(mp);
260952a6212SJordan K. Hubbard #endif
261952a6212SJordan K. Hubbard 	} else {
26227a0bc89SDoug Rabson 		if (devvp != pmp->pm_devvp)
263952a6212SJordan K. Hubbard 			error = EINVAL;	/* XXX needs translation */
26427a0bc89SDoug Rabson 		else
26527a0bc89SDoug Rabson 			vrele(devvp);
26627a0bc89SDoug Rabson 	}
26727a0bc89SDoug Rabson 	if (error) {
26827a0bc89SDoug Rabson 		vrele(devvp);
269952a6212SJordan K. Hubbard 		return (error);
270952a6212SJordan K. Hubbard 	}
271952a6212SJordan K. Hubbard 
272952a6212SJordan K. Hubbard 	error = update_mp(mp, &args);
273952a6212SJordan K. Hubbard 	if (error) {
274b40ce416SJulian Elischer 		msdosfs_unmount(mp, MNT_FORCE, td);
27527a0bc89SDoug Rabson 		return error;
27627a0bc89SDoug Rabson 	}
277952a6212SJordan K. Hubbard 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
278952a6212SJordan K. Hubbard 	    &size);
279952a6212SJordan K. Hubbard 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
280b40ce416SJulian Elischer 	(void) msdosfs_statfs(mp, &mp->mnt_stat, td);
28127a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
282623ae52eSPoul-Henning Kamp 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
28327a0bc89SDoug Rabson #endif
284952a6212SJordan K. Hubbard 	return (0);
28527a0bc89SDoug Rabson }
28627a0bc89SDoug Rabson 
2877fefffeeSPoul-Henning Kamp static int
288b40ce416SJulian Elischer mountmsdosfs(devvp, mp, td, argp)
28927a0bc89SDoug Rabson 	struct vnode *devvp;
29027a0bc89SDoug Rabson 	struct mount *mp;
291b40ce416SJulian Elischer 	struct thread *td;
292952a6212SJordan K. Hubbard 	struct msdosfs_args *argp;
29327a0bc89SDoug Rabson {
294952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp;
295952a6212SJordan K. Hubbard 	struct buf *bp;
29627a0bc89SDoug Rabson 	dev_t dev = devvp->v_rdev;
29727a0bc89SDoug Rabson 	union bootsector *bsp;
29827a0bc89SDoug Rabson 	struct byte_bpb33 *b33;
29927a0bc89SDoug Rabson 	struct byte_bpb50 *b50;
300952a6212SJordan K. Hubbard 	struct byte_bpb710 *b710;
301952a6212SJordan K. Hubbard 	u_int8_t SecPerClust;
302499d3ffaSBoris Popov 	u_long clusters;
303952a6212SJordan K. Hubbard 	int	ronly, error;
30427a0bc89SDoug Rabson 
30527a0bc89SDoug Rabson 	/*
306952a6212SJordan K. Hubbard 	 * Disallow multiple mounts of the same device.
307952a6212SJordan K. Hubbard 	 * Disallow mounting of a device that is currently in use
308952a6212SJordan K. Hubbard 	 * (except for root, which might share swap device for miniroot).
309952a6212SJordan K. Hubbard 	 * Flush out any old buffers remaining from a previous use.
31027a0bc89SDoug Rabson 	 */
311c3c6d51eSPoul-Henning Kamp 	error = vfs_mountedon(devvp);
312c3c6d51eSPoul-Henning Kamp 	if (error)
313952a6212SJordan K. Hubbard 		return (error);
314952a6212SJordan K. Hubbard 	if (vcount(devvp) > 1 && devvp != rootvp)
315952a6212SJordan K. Hubbard 		return (EBUSY);
316b40ce416SJulian Elischer 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
317a854ed98SJohn Baldwin 	error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0);
318b40ce416SJulian Elischer 	VOP_UNLOCK(devvp, 0, td);
319c3c6d51eSPoul-Henning Kamp 	if (error)
320952a6212SJordan K. Hubbard 		return (error);
32127a0bc89SDoug Rabson 
322952a6212SJordan K. Hubbard 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
323b40ce416SJulian Elischer 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
324b40ce416SJulian Elischer 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td);
325b40ce416SJulian Elischer 	VOP_UNLOCK(devvp, 0, td);
326c3c6d51eSPoul-Henning Kamp 	if (error)
327952a6212SJordan K. Hubbard 		return (error);
328952a6212SJordan K. Hubbard 
329952a6212SJordan K. Hubbard 	bp  = NULL; /* both used in error_exit */
330952a6212SJordan K. Hubbard 	pmp = NULL;
331952a6212SJordan K. Hubbard 
33227a0bc89SDoug Rabson 	/*
333952a6212SJordan K. Hubbard 	 * Read the boot sector of the filesystem, and then check the
334952a6212SJordan K. Hubbard 	 * boot signature.  If not a dos boot sector then error out.
33501f6cfbaSYoshihiro Takahashi 	 *
33601f6cfbaSYoshihiro Takahashi 	 * NOTE: 2048 is a maximum sector size in current...
33727a0bc89SDoug Rabson 	 */
33801f6cfbaSYoshihiro Takahashi 	error = bread(devvp, 0, 2048, NOCRED, &bp);
339c3c6d51eSPoul-Henning Kamp 	if (error)
34027a0bc89SDoug Rabson 		goto error_exit;
341952a6212SJordan K. Hubbard 	bp->b_flags |= B_AGE;
342952a6212SJordan K. Hubbard 	bsp = (union bootsector *)bp->b_data;
34327a0bc89SDoug Rabson 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
34427a0bc89SDoug Rabson 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
345952a6212SJordan K. Hubbard 	b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
346952a6212SJordan K. Hubbard 
34701f6cfbaSYoshihiro Takahashi #ifndef MSDOSFS_NOCHECKSIG
348952a6212SJordan K. Hubbard 	if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
349952a6212SJordan K. Hubbard 	    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
35027a0bc89SDoug Rabson 		error = EINVAL;
35127a0bc89SDoug Rabson 		goto error_exit;
35227a0bc89SDoug Rabson 	}
35301f6cfbaSYoshihiro Takahashi #endif
35427a0bc89SDoug Rabson 
3557cc0979fSDavid Malone 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
35627a0bc89SDoug Rabson 	pmp->pm_mountp = mp;
35727a0bc89SDoug Rabson 
35827a0bc89SDoug Rabson 	/*
35927a0bc89SDoug Rabson 	 * Compute several useful quantities from the bpb in the
36027a0bc89SDoug Rabson 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
36127a0bc89SDoug Rabson 	 * the fields that are different between dos 5 and dos 3.3.
36227a0bc89SDoug Rabson 	 */
363952a6212SJordan K. Hubbard 	SecPerClust = b50->bpbSecPerClust;
36427a0bc89SDoug Rabson 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
36527a0bc89SDoug Rabson 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
36627a0bc89SDoug Rabson 	pmp->pm_FATs = b50->bpbFATs;
36727a0bc89SDoug Rabson 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
36827a0bc89SDoug Rabson 	pmp->pm_Sectors = getushort(b50->bpbSectors);
36927a0bc89SDoug Rabson 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
37027a0bc89SDoug Rabson 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
37127a0bc89SDoug Rabson 	pmp->pm_Heads = getushort(b50->bpbHeads);
372952a6212SJordan K. Hubbard 	pmp->pm_Media = b50->bpbMedia;
37327a0bc89SDoug Rabson 
37401f6cfbaSYoshihiro Takahashi 	/* calculate the ratio of sector size to DEV_BSIZE */
37501f6cfbaSYoshihiro Takahashi 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
37601f6cfbaSYoshihiro Takahashi 
37727a0bc89SDoug Rabson 	/* XXX - We should probably check more values here */
378952a6212SJordan K. Hubbard 	if (!pmp->pm_BytesPerSec || !SecPerClust
379952a6212SJordan K. Hubbard 		|| !pmp->pm_Heads || pmp->pm_Heads > 255
380ad63a118SSatoshi Asami #ifdef PC98
381952a6212SJordan K. Hubbard     		|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
382ad63a118SSatoshi Asami #else
383952a6212SJordan K. Hubbard 		|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
384ad63a118SSatoshi Asami #endif
38527a0bc89SDoug Rabson 		error = EINVAL;
38627a0bc89SDoug Rabson 		goto error_exit;
38727a0bc89SDoug Rabson 	}
38827a0bc89SDoug Rabson 
38927a0bc89SDoug Rabson 	if (pmp->pm_Sectors == 0) {
39027a0bc89SDoug Rabson 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
39127a0bc89SDoug Rabson 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
39227a0bc89SDoug Rabson 	} else {
39327a0bc89SDoug Rabson 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
39427a0bc89SDoug Rabson 		pmp->pm_HugeSectors = pmp->pm_Sectors;
39527a0bc89SDoug Rabson 	}
396c681be37SDmitrij Tejblum 	if (pmp->pm_HugeSectors > 0xffffffff /
397c681be37SDmitrij Tejblum 	    (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
398952a6212SJordan K. Hubbard 		/*
399952a6212SJordan K. Hubbard 		 * We cannot deal currently with this size of disk
400952a6212SJordan K. Hubbard 		 * due to fileid limitations (see msdosfs_getattr and
401952a6212SJordan K. Hubbard 		 * msdosfs_readdir)
402952a6212SJordan K. Hubbard 		 */
403952a6212SJordan K. Hubbard 		error = EINVAL;
404aaf0bb19SAndrey A. Chernov 		printf("mountmsdosfs(): disk too big, sorry\n");
405952a6212SJordan K. Hubbard 		goto error_exit;
406952a6212SJordan K. Hubbard 	}
407952a6212SJordan K. Hubbard 
408952a6212SJordan K. Hubbard 	if (pmp->pm_RootDirEnts == 0) {
409952a6212SJordan K. Hubbard 		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
410952a6212SJordan K. Hubbard 		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
411952a6212SJordan K. Hubbard 		    || pmp->pm_Sectors
412952a6212SJordan K. Hubbard 		    || pmp->pm_FATsecs
413952a6212SJordan K. Hubbard 		    || getushort(b710->bpbFSVers)) {
414952a6212SJordan K. Hubbard 			error = EINVAL;
415aaf0bb19SAndrey A. Chernov 			printf("mountmsdosfs(): bad FAT32 filesystem\n");
416952a6212SJordan K. Hubbard 			goto error_exit;
417952a6212SJordan K. Hubbard 		}
418952a6212SJordan K. Hubbard 		pmp->pm_fatmask = FAT32_MASK;
419952a6212SJordan K. Hubbard 		pmp->pm_fatmult = 4;
420952a6212SJordan K. Hubbard 		pmp->pm_fatdiv = 1;
421952a6212SJordan K. Hubbard 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
422952a6212SJordan K. Hubbard 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
423952a6212SJordan K. Hubbard 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
424952a6212SJordan K. Hubbard 		else
425952a6212SJordan K. Hubbard 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
426952a6212SJordan K. Hubbard 	} else
427952a6212SJordan K. Hubbard 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
428952a6212SJordan K. Hubbard 
429952a6212SJordan K. Hubbard 	/*
430952a6212SJordan K. Hubbard 	 * Check a few values (could do some more):
431952a6212SJordan K. Hubbard 	 * - logical sector size: power of 2, >= block size
432952a6212SJordan K. Hubbard 	 * - sectors per cluster: power of 2, >= 1
433952a6212SJordan K. Hubbard 	 * - number of sectors:   >= 1, <= size of partition
434952a6212SJordan K. Hubbard 	 */
435952a6212SJordan K. Hubbard 	if ( (SecPerClust == 0)
436952a6212SJordan K. Hubbard 	  || (SecPerClust & (SecPerClust - 1))
43701f6cfbaSYoshihiro Takahashi 	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
438952a6212SJordan K. Hubbard 	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
439952a6212SJordan K. Hubbard 	  || (pmp->pm_HugeSectors == 0)
440952a6212SJordan K. Hubbard 	) {
441952a6212SJordan K. Hubbard 		error = EINVAL;
442952a6212SJordan K. Hubbard 		goto error_exit;
443952a6212SJordan K. Hubbard 	}
44401f6cfbaSYoshihiro Takahashi 
44501f6cfbaSYoshihiro Takahashi 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
44601f6cfbaSYoshihiro Takahashi 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
44701f6cfbaSYoshihiro Takahashi 	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
44801f6cfbaSYoshihiro Takahashi 	SecPerClust         *= pmp->pm_BlkPerSec;
44901f6cfbaSYoshihiro Takahashi 
45001f6cfbaSYoshihiro Takahashi 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
45101f6cfbaSYoshihiro Takahashi 
452952a6212SJordan K. Hubbard 	if (FAT32(pmp)) {
453952a6212SJordan K. Hubbard 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
454952a6212SJordan K. Hubbard 		pmp->pm_firstcluster = pmp->pm_fatblk
455952a6212SJordan K. Hubbard 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
45601f6cfbaSYoshihiro Takahashi 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
457952a6212SJordan K. Hubbard 	} else {
45827a0bc89SDoug Rabson 		pmp->pm_rootdirblk = pmp->pm_fatblk +
45927a0bc89SDoug Rabson 			(pmp->pm_FATs * pmp->pm_FATsecs);
460952a6212SJordan K. Hubbard 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
46101f6cfbaSYoshihiro Takahashi 				       + DEV_BSIZE - 1)
46201f6cfbaSYoshihiro Takahashi 			/ DEV_BSIZE; /* in blocks */
46327a0bc89SDoug Rabson 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
464952a6212SJordan K. Hubbard 	}
465952a6212SJordan K. Hubbard 
466499d3ffaSBoris Popov 	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
467499d3ffaSBoris Popov 	    SecPerClust + 1;
46801f6cfbaSYoshihiro Takahashi 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
469952a6212SJordan K. Hubbard 
470952a6212SJordan K. Hubbard 	if (pmp->pm_fatmask == 0) {
471952a6212SJordan K. Hubbard 		if (pmp->pm_maxcluster
472952a6212SJordan K. Hubbard 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
47327a0bc89SDoug Rabson 			/*
474952a6212SJordan K. Hubbard 			 * This will usually be a floppy disk. This size makes
475952a6212SJordan K. Hubbard 			 * sure that one fat entry will not be split across
476952a6212SJordan K. Hubbard 			 * multiple blocks.
47727a0bc89SDoug Rabson 			 */
478952a6212SJordan K. Hubbard 			pmp->pm_fatmask = FAT12_MASK;
479952a6212SJordan K. Hubbard 			pmp->pm_fatmult = 3;
480952a6212SJordan K. Hubbard 			pmp->pm_fatdiv = 2;
481952a6212SJordan K. Hubbard 		} else {
482952a6212SJordan K. Hubbard 			pmp->pm_fatmask = FAT16_MASK;
483952a6212SJordan K. Hubbard 			pmp->pm_fatmult = 2;
484952a6212SJordan K. Hubbard 			pmp->pm_fatdiv = 1;
485952a6212SJordan K. Hubbard 		}
486952a6212SJordan K. Hubbard 	}
487499d3ffaSBoris Popov 
488499d3ffaSBoris Popov 	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
489499d3ffaSBoris Popov 	if (pmp->pm_maxcluster >= clusters) {
490499d3ffaSBoris Popov 		printf("Warning: number of clusters (%ld) exceeds FAT "
491499d3ffaSBoris Popov 		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
492499d3ffaSBoris Popov 		pmp->pm_maxcluster = clusters - 1;
493499d3ffaSBoris Popov 	}
494499d3ffaSBoris Popov 
495499d3ffaSBoris Popov 
496952a6212SJordan K. Hubbard 	if (FAT12(pmp))
49727a0bc89SDoug Rabson 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
49827a0bc89SDoug Rabson 	else
4997c58e473SMatthew Dillon 		pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
500952a6212SJordan K. Hubbard 
50101f6cfbaSYoshihiro Takahashi 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
50201f6cfbaSYoshihiro Takahashi 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
50327a0bc89SDoug Rabson 
50427a0bc89SDoug Rabson 	/*
50527a0bc89SDoug Rabson 	 * Compute mask and shift value for isolating cluster relative byte
50627a0bc89SDoug Rabson 	 * offsets and cluster numbers from a file offset.
50727a0bc89SDoug Rabson 	 */
50801f6cfbaSYoshihiro Takahashi 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
509952a6212SJordan K. Hubbard 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
510952a6212SJordan K. Hubbard 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
51127a0bc89SDoug Rabson 
512952a6212SJordan K. Hubbard 	/*
513952a6212SJordan K. Hubbard 	 * Check for valid cluster size
514952a6212SJordan K. Hubbard 	 * must be a power of 2
515952a6212SJordan K. Hubbard 	 */
516952a6212SJordan K. Hubbard 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
517952a6212SJordan K. Hubbard 		error = EINVAL;
518952a6212SJordan K. Hubbard 		goto error_exit;
519ad63a118SSatoshi Asami 	}
52027a0bc89SDoug Rabson 
52127a0bc89SDoug Rabson 	/*
52227a0bc89SDoug Rabson 	 * Release the bootsector buffer.
52327a0bc89SDoug Rabson 	 */
524952a6212SJordan K. Hubbard 	brelse(bp);
525952a6212SJordan K. Hubbard 	bp = NULL;
526952a6212SJordan K. Hubbard 
527952a6212SJordan K. Hubbard 	/*
528952a6212SJordan K. Hubbard 	 * Check FSInfo.
529952a6212SJordan K. Hubbard 	 */
530952a6212SJordan K. Hubbard 	if (pmp->pm_fsinfo) {
531952a6212SJordan K. Hubbard 		struct fsinfo *fp;
532952a6212SJordan K. Hubbard 
53301f6cfbaSYoshihiro Takahashi 		if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
53401f6cfbaSYoshihiro Takahashi 		    NOCRED, &bp)) != 0)
535952a6212SJordan K. Hubbard 			goto error_exit;
536952a6212SJordan K. Hubbard 		fp = (struct fsinfo *)bp->b_data;
537952a6212SJordan K. Hubbard 		if (!bcmp(fp->fsisig1, "RRaA", 4)
538952a6212SJordan K. Hubbard 		    && !bcmp(fp->fsisig2, "rrAa", 4)
539952a6212SJordan K. Hubbard 		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
540952a6212SJordan K. Hubbard 		    && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
541952a6212SJordan K. Hubbard 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
542952a6212SJordan K. Hubbard 		else
543952a6212SJordan K. Hubbard 			pmp->pm_fsinfo = 0;
544952a6212SJordan K. Hubbard 		brelse(bp);
545952a6212SJordan K. Hubbard 		bp = NULL;
546952a6212SJordan K. Hubbard 	}
547952a6212SJordan K. Hubbard 
548952a6212SJordan K. Hubbard 	/*
549952a6212SJordan K. Hubbard 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?		XXX
550952a6212SJordan K. Hubbard 	 */
55127a0bc89SDoug Rabson 
55227a0bc89SDoug Rabson 	/*
55327a0bc89SDoug Rabson 	 * Allocate memory for the bitmap of allocated clusters, and then
55427a0bc89SDoug Rabson 	 * fill it in.
55527a0bc89SDoug Rabson 	 */
55627a0bc89SDoug Rabson 	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
55727a0bc89SDoug Rabson 				   / N_INUSEBITS)
55827a0bc89SDoug Rabson 				  * sizeof(*pmp->pm_inusemap),
55927a0bc89SDoug Rabson 				  M_MSDOSFSFAT, M_WAITOK);
56027a0bc89SDoug Rabson 
56127a0bc89SDoug Rabson 	/*
56227a0bc89SDoug Rabson 	 * fillinusemap() needs pm_devvp.
56327a0bc89SDoug Rabson 	 */
56427a0bc89SDoug Rabson 	pmp->pm_dev = dev;
56527a0bc89SDoug Rabson 	pmp->pm_devvp = devvp;
56627a0bc89SDoug Rabson 
56727a0bc89SDoug Rabson 	/*
56827a0bc89SDoug Rabson 	 * Have the inuse map filled in.
56927a0bc89SDoug Rabson 	 */
570952a6212SJordan K. Hubbard 	if ((error = fillinusemap(pmp)) != 0)
57127a0bc89SDoug Rabson 		goto error_exit;
57227a0bc89SDoug Rabson 
57327a0bc89SDoug Rabson 	/*
57427a0bc89SDoug Rabson 	 * If they want fat updates to be synchronous then let them suffer
57527a0bc89SDoug Rabson 	 * the performance degradation in exchange for the on disk copy of
57627a0bc89SDoug Rabson 	 * the fat being correct just about all the time.  I suppose this
57727a0bc89SDoug Rabson 	 * would be a good thing to turn on if the kernel is still flakey.
57827a0bc89SDoug Rabson 	 */
579952a6212SJordan K. Hubbard 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
580952a6212SJordan K. Hubbard 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
58127a0bc89SDoug Rabson 
58227a0bc89SDoug Rabson 	/*
58327a0bc89SDoug Rabson 	 * Finish up.
58427a0bc89SDoug Rabson 	 */
585952a6212SJordan K. Hubbard 	if (ronly)
586952a6212SJordan K. Hubbard 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
587952a6212SJordan K. Hubbard 	else
58827a0bc89SDoug Rabson 		pmp->pm_fmod = 1;
58927a0bc89SDoug Rabson 	mp->mnt_data = (qaddr_t) pmp;
590939cb752SBruce Evans 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
591af3f60d5SBruce Evans 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
592cc9d8990SPeter Wemm 	mp->mnt_flag |= MNT_LOCAL;
5937eb9fca5SEivind Eklund 	devvp->v_rdev->si_mountpoint = mp;
59427a0bc89SDoug Rabson 
59527a0bc89SDoug Rabson 	return 0;
59627a0bc89SDoug Rabson 
597952a6212SJordan K. Hubbard error_exit:
598952a6212SJordan K. Hubbard 	if (bp)
599952a6212SJordan K. Hubbard 		brelse(bp);
600b40ce416SJulian Elischer 	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, td);
60127a0bc89SDoug Rabson 	if (pmp) {
60227a0bc89SDoug Rabson 		if (pmp->pm_inusemap)
603952a6212SJordan K. Hubbard 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
604952a6212SJordan K. Hubbard 		free(pmp, M_MSDOSFSMNT);
60527a0bc89SDoug Rabson 		mp->mnt_data = (qaddr_t)0;
60627a0bc89SDoug Rabson 	}
607952a6212SJordan K. Hubbard 	return (error);
60827a0bc89SDoug Rabson }
60927a0bc89SDoug Rabson 
61027a0bc89SDoug Rabson /*
61127a0bc89SDoug Rabson  * Unmount the filesystem described by mp.
61227a0bc89SDoug Rabson  */
6137fefffeeSPoul-Henning Kamp static int
614b40ce416SJulian Elischer msdosfs_unmount(mp, mntflags, td)
61527a0bc89SDoug Rabson 	struct mount *mp;
61627a0bc89SDoug Rabson 	int mntflags;
617b40ce416SJulian Elischer 	struct thread *td;
61827a0bc89SDoug Rabson {
619952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp;
620952a6212SJordan K. Hubbard 	int error, flags;
62127a0bc89SDoug Rabson 
622952a6212SJordan K. Hubbard 	flags = 0;
623952a6212SJordan K. Hubbard 	if (mntflags & MNT_FORCE)
62427a0bc89SDoug Rabson 		flags |= FORCECLOSE;
6250864ef1eSIan Dowse 	error = vflush(mp, 0, flags);
626c3c6d51eSPoul-Henning Kamp 	if (error)
62727a0bc89SDoug Rabson 		return error;
628952a6212SJordan K. Hubbard 	pmp = VFSTOMSDOSFS(mp);
6297eb9fca5SEivind Eklund 	pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
630952a6212SJordan K. Hubbard #ifdef MSDOSFS_DEBUG
631952a6212SJordan K. Hubbard 	{
632952a6212SJordan K. Hubbard 		struct vnode *vp = pmp->pm_devvp;
633952a6212SJordan K. Hubbard 
634952a6212SJordan K. Hubbard 		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
635e6e370a7SJeff Roberson 		printf("iflag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
636e6e370a7SJeff Roberson 		    vp->vi_flag, vp->v_usecount, vp->v_writecount,
637e6e370a7SJeff Roberson 		    vp->v_holdcnt);
63867ddfcafSMatthew Dillon 		printf("id %lu, mount %p, op %p\n",
63967ddfcafSMatthew Dillon 		    vp->v_id, vp->v_mount, vp->v_op);
640952a6212SJordan K. Hubbard 		printf("freef %p, freeb %p, mount %p\n",
641fc2ffbe6SPoul-Henning Kamp 		    TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
642952a6212SJordan K. Hubbard 		    vp->v_mount);
643952a6212SJordan K. Hubbard 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
64440c8cfe5SPeter Wemm 		    TAILQ_FIRST(&vp->v_cleanblkhd),
64540c8cfe5SPeter Wemm 		    TAILQ_FIRST(&vp->v_dirtyblkhd),
646952a6212SJordan K. Hubbard 		    vp->v_numoutput, vp->v_type);
647952a6212SJordan K. Hubbard 		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
648952a6212SJordan K. Hubbard 		    vp->v_socket, vp->v_tag,
649952a6212SJordan K. Hubbard 		    ((u_int *)vp->v_data)[0],
650952a6212SJordan K. Hubbard 		    ((u_int *)vp->v_data)[1]);
651952a6212SJordan K. Hubbard 	}
652952a6212SJordan K. Hubbard #endif
653b1897c19SJulian Elischer 	error = VOP_CLOSE(pmp->pm_devvp,
654b1897c19SJulian Elischer 		    (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
655b40ce416SJulian Elischer 		    NOCRED, td);
65627a0bc89SDoug Rabson 	vrele(pmp->pm_devvp);
657952a6212SJordan K. Hubbard 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
658952a6212SJordan K. Hubbard 	free(pmp, M_MSDOSFSMNT);
65927a0bc89SDoug Rabson 	mp->mnt_data = (qaddr_t)0;
660cc9d8990SPeter Wemm 	mp->mnt_flag &= ~MNT_LOCAL;
661952a6212SJordan K. Hubbard 	return (error);
66227a0bc89SDoug Rabson }
66327a0bc89SDoug Rabson 
6647fefffeeSPoul-Henning Kamp static int
66527a0bc89SDoug Rabson msdosfs_root(mp, vpp)
66627a0bc89SDoug Rabson 	struct mount *mp;
66727a0bc89SDoug Rabson 	struct vnode **vpp;
66827a0bc89SDoug Rabson {
669952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
67027a0bc89SDoug Rabson 	struct denode *ndep;
67127a0bc89SDoug Rabson 	int error;
67227a0bc89SDoug Rabson 
67327a0bc89SDoug Rabson #ifdef MSDOSFS_DEBUG
674952a6212SJordan K. Hubbard 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
67527a0bc89SDoug Rabson #endif
676952a6212SJordan K. Hubbard 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
677952a6212SJordan K. Hubbard 	if (error)
678952a6212SJordan K. Hubbard 		return (error);
67927a0bc89SDoug Rabson 	*vpp = DETOV(ndep);
680952a6212SJordan K. Hubbard 	return (0);
68127a0bc89SDoug Rabson }
68227a0bc89SDoug Rabson 
6837fefffeeSPoul-Henning Kamp static int
684b40ce416SJulian Elischer msdosfs_statfs(mp, sbp, td)
68527a0bc89SDoug Rabson 	struct mount *mp;
68627a0bc89SDoug Rabson 	struct statfs *sbp;
687b40ce416SJulian Elischer 	struct thread *td;
68827a0bc89SDoug Rabson {
689952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp;
69027a0bc89SDoug Rabson 
691952a6212SJordan K. Hubbard 	pmp = VFSTOMSDOSFS(mp);
69227a0bc89SDoug Rabson 	sbp->f_bsize = pmp->pm_bpcluster;
69327a0bc89SDoug Rabson 	sbp->f_iosize = pmp->pm_bpcluster;
694499d3ffaSBoris Popov 	sbp->f_blocks = pmp->pm_maxcluster + 1;
69527a0bc89SDoug Rabson 	sbp->f_bfree = pmp->pm_freeclustercount;
69627a0bc89SDoug Rabson 	sbp->f_bavail = pmp->pm_freeclustercount;
69727a0bc89SDoug Rabson 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
69827a0bc89SDoug Rabson 	sbp->f_ffree = 0;	/* what to put in here? */
69927a0bc89SDoug Rabson 	if (sbp != &mp->mnt_stat) {
700af3f60d5SBruce Evans 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
701952a6212SJordan K. Hubbard 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
702952a6212SJordan K. Hubbard 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
70327a0bc89SDoug Rabson 	}
704952a6212SJordan K. Hubbard 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
705952a6212SJordan K. Hubbard 	return (0);
70627a0bc89SDoug Rabson }
70727a0bc89SDoug Rabson 
7087fefffeeSPoul-Henning Kamp static int
709b40ce416SJulian Elischer msdosfs_sync(mp, waitfor, cred, td)
71027a0bc89SDoug Rabson 	struct mount *mp;
71127a0bc89SDoug Rabson 	int waitfor;
71227a0bc89SDoug Rabson 	struct ucred *cred;
713b40ce416SJulian Elischer 	struct thread *td;
71427a0bc89SDoug Rabson {
715952a6212SJordan K. Hubbard 	struct vnode *vp, *nvp;
71627a0bc89SDoug Rabson 	struct denode *dep;
717952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
718952a6212SJordan K. Hubbard 	int error, allerror = 0;
71927a0bc89SDoug Rabson 
72027a0bc89SDoug Rabson 	/*
72127a0bc89SDoug Rabson 	 * If we ever switch to not updating all of the fats all the time,
72227a0bc89SDoug Rabson 	 * this would be the place to update them from the first one.
72327a0bc89SDoug Rabson 	 */
724dfd5dee1SPeter Wemm 	if (pmp->pm_fmod != 0) {
725952a6212SJordan K. Hubbard 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
72627a0bc89SDoug Rabson 			panic("msdosfs_sync: rofs mod");
72727a0bc89SDoug Rabson 		else {
72827a0bc89SDoug Rabson 			/* update fats here */
72927a0bc89SDoug Rabson 		}
730dfd5dee1SPeter Wemm 	}
73127a0bc89SDoug Rabson 	/*
732952a6212SJordan K. Hubbard 	 * Write back each (modified) denode.
73327a0bc89SDoug Rabson 	 */
7349ed346baSBosko Milekic 	mtx_lock(&mntvnode_mtx);
73527a0bc89SDoug Rabson loop:
736c72ccd01SMatthew Dillon 	for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
737952a6212SJordan K. Hubbard 		/*
738952a6212SJordan K. Hubbard 		 * If the vnode that we are about to sync is no longer
739c681be37SDmitrij Tejblum 		 * associated with this mount point, start over.
740952a6212SJordan K. Hubbard 		 */
741952a6212SJordan K. Hubbard 		if (vp->v_mount != mp)
74227a0bc89SDoug Rabson 			goto loop;
743c72ccd01SMatthew Dillon 		nvp = TAILQ_NEXT(vp, v_nmntvnodes);
744797c3dbaSJohn Baldwin 
745797c3dbaSJohn Baldwin 		mtx_unlock(&mntvnode_mtx);
746797c3dbaSJohn Baldwin 		mtx_lock(&vp->v_interlock);
74727a0bc89SDoug Rabson 		dep = VTODE(vp);
748c681be37SDmitrij Tejblum 		if (vp->v_type == VNON ||
749831a80b0SMatthew Dillon 		    ((dep->de_flag &
750c681be37SDmitrij Tejblum 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
751831a80b0SMatthew Dillon 		    (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
7529ed346baSBosko Milekic 			mtx_unlock(&vp->v_interlock);
753797c3dbaSJohn Baldwin 			mtx_lock(&mntvnode_mtx);
75427a0bc89SDoug Rabson 			continue;
755af3f60d5SBruce Evans 		}
756b40ce416SJulian Elischer 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
757af3f60d5SBruce Evans 		if (error) {
7589ed346baSBosko Milekic 			mtx_lock(&mntvnode_mtx);
759af3f60d5SBruce Evans 			if (error == ENOENT)
76027a0bc89SDoug Rabson 				goto loop;
761af3f60d5SBruce Evans 			continue;
762af3f60d5SBruce Evans 		}
763b40ce416SJulian Elischer 		error = VOP_FSYNC(vp, cred, waitfor, td);
764c3c6d51eSPoul-Henning Kamp 		if (error)
76527a0bc89SDoug Rabson 			allerror = error;
766b40ce416SJulian Elischer 		VOP_UNLOCK(vp, 0, td);
767c681be37SDmitrij Tejblum 		vrele(vp);
7689ed346baSBosko Milekic 		mtx_lock(&mntvnode_mtx);
76927a0bc89SDoug Rabson 	}
7709ed346baSBosko Milekic 	mtx_unlock(&mntvnode_mtx);
77127a0bc89SDoug Rabson 
77227a0bc89SDoug Rabson 	/*
77327a0bc89SDoug Rabson 	 * Flush filesystem control info.
77427a0bc89SDoug Rabson 	 */
775c681be37SDmitrij Tejblum 	if (waitfor != MNT_LAZY) {
776b40ce416SJulian Elischer 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
777b40ce416SJulian Elischer 		error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, td);
778c3c6d51eSPoul-Henning Kamp 		if (error)
77927a0bc89SDoug Rabson 			allerror = error;
780b40ce416SJulian Elischer 		VOP_UNLOCK(pmp->pm_devvp, 0, td);
781c681be37SDmitrij Tejblum 	}
782952a6212SJordan K. Hubbard 	return (allerror);
78327a0bc89SDoug Rabson }
78427a0bc89SDoug Rabson 
7857fefffeeSPoul-Henning Kamp static int
786c24fda81SAlfred Perlstein msdosfs_fhtovp(mp, fhp, vpp)
78727a0bc89SDoug Rabson 	struct mount *mp;
78827a0bc89SDoug Rabson 	struct fid *fhp;
78927a0bc89SDoug Rabson 	struct vnode **vpp;
79027a0bc89SDoug Rabson {
791952a6212SJordan K. Hubbard 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
79227a0bc89SDoug Rabson 	struct defid *defhp = (struct defid *) fhp;
79327a0bc89SDoug Rabson 	struct denode *dep;
79427a0bc89SDoug Rabson 	int error;
79527a0bc89SDoug Rabson 
796952a6212SJordan K. Hubbard 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
79727a0bc89SDoug Rabson 	if (error) {
79827a0bc89SDoug Rabson 		*vpp = NULLVP;
799952a6212SJordan K. Hubbard 		return (error);
80027a0bc89SDoug Rabson 	}
80127a0bc89SDoug Rabson 	*vpp = DETOV(dep);
802c24fda81SAlfred Perlstein 	return (0);
803c24fda81SAlfred Perlstein }
804c24fda81SAlfred Perlstein 
805c24fda81SAlfred Perlstein static int
80627a0bc89SDoug Rabson msdosfs_vptofh(vp, fhp)
80727a0bc89SDoug Rabson 	struct vnode *vp;
80827a0bc89SDoug Rabson 	struct fid *fhp;
80927a0bc89SDoug Rabson {
810952a6212SJordan K. Hubbard 	struct denode *dep;
811952a6212SJordan K. Hubbard 	struct defid *defhp;
81227a0bc89SDoug Rabson 
813952a6212SJordan K. Hubbard 	dep = VTODE(vp);
814952a6212SJordan K. Hubbard 	defhp = (struct defid *)fhp;
81527a0bc89SDoug Rabson 	defhp->defid_len = sizeof(struct defid);
81627a0bc89SDoug Rabson 	defhp->defid_dirclust = dep->de_dirclust;
81727a0bc89SDoug Rabson 	defhp->defid_dirofs = dep->de_diroffset;
818952a6212SJordan K. Hubbard 	/* defhp->defid_gen = dep->de_gen; */
819952a6212SJordan K. Hubbard 	return (0);
82027a0bc89SDoug Rabson }
82127a0bc89SDoug Rabson 
82230ffadf3SPoul-Henning Kamp static struct vfsops msdosfs_vfsops = {
82327a0bc89SDoug Rabson 	msdosfs_mount,
8245a5fccc8SAlfred Perlstein 	vfs_stdstart,
82527a0bc89SDoug Rabson 	msdosfs_unmount,
82627a0bc89SDoug Rabson 	msdosfs_root,
8275a5fccc8SAlfred Perlstein 	vfs_stdquotactl,
82827a0bc89SDoug Rabson 	msdosfs_statfs,
82927a0bc89SDoug Rabson 	msdosfs_sync,
8305a5fccc8SAlfred Perlstein 	vfs_stdvget,
83127a0bc89SDoug Rabson 	msdosfs_fhtovp,
832a13234bbSPoul-Henning Kamp 	vfs_stdcheckexp,
83327a0bc89SDoug Rabson 	msdosfs_vptofh,
83491f37dcbSRobert Watson 	msdosfs_init,
835432a8400SBoris Popov 	msdosfs_uninit,
83691f37dcbSRobert Watson 	vfs_stdextattrctl,
83727a0bc89SDoug Rabson };
838c901836cSGarrett Wollman 
8394ccd7546SRuslan Ermilov VFS_SET(msdosfs_vfsops, msdosfs, 0);
840