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