xref: /netbsd-src/sys/fs/msdosfs/msdosfs_vfsops.c (revision d20841bb642898112fe68f0ad3f7b26dddf56f07)
1 /*	$NetBSD: msdosfs_vfsops.c,v 1.12 2003/12/04 19:38:23 atatat Exp $	*/
2 
3 /*-
4  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6  * All rights reserved.
7  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by TooLs GmbH.
20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /*
35  * Written by Paul Popelka (paulp@uts.amdahl.com)
36  *
37  * You can do anything you want with this software, just don't say you wrote
38  * it, and don't remove this notice.
39  *
40  * This software is provided "as is".
41  *
42  * The author supplies this software to be publicly redistributed on the
43  * understanding that the author is not responsible for the correct
44  * functioning of this software in any circumstances and is not liable for
45  * any damages caused by this software.
46  *
47  * October 1992
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_vfsops.c,v 1.12 2003/12/04 19:38:23 atatat Exp $");
52 
53 #if defined(_KERNEL_OPT)
54 #include "opt_quota.h"
55 #include "opt_compat_netbsd.h"
56 #endif
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/sysctl.h>
61 #include <sys/namei.h>
62 #include <sys/proc.h>
63 #include <sys/kernel.h>
64 #include <sys/vnode.h>
65 #include <miscfs/specfs/specdev.h> /* XXX */	/* defines v_rdev */
66 #include <sys/mount.h>
67 #include <sys/buf.h>
68 #include <sys/file.h>
69 #include <sys/device.h>
70 #include <sys/disklabel.h>
71 #include <sys/ioctl.h>
72 #include <sys/malloc.h>
73 #include <sys/dirent.h>
74 #include <sys/stat.h>
75 #include <sys/conf.h>
76 
77 #include <fs/msdosfs/bpb.h>
78 #include <fs/msdosfs/bootsect.h>
79 #include <fs/msdosfs/direntry.h>
80 #include <fs/msdosfs/denode.h>
81 #include <fs/msdosfs/msdosfsmount.h>
82 #include <fs/msdosfs/fat.h>
83 
84 int msdosfs_mountroot __P((void));
85 int msdosfs_mount __P((struct mount *, const char *, void *,
86     struct nameidata *, struct proc *));
87 int msdosfs_start __P((struct mount *, int, struct proc *));
88 int msdosfs_unmount __P((struct mount *, int, struct proc *));
89 int msdosfs_root __P((struct mount *, struct vnode **));
90 int msdosfs_quotactl __P((struct mount *, int, uid_t, caddr_t, struct proc *));
91 int msdosfs_statfs __P((struct mount *, struct statfs *, struct proc *));
92 int msdosfs_sync __P((struct mount *, int, struct ucred *, struct proc *));
93 int msdosfs_vget __P((struct mount *, ino_t, struct vnode **));
94 int msdosfs_fhtovp __P((struct mount *, struct fid *, struct vnode **));
95 int msdosfs_checkexp __P((struct mount *, struct mbuf *, int *,
96     struct ucred **));
97 int msdosfs_vptofh __P((struct vnode *, struct fid *));
98 
99 int msdosfs_mountfs __P((struct vnode *, struct mount *, struct proc *,
100     struct msdosfs_args *));
101 
102 static int update_mp __P((struct mount *, struct msdosfs_args *));
103 
104 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOS FS mount structure");
105 MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS fat", "MSDOS FS fat table");
106 
107 #define ROOTNAME "root_device"
108 
109 extern const struct vnodeopv_desc msdosfs_vnodeop_opv_desc;
110 
111 const struct vnodeopv_desc * const msdosfs_vnodeopv_descs[] = {
112 	&msdosfs_vnodeop_opv_desc,
113 	NULL,
114 };
115 
116 struct vfsops msdosfs_vfsops = {
117 	MOUNT_MSDOS,
118 	msdosfs_mount,
119 	msdosfs_start,
120 	msdosfs_unmount,
121 	msdosfs_root,
122 	msdosfs_quotactl,
123 	msdosfs_statfs,
124 	msdosfs_sync,
125 	msdosfs_vget,
126 	msdosfs_fhtovp,
127 	msdosfs_vptofh,
128 	msdosfs_init,
129 	msdosfs_reinit,
130 	msdosfs_done,
131 	NULL,
132 	msdosfs_mountroot,
133 	msdosfs_checkexp,
134 	msdosfs_vnodeopv_descs,
135 };
136 
137 static int
138 update_mp(mp, argp)
139 	struct mount *mp;
140 	struct msdosfs_args *argp;
141 {
142 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
143 	int error;
144 
145 	pmp->pm_gid = argp->gid;
146 	pmp->pm_uid = argp->uid;
147 	pmp->pm_mask = argp->mask & ALLPERMS;
148 	pmp->pm_dirmask = argp->dirmask & ALLPERMS;
149 	pmp->pm_gmtoff = argp->gmtoff;
150 	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
151 
152 	/*
153 	 * GEMDOS knows nothing (yet) about win95
154 	 */
155 	if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
156 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
157 
158 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
159 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
160 	else if (!(pmp->pm_flags &
161 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
162 		struct vnode *rootvp;
163 
164 		/*
165 		 * Try to divine whether to support Win'95 long filenames
166 		 */
167 		if (FAT32(pmp))
168 			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
169 		else {
170 			if ((error = msdosfs_root(mp, &rootvp)) != 0)
171 				return error;
172 			pmp->pm_flags |= findwin95(VTODE(rootvp))
173 				? MSDOSFSMNT_LONGNAME
174 					: MSDOSFSMNT_SHORTNAME;
175 			vput(rootvp);
176 		}
177 	}
178 	return 0;
179 }
180 
181 int
182 msdosfs_mountroot()
183 {
184 	struct mount *mp;
185 	struct proc *p = curproc;	/* XXX */
186 	int error;
187 	struct msdosfs_args args;
188 
189 	if (root_device->dv_class != DV_DISK)
190 		return (ENODEV);
191 
192 	/*
193 	 * Get vnodes for swapdev and rootdev.
194 	 */
195 	if (bdevvp(rootdev, &rootvp))
196 		panic("msdosfs_mountroot: can't setup rootvp");
197 
198 	if ((error = vfs_rootmountalloc(MOUNT_MSDOS, "root_device", &mp))) {
199 		vrele(rootvp);
200 		return (error);
201 	}
202 
203 	args.flags = MSDOSFSMNT_VERSIONED;
204 	args.uid = 0;
205 	args.gid = 0;
206 	args.mask = 0777;
207 	args.version = MSDOSFSMNT_VERSION;
208 	args.dirmask = 0777;
209 
210 	if ((error = msdosfs_mountfs(rootvp, mp, p, &args)) != 0) {
211 		mp->mnt_op->vfs_refcount--;
212 		vfs_unbusy(mp);
213 		free(mp, M_MOUNT);
214 		vrele(rootvp);
215 		return (error);
216 	}
217 
218 	if ((error = update_mp(mp, &args)) != 0) {
219 		(void)msdosfs_unmount(mp, 0, p);
220 		vfs_unbusy(mp);
221 		free(mp, M_MOUNT);
222 		vrele(rootvp);
223 		return (error);
224 	}
225 
226 	simple_lock(&mountlist_slock);
227 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
228 	simple_unlock(&mountlist_slock);
229 	(void)msdosfs_statfs(mp, &mp->mnt_stat, p);
230 	vfs_unbusy(mp);
231 	return (0);
232 }
233 
234 /*
235  * mp - path - addr in user space of mount point (ie /usr or whatever)
236  * data - addr in user space of mount params including the name of the block
237  * special file to treat as a filesystem.
238  */
239 int
240 msdosfs_mount(mp, path, data, ndp, p)
241 	struct mount *mp;
242 	const char *path;
243 	void *data;
244 	struct nameidata *ndp;
245 	struct proc *p;
246 {
247 	struct vnode *devvp;	  /* vnode for blk device to mount */
248 	struct msdosfs_args args; /* will hold data from mount request */
249 	/* msdosfs specific mount control block */
250 	struct msdosfsmount *pmp = NULL;
251 	int error, flags;
252 	mode_t accessmode;
253 
254 	if (mp->mnt_flag & MNT_GETARGS) {
255 		pmp = VFSTOMSDOSFS(mp);
256 		if (pmp == NULL)
257 			return EIO;
258 		args.fspec = NULL;
259 		args.uid = pmp->pm_uid;
260 		args.gid = pmp->pm_gid;
261 		args.mask = pmp->pm_mask;
262 		args.flags = pmp->pm_flags;
263 		args.version = MSDOSFSMNT_VERSION;
264 		args.dirmask = pmp->pm_dirmask;
265 		vfs_showexport(mp, &args.export, &pmp->pm_export);
266 		return copyout(&args, data, sizeof(args));
267 	}
268 	error = copyin(data, &args, sizeof(struct msdosfs_args));
269 	if (error)
270 		return (error);
271 
272 	/*
273 	 * If not versioned (i.e. using old mount_msdos(8)), fill in
274 	 * the additional structure items with suitable defaults.
275 	 */
276 	if ((args.flags & MSDOSFSMNT_VERSIONED) == 0) {
277 		args.version = 1;
278 		args.dirmask = args.mask;
279 	}
280 
281 	/*
282 	 * If updating, check whether changing from read-only to
283 	 * read/write; if there is no device name, that's all we do.
284 	 */
285 	if (mp->mnt_flag & MNT_UPDATE) {
286 		pmp = VFSTOMSDOSFS(mp);
287 		error = 0;
288 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
289 			flags = WRITECLOSE;
290 			if (mp->mnt_flag & MNT_FORCE)
291 				flags |= FORCECLOSE;
292 			error = vflush(mp, NULLVP, flags);
293 		}
294 		if (!error && (mp->mnt_flag & MNT_RELOAD))
295 			/* not yet implemented */
296 			error = EOPNOTSUPP;
297 		if (error)
298 			return (error);
299 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_iflag & IMNT_WANTRDWR)) {
300 			/*
301 			 * If upgrade to read-write by non-root, then verify
302 			 * that user has necessary permissions on the device.
303 			 */
304 			if (p->p_ucred->cr_uid != 0) {
305 				devvp = pmp->pm_devvp;
306 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
307 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
308 						   p->p_ucred, p);
309 				VOP_UNLOCK(devvp, 0);
310 				if (error)
311 					return (error);
312 			}
313 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
314 		}
315 		if (args.fspec == 0) {
316 #ifdef	__notyet__		/* doesn't work correctly with current mountd	XXX */
317 			if (args.flags & MSDOSFSMNT_MNTOPT) {
318 				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
319 				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
320 				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
321 					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
322 			}
323 #endif
324 			/*
325 			 * Process export requests.
326 			 */
327 			return (vfs_export(mp, &pmp->pm_export, &args.export));
328 		}
329 	}
330 	/*
331 	 * Not an update, or updating the name: look up the name
332 	 * and verify that it refers to a sensible block device.
333 	 */
334 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
335 	if ((error = namei(ndp)) != 0)
336 		return (error);
337 	devvp = ndp->ni_vp;
338 
339 	if (devvp->v_type != VBLK) {
340 		vrele(devvp);
341 		return (ENOTBLK);
342 	}
343 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
344 		vrele(devvp);
345 		return (ENXIO);
346 	}
347 	/*
348 	 * If mount by non-root, then verify that user has necessary
349 	 * permissions on the device.
350 	 */
351 	if (p->p_ucred->cr_uid != 0) {
352 		accessmode = VREAD;
353 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
354 			accessmode |= VWRITE;
355 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
356 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
357 		VOP_UNLOCK(devvp, 0);
358 		if (error) {
359 			vrele(devvp);
360 			return (error);
361 		}
362 	}
363 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
364 		error = msdosfs_mountfs(devvp, mp, p, &args);
365 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
366 		pmp = VFSTOMSDOSFS(mp);
367 #endif
368 	} else {
369 		if (devvp != pmp->pm_devvp)
370 			error = EINVAL;	/* needs translation */
371 		else
372 			vrele(devvp);
373 	}
374 	if (error) {
375 		vrele(devvp);
376 		return (error);
377 	}
378 
379 	if ((error = update_mp(mp, &args)) != 0) {
380 		msdosfs_unmount(mp, MNT_FORCE, p);
381 		return error;
382 	}
383 
384 #ifdef MSDOSFS_DEBUG
385 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
386 #endif
387 	return set_statfs_info(path, UIO_USERSPACE, args.fspec, UIO_USERSPACE,
388 	    mp, p);
389 }
390 
391 int
392 msdosfs_mountfs(devvp, mp, p, argp)
393 	struct vnode *devvp;
394 	struct mount *mp;
395 	struct proc *p;
396 	struct msdosfs_args *argp;
397 {
398 	struct msdosfsmount *pmp;
399 	struct buf *bp;
400 	dev_t dev = devvp->v_rdev;
401 	struct partinfo dpart;
402 	union bootsector *bsp;
403 	struct byte_bpb33 *b33;
404 	struct byte_bpb50 *b50;
405 	struct byte_bpb710 *b710;
406 	u_int8_t SecPerClust;
407 	int	ronly, error;
408 	int	bsize = 0, dtype = 0, tmp;
409 	u_long	dirsperblk;
410 
411 	/*
412 	 * Disallow multiple mounts of the same device.
413 	 * Disallow mounting of a device that is currently in use
414 	 * (except for root, which might share swap device for miniroot).
415 	 * Flush out any old buffers remaining from a previous use.
416 	 */
417 	if ((error = vfs_mountedon(devvp)) != 0)
418 		return (error);
419 	if (vcount(devvp) > 1 && devvp != rootvp)
420 		return (EBUSY);
421 	if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
422 		return (error);
423 
424 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
425 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
426 	if (error)
427 		return (error);
428 
429 	bp  = NULL; /* both used in error_exit */
430 	pmp = NULL;
431 
432 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
433 		/*
434 	 	 * We need the disklabel to calculate the size of a FAT entry
435 		 * later on. Also make sure the partition contains a filesystem
436 		 * of type FS_MSDOS. This doesn't work for floppies, so we have
437 		 * to check for them too.
438 	 	 *
439 	 	 * At least some parts of the msdos fs driver seem to assume
440 		 * that the size of a disk block will always be 512 bytes.
441 		 * Let's check it...
442 		 */
443 		error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, p);
444 		if (error)
445 			goto error_exit;
446 		tmp   = dpart.part->p_fstype;
447 		dtype = dpart.disklab->d_type;
448 		bsize = dpart.disklab->d_secsize;
449 		if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
450 			error = EINVAL;
451 			goto error_exit;
452 		}
453 	}
454 
455 	/*
456 	 * Read the boot sector of the filesystem, and then check the
457 	 * boot signature.  If not a dos boot sector then error out.
458 	 */
459 	if ((error = bread(devvp, 0, 512, NOCRED, &bp)) != 0)
460 		goto error_exit;
461 	bp->b_flags |= B_AGE;
462 	bsp = (union bootsector *)bp->b_data;
463 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
464 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
465 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
466 
467 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
468 		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
469 		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
470 			error = EINVAL;
471 			goto error_exit;
472 		}
473 	}
474 
475 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
476 	memset(pmp, 0, sizeof *pmp);
477 	pmp->pm_mountp = mp;
478 
479 	/*
480 	 * Compute several useful quantities from the bpb in the
481 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
482 	 * the fields that are different between dos 5 and dos 3.3.
483 	 */
484 	SecPerClust = b50->bpbSecPerClust;
485 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
486 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
487 	pmp->pm_FATs = b50->bpbFATs;
488 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
489 	pmp->pm_Sectors = getushort(b50->bpbSectors);
490 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
491 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
492 	pmp->pm_Heads = getushort(b50->bpbHeads);
493 	pmp->pm_Media = b50->bpbMedia;
494 
495 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
496 		/* XXX - We should probably check more values here */
497     		if (!pmp->pm_BytesPerSec || !SecPerClust
498 	    		|| pmp->pm_Heads > 255 || pmp->pm_SecPerTrack > 63) {
499 			error = EINVAL;
500 			goto error_exit;
501 		}
502 	}
503 
504 	if (pmp->pm_Sectors == 0) {
505 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
506 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
507 	} else {
508 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
509 		pmp->pm_HugeSectors = pmp->pm_Sectors;
510 	}
511 	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
512 	if (pmp->pm_HugeSectors > 0xffffffff / dirsperblk + 1) {
513 		/*
514 		 * We cannot deal currently with this size of disk
515 		 * due to fileid limitations (see msdosfs_getattr and
516 		 * msdosfs_readdir)
517 		 */
518 		error = EINVAL;
519 		goto error_exit;
520 	}
521 
522 	if (pmp->pm_RootDirEnts == 0) {
523 		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
524 		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
525 		    || pmp->pm_Sectors
526 		    || pmp->pm_FATsecs
527 		    || getushort(b710->bpbFSVers)) {
528 			error = EINVAL;
529 			goto error_exit;
530 		}
531 		pmp->pm_fatmask = FAT32_MASK;
532 		pmp->pm_fatmult = 4;
533 		pmp->pm_fatdiv = 1;
534 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
535 
536 		/* mirrorring is enabled if the FATMIRROR bit is not set */
537 		if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
538 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
539 		else
540 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
541 	} else
542 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
543 
544 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
545 		if (FAT32(pmp)) {
546 			/*
547 			 * GEMDOS doesn't know fat32.
548 			 */
549 			error = EINVAL;
550 			goto error_exit;
551 		}
552 
553 		/*
554 		 * Check a few values (could do some more):
555 		 * - logical sector size: power of 2, >= block size
556 		 * - sectors per cluster: power of 2, >= 1
557 		 * - number of sectors:   >= 1, <= size of partition
558 		 */
559 		if ( (SecPerClust == 0)
560 		  || (SecPerClust & (SecPerClust - 1))
561 		  || (pmp->pm_BytesPerSec < bsize)
562 		  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
563 		  || (pmp->pm_HugeSectors == 0)
564 		  || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
565 							> dpart.part->p_size)
566 		   ) {
567 			error = EINVAL;
568 			goto error_exit;
569 		}
570 		/*
571 		 * XXX - Many parts of the msdos fs driver seem to assume that
572 		 * the number of bytes per logical sector (BytesPerSec) will
573 		 * always be the same as the number of bytes per disk block
574 		 * Let's pretend it is.
575 		 */
576 		tmp = pmp->pm_BytesPerSec / bsize;
577 		pmp->pm_BytesPerSec  = bsize;
578 		pmp->pm_HugeSectors *= tmp;
579 		pmp->pm_HiddenSects *= tmp;
580 		pmp->pm_ResSectors  *= tmp;
581 		pmp->pm_Sectors     *= tmp;
582 		pmp->pm_FATsecs     *= tmp;
583 		SecPerClust         *= tmp;
584 	}
585 	pmp->pm_fatblk = pmp->pm_ResSectors;
586 	if (FAT32(pmp)) {
587 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
588 		pmp->pm_firstcluster = pmp->pm_fatblk
589 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
590 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
591 	} else {
592 		pmp->pm_rootdirblk = pmp->pm_fatblk +
593 			(pmp->pm_FATs * pmp->pm_FATsecs);
594 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
595 				       + pmp->pm_BytesPerSec - 1)
596 			/ pmp->pm_BytesPerSec;/* in sectors */
597 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
598 	}
599 
600 	pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
601 	    SecPerClust;
602 	pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
603 	pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
604 
605 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
606 		if (pmp->pm_nmbrofclusters <= (0xff0 - 2)
607 		      && (dtype == DTYPE_FLOPPY
608 			  || (dtype == DTYPE_VND
609 				&& (pmp->pm_Heads == 1 || pmp->pm_Heads == 2)))
610 		    ) {
611 			pmp->pm_fatmask = FAT12_MASK;
612 			pmp->pm_fatmult = 3;
613 			pmp->pm_fatdiv = 2;
614 		} else {
615 			pmp->pm_fatmask = FAT16_MASK;
616 			pmp->pm_fatmult = 2;
617 			pmp->pm_fatdiv = 1;
618 		}
619 	} else if (pmp->pm_fatmask == 0) {
620 		if (pmp->pm_maxcluster
621 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
622 			/*
623 			 * This will usually be a floppy disk. This size makes
624 			 * sure that one fat entry will not be split across
625 			 * multiple blocks.
626 			 */
627 			pmp->pm_fatmask = FAT12_MASK;
628 			pmp->pm_fatmult = 3;
629 			pmp->pm_fatdiv = 2;
630 		} else {
631 			pmp->pm_fatmask = FAT16_MASK;
632 			pmp->pm_fatmult = 2;
633 			pmp->pm_fatdiv = 1;
634 		}
635 	}
636 	if (FAT12(pmp))
637 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
638 	else
639 		pmp->pm_fatblocksize = MAXBSIZE;
640 
641 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
642 	pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
643 
644 	/*
645 	 * Compute mask and shift value for isolating cluster relative byte
646 	 * offsets and cluster numbers from a file offset.
647 	 */
648 	pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
649 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
650 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
651 
652 	/*
653 	 * Check for valid cluster size
654 	 * must be a power of 2
655 	 */
656 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
657 		error = EINVAL;
658 		goto error_exit;
659 	}
660 
661 	/*
662 	 * Release the bootsector buffer.
663 	 */
664 	brelse(bp);
665 	bp = NULL;
666 
667 	/*
668 	 * Check FSInfo.
669 	 */
670 	if (pmp->pm_fsinfo) {
671 		struct fsinfo *fp;
672 
673 		if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0)
674 			goto error_exit;
675 		fp = (struct fsinfo *)bp->b_data;
676 		if (!memcmp(fp->fsisig1, "RRaA", 4)
677 		    && !memcmp(fp->fsisig2, "rrAa", 4)
678 		    && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
679 		    && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
680 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
681 		else
682 			pmp->pm_fsinfo = 0;
683 		brelse(bp);
684 		bp = NULL;
685 	}
686 
687 	/*
688 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?
689 	 * XXX
690 	 */
691 	if (pmp->pm_fsinfo) {
692 		if (pmp->pm_nxtfree == (u_long)-1)
693 			pmp->pm_fsinfo = 0;
694 	}
695 
696 	/*
697 	 * Allocate memory for the bitmap of allocated clusters, and then
698 	 * fill it in.
699 	 */
700 	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
701 				   / N_INUSEBITS)
702 				  * sizeof(*pmp->pm_inusemap),
703 				  M_MSDOSFSFAT, M_WAITOK);
704 
705 	/*
706 	 * fillinusemap() needs pm_devvp.
707 	 */
708 	pmp->pm_dev = dev;
709 	pmp->pm_devvp = devvp;
710 
711 	/*
712 	 * Have the inuse map filled in.
713 	 */
714 	if ((error = fillinusemap(pmp)) != 0)
715 		goto error_exit;
716 
717 	/*
718 	 * If they want fat updates to be synchronous then let them suffer
719 	 * the performance degradation in exchange for the on disk copy of
720 	 * the fat being correct just about all the time.  I suppose this
721 	 * would be a good thing to turn on if the kernel is still flakey.
722 	 */
723 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
724 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
725 
726 	/*
727 	 * Finish up.
728 	 */
729 	if (ronly)
730 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
731 	else
732 		pmp->pm_fmod = 1;
733 	mp->mnt_data = pmp;
734         mp->mnt_stat.f_fsid.val[0] = (long)dev;
735         mp->mnt_stat.f_fsid.val[1] = makefstype(MOUNT_MSDOS);
736 	mp->mnt_flag |= MNT_LOCAL;
737 	mp->mnt_dev_bshift = pmp->pm_bnshift;
738 	mp->mnt_fs_bshift = pmp->pm_cnshift;
739 
740 #ifdef QUOTA
741 	/*
742 	 * If we ever do quotas for DOS filesystems this would be a place
743 	 * to fill in the info in the msdosfsmount structure. You dolt,
744 	 * quotas on dos filesystems make no sense because files have no
745 	 * owners on dos filesystems. of course there is some empty space
746 	 * in the directory entry where we could put uid's and gid's.
747 	 */
748 #endif
749 	devvp->v_specmountpoint = mp;
750 
751 	return (0);
752 
753 error_exit:;
754 	if (bp)
755 		brelse(bp);
756 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
757 	(void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
758 	VOP_UNLOCK(devvp, 0);
759 	if (pmp) {
760 		if (pmp->pm_inusemap)
761 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
762 		free(pmp, M_MSDOSFSMNT);
763 		mp->mnt_data = NULL;
764 	}
765 	return (error);
766 }
767 
768 int
769 msdosfs_start(mp, flags, p)
770 	struct mount *mp;
771 	int flags;
772 	struct proc *p;
773 {
774 
775 	return (0);
776 }
777 
778 /*
779  * Unmount the filesystem described by mp.
780  */
781 int
782 msdosfs_unmount(mp, mntflags, p)
783 	struct mount *mp;
784 	int mntflags;
785 	struct proc *p;
786 {
787 	struct msdosfsmount *pmp;
788 	int error, flags;
789 
790 	flags = 0;
791 	if (mntflags & MNT_FORCE)
792 		flags |= FORCECLOSE;
793 #ifdef QUOTA
794 #endif
795 	if ((error = vflush(mp, NULLVP, flags)) != 0)
796 		return (error);
797 	pmp = VFSTOMSDOSFS(mp);
798 	if (pmp->pm_devvp->v_type != VBAD)
799 		pmp->pm_devvp->v_specmountpoint = NULL;
800 #ifdef MSDOSFS_DEBUG
801 	{
802 		struct vnode *vp = pmp->pm_devvp;
803 
804 		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
805 		printf("flag %08x, usecount %d, writecount %ld, holdcnt %ld\n",
806 		    vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
807 		printf("id %lu, mount %p, op %p\n",
808 		    vp->v_id, vp->v_mount, vp->v_op);
809 		printf("freef %p, freeb %p, mount %p\n",
810 		    vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
811 		    vp->v_mount);
812 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
813 		    vp->v_cleanblkhd.lh_first,
814 		    vp->v_dirtyblkhd.lh_first,
815 		    vp->v_numoutput, vp->v_type);
816 		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
817 		    vp->v_socket, vp->v_tag,
818 		    ((u_int *)vp->v_data)[0],
819 		    ((u_int *)vp->v_data)[1]);
820 	}
821 #endif
822 	vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
823 	error = VOP_CLOSE(pmp->pm_devvp,
824 	    pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, p);
825 	vput(pmp->pm_devvp);
826 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
827 	free(pmp, M_MSDOSFSMNT);
828 	mp->mnt_data = NULL;
829 	mp->mnt_flag &= ~MNT_LOCAL;
830 	return (error);
831 }
832 
833 int
834 msdosfs_root(mp, vpp)
835 	struct mount *mp;
836 	struct vnode **vpp;
837 {
838 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
839 	struct denode *ndep;
840 	int error;
841 
842 #ifdef MSDOSFS_DEBUG
843 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
844 #endif
845 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
846 		return (error);
847 	*vpp = DETOV(ndep);
848 	return (0);
849 }
850 
851 int
852 msdosfs_quotactl(mp, cmds, uid, arg, p)
853 	struct mount *mp;
854 	int cmds;
855 	uid_t uid;
856 	caddr_t arg;
857 	struct proc *p;
858 {
859 
860 #ifdef QUOTA
861 	return (EOPNOTSUPP);
862 #else
863 	return (EOPNOTSUPP);
864 #endif
865 }
866 
867 int
868 msdosfs_statfs(mp, sbp, p)
869 	struct mount *mp;
870 	struct statfs *sbp;
871 	struct proc *p;
872 {
873 	struct msdosfsmount *pmp;
874 
875 	pmp = VFSTOMSDOSFS(mp);
876 #ifdef COMPAT_09
877 	sbp->f_type = 4;
878 #else
879 	sbp->f_type = 0;
880 #endif
881 	sbp->f_bsize = pmp->pm_bpcluster;
882 	sbp->f_iosize = pmp->pm_bpcluster;
883 	sbp->f_blocks = pmp->pm_nmbrofclusters;
884 	sbp->f_bfree = pmp->pm_freeclustercount;
885 	sbp->f_bavail = pmp->pm_freeclustercount;
886 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
887 	sbp->f_ffree = 0;	/* what to put in here? */
888 	copy_statfs_info(sbp, mp);
889 	return (0);
890 }
891 
892 int
893 msdosfs_sync(mp, waitfor, cred, p)
894 	struct mount *mp;
895 	int waitfor;
896 	struct ucred *cred;
897 	struct proc *p;
898 {
899 	struct vnode *vp, *nvp;
900 	struct denode *dep;
901 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
902 	int error, allerror = 0;
903 
904 	/*
905 	 * If we ever switch to not updating all of the fats all the time,
906 	 * this would be the place to update them from the first one.
907 	 */
908 	if (pmp->pm_fmod != 0) {
909 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
910 			panic("msdosfs_sync: rofs mod");
911 		else {
912 			/* update fats here */
913 		}
914 	}
915 	/*
916 	 * Write back each (modified) denode.
917 	 */
918 	simple_lock(&mntvnode_slock);
919 loop:
920 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
921 		/*
922 		 * If the vnode that we are about to sync is no longer
923 		 * assoicated with this mount point, start over.
924 		 */
925 		if (vp->v_mount != mp)
926 			goto loop;
927 		simple_lock(&vp->v_interlock);
928 		nvp = vp->v_mntvnodes.le_next;
929 		dep = VTODE(vp);
930 		if (waitfor == MNT_LAZY || vp->v_type == VNON ||
931 		    (((dep->de_flag &
932 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
933 		     (LIST_EMPTY(&vp->v_dirtyblkhd) &&
934 		      vp->v_uobj.uo_npages == 0))) {
935 			simple_unlock(&vp->v_interlock);
936 			continue;
937 		}
938 		simple_unlock(&mntvnode_slock);
939 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
940 		if (error) {
941 			simple_lock(&mntvnode_slock);
942 			if (error == ENOENT)
943 				goto loop;
944 			continue;
945 		}
946 		if ((error = VOP_FSYNC(vp, cred,
947 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
948 			allerror = error;
949 		vput(vp);
950 		simple_lock(&mntvnode_slock);
951 	}
952 	simple_unlock(&mntvnode_slock);
953 	/*
954 	 * Force stale file system control information to be flushed.
955 	 */
956 	if ((error = VOP_FSYNC(pmp->pm_devvp, cred,
957 	    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
958 		allerror = error;
959 #ifdef QUOTA
960 	/* qsync(mp); */
961 #endif
962 	return (allerror);
963 }
964 
965 int
966 msdosfs_fhtovp(mp, fhp, vpp)
967 	struct mount *mp;
968 	struct fid *fhp;
969 	struct vnode **vpp;
970 {
971 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
972 	struct defid *defhp = (struct defid *) fhp;
973 	struct denode *dep;
974 	int error;
975 
976 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
977 	if (error) {
978 		*vpp = NULLVP;
979 		return (error);
980 	}
981 	*vpp = DETOV(dep);
982 	return (0);
983 }
984 
985 int
986 msdosfs_checkexp(mp, nam, exflagsp, credanonp)
987 	struct mount *mp;
988 	struct mbuf *nam;
989 	int *exflagsp;
990 	struct ucred **credanonp;
991 {
992 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
993 	struct netcred *np;
994 
995 	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
996 	if (np == NULL)
997 		return (EACCES);
998 	*exflagsp = np->netc_exflags;
999 	*credanonp = &np->netc_anon;
1000 	return (0);
1001 }
1002 
1003 int
1004 msdosfs_vptofh(vp, fhp)
1005 	struct vnode *vp;
1006 	struct fid *fhp;
1007 {
1008 	struct denode *dep;
1009 	struct defid *defhp;
1010 
1011 	dep = VTODE(vp);
1012 	defhp = (struct defid *)fhp;
1013 	defhp->defid_len = sizeof(struct defid);
1014 	defhp->defid_dirclust = dep->de_dirclust;
1015 	defhp->defid_dirofs = dep->de_diroffset;
1016 	/* defhp->defid_gen = dep->de_gen; */
1017 	return (0);
1018 }
1019 
1020 int
1021 msdosfs_vget(mp, ino, vpp)
1022 	struct mount *mp;
1023 	ino_t ino;
1024 	struct vnode **vpp;
1025 {
1026 
1027 	return (EOPNOTSUPP);
1028 }
1029 
1030 SYSCTL_SETUP(sysctl_vfs_msdosfs_setup, "sysctl vfs.msdosfs subtree setup")
1031 {
1032 
1033 	sysctl_createv(SYSCTL_PERMANENT,
1034 		       CTLTYPE_NODE, "vfs", NULL,
1035 		       NULL, 0, NULL, 0,
1036 		       CTL_VFS, CTL_EOL);
1037 	sysctl_createv(SYSCTL_PERMANENT,
1038 		       CTLTYPE_NODE, "msdosfs", NULL,
1039 		       NULL, 0, NULL, 0,
1040 		       CTL_VFS, 4, CTL_EOL);
1041 	/*
1042 	 * XXX the "4" above could be dynamic, thereby eliminating one
1043 	 * more instance of the "number to vfs" mapping problem, but
1044 	 * "4" is the order as taken from sys/mount.h
1045 	 */
1046 }
1047