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