xref: /openbsd-src/sys/msdosfs/msdosfs_vfsops.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: msdosfs_vfsops.c,v 1.61 2011/07/04 20:35:35 deraadt Exp $	*/
2 /*	$NetBSD: msdosfs_vfsops.c,v 1.48 1997/10/18 02:54:57 briggs Exp $	*/
3 
4 /*-
5  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7  * All rights reserved.
8  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by TooLs GmbH.
21  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*
36  * Written by Paul Popelka (paulp@uts.amdahl.com)
37  *
38  * You can do anything you want with this software, just don't say you wrote
39  * it, and don't remove this notice.
40  *
41  * This software is provided "as is".
42  *
43  * The author supplies this software to be publicly redistributed on the
44  * understanding that the author is not responsible for the correct
45  * functioning of this software in any circumstances and is not liable for
46  * any damages caused by this software.
47  *
48  * October 1992
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/namei.h>
54 #include <sys/proc.h>
55 #include <sys/kernel.h>
56 #include <sys/vnode.h>
57 #include <sys/specdev.h> /* XXX */	/* defines v_rdev */
58 #include <sys/mount.h>
59 #include <sys/buf.h>
60 #include <sys/file.h>
61 #include <sys/disklabel.h>
62 #include <sys/ioctl.h>
63 #include <sys/malloc.h>
64 #include <sys/dirent.h>
65 #include <sys/disk.h>
66 
67 #include <msdosfs/bpb.h>
68 #include <msdosfs/bootsect.h>
69 #include <msdosfs/direntry.h>
70 #include <msdosfs/denode.h>
71 #include <msdosfs/msdosfsmount.h>
72 #include <msdosfs/fat.h>
73 
74 int msdosfs_mount(struct mount *, const char *, void *, struct nameidata *,
75 		       struct proc *);
76 int msdosfs_start(struct mount *, int, struct proc *);
77 int msdosfs_unmount(struct mount *, int, struct proc *);
78 int msdosfs_root(struct mount *, struct vnode **);
79 int msdosfs_statfs(struct mount *, struct statfs *, struct proc *);
80 int msdosfs_sync(struct mount *, int, struct ucred *, struct proc *);
81 int msdosfs_fhtovp(struct mount *, struct fid *, struct vnode **);
82 int msdosfs_vptofh(struct vnode *, struct fid *);
83 int msdosfs_check_export(struct mount *mp, struct mbuf *nam,
84 			      int *extflagsp, struct ucred **credanonp);
85 
86 int msdosfs_mountfs(struct vnode *, struct mount *, struct proc *,
87 			 struct msdosfs_args *);
88 
89 int msdosfs_sync_vnode(struct vnode *, void *);
90 
91 /*
92  * mp - path - addr in user space of mount point (ie /usr or whatever)
93  * data - addr in user space of mount params including the name of the block
94  * special file to treat as a filesystem.
95  */
96 int
97 msdosfs_mount(struct mount *mp, const char *path, void *data,
98     struct nameidata *ndp, struct proc *p)
99 {
100 	struct vnode *devvp;	  /* vnode for blk device to mount */
101 	struct msdosfs_args args; /* will hold data from mount request */
102 	/* msdosfs specific mount control block */
103 	struct msdosfsmount *pmp = NULL;
104 	size_t size;
105 	int error, flags;
106 	mode_t accessmode;
107 	char *fspec = NULL;
108 
109 	error = copyin(data, &args, sizeof(struct msdosfs_args));
110 	if (error)
111 		return (error);
112 
113 	/*
114 	 * If updating, check whether changing from read-only to
115 	 * read/write; if there is no device name, that's all we do.
116 	 */
117 	if (mp->mnt_flag & MNT_UPDATE) {
118 		pmp = VFSTOMSDOSFS(mp);
119 		error = 0;
120 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
121 		    (mp->mnt_flag & MNT_RDONLY)) {
122 			flags = WRITECLOSE;
123 			if (mp->mnt_flag & MNT_FORCE)
124 				flags |= FORCECLOSE;
125 			error = vflush(mp, NULLVP, flags);
126 		}
127 		if (!error && (mp->mnt_flag & MNT_RELOAD))
128 			/* not yet implemented */
129 			error = EOPNOTSUPP;
130 		if (error)
131 			return (error);
132 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
133 		    (mp->mnt_flag & MNT_WANTRDWR)) {
134 			/*
135 			 * If upgrade to read-write by non-root, then verify
136 			 * that user has necessary permissions on the device.
137 			 */
138 			if (suser(p, 0) != 0) {
139 				devvp = pmp->pm_devvp;
140 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
141 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
142 						   p->p_ucred, p);
143 				VOP_UNLOCK(devvp, 0, p);
144 				if (error)
145 					return (error);
146 			}
147 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
148 		}
149 		if (args.fspec == 0) {
150 #ifdef	__notyet__		/* doesn't work correctly with current mountd	XXX */
151 			if (args.flags & MSDOSFSMNT_MNTOPT) {
152 				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
153 				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
154 				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
155 					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
156 			}
157 #endif
158 			/*
159 			 * Process export requests.
160 			 */
161 			return (vfs_export(mp, &pmp->pm_export,
162 			    &args.export_info));
163 		}
164 	}
165 
166 	/*
167 	 * Not an update, or updating the name: look up the name
168 	 * and verify that it refers to a sensible block device.
169 	 */
170 	fspec = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
171 	error = copyinstr(args.fspec, fspec, MNAMELEN - 1, &size);
172 	if (error)
173 		goto error;
174 	disk_map(fspec, fspec, MNAMELEN, DM_OPENBLCK);
175 
176 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, p);
177 	if ((error = namei(ndp)) != 0)
178 		goto error;
179 
180 	devvp = ndp->ni_vp;
181 
182 	if (devvp->v_type != VBLK) {
183 		error = ENOTBLK;
184 		goto error_devvp;
185 	}
186 	if (major(devvp->v_rdev) >= nblkdev) {
187 		error = ENXIO;
188 		goto error_devvp;
189 	}
190 
191 	/*
192 	 * If mount by non-root, then verify that user has necessary
193 	 * permissions on the device.
194 	 */
195 	if (suser(p, 0) != 0) {
196 		accessmode = VREAD;
197 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
198 			accessmode |= VWRITE;
199 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
200 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
201 		VOP_UNLOCK(devvp, 0, p);
202 		if (error)
203 			goto error_devvp;
204 	}
205 
206 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
207 		error = msdosfs_mountfs(devvp, mp, p, &args);
208 	else {
209 		if (devvp != pmp->pm_devvp)
210 			error = EINVAL;	/* XXX needs translation */
211 		else
212 			vrele(devvp);
213 	}
214 	if (error)
215 		goto error_devvp;
216 
217 	pmp = VFSTOMSDOSFS(mp);
218 	pmp->pm_gid = args.gid;
219 	pmp->pm_uid = args.uid;
220 	pmp->pm_mask = args.mask;
221 	pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
222 
223 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
224 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
225 	else if (!(pmp->pm_flags &
226 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
227 		struct vnode *rvp;
228 
229 		/*
230 		 * Try to divine whether to support Win'95 long filenames
231 		 */
232 		if (FAT32(pmp))
233 		        pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
234 		else {
235 		        if ((error = msdosfs_root(mp, &rvp)) != 0) {
236 			        msdosfs_unmount(mp, MNT_FORCE, p);
237 			        goto error;
238 			}
239 			pmp->pm_flags |= findwin95(VTODE(rvp))
240 			     ? MSDOSFSMNT_LONGNAME
241 			     : MSDOSFSMNT_SHORTNAME;
242 			vput(rvp);
243 		}
244 	}
245 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
246 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
247 
248 	size = strlcpy(mp->mnt_stat.f_mntfromname, fspec, MNAMELEN - 1);
249 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
250 	bcopy(&args, &mp->mnt_stat.mount_info.msdosfs_args, sizeof(args));
251 #ifdef MSDOSFS_DEBUG
252 	printf("msdosfs_mount(): mp %x, pmp %x, inusemap %x\n", mp,
253 	    pmp, pmp->pm_inusemap);
254 #endif
255 	return (0);
256 
257 error_devvp:
258 	vrele(devvp);
259 
260 error:
261 	if (fspec)
262 		free(fspec, M_MOUNT);
263 
264 	return (error);
265 }
266 
267 int
268 msdosfs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p,
269     struct msdosfs_args *argp)
270 {
271 	struct msdosfsmount *pmp;
272 	struct buf *bp;
273 	dev_t dev = devvp->v_rdev;
274 	union bootsector *bsp;
275 	struct byte_bpb33 *b33;
276 	struct byte_bpb50 *b50;
277 	struct byte_bpb710 *b710;
278 	extern struct vnode *rootvp;
279 	u_int8_t SecPerClust;
280 	int	ronly, error, bmapsiz;
281 	uint32_t fat_max_clusters;
282 
283 	/*
284 	 * Disallow multiple mounts of the same device.
285 	 * Disallow mounting of a device that is currently in use
286 	 * (except for root, which might share swap device for miniroot).
287 	 * Flush out any old buffers remaining from a previous use.
288 	 */
289 	if ((error = vfs_mountedon(devvp)) != 0)
290 		return (error);
291 	if (vcount(devvp) > 1 && devvp != rootvp)
292 		return (EBUSY);
293 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
294 	error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
295 	VOP_UNLOCK(devvp, 0, p);
296 	if (error)
297 		return (error);
298 
299 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
300 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
301 	if (error)
302 		return (error);
303 
304 	bp  = NULL; /* both used in error_exit */
305 	pmp = NULL;
306 
307 	/*
308 	 * Read the boot sector of the filesystem, and then check the
309 	 * boot signature.  If not a dos boot sector then error out.
310 	 */
311 	if ((error = bread(devvp, 0, 4096, &bp)) != 0)
312 		goto error_exit;
313 	bp->b_flags |= B_AGE;
314 	bsp = (union bootsector *)bp->b_data;
315 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
316 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
317 	b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
318 
319 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
320 	pmp->pm_mountp = mp;
321 
322 	/*
323 	 * Compute several useful quantities from the bpb in the
324 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
325 	 * the fields that are different between dos 5 and dos 3.3.
326 	 */
327 	SecPerClust = b50->bpbSecPerClust;
328 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
329 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
330 	pmp->pm_FATs = b50->bpbFATs;
331 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
332 	pmp->pm_Sectors = getushort(b50->bpbSectors);
333 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
334 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
335 	pmp->pm_Heads = getushort(b50->bpbHeads);
336 	pmp->pm_Media = b50->bpbMedia;
337 
338 	/* Determine the number of DEV_BSIZE blocks in a MSDOSFS sector */
339 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
340 
341     	if (!pmp->pm_BytesPerSec || !SecPerClust || pmp->pm_SecPerTrack > 64) {
342 		error = EFTYPE;
343 		goto error_exit;
344 	}
345 
346 	if (pmp->pm_Sectors == 0) {
347 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
348 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
349 	} else {
350 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
351 		pmp->pm_HugeSectors = pmp->pm_Sectors;
352 	}
353 
354 	if (pmp->pm_RootDirEnts == 0) {
355 		if (pmp->pm_Sectors || pmp->pm_FATsecs ||
356 		    getushort(b710->bpbFSVers)) {
357 		        error = EINVAL;
358 			goto error_exit;
359 		}
360 		pmp->pm_fatmask = FAT32_MASK;
361 		pmp->pm_fatmult = 4;
362 		pmp->pm_fatdiv = 1;
363 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
364 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
365 		        pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
366 		else
367 		        pmp->pm_flags |= MSDOSFS_FATMIRROR;
368 	} else
369 	        pmp->pm_flags |= MSDOSFS_FATMIRROR;
370 
371 	/*
372 	 * More sanity checks:
373 	 *	MSDOSFS sectors per cluster: >0 && power of 2
374 	 *	MSDOSFS sector size: >= DEV_BSIZE && power of 2
375 	 *	HUGE sector count: >0
376 	 * 	FAT sectors: >0
377 	 */
378 	if ((SecPerClust == 0) || (SecPerClust & (SecPerClust - 1)) ||
379 	    (pmp->pm_BytesPerSec < DEV_BSIZE) ||
380 	    (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) ||
381 	    (pmp->pm_HugeSectors == 0) || (pmp->pm_FATsecs == 0)) {
382 		error = EINVAL;
383 		goto error_exit;
384 	}
385 
386 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
387 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec;
388 	pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
389 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
390 	SecPerClust *= pmp->pm_BlkPerSec;
391 
392 	if (FAT32(pmp)) {
393 	        pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
394 		pmp->pm_firstcluster = pmp->pm_fatblk
395 		        + (pmp->pm_FATs * pmp->pm_FATsecs);
396 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
397 	} else {
398 	        pmp->pm_rootdirblk = pmp->pm_fatblk +
399 		        (pmp->pm_FATs * pmp->pm_FATsecs);
400 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
401 				       + DEV_BSIZE - 1) / DEV_BSIZE;
402 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
403 	}
404 
405 	pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
406 	    SecPerClust;
407 	pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
408 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE;
409 
410 	if (pmp->pm_fatmask == 0) {
411 		if (pmp->pm_maxcluster
412 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
413 			/*
414 			 * This will usually be a floppy disk. This size makes
415 			 * sure that one fat entry will not be split across
416 			 * multiple blocks.
417 			 */
418 			pmp->pm_fatmask = FAT12_MASK;
419 			pmp->pm_fatmult = 3;
420 			pmp->pm_fatdiv = 2;
421 		} else {
422 			pmp->pm_fatmask = FAT16_MASK;
423 			pmp->pm_fatmult = 2;
424 			pmp->pm_fatdiv = 1;
425 		}
426 	}
427 	if (FAT12(pmp))
428 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
429 	else
430 		pmp->pm_fatblocksize = MAXBSIZE;
431 
432 	/*
433 	 * We now have the number of sectors in each FAT, so can work
434 	 * out how many clusters can be represented in a FAT.  Let's
435 	 * make sure the file system doesn't claim to have more clusters
436 	 * than this.
437 	 *
438 	 * We perform the calculation like we do to avoid integer overflow.
439 	 *
440 	 * This will give us a count of clusters.  They are numbered
441 	 * from 0, so the max cluster value is one less than the value
442 	 * we end up with.
443 	 */
444 	fat_max_clusters = pmp->pm_fatsize / pmp->pm_fatmult;
445 	fat_max_clusters *= pmp->pm_fatdiv;
446 	if (pmp->pm_maxcluster >= fat_max_clusters) {
447 #ifndef SMALL_KERNEL
448 		printf("msdosfs: reducing max cluster to %d from %d "
449 		    "due to FAT size\n", fat_max_clusters - 1,
450 		    pmp->pm_maxcluster);
451 #endif
452 		pmp->pm_maxcluster = fat_max_clusters - 1;
453 	}
454 
455 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
456 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
457 
458 	/*
459 	 * Compute mask and shift value for isolating cluster relative byte
460 	 * offsets and cluster numbers from a file offset.
461 	 */
462 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
463 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
464 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
465 
466 	/*
467 	 * Check for valid cluster size
468 	 * must be a power of 2
469 	 */
470 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
471 		error = EFTYPE;
472 		goto error_exit;
473 	}
474 
475 	/*
476 	 * Release the bootsector buffer.
477 	 */
478 	brelse(bp);
479 	bp = NULL;
480 
481 	/*
482 	 * Check FSInfo
483 	 */
484 	if (pmp->pm_fsinfo) {
485 	        struct fsinfo *fp;
486 
487 		if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
488 		    &bp)) != 0)
489 		        goto error_exit;
490 		fp = (struct fsinfo *)bp->b_data;
491 		if (!bcmp(fp->fsisig1, "RRaA", 4)
492 		    && !bcmp(fp->fsisig2, "rrAa", 4)
493 		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
494 		    && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
495 		        /* Valid FSInfo. */
496 			;
497 		else
498 		        pmp->pm_fsinfo = 0;
499 		/* XXX make sure this tiny buf doesn't come back in fillinusemap! */
500 		SET(bp->b_flags, B_INVAL);
501 		brelse(bp);
502 		bp = NULL;
503 	}
504 
505 	/*
506 	 * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX
507 	 */
508 
509 	/*
510 	 * Allocate memory for the bitmap of allocated clusters, and then
511 	 * fill it in.
512 	 */
513 	bmapsiz = (pmp->pm_maxcluster + N_INUSEBITS - 1) / N_INUSEBITS;
514 	if (bmapsiz == 0 || SIZE_MAX / bmapsiz < sizeof(*pmp->pm_inusemap)) {
515 		/* detect multiplicative integer overflow */
516 		error = EINVAL;
517 		goto error_exit;
518 	}
519 	pmp->pm_inusemap = malloc(bmapsiz * sizeof(*pmp->pm_inusemap),
520 	    M_MSDOSFSFAT, M_WAITOK | M_CANFAIL);
521 	if (pmp->pm_inusemap == NULL) {
522 		error = EINVAL;
523 		goto error_exit;
524 	}
525 
526 	/*
527 	 * fillinusemap() needs pm_devvp.
528 	 */
529 	pmp->pm_dev = dev;
530 	pmp->pm_devvp = devvp;
531 
532 	/*
533 	 * Have the inuse map filled in.
534 	 */
535 	if ((error = fillinusemap(pmp)) != 0)
536 		goto error_exit;
537 
538 	/*
539 	 * If they want fat updates to be synchronous then let them suffer
540 	 * the performance degradation in exchange for the on disk copy of
541 	 * the fat being correct just about all the time.  I suppose this
542 	 * would be a good thing to turn on if the kernel is still flakey.
543 	 */
544 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
545 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
546 
547 	/*
548 	 * Finish up.
549 	 */
550 	if (ronly)
551 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
552 	else
553 		pmp->pm_fmod = 1;
554 	mp->mnt_data = (qaddr_t)pmp;
555         mp->mnt_stat.f_fsid.val[0] = (long)dev;
556         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
557 #ifdef QUOTA
558 	/*
559 	 * If we ever do quotas for DOS filesystems this would be a place
560 	 * to fill in the info in the msdosfsmount structure. You dolt,
561 	 * quotas on dos filesystems make no sense because files have no
562 	 * owners on dos filesystems. of course there is some empty space
563 	 * in the directory entry where we could put uid's and gid's.
564 	 */
565 #endif
566 	devvp->v_specmountpoint = mp;
567 
568 	return (0);
569 
570 error_exit:
571 	devvp->v_specmountpoint = NULL;
572 	if (bp)
573 		brelse(bp);
574 
575 	vn_lock(devvp, LK_EXCLUSIVE|LK_RETRY, p);
576 	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
577 	VOP_UNLOCK(devvp, 0, p);
578 
579 	if (pmp) {
580 		if (pmp->pm_inusemap)
581 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
582 		free(pmp, M_MSDOSFSMNT);
583 		mp->mnt_data = (qaddr_t)0;
584 	}
585 	return (error);
586 }
587 
588 int
589 msdosfs_start(struct mount *mp, int flags, struct proc *p)
590 {
591 
592 	return (0);
593 }
594 
595 /*
596  * Unmount the filesystem described by mp.
597  */
598 int
599 msdosfs_unmount(struct mount *mp, int mntflags,struct proc *p)
600 {
601 	struct msdosfsmount *pmp;
602 	int error, flags;
603 	struct vnode *vp;
604 
605 	flags = 0;
606 	if (mntflags & MNT_FORCE)
607 		flags |= FORCECLOSE;
608 #ifdef QUOTA
609 #endif
610 	if ((error = vflush(mp, NULLVP, flags)) != 0)
611 		return (error);
612 	pmp = VFSTOMSDOSFS(mp);
613 	pmp->pm_devvp->v_specmountpoint = NULL;
614 	vp = pmp->pm_devvp;
615 #ifdef MSDOSFS_DEBUG
616 	vprint("msdosfs_umount(): just before calling VOP_CLOSE()\n", vp);
617 #endif
618 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
619 	error = VOP_CLOSE(vp,
620 	   pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, p);
621 	vput(vp);
622 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
623 	free(pmp, M_MSDOSFSMNT);
624 	mp->mnt_data = (qaddr_t)0;
625 	mp->mnt_flag &= ~MNT_LOCAL;
626 	return (error);
627 }
628 
629 int
630 msdosfs_root(struct mount *mp, struct vnode **vpp)
631 {
632 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
633 	struct denode *ndep;
634 	int error;
635 
636 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
637 		return (error);
638 
639 #ifdef MSDOSFS_DEBUG
640 	printf("msdosfs_root(); mp %08x, pmp %08x, ndep %08x, vp %08x\n",
641 	    mp, pmp, ndep, DETOV(ndep));
642 #endif
643 
644 	*vpp = DETOV(ndep);
645 	return (0);
646 }
647 
648 int
649 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
650 {
651 	struct msdosfsmount *pmp;
652 
653 	pmp = VFSTOMSDOSFS(mp);
654 	sbp->f_bsize = pmp->pm_bpcluster;
655 	sbp->f_iosize = pmp->pm_bpcluster;
656 	sbp->f_blocks = pmp->pm_nmbrofclusters;
657 	sbp->f_bfree = pmp->pm_freeclustercount;
658 	sbp->f_bavail = pmp->pm_freeclustercount;
659 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
660 	sbp->f_ffree = 0;	/* what to put in here? */
661 	if (sbp != &mp->mnt_stat) {
662 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
663 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
664 		bcopy(&mp->mnt_stat.mount_info.msdosfs_args,
665 		    &sbp->mount_info.msdosfs_args, sizeof(struct msdosfs_args));
666 	}
667 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
668 	return (0);
669 }
670 
671 
672 struct msdosfs_sync_arg {
673 	struct proc *p;
674 	struct ucred *cred;
675 	int allerror;
676 	int waitfor;
677 };
678 
679 int
680 msdosfs_sync_vnode(struct vnode *vp, void *arg)
681 {
682 	struct msdosfs_sync_arg *msa = arg;
683 	int error;
684 	struct denode *dep;
685 
686 	dep = VTODE(vp);
687 	if (vp->v_type == VNON ||
688 	    ((dep->de_flag & (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0
689 	      && LIST_EMPTY(&vp->v_dirtyblkhd)) ||
690 	    msa->waitfor == MNT_LAZY) {
691 		return (0);
692 	}
693 
694 	if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT, msa->p))
695 		return (0);
696 
697 	if ((error = VOP_FSYNC(vp, msa->cred, msa->waitfor, msa->p)) != 0)
698 		msa->allerror = error;
699 	VOP_UNLOCK(vp, 0, msa->p);
700 	vrele(vp);
701 
702 	return (0);
703 }
704 
705 
706 int
707 msdosfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
708 {
709 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
710 	struct msdosfs_sync_arg msa;
711 	int error;
712 
713 	msa.allerror = 0;
714 	msa.p = p;
715 	msa.cred = cred;
716 	msa.waitfor = waitfor;
717 
718 	/*
719 	 * If we ever switch to not updating all of the fats all the time,
720 	 * this would be the place to update them from the first one.
721 	 */
722 	if (pmp->pm_fmod != 0) {
723 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
724 			panic("msdosfs_sync: rofs mod");
725 		else {
726 			/* update fats here */
727 		}
728 	}
729 	/*
730 	 * Write back each (modified) denode.
731 	 */
732 	vfs_mount_foreach_vnode(mp, msdosfs_sync_vnode, &msa);
733 
734 	/*
735 	 * Force stale file system control information to be flushed.
736 	 */
737 	if (waitfor != MNT_LAZY) {
738 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
739 		if ((error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p)) != 0)
740 			msa.allerror = error;
741 		VOP_UNLOCK(pmp->pm_devvp, 0, p);
742 	}
743 
744 	return (msa.allerror);
745 }
746 
747 int
748 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
749 {
750 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
751 	struct defid *defhp = (struct defid *) fhp;
752 	struct denode *dep;
753 	int error;
754 
755 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
756 	if (error) {
757 		*vpp = NULLVP;
758 		return (error);
759 	}
760 	*vpp = DETOV(dep);
761 	return (0);
762 }
763 
764 int
765 msdosfs_vptofh(struct vnode *vp, struct fid *fhp)
766 {
767 	struct denode *dep;
768 	struct defid *defhp;
769 
770 	dep = VTODE(vp);
771 	defhp = (struct defid *)fhp;
772 	defhp->defid_len = sizeof(struct defid);
773 	defhp->defid_dirclust = dep->de_dirclust;
774 	defhp->defid_dirofs = dep->de_diroffset;
775 	/* defhp->defid_gen = dep->de_gen; */
776 	return (0);
777 }
778 
779 int
780 msdosfs_check_export(struct mount *mp, struct mbuf *nam, int *exflagsp,
781     struct ucred **credanonp)
782 {
783 	struct netcred *np;
784 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
785 
786 	/*
787 	 * Get the export permission structure for this <mp, client> tuple.
788 	 */
789 	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
790 	if (np == NULL)
791 		return (EACCES);
792 
793 	*exflagsp = np->netc_exflags;
794 	*credanonp = &np->netc_anon;
795 	return (0);
796 }
797 
798 #define msdosfs_vget ((int (*)(struct mount *, ino_t, struct vnode **)) \
799 		      eopnotsupp)
800 
801 #define msdosfs_quotactl ((int (*)(struct mount *, int, uid_t, caddr_t, \
802 					struct proc *))eopnotsupp)
803 
804 #define msdosfs_sysctl ((int (*)(int *, u_int, void *, size_t *, void *, \
805                                     size_t, struct proc *))eopnotsupp)
806 
807 const struct vfsops msdosfs_vfsops = {
808 	msdosfs_mount,
809 	msdosfs_start,
810 	msdosfs_unmount,
811 	msdosfs_root,
812 	msdosfs_quotactl,
813 	msdosfs_statfs,
814 	msdosfs_sync,
815 	msdosfs_vget,
816 	msdosfs_fhtovp,
817 	msdosfs_vptofh,
818 	msdosfs_init,
819 	msdosfs_sysctl,
820 	msdosfs_check_export
821 };
822