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