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