xref: /openbsd/sys/msdosfs/msdosfs_vfsops.c (revision 91f110e0)
1 /*	$OpenBSD: msdosfs_vfsops.c,v 1.63 2013/04/15 15:32:19 jsing 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 	char fname[MNAMELEN];
105 	char fspec[MNAMELEN];
106 	int error, flags;
107 	mode_t accessmode;
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 == NULL) {
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 	error = copyinstr(args.fspec, fspec, sizeof(fspec), NULL);
171 	if (error)
172 		goto error;
173 
174 	if (disk_map(fspec, fname, sizeof(fname), DM_OPENBLCK) == -1)
175 		bcopy(fspec, fname, sizeof(fname));
176 
177 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fname, p);
178 	if ((error = namei(ndp)) != 0)
179 		goto error;
180 
181 	devvp = ndp->ni_vp;
182 
183 	if (devvp->v_type != VBLK) {
184 		error = ENOTBLK;
185 		goto error_devvp;
186 	}
187 	if (major(devvp->v_rdev) >= nblkdev) {
188 		error = ENXIO;
189 		goto error_devvp;
190 	}
191 
192 	/*
193 	 * If mount by non-root, then verify that user has necessary
194 	 * permissions on the device.
195 	 */
196 	if (suser(p, 0) != 0) {
197 		accessmode = VREAD;
198 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
199 			accessmode |= VWRITE;
200 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
201 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
202 		VOP_UNLOCK(devvp, 0, p);
203 		if (error)
204 			goto error_devvp;
205 	}
206 
207 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
208 		error = msdosfs_mountfs(devvp, mp, p, &args);
209 	else {
210 		if (devvp != pmp->pm_devvp)
211 			error = EINVAL;	/* XXX needs translation */
212 		else
213 			vrele(devvp);
214 	}
215 	if (error)
216 		goto error_devvp;
217 
218 	pmp = VFSTOMSDOSFS(mp);
219 	pmp->pm_gid = args.gid;
220 	pmp->pm_uid = args.uid;
221 	pmp->pm_mask = args.mask;
222 	pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
223 
224 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
225 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
226 	else if (!(pmp->pm_flags &
227 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
228 		struct vnode *rvp;
229 
230 		/*
231 		 * Try to divine whether to support Win'95 long filenames
232 		 */
233 		if (FAT32(pmp))
234 		        pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
235 		else {
236 		        if ((error = msdosfs_root(mp, &rvp)) != 0) {
237 			        msdosfs_unmount(mp, MNT_FORCE, p);
238 			        goto error;
239 			}
240 			pmp->pm_flags |= findwin95(VTODE(rvp))
241 			     ? MSDOSFSMNT_LONGNAME
242 			     : MSDOSFSMNT_SHORTNAME;
243 			vput(rvp);
244 		}
245 	}
246 
247 	bzero(mp->mnt_stat.f_mntonname, MNAMELEN);
248 	strlcpy(mp->mnt_stat.f_mntonname, path, MNAMELEN);
249 	bzero(mp->mnt_stat.f_mntfromname, MNAMELEN);
250 	strlcpy(mp->mnt_stat.f_mntfromname, fname, MNAMELEN);
251 	bzero(mp->mnt_stat.f_mntfromspec, MNAMELEN);
252 	strlcpy(mp->mnt_stat.f_mntfromspec, fspec, MNAMELEN);
253 	bcopy(&args, &mp->mnt_stat.mount_info.msdosfs_args, sizeof(args));
254 
255 #ifdef MSDOSFS_DEBUG
256 	printf("msdosfs_mount(): mp %x, pmp %x, inusemap %x\n", mp,
257 	    pmp, pmp->pm_inusemap);
258 #endif
259 
260 	return (0);
261 
262 error_devvp:
263 	vrele(devvp);
264 
265 error:
266 	return (error);
267 }
268 
269 int
270 msdosfs_mountfs(struct vnode *devvp, struct mount *mp, struct proc *p,
271     struct msdosfs_args *argp)
272 {
273 	struct msdosfsmount *pmp;
274 	struct buf *bp;
275 	dev_t dev = devvp->v_rdev;
276 	union bootsector *bsp;
277 	struct byte_bpb33 *b33;
278 	struct byte_bpb50 *b50;
279 	struct byte_bpb710 *b710;
280 	extern struct vnode *rootvp;
281 	u_int8_t SecPerClust;
282 	int	ronly, error, bmapsiz;
283 	uint32_t fat_max_clusters;
284 
285 	/*
286 	 * Disallow multiple mounts of the same device.
287 	 * Disallow mounting of a device that is currently in use
288 	 * (except for root, which might share swap device for miniroot).
289 	 * Flush out any old buffers remaining from a previous use.
290 	 */
291 	if ((error = vfs_mountedon(devvp)) != 0)
292 		return (error);
293 	if (vcount(devvp) > 1 && devvp != rootvp)
294 		return (EBUSY);
295 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
296 	error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
297 	VOP_UNLOCK(devvp, 0, p);
298 	if (error)
299 		return (error);
300 
301 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
302 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
303 	if (error)
304 		return (error);
305 
306 	bp  = NULL; /* both used in error_exit */
307 	pmp = NULL;
308 
309 	/*
310 	 * Read the boot sector of the filesystem, and then check the
311 	 * boot signature.  If not a dos boot sector then error out.
312 	 */
313 	if ((error = bread(devvp, 0, 4096, &bp)) != 0)
314 		goto error_exit;
315 	bp->b_flags |= B_AGE;
316 	bsp = (union bootsector *)bp->b_data;
317 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
318 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
319 	b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
320 
321 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
322 	pmp->pm_mountp = mp;
323 
324 	/*
325 	 * Compute several useful quantities from the bpb in the
326 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
327 	 * the fields that are different between dos 5 and dos 3.3.
328 	 */
329 	SecPerClust = b50->bpbSecPerClust;
330 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
331 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
332 	pmp->pm_FATs = b50->bpbFATs;
333 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
334 	pmp->pm_Sectors = getushort(b50->bpbSectors);
335 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
336 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
337 	pmp->pm_Heads = getushort(b50->bpbHeads);
338 	pmp->pm_Media = b50->bpbMedia;
339 
340 	/* Determine the number of DEV_BSIZE blocks in a MSDOSFS sector */
341 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
342 
343     	if (!pmp->pm_BytesPerSec || !SecPerClust || pmp->pm_SecPerTrack > 64) {
344 		error = EFTYPE;
345 		goto error_exit;
346 	}
347 
348 	if (pmp->pm_Sectors == 0) {
349 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
350 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
351 	} else {
352 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
353 		pmp->pm_HugeSectors = pmp->pm_Sectors;
354 	}
355 
356 	if (pmp->pm_RootDirEnts == 0) {
357 		if (pmp->pm_Sectors || pmp->pm_FATsecs ||
358 		    getushort(b710->bpbFSVers)) {
359 		        error = EINVAL;
360 			goto error_exit;
361 		}
362 		pmp->pm_fatmask = FAT32_MASK;
363 		pmp->pm_fatmult = 4;
364 		pmp->pm_fatdiv = 1;
365 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
366 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
367 		        pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
368 		else
369 		        pmp->pm_flags |= MSDOSFS_FATMIRROR;
370 	} else
371 	        pmp->pm_flags |= MSDOSFS_FATMIRROR;
372 
373 	/*
374 	 * More sanity checks:
375 	 *	MSDOSFS sectors per cluster: >0 && power of 2
376 	 *	MSDOSFS sector size: >= DEV_BSIZE && power of 2
377 	 *	HUGE sector count: >0
378 	 * 	FAT sectors: >0
379 	 */
380 	if ((SecPerClust == 0) || (SecPerClust & (SecPerClust - 1)) ||
381 	    (pmp->pm_BytesPerSec < DEV_BSIZE) ||
382 	    (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) ||
383 	    (pmp->pm_HugeSectors == 0) || (pmp->pm_FATsecs == 0)) {
384 		error = EINVAL;
385 		goto error_exit;
386 	}
387 
388 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
389 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec;
390 	pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
391 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
392 	SecPerClust *= pmp->pm_BlkPerSec;
393 
394 	if (FAT32(pmp)) {
395 	        pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
396 		pmp->pm_firstcluster = pmp->pm_fatblk
397 		        + (pmp->pm_FATs * pmp->pm_FATsecs);
398 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
399 	} else {
400 	        pmp->pm_rootdirblk = pmp->pm_fatblk +
401 		        (pmp->pm_FATs * pmp->pm_FATsecs);
402 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
403 				       + DEV_BSIZE - 1) / DEV_BSIZE;
404 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
405 	}
406 
407 	pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
408 	    SecPerClust;
409 	pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
410 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE;
411 
412 	if (pmp->pm_fatmask == 0) {
413 		if (pmp->pm_maxcluster
414 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
415 			/*
416 			 * This will usually be a floppy disk. This size makes
417 			 * sure that one fat entry will not be split across
418 			 * multiple blocks.
419 			 */
420 			pmp->pm_fatmask = FAT12_MASK;
421 			pmp->pm_fatmult = 3;
422 			pmp->pm_fatdiv = 2;
423 		} else {
424 			pmp->pm_fatmask = FAT16_MASK;
425 			pmp->pm_fatmult = 2;
426 			pmp->pm_fatdiv = 1;
427 		}
428 	}
429 	if (FAT12(pmp))
430 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
431 	else
432 		pmp->pm_fatblocksize = MAXBSIZE;
433 
434 	/*
435 	 * We now have the number of sectors in each FAT, so can work
436 	 * out how many clusters can be represented in a FAT.  Let's
437 	 * make sure the file system doesn't claim to have more clusters
438 	 * than this.
439 	 *
440 	 * We perform the calculation like we do to avoid integer overflow.
441 	 *
442 	 * This will give us a count of clusters.  They are numbered
443 	 * from 0, so the max cluster value is one less than the value
444 	 * we end up with.
445 	 */
446 	fat_max_clusters = pmp->pm_fatsize / pmp->pm_fatmult;
447 	fat_max_clusters *= pmp->pm_fatdiv;
448 	if (pmp->pm_maxcluster >= fat_max_clusters) {
449 #ifndef SMALL_KERNEL
450 		printf("msdosfs: reducing max cluster to %d from %d "
451 		    "due to FAT size\n", fat_max_clusters - 1,
452 		    pmp->pm_maxcluster);
453 #endif
454 		pmp->pm_maxcluster = fat_max_clusters - 1;
455 	}
456 
457 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
458 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
459 
460 	/*
461 	 * Compute mask and shift value for isolating cluster relative byte
462 	 * offsets and cluster numbers from a file offset.
463 	 */
464 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
465 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
466 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
467 
468 	/*
469 	 * Check for valid cluster size
470 	 * must be a power of 2
471 	 */
472 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
473 		error = EFTYPE;
474 		goto error_exit;
475 	}
476 
477 	/*
478 	 * Release the bootsector buffer.
479 	 */
480 	brelse(bp);
481 	bp = NULL;
482 
483 	/*
484 	 * Check FSInfo
485 	 */
486 	if (pmp->pm_fsinfo) {
487 	        struct fsinfo *fp;
488 
489 		if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
490 		    &bp)) != 0)
491 		        goto error_exit;
492 		fp = (struct fsinfo *)bp->b_data;
493 		if (!bcmp(fp->fsisig1, "RRaA", 4)
494 		    && !bcmp(fp->fsisig2, "rrAa", 4)
495 		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
496 		    && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
497 		        /* Valid FSInfo. */
498 			;
499 		else
500 		        pmp->pm_fsinfo = 0;
501 		/* XXX make sure this tiny buf doesn't come back in fillinusemap! */
502 		SET(bp->b_flags, B_INVAL);
503 		brelse(bp);
504 		bp = NULL;
505 	}
506 
507 	/*
508 	 * Check and validate (or perhaps invalidate?) the fsinfo structure? XXX
509 	 */
510 
511 	/*
512 	 * Allocate memory for the bitmap of allocated clusters, and then
513 	 * fill it in.
514 	 */
515 	bmapsiz = (pmp->pm_maxcluster + N_INUSEBITS - 1) / N_INUSEBITS;
516 	if (bmapsiz == 0 || SIZE_MAX / bmapsiz < sizeof(*pmp->pm_inusemap)) {
517 		/* detect multiplicative integer overflow */
518 		error = EINVAL;
519 		goto error_exit;
520 	}
521 	pmp->pm_inusemap = malloc(bmapsiz * sizeof(*pmp->pm_inusemap),
522 	    M_MSDOSFSFAT, M_WAITOK | M_CANFAIL);
523 	if (pmp->pm_inusemap == NULL) {
524 		error = EINVAL;
525 		goto error_exit;
526 	}
527 
528 	/*
529 	 * fillinusemap() needs pm_devvp.
530 	 */
531 	pmp->pm_dev = dev;
532 	pmp->pm_devvp = devvp;
533 
534 	/*
535 	 * Have the inuse map filled in.
536 	 */
537 	if ((error = fillinusemap(pmp)) != 0)
538 		goto error_exit;
539 
540 	/*
541 	 * If they want fat updates to be synchronous then let them suffer
542 	 * the performance degradation in exchange for the on disk copy of
543 	 * the fat being correct just about all the time.  I suppose this
544 	 * would be a good thing to turn on if the kernel is still flakey.
545 	 */
546 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
547 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
548 
549 	/*
550 	 * Finish up.
551 	 */
552 	if (ronly)
553 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
554 	else
555 		pmp->pm_fmod = 1;
556 	mp->mnt_data = (qaddr_t)pmp;
557         mp->mnt_stat.f_fsid.val[0] = (long)dev;
558         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
559 #ifdef QUOTA
560 	/*
561 	 * If we ever do quotas for DOS filesystems this would be a place
562 	 * to fill in the info in the msdosfsmount structure. You dolt,
563 	 * quotas on dos filesystems make no sense because files have no
564 	 * owners on dos filesystems. of course there is some empty space
565 	 * in the directory entry where we could put uid's and gid's.
566 	 */
567 #endif
568 	devvp->v_specmountpoint = mp;
569 
570 	return (0);
571 
572 error_exit:
573 	devvp->v_specmountpoint = NULL;
574 	if (bp)
575 		brelse(bp);
576 
577 	vn_lock(devvp, LK_EXCLUSIVE|LK_RETRY, p);
578 	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
579 	VOP_UNLOCK(devvp, 0, p);
580 
581 	if (pmp) {
582 		if (pmp->pm_inusemap)
583 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
584 		free(pmp, M_MSDOSFSMNT);
585 		mp->mnt_data = (qaddr_t)0;
586 	}
587 	return (error);
588 }
589 
590 int
591 msdosfs_start(struct mount *mp, int flags, struct proc *p)
592 {
593 
594 	return (0);
595 }
596 
597 /*
598  * Unmount the filesystem described by mp.
599  */
600 int
601 msdosfs_unmount(struct mount *mp, int mntflags,struct proc *p)
602 {
603 	struct msdosfsmount *pmp;
604 	int error, flags;
605 	struct vnode *vp;
606 
607 	flags = 0;
608 	if (mntflags & MNT_FORCE)
609 		flags |= FORCECLOSE;
610 #ifdef QUOTA
611 #endif
612 	if ((error = vflush(mp, NULLVP, flags)) != 0)
613 		return (error);
614 	pmp = VFSTOMSDOSFS(mp);
615 	pmp->pm_devvp->v_specmountpoint = NULL;
616 	vp = pmp->pm_devvp;
617 #ifdef MSDOSFS_DEBUG
618 	vprint("msdosfs_umount(): just before calling VOP_CLOSE()\n", vp);
619 #endif
620 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p);
621 	error = VOP_CLOSE(vp,
622 	   pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, p);
623 	vput(vp);
624 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
625 	free(pmp, M_MSDOSFSMNT);
626 	mp->mnt_data = (qaddr_t)0;
627 	mp->mnt_flag &= ~MNT_LOCAL;
628 	return (error);
629 }
630 
631 int
632 msdosfs_root(struct mount *mp, struct vnode **vpp)
633 {
634 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
635 	struct denode *ndep;
636 	int error;
637 
638 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
639 		return (error);
640 
641 #ifdef MSDOSFS_DEBUG
642 	printf("msdosfs_root(); mp %08x, pmp %08x, ndep %08x, vp %08x\n",
643 	    mp, pmp, ndep, DETOV(ndep));
644 #endif
645 
646 	*vpp = DETOV(ndep);
647 	return (0);
648 }
649 
650 int
651 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct proc *p)
652 {
653 	struct msdosfsmount *pmp;
654 
655 	pmp = VFSTOMSDOSFS(mp);
656 	sbp->f_bsize = pmp->pm_bpcluster;
657 	sbp->f_iosize = pmp->pm_bpcluster;
658 	sbp->f_blocks = pmp->pm_nmbrofclusters;
659 	sbp->f_bfree = pmp->pm_freeclustercount;
660 	sbp->f_bavail = pmp->pm_freeclustercount;
661 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
662 	sbp->f_ffree = 0;	/* what to put in here? */
663 	if (sbp != &mp->mnt_stat) {
664 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
665 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
666 		bcopy(&mp->mnt_stat.mount_info.msdosfs_args,
667 		    &sbp->mount_info.msdosfs_args, sizeof(struct msdosfs_args));
668 	}
669 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
670 	return (0);
671 }
672 
673 
674 struct msdosfs_sync_arg {
675 	struct proc *p;
676 	struct ucred *cred;
677 	int allerror;
678 	int waitfor;
679 };
680 
681 int
682 msdosfs_sync_vnode(struct vnode *vp, void *arg)
683 {
684 	struct msdosfs_sync_arg *msa = arg;
685 	int error;
686 	struct denode *dep;
687 
688 	dep = VTODE(vp);
689 	if (vp->v_type == VNON ||
690 	    ((dep->de_flag & (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0
691 	      && LIST_EMPTY(&vp->v_dirtyblkhd)) ||
692 	    msa->waitfor == MNT_LAZY) {
693 		return (0);
694 	}
695 
696 	if (vget(vp, LK_EXCLUSIVE | LK_NOWAIT, msa->p))
697 		return (0);
698 
699 	if ((error = VOP_FSYNC(vp, msa->cred, msa->waitfor, msa->p)) != 0)
700 		msa->allerror = error;
701 	VOP_UNLOCK(vp, 0, msa->p);
702 	vrele(vp);
703 
704 	return (0);
705 }
706 
707 
708 int
709 msdosfs_sync(struct mount *mp, int waitfor, struct ucred *cred, struct proc *p)
710 {
711 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
712 	struct msdosfs_sync_arg msa;
713 	int error;
714 
715 	msa.allerror = 0;
716 	msa.p = p;
717 	msa.cred = cred;
718 	msa.waitfor = waitfor;
719 
720 	/*
721 	 * If we ever switch to not updating all of the fats all the time,
722 	 * this would be the place to update them from the first one.
723 	 */
724 	if (pmp->pm_fmod != 0) {
725 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
726 			panic("msdosfs_sync: rofs mod");
727 		else {
728 			/* update fats here */
729 		}
730 	}
731 	/*
732 	 * Write back each (modified) denode.
733 	 */
734 	vfs_mount_foreach_vnode(mp, msdosfs_sync_vnode, &msa);
735 
736 	/*
737 	 * Force stale file system control information to be flushed.
738 	 */
739 	if (waitfor != MNT_LAZY) {
740 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
741 		if ((error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p)) != 0)
742 			msa.allerror = error;
743 		VOP_UNLOCK(pmp->pm_devvp, 0, p);
744 	}
745 
746 	return (msa.allerror);
747 }
748 
749 int
750 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
751 {
752 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
753 	struct defid *defhp = (struct defid *) fhp;
754 	struct denode *dep;
755 	int error;
756 
757 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
758 	if (error) {
759 		*vpp = NULLVP;
760 		return (error);
761 	}
762 	*vpp = DETOV(dep);
763 	return (0);
764 }
765 
766 int
767 msdosfs_vptofh(struct vnode *vp, struct fid *fhp)
768 {
769 	struct denode *dep;
770 	struct defid *defhp;
771 
772 	dep = VTODE(vp);
773 	defhp = (struct defid *)fhp;
774 	defhp->defid_len = sizeof(struct defid);
775 	defhp->defid_dirclust = dep->de_dirclust;
776 	defhp->defid_dirofs = dep->de_diroffset;
777 	/* defhp->defid_gen = dep->de_gen; */
778 	return (0);
779 }
780 
781 int
782 msdosfs_check_export(struct mount *mp, struct mbuf *nam, int *exflagsp,
783     struct ucred **credanonp)
784 {
785 	struct netcred *np;
786 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
787 
788 	/*
789 	 * Get the export permission structure for this <mp, client> tuple.
790 	 */
791 	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
792 	if (np == NULL)
793 		return (EACCES);
794 
795 	*exflagsp = np->netc_exflags;
796 	*credanonp = &np->netc_anon;
797 	return (0);
798 }
799 
800 #define msdosfs_vget ((int (*)(struct mount *, ino_t, struct vnode **)) \
801 		      eopnotsupp)
802 
803 #define msdosfs_quotactl ((int (*)(struct mount *, int, uid_t, caddr_t, \
804 					struct proc *))eopnotsupp)
805 
806 #define msdosfs_sysctl ((int (*)(int *, u_int, void *, size_t *, void *, \
807                                     size_t, struct proc *))eopnotsupp)
808 
809 const struct vfsops msdosfs_vfsops = {
810 	msdosfs_mount,
811 	msdosfs_start,
812 	msdosfs_unmount,
813 	msdosfs_root,
814 	msdosfs_quotactl,
815 	msdosfs_statfs,
816 	msdosfs_sync,
817 	msdosfs_vget,
818 	msdosfs_fhtovp,
819 	msdosfs_vptofh,
820 	msdosfs_init,
821 	msdosfs_sysctl,
822 	msdosfs_check_export
823 };
824