xref: /freebsd/sys/fs/msdosfs/msdosfs_vfsops.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /* $FreeBSD$ */
2 /*	$NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws 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/conf.h>
54 #include <sys/namei.h>
55 #include <sys/proc.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
59 #include <sys/bio.h>
60 #include <sys/buf.h>
61 #include <sys/fcntl.h>
62 #include <sys/malloc.h>
63 #include <sys/stat.h> 				/* defines ALLPERMS */
64 
65 #include <machine/mutex.h>
66 
67 #include <msdosfs/bpb.h>
68 #include <msdosfs/bootsect.h>
69 #include <msdosfs/direntry.h>
70 #include <msdosfs/denode.h>
71 #include <msdosfs/msdosfsmount.h>
72 #include <msdosfs/fat.h>
73 
74 #define MSDOSFS_DFLTBSIZE       4096
75 
76 #if 1 /*def PC98*/
77 /*
78  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
79  *       garbage or a random value :-{
80  *       If you want to use that broken-signatured media, define the
81  *       following symbol even though PC/AT.
82  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
83  */
84 #define	MSDOSFS_NOCHECKSIG
85 #endif
86 
87 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
88 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
89 
90 static int	update_mp __P((struct mount *mp, struct msdosfs_args *argp));
91 static int	mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
92 				  struct proc *p, struct msdosfs_args *argp));
93 static int	msdosfs_fhtovp __P((struct mount *, struct fid *,
94 				    struct vnode **));
95 static int	msdosfs_checkexp __P((struct mount *, struct sockaddr *,
96 				    int *, struct ucred **));
97 static int	msdosfs_mount __P((struct mount *, char *, caddr_t,
98 				   struct nameidata *, struct proc *));
99 static int	msdosfs_root __P((struct mount *, struct vnode **));
100 static int	msdosfs_statfs __P((struct mount *, struct statfs *,
101 				    struct proc *));
102 static int	msdosfs_sync __P((struct mount *, int, struct ucred *,
103 				  struct proc *));
104 static int	msdosfs_unmount __P((struct mount *, int, struct proc *));
105 static int	msdosfs_vptofh __P((struct vnode *, struct fid *));
106 
107 static int
108 update_mp(mp, argp)
109 	struct mount *mp;
110 	struct msdosfs_args *argp;
111 {
112 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
113 	int error;
114 
115 	pmp->pm_gid = argp->gid;
116 	pmp->pm_uid = argp->uid;
117 	pmp->pm_mask = argp->mask & ALLPERMS;
118 	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
119 	if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
120 		bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
121 		bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
122 		bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
123 	}
124 	if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
125 		bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
126 		bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
127 	}
128 
129 #ifndef __FreeBSD__
130 	/*
131 	 * GEMDOS knows nothing (yet) about win95
132 	 */
133 	if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
134 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
135 #endif
136 
137 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
138 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
139 	else if (!(pmp->pm_flags &
140 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
141 		struct vnode *rootvp;
142 
143 		/*
144 		 * Try to divine whether to support Win'95 long filenames
145 		 */
146 		if (FAT32(pmp))
147 			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
148 		else {
149 			if ((error = msdosfs_root(mp, &rootvp)) != 0)
150 				return error;
151 			pmp->pm_flags |= findwin95(VTODE(rootvp))
152 				? MSDOSFSMNT_LONGNAME
153 					: MSDOSFSMNT_SHORTNAME;
154 			vput(rootvp);
155 		}
156 	}
157 	return 0;
158 }
159 
160 #ifndef __FreeBSD__
161 int
162 msdosfs_mountroot()
163 {
164 	register struct mount *mp;
165 	struct proc *p = curproc;	/* XXX */
166 	size_t size;
167 	int error;
168 	struct msdosfs_args args;
169 
170 	if (root_device->dv_class != DV_DISK)
171 		return (ENODEV);
172 
173 	/*
174 	 * Get vnodes for swapdev and rootdev.
175 	 */
176 	if (bdevvp(rootdev, &rootvp))
177 		panic("msdosfs_mountroot: can't setup rootvp");
178 
179 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
180 	bzero((char *)mp, (u_long)sizeof(struct mount));
181 	mp->mnt_op = &msdosfs_vfsops;
182 	mp->mnt_flag = 0;
183 	LIST_INIT(&mp->mnt_vnodelist);
184 
185 	args.flags = 0;
186 	args.uid = 0;
187 	args.gid = 0;
188 	args.mask = 0777;
189 
190 	if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) {
191 		free(mp, M_MOUNT);
192 		return (error);
193 	}
194 
195 	if ((error = update_mp(mp, &args)) != 0) {
196 		(void)msdosfs_unmount(mp, 0, p);
197 		free(mp, M_MOUNT);
198 		return (error);
199 	}
200 
201 	if ((error = vfs_lock(mp)) != 0) {
202 		(void)msdosfs_unmount(mp, 0, p);
203 		free(mp, M_MOUNT);
204 		return (error);
205 	}
206 
207 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
208 	mp->mnt_vnodecovered = NULLVP;
209 	(void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1,
210 	    &size);
211 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
212 	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
213 	    &size);
214 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
215 	(void)msdosfs_statfs(mp, &mp->mnt_stat, p);
216 	vfs_unlock(mp);
217 	return (0);
218 }
219 #endif
220 
221 /*
222  * mp - path - addr in user space of mount point (ie /usr or whatever)
223  * data - addr in user space of mount params including the name of the block
224  * special file to treat as a filesystem.
225  */
226 static int
227 msdosfs_mount(mp, path, data, ndp, p)
228 	struct mount *mp;
229 	char *path;
230 	caddr_t data;
231 	struct nameidata *ndp;
232 	struct proc *p;
233 {
234 	struct vnode *devvp;	  /* vnode for blk device to mount */
235 	struct msdosfs_args args; /* will hold data from mount request */
236 	/* msdosfs specific mount control block */
237 	struct msdosfsmount *pmp = NULL;
238 	size_t size;
239 	int error, flags;
240 	mode_t accessmode;
241 
242 	error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
243 	if (error)
244 		return (error);
245 	if (args.magic != MSDOSFS_ARGSMAGIC)
246 		args.flags = 0;
247 	/*
248 	 * If updating, check whether changing from read-only to
249 	 * read/write; if there is no device name, that's all we do.
250 	 */
251 	if (mp->mnt_flag & MNT_UPDATE) {
252 		pmp = VFSTOMSDOSFS(mp);
253 		error = 0;
254 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
255 			flags = WRITECLOSE;
256 			if (mp->mnt_flag & MNT_FORCE)
257 				flags |= FORCECLOSE;
258 			error = vflush(mp, NULLVP, flags);
259 		}
260 		if (!error && (mp->mnt_flag & MNT_RELOAD))
261 			/* not yet implemented */
262 			error = EOPNOTSUPP;
263 		if (error)
264 			return (error);
265 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
266 			/*
267 			 * If upgrade to read-write by non-root, then verify
268 			 * that user has necessary permissions on the device.
269 			 */
270 			if (p->p_ucred->cr_uid != 0) {
271 				devvp = pmp->pm_devvp;
272 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
273 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
274 						   p->p_ucred, p);
275 				if (error) {
276 					VOP_UNLOCK(devvp, 0, p);
277 					return (error);
278 				}
279 				VOP_UNLOCK(devvp, 0, p);
280 			}
281 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
282 		}
283 		if (args.fspec == 0) {
284 #ifdef	__notyet__		/* doesn't work correctly with current mountd	XXX */
285 			if (args.flags & MSDOSFSMNT_MNTOPT) {
286 				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
287 				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
288 				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
289 					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
290 			}
291 #endif
292 			/*
293 			 * Process export requests.
294 			 */
295 			return (vfs_export(mp, &pmp->pm_export, &args.export));
296 		}
297 	}
298 	/*
299 	 * Not an update, or updating the name: look up the name
300 	 * and verify that it refers to a sensible block device.
301 	 */
302 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
303 	error = namei(ndp);
304 	if (error)
305 		return (error);
306 	devvp = ndp->ni_vp;
307 	NDFREE(ndp, NDF_ONLY_PNBUF);
308 
309 	if (!vn_isdisk(devvp, &error)) {
310 		vrele(devvp);
311 		return (error);
312 	}
313 	/*
314 	 * If mount by non-root, then verify that user has necessary
315 	 * permissions on the device.
316 	 */
317 	if (p->p_ucred->cr_uid != 0) {
318 		accessmode = VREAD;
319 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
320 			accessmode |= VWRITE;
321 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
322 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
323 		if (error) {
324 			vput(devvp);
325 			return (error);
326 		}
327 		VOP_UNLOCK(devvp, 0, p);
328 	}
329 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
330 		error = mountmsdosfs(devvp, mp, p, &args);
331 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
332 		pmp = VFSTOMSDOSFS(mp);
333 #endif
334 	} else {
335 		if (devvp != pmp->pm_devvp)
336 			error = EINVAL;	/* XXX needs translation */
337 		else
338 			vrele(devvp);
339 	}
340 	if (error) {
341 		vrele(devvp);
342 		return (error);
343 	}
344 
345 	error = update_mp(mp, &args);
346 	if (error) {
347 		msdosfs_unmount(mp, MNT_FORCE, p);
348 		return error;
349 	}
350 
351 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
352 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
353 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
354 	    &size);
355 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
356 	(void) msdosfs_statfs(mp, &mp->mnt_stat, p);
357 #ifdef MSDOSFS_DEBUG
358 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
359 #endif
360 	return (0);
361 }
362 
363 static int
364 mountmsdosfs(devvp, mp, p, argp)
365 	struct vnode *devvp;
366 	struct mount *mp;
367 	struct proc *p;
368 	struct msdosfs_args *argp;
369 {
370 	struct msdosfsmount *pmp;
371 	struct buf *bp;
372 	dev_t dev = devvp->v_rdev;
373 #ifndef __FreeBSD__
374 	struct partinfo dpart;
375 	int bsize = 0, dtype = 0, tmp;
376 #endif
377 	union bootsector *bsp;
378 	struct byte_bpb33 *b33;
379 	struct byte_bpb50 *b50;
380 	struct byte_bpb710 *b710;
381 	u_int8_t SecPerClust;
382 	u_long clusters;
383 	int	ronly, error;
384 
385 	/*
386 	 * Disallow multiple mounts of the same device.
387 	 * Disallow mounting of a device that is currently in use
388 	 * (except for root, which might share swap device for miniroot).
389 	 * Flush out any old buffers remaining from a previous use.
390 	 */
391 	error = vfs_mountedon(devvp);
392 	if (error)
393 		return (error);
394 	if (vcount(devvp) > 1 && devvp != rootvp)
395 		return (EBUSY);
396 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
397 	error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0);
398 	VOP_UNLOCK(devvp, 0, p);
399 	if (error)
400 		return (error);
401 
402 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
403 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
404 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
405 	VOP_UNLOCK(devvp, 0, p);
406 	if (error)
407 		return (error);
408 
409 	bp  = NULL; /* both used in error_exit */
410 	pmp = NULL;
411 
412 #ifndef __FreeBSD__
413 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
414 		/*
415 	 	 * We need the disklabel to calculate the size of a FAT entry
416 		 * later on. Also make sure the partition contains a filesystem
417 		 * of type FS_MSDOS. This doesn't work for floppies, so we have
418 		 * to check for them too.
419 	 	 *
420 	 	 * At least some parts of the msdos fs driver seem to assume
421 		 * that the size of a disk block will always be 512 bytes.
422 		 * Let's check it...
423 		 */
424 		error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
425 				  FREAD, NOCRED, p);
426 		if (error)
427 			goto error_exit;
428 		tmp   = dpart.part->p_fstype;
429 		dtype = dpart.disklab->d_type;
430 		bsize = dpart.disklab->d_secsize;
431 		if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
432 			error = EINVAL;
433 			goto error_exit;
434 		}
435 	}
436 #endif
437 
438 	/*
439 	 * Read the boot sector of the filesystem, and then check the
440 	 * boot signature.  If not a dos boot sector then error out.
441 	 *
442 	 * NOTE: 2048 is a maximum sector size in current...
443 	 */
444 	error = bread(devvp, 0, 2048, NOCRED, &bp);
445 	if (error)
446 		goto error_exit;
447 	bp->b_flags |= B_AGE;
448 	bsp = (union bootsector *)bp->b_data;
449 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
450 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
451 	b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
452 
453 #ifndef __FreeBSD__
454 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
455 #endif
456 #ifndef MSDOSFS_NOCHECKSIG
457 		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
458 		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
459 			error = EINVAL;
460 			goto error_exit;
461 		}
462 #endif
463 #ifndef __FreeBSD__
464 	}
465 #endif
466 
467 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
468 	bzero((caddr_t)pmp, sizeof *pmp);
469 	pmp->pm_mountp = mp;
470 
471 	/*
472 	 * Compute several useful quantities from the bpb in the
473 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
474 	 * the fields that are different between dos 5 and dos 3.3.
475 	 */
476 	SecPerClust = b50->bpbSecPerClust;
477 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
478 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
479 	pmp->pm_FATs = b50->bpbFATs;
480 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
481 	pmp->pm_Sectors = getushort(b50->bpbSectors);
482 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
483 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
484 	pmp->pm_Heads = getushort(b50->bpbHeads);
485 	pmp->pm_Media = b50->bpbMedia;
486 
487 	/* calculate the ratio of sector size to DEV_BSIZE */
488 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
489 
490 #ifndef __FreeBSD__
491 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
492 #endif
493 		/* XXX - We should probably check more values here */
494 		if (!pmp->pm_BytesPerSec || !SecPerClust
495 			|| !pmp->pm_Heads || pmp->pm_Heads > 255
496 #ifdef PC98
497 	    		|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
498 #else
499 			|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
500 #endif
501 			error = EINVAL;
502 			goto error_exit;
503 		}
504 #ifndef __FreeBSD__
505 	}
506 #endif
507 
508 	if (pmp->pm_Sectors == 0) {
509 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
510 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
511 	} else {
512 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
513 		pmp->pm_HugeSectors = pmp->pm_Sectors;
514 	}
515 	if (pmp->pm_HugeSectors > 0xffffffff /
516 	    (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
517 		/*
518 		 * We cannot deal currently with this size of disk
519 		 * due to fileid limitations (see msdosfs_getattr and
520 		 * msdosfs_readdir)
521 		 */
522 		error = EINVAL;
523 		printf("mountmsdosfs(): disk too big, sorry\n");
524 		goto error_exit;
525 	}
526 
527 	if (pmp->pm_RootDirEnts == 0) {
528 		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
529 		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
530 		    || pmp->pm_Sectors
531 		    || pmp->pm_FATsecs
532 		    || getushort(b710->bpbFSVers)) {
533 			error = EINVAL;
534 			printf("mountmsdosfs(): bad FAT32 filesystem\n");
535 			goto error_exit;
536 		}
537 		pmp->pm_fatmask = FAT32_MASK;
538 		pmp->pm_fatmult = 4;
539 		pmp->pm_fatdiv = 1;
540 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
541 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
542 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
543 		else
544 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
545 	} else
546 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
547 
548 	/*
549 	 * Check a few values (could do some more):
550 	 * - logical sector size: power of 2, >= block size
551 	 * - sectors per cluster: power of 2, >= 1
552 	 * - number of sectors:   >= 1, <= size of partition
553 	 */
554 	if ( (SecPerClust == 0)
555 	  || (SecPerClust & (SecPerClust - 1))
556 	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
557 	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
558 	  || (pmp->pm_HugeSectors == 0)
559 	) {
560 		error = EINVAL;
561 		goto error_exit;
562 	}
563 
564 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
565 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
566 	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
567 	SecPerClust         *= pmp->pm_BlkPerSec;
568 
569 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
570 
571 	if (FAT32(pmp)) {
572 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
573 		pmp->pm_firstcluster = pmp->pm_fatblk
574 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
575 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
576 	} else {
577 		pmp->pm_rootdirblk = pmp->pm_fatblk +
578 			(pmp->pm_FATs * pmp->pm_FATsecs);
579 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
580 				       + DEV_BSIZE - 1)
581 			/ DEV_BSIZE; /* in blocks */
582 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
583 	}
584 
585 	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
586 	    SecPerClust + 1;
587 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
588 
589 #ifndef __FreeBSD__
590 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
591 		if ((pmp->pm_maxcluster <= (0xff0 - 2))
592 		      && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
593 		      && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
594 		    ) {
595 			pmp->pm_fatmask = FAT12_MASK;
596 			pmp->pm_fatmult = 3;
597 			pmp->pm_fatdiv = 2;
598 		} else {
599 			pmp->pm_fatmask = FAT16_MASK;
600 			pmp->pm_fatmult = 2;
601 			pmp->pm_fatdiv = 1;
602 		}
603 	} else
604 #endif
605 	if (pmp->pm_fatmask == 0) {
606 		if (pmp->pm_maxcluster
607 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
608 			/*
609 			 * This will usually be a floppy disk. This size makes
610 			 * sure that one fat entry will not be split across
611 			 * multiple blocks.
612 			 */
613 			pmp->pm_fatmask = FAT12_MASK;
614 			pmp->pm_fatmult = 3;
615 			pmp->pm_fatdiv = 2;
616 		} else {
617 			pmp->pm_fatmask = FAT16_MASK;
618 			pmp->pm_fatmult = 2;
619 			pmp->pm_fatdiv = 1;
620 		}
621 	}
622 
623 	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
624 	if (pmp->pm_maxcluster >= clusters) {
625 		printf("Warning: number of clusters (%ld) exceeds FAT "
626 		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
627 		pmp->pm_maxcluster = clusters - 1;
628 	}
629 
630 
631 	if (FAT12(pmp))
632 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
633 	else
634 		pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
635 
636 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
637 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
638 
639 	/*
640 	 * Compute mask and shift value for isolating cluster relative byte
641 	 * offsets and cluster numbers from a file offset.
642 	 */
643 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
644 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
645 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
646 
647 	/*
648 	 * Check for valid cluster size
649 	 * must be a power of 2
650 	 */
651 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
652 		error = EINVAL;
653 		goto error_exit;
654 	}
655 
656 	/*
657 	 * Release the bootsector buffer.
658 	 */
659 	brelse(bp);
660 	bp = NULL;
661 
662 	/*
663 	 * Check FSInfo.
664 	 */
665 	if (pmp->pm_fsinfo) {
666 		struct fsinfo *fp;
667 
668 		if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
669 		    NOCRED, &bp)) != 0)
670 			goto error_exit;
671 		fp = (struct fsinfo *)bp->b_data;
672 		if (!bcmp(fp->fsisig1, "RRaA", 4)
673 		    && !bcmp(fp->fsisig2, "rrAa", 4)
674 		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
675 		    && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
676 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
677 		else
678 			pmp->pm_fsinfo = 0;
679 		brelse(bp);
680 		bp = NULL;
681 	}
682 
683 	/*
684 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?		XXX
685 	 */
686 
687 	/*
688 	 * Allocate memory for the bitmap of allocated clusters, and then
689 	 * fill it in.
690 	 */
691 	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
692 				   / N_INUSEBITS)
693 				  * sizeof(*pmp->pm_inusemap),
694 				  M_MSDOSFSFAT, M_WAITOK);
695 
696 	/*
697 	 * fillinusemap() needs pm_devvp.
698 	 */
699 	pmp->pm_dev = dev;
700 	pmp->pm_devvp = devvp;
701 
702 	/*
703 	 * Have the inuse map filled in.
704 	 */
705 	if ((error = fillinusemap(pmp)) != 0)
706 		goto error_exit;
707 
708 	/*
709 	 * If they want fat updates to be synchronous then let them suffer
710 	 * the performance degradation in exchange for the on disk copy of
711 	 * the fat being correct just about all the time.  I suppose this
712 	 * would be a good thing to turn on if the kernel is still flakey.
713 	 */
714 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
715 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
716 
717 	/*
718 	 * Finish up.
719 	 */
720 	if (ronly)
721 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
722 	else
723 		pmp->pm_fmod = 1;
724 	mp->mnt_data = (qaddr_t) pmp;
725 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
726 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
727 	mp->mnt_flag |= MNT_LOCAL;
728 	devvp->v_rdev->si_mountpoint = mp;
729 
730 	return 0;
731 
732 error_exit:
733 	if (bp)
734 		brelse(bp);
735 	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, p);
736 	if (pmp) {
737 		if (pmp->pm_inusemap)
738 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
739 		free(pmp, M_MSDOSFSMNT);
740 		mp->mnt_data = (qaddr_t)0;
741 	}
742 	return (error);
743 }
744 
745 /*
746  * Unmount the filesystem described by mp.
747  */
748 static int
749 msdosfs_unmount(mp, mntflags, p)
750 	struct mount *mp;
751 	int mntflags;
752 	struct proc *p;
753 {
754 	struct msdosfsmount *pmp;
755 	int error, flags;
756 
757 	flags = 0;
758 	if (mntflags & MNT_FORCE)
759 		flags |= FORCECLOSE;
760 	error = vflush(mp, NULLVP, flags);
761 	if (error)
762 		return error;
763 	pmp = VFSTOMSDOSFS(mp);
764 	pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
765 #ifdef MSDOSFS_DEBUG
766 	{
767 		struct vnode *vp = pmp->pm_devvp;
768 
769 		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
770 		printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
771 		    vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
772 		printf("id %lu, mount %p, op %p\n",
773 		    vp->v_id, vp->v_mount, vp->v_op);
774 		printf("freef %p, freeb %p, mount %p\n",
775 		    vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
776 		    vp->v_mount);
777 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
778 		    TAILQ_FIRST(&vp->v_cleanblkhd),
779 		    TAILQ_FIRST(&vp->v_dirtyblkhd),
780 		    vp->v_numoutput, vp->v_type);
781 		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
782 		    vp->v_socket, vp->v_tag,
783 		    ((u_int *)vp->v_data)[0],
784 		    ((u_int *)vp->v_data)[1]);
785 	}
786 #endif
787 	error = VOP_CLOSE(pmp->pm_devvp,
788 		    (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
789 		    NOCRED, p);
790 	vrele(pmp->pm_devvp);
791 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
792 	free(pmp, M_MSDOSFSMNT);
793 	mp->mnt_data = (qaddr_t)0;
794 	mp->mnt_flag &= ~MNT_LOCAL;
795 	return (error);
796 }
797 
798 static int
799 msdosfs_root(mp, vpp)
800 	struct mount *mp;
801 	struct vnode **vpp;
802 {
803 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
804 	struct denode *ndep;
805 	int error;
806 
807 #ifdef MSDOSFS_DEBUG
808 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
809 #endif
810 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
811 	if (error)
812 		return (error);
813 	*vpp = DETOV(ndep);
814 	return (0);
815 }
816 
817 static int
818 msdosfs_statfs(mp, sbp, p)
819 	struct mount *mp;
820 	struct statfs *sbp;
821 	struct proc *p;
822 {
823 	struct msdosfsmount *pmp;
824 
825 	pmp = VFSTOMSDOSFS(mp);
826 	sbp->f_bsize = pmp->pm_bpcluster;
827 	sbp->f_iosize = pmp->pm_bpcluster;
828 	sbp->f_blocks = pmp->pm_maxcluster + 1;
829 	sbp->f_bfree = pmp->pm_freeclustercount;
830 	sbp->f_bavail = pmp->pm_freeclustercount;
831 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
832 	sbp->f_ffree = 0;	/* what to put in here? */
833 	if (sbp != &mp->mnt_stat) {
834 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
835 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
836 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
837 	}
838 	strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
839 	return (0);
840 }
841 
842 static int
843 msdosfs_sync(mp, waitfor, cred, p)
844 	struct mount *mp;
845 	int waitfor;
846 	struct ucred *cred;
847 	struct proc *p;
848 {
849 	struct vnode *vp, *nvp;
850 	struct denode *dep;
851 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
852 	int error, allerror = 0;
853 
854 	/*
855 	 * If we ever switch to not updating all of the fats all the time,
856 	 * this would be the place to update them from the first one.
857 	 */
858 	if (pmp->pm_fmod != 0) {
859 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
860 			panic("msdosfs_sync: rofs mod");
861 		else {
862 			/* update fats here */
863 		}
864 	}
865 	/*
866 	 * Write back each (modified) denode.
867 	 */
868 	simple_lock(&mntvnode_slock);
869 loop:
870 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
871 		/*
872 		 * If the vnode that we are about to sync is no longer
873 		 * associated with this mount point, start over.
874 		 */
875 		if (vp->v_mount != mp)
876 			goto loop;
877 
878 		mtx_enter(&vp->v_interlock, MTX_DEF);
879 		nvp = vp->v_mntvnodes.le_next;
880 		dep = VTODE(vp);
881 		if (vp->v_type == VNON ||
882 		    ((dep->de_flag &
883 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
884 		    (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
885 			mtx_exit(&vp->v_interlock, MTX_DEF);
886 			continue;
887 		}
888 		simple_unlock(&mntvnode_slock);
889 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, p);
890 		if (error) {
891 			simple_lock(&mntvnode_slock);
892 			if (error == ENOENT)
893 				goto loop;
894 			continue;
895 		}
896 		error = VOP_FSYNC(vp, cred, waitfor, p);
897 		if (error)
898 			allerror = error;
899 		VOP_UNLOCK(vp, 0, p);
900 		vrele(vp);
901 		simple_lock(&mntvnode_slock);
902 	}
903 	simple_unlock(&mntvnode_slock);
904 
905 	/*
906 	 * Flush filesystem control info.
907 	 */
908 	if (waitfor != MNT_LAZY) {
909 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, p);
910 		error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, p);
911 		if (error)
912 			allerror = error;
913 		VOP_UNLOCK(pmp->pm_devvp, 0, p);
914 	}
915 	return (allerror);
916 }
917 
918 static int
919 msdosfs_fhtovp(mp, fhp, vpp)
920 	struct mount *mp;
921 	struct fid *fhp;
922 	struct vnode **vpp;
923 {
924 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
925 	struct defid *defhp = (struct defid *) fhp;
926 	struct denode *dep;
927 	int error;
928 
929 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
930 	if (error) {
931 		*vpp = NULLVP;
932 		return (error);
933 	}
934 	*vpp = DETOV(dep);
935 	return (0);
936 }
937 
938 static int
939 msdosfs_checkexp(mp, nam,  exflagsp, credanonp)
940 	struct mount *mp;
941 	struct sockaddr *nam;
942 	int *exflagsp;
943 	struct ucred **credanonp;
944 {
945 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
946 	struct netcred *np;
947 
948 	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
949 	if (np == NULL)
950 		return (EACCES);
951 	*exflagsp = np->netc_exflags;
952 	*credanonp = &np->netc_anon;
953 	return (0);
954 }
955 
956 static int
957 msdosfs_vptofh(vp, fhp)
958 	struct vnode *vp;
959 	struct fid *fhp;
960 {
961 	struct denode *dep;
962 	struct defid *defhp;
963 
964 	dep = VTODE(vp);
965 	defhp = (struct defid *)fhp;
966 	defhp->defid_len = sizeof(struct defid);
967 	defhp->defid_dirclust = dep->de_dirclust;
968 	defhp->defid_dirofs = dep->de_diroffset;
969 	/* defhp->defid_gen = dep->de_gen; */
970 	return (0);
971 }
972 
973 static struct vfsops msdosfs_vfsops = {
974 	msdosfs_mount,
975 	vfs_stdstart,
976 	msdosfs_unmount,
977 	msdosfs_root,
978 	vfs_stdquotactl,
979 	msdosfs_statfs,
980 	msdosfs_sync,
981 	vfs_stdvget,
982 	msdosfs_fhtovp,
983 	msdosfs_checkexp,
984 	msdosfs_vptofh,
985 	msdosfs_init,
986 	msdosfs_uninit,
987 	vfs_stdextattrctl,
988 };
989 
990 VFS_SET(msdosfs_vfsops, msdos, 0);
991