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