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