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