xref: /freebsd/sys/fs/msdosfs/msdosfs_vfsops.c (revision ebccf1e3a6b11b97cbf5f813dd76636e892a9035)
1 /* $FreeBSD$ */
2 /*	$NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $	*/
3 
4 /*-
5  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
7  * All rights reserved.
8  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by TooLs GmbH.
21  * 4. The name of TooLs GmbH may not be used to endorse or promote products
22  *    derived from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
27  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
30  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
32  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
33  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 /*-
36  * Written by Paul Popelka (paulp@uts.amdahl.com)
37  *
38  * You can do anything you want with this software, just don't say you wrote
39  * it, and don't remove this notice.
40  *
41  * This software is provided "as is".
42  *
43  * The author supplies this software to be publicly redistributed on the
44  * understanding that the author is not responsible for the correct
45  * functioning of this software in any circumstances and is not liable for
46  * any damages caused by this software.
47  *
48  * October 1992
49  */
50 
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/conf.h>
54 #include <sys/namei.h>
55 #include <sys/proc.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
59 #include <sys/bio.h>
60 #include <sys/buf.h>
61 #include <sys/fcntl.h>
62 #include <sys/malloc.h>
63 #include <sys/stat.h> 				/* defines ALLPERMS */
64 #include <sys/iconv.h>
65 #include <sys/mutex.h>
66 
67 #include <fs/msdosfs/bpb.h>
68 #include <fs/msdosfs/bootsect.h>
69 #include <fs/msdosfs/msdosfsmount.h>
70 #include <fs/msdosfs/direntry.h>
71 #include <fs/msdosfs/denode.h>
72 #include <fs/msdosfs/fat.h>
73 
74 #include <geom/geom.h>
75 #include <geom/geom_vfs.h>
76 
77 #include "opt_msdosfs.h"
78 
79 /* List of mount options we support */
80 static const char *msdosfs_opts[] = {
81 	"from",
82 	"export",
83 	"uid", "gid", "mask", "dirmask",
84 	"shortname", "longname", "win95",
85 	"kiconv", "cs_win", "cs_dos", "cs_local",
86 	NULL
87 };
88 
89 #define MSDOSFS_DFLTBSIZE       4096
90 
91 #if 1 /*def PC98*/
92 /*
93  * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
94  *       garbage or a random value :-{
95  *       If you want to use that broken-signatured media, define the
96  *       following symbol even though PC/AT.
97  *       (ex. mount PC-98 DOS formatted FD on PC/AT)
98  */
99 #define	MSDOSFS_NOCHECKSIG
100 #endif
101 
102 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
103 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
104 
105 struct iconv_functions *msdosfs_iconv = NULL;
106 
107 static int	update_mp(struct mount *mp, struct thread *td);
108 static int	mountmsdosfs(struct vnode *devvp, struct mount *mp,
109 		    struct thread *td);
110 static vfs_fhtovp_t	msdosfs_fhtovp;
111 static vfs_mount_t	msdosfs_mount;
112 static vfs_root_t	msdosfs_root;
113 static vfs_statfs_t	msdosfs_statfs;
114 static vfs_sync_t	msdosfs_sync;
115 static vfs_unmount_t	msdosfs_unmount;
116 static vfs_vptofh_t	msdosfs_vptofh;
117 
118 /* Maximum length of a character set name (arbitrary). */
119 #define	MAXCSLEN	64
120 
121 static int
122 update_mp(mp, td)
123 	struct mount *mp;
124 	struct thread *td;
125 {
126 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
127 	void *dos, *win, *local;
128 	int error, v;
129 
130 	if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
131 		if (msdosfs_iconv != NULL) {
132 			error = vfs_getopt(mp->mnt_optnew,
133 			    "cs_win", &win, NULL);
134 			if (!error)
135 				error = vfs_getopt(mp->mnt_optnew,
136 				    "cs_local", &local, NULL);
137 			if (!error)
138 				error = vfs_getopt(mp->mnt_optnew,
139 				    "cs_dos", &dos, NULL);
140 			if (!error) {
141 				msdosfs_iconv->open(win, local, &pmp->pm_u2w);
142 				msdosfs_iconv->open(local, win, &pmp->pm_w2u);
143 				msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
144 				msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
145 			}
146 			if (error != 0)
147 				return (error);
148 		} else {
149 			pmp->pm_w2u = NULL;
150 			pmp->pm_u2w = NULL;
151 			pmp->pm_d2u = NULL;
152 			pmp->pm_u2d = NULL;
153 		}
154 	}
155 
156 	if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
157 		pmp->pm_gid = v;
158 	if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
159 		pmp->pm_uid = v;
160 	if (1 == vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v))
161 		pmp->pm_mask = v & ALLPERMS;
162 	if (1 == vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v))
163 		pmp->pm_dirmask = v & ALLPERMS;
164 	vfs_flagopt(mp->mnt_optnew, "shortname",
165 	    &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
166 	vfs_flagopt(mp->mnt_optnew, "longname",
167 	    &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
168 	vfs_flagopt(mp->mnt_optnew, "kiconv",
169 	    &pmp->pm_flags, MSDOSFSMNT_KICONV);
170 
171 	/* XXX: Can't use flagopt due to negative option */
172 	if (!vfs_getopt(mp->mnt_optnew, "win95", NULL, NULL))
173 		pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
174 	else
175 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
176 
177 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
178 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
179 	else if (!(pmp->pm_flags &
180 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
181 		struct vnode *rootvp;
182 
183 		/*
184 		 * Try to divine whether to support Win'95 long filenames
185 		 */
186 		if (FAT32(pmp))
187 			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
188 		else {
189 			if ((error =
190 			    msdosfs_root(mp, LK_EXCLUSIVE, &rootvp, td)) != 0)
191 				return error;
192 			pmp->pm_flags |= findwin95(VTODE(rootvp))
193 				? MSDOSFSMNT_LONGNAME
194 					: MSDOSFSMNT_SHORTNAME;
195 			vput(rootvp);
196 		}
197 	}
198 	return 0;
199 }
200 
201 static int
202 msdosfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
203 {
204 	struct msdosfs_args args;
205 	int error;
206 
207 	if (data == NULL)
208 		return (EINVAL);
209 	error = copyin(data, &args, sizeof args);
210 	if (error)
211 		return (error);
212 
213 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
214 	ma = mount_arg(ma, "export", &args.export, sizeof args.export);
215 	ma = mount_argf(ma, "uid", "%d", args.uid);
216 	ma = mount_argf(ma, "gid", "%d", args.gid);
217 	ma = mount_argf(ma, "mask", "%d", args.mask);
218 	ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
219 
220         ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
221         ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
222         ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
223         ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
224 
225         ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
226         ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
227         ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
228 
229 	error = kernel_mount(ma, flags);
230 
231 	return (error);
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 static int
240 msdosfs_mount(struct mount *mp, struct thread *td)
241 {
242 	struct vnode *devvp;	  /* vnode for blk device to mount */
243 	struct export_args export;
244 	/* msdosfs specific mount control block */
245 	struct msdosfsmount *pmp = NULL;
246 	struct nameidata ndp;
247 	int error, flags;
248 	mode_t accessmode;
249 	char *from;
250 
251 	if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
252 		return (EINVAL);
253 
254 	/*
255 	 * If updating, check whether changing from read-only to
256 	 * read/write; if there is no device name, that's all we do.
257 	 */
258 	if (mp->mnt_flag & MNT_UPDATE) {
259 		pmp = VFSTOMSDOSFS(mp);
260 
261 		error = vfs_copyopt(mp->mnt_optnew, "export",
262 		    &export, sizeof export);
263 		if (error == 0 && export.ex_flags != 0) {
264 			/*
265 			 * Process export requests.
266 			 */
267 			if ((export.ex_flags & MNT_EXPORTED) != 0 &&
268 			    (pmp->pm_flags & MSDOSFS_LARGEFS) != 0)
269 				return (EOPNOTSUPP);
270 			return (vfs_export(mp, &export));
271 		}
272 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
273 		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
274 			error = VFS_SYNC(mp, MNT_WAIT, td);
275 			if (error)
276 				return (error);
277 			flags = WRITECLOSE;
278 			if (mp->mnt_flag & MNT_FORCE)
279 				flags |= FORCECLOSE;
280 			error = vflush(mp, 0, flags, td);
281 			if (error)
282 				return (error);
283 			DROP_GIANT();
284 			g_topology_lock();
285 			g_access(pmp->pm_cp, 0, -1, 0);
286 			g_topology_unlock();
287 			PICKUP_GIANT();
288 		} else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
289 		    !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
290 			/*
291 			 * If upgrade to read-write by non-root, then verify
292 			 * that user has necessary permissions on the device.
293 			 */
294 			if (suser(td)) {
295 				devvp = pmp->pm_devvp;
296 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
297 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
298 						   td->td_ucred, td);
299 				if (error) {
300 					VOP_UNLOCK(devvp, 0, td);
301 					return (error);
302 				}
303 				VOP_UNLOCK(devvp, 0, td);
304 			}
305 			DROP_GIANT();
306 			g_topology_lock();
307 			error = g_access(pmp->pm_cp, 0, 1, 0);
308 			g_topology_unlock();
309 			PICKUP_GIANT();
310 			if (error)
311 				return (error);
312 
313 			/* Now that the volume is modifiable, mark it dirty. */
314 			error = markvoldirty(pmp, 1);
315 			if (error)
316 				return (error);
317 		}
318 		vfs_flagopt(mp->mnt_optnew, "ro",
319 		    &pmp->pm_flags, MSDOSFSMNT_RONLY);
320 		vfs_flagopt(mp->mnt_optnew, "ro",
321 		    &mp->mnt_flag, MNT_RDONLY);
322 		if (vfs_getopt(mp->mnt_optnew, "from", NULL, NULL)) {
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 	}
333 	/*
334 	 * Not an update, or updating the name: look up the name
335 	 * and verify that it refers to a sensible disk device.
336 	 */
337 	if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
338 		return (EINVAL);
339 	NDINIT(&ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, from, td);
340 	error = namei(&ndp);
341 	if (error)
342 		return (error);
343 	devvp = ndp.ni_vp;
344 	NDFREE(&ndp, NDF_ONLY_PNBUF);
345 
346 	if (!vn_isdisk(devvp, &error)) {
347 		vrele(devvp);
348 		return (error);
349 	}
350 	/*
351 	 * If mount by non-root, then verify that user has necessary
352 	 * permissions on the device.
353 	 */
354 	if (suser(td)) {
355 		accessmode = VREAD;
356 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
357 			accessmode |= VWRITE;
358 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
359 		error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
360 		if (error) {
361 			vput(devvp);
362 			return (error);
363 		}
364 		VOP_UNLOCK(devvp, 0, td);
365 	}
366 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
367 		error = mountmsdosfs(devvp, mp, td);
368 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
369 		pmp = VFSTOMSDOSFS(mp);
370 #endif
371 	} else {
372 		if (devvp != pmp->pm_devvp)
373 			error = EINVAL;	/* XXX needs translation */
374 		else
375 			vrele(devvp);
376 	}
377 	if (error) {
378 		vrele(devvp);
379 		return (error);
380 	}
381 
382 	error = update_mp(mp, td);
383 	if (error) {
384 		if ((mp->mnt_flag & MNT_UPDATE) == 0)
385 			msdosfs_unmount(mp, MNT_FORCE, td);
386 		return error;
387 	}
388 
389 	vfs_mountedfrom(mp, from);
390 #ifdef MSDOSFS_DEBUG
391 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
392 #endif
393 	return (0);
394 }
395 
396 static int
397 mountmsdosfs(devvp, mp, td)
398 	struct vnode *devvp;
399 	struct mount *mp;
400 	struct thread *td;
401 {
402 	struct msdosfsmount *pmp;
403 	struct buf *bp;
404 	struct cdev *dev = devvp->v_rdev;
405 	union bootsector *bsp;
406 	struct byte_bpb33 *b33;
407 	struct byte_bpb50 *b50;
408 	struct byte_bpb710 *b710;
409 	u_int8_t SecPerClust;
410 	u_long clusters;
411 	int	ronly, error;
412 	struct g_consumer *cp;
413 	struct bufobj *bo;
414 
415 	ronly = !vfs_getopt(mp->mnt_optnew, "ro", NULL, NULL);
416 	/* XXX: use VOP_ACCESS to check FS perms */
417 	DROP_GIANT();
418 	g_topology_lock();
419 	error = g_vfs_open(devvp, &cp, "msdos", ronly ? 0 : 1);
420 	g_topology_unlock();
421 	PICKUP_GIANT();
422 	VOP_UNLOCK(devvp, 0, td);
423 	if (error)
424 		return (error);
425 
426 	bo = &devvp->v_bufobj;
427 	bp  = NULL; /* both used in error_exit */
428 	pmp = NULL;
429 
430 	/*
431 	 * Read the boot sector of the filesystem, and then check the
432 	 * boot signature.  If not a dos boot sector then error out.
433 	 *
434 	 * NOTE: 2048 is a maximum sector size in current...
435 	 */
436 	error = bread(devvp, 0, 2048, NOCRED, &bp);
437 	if (error)
438 		goto error_exit;
439 	bp->b_flags |= B_AGE;
440 	bsp = (union bootsector *)bp->b_data;
441 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
442 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
443 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
444 
445 #ifndef MSDOSFS_NOCHECKSIG
446 	if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
447 	    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
448 		error = EINVAL;
449 		goto error_exit;
450 	}
451 #endif
452 
453 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
454 	pmp->pm_mountp = mp;
455 	pmp->pm_cp = cp;
456 	pmp->pm_bo = bo;
457 
458 	/*
459 	 * Compute several useful quantities from the bpb in the
460 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
461 	 * the fields that are different between dos 5 and dos 3.3.
462 	 */
463 	SecPerClust = b50->bpbSecPerClust;
464 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
465 	if (pmp->pm_BytesPerSec < DEV_BSIZE) {
466 		error = EINVAL;
467 		goto error_exit;
468 	}
469 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
470 	pmp->pm_FATs = b50->bpbFATs;
471 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
472 	pmp->pm_Sectors = getushort(b50->bpbSectors);
473 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
474 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
475 	pmp->pm_Heads = getushort(b50->bpbHeads);
476 	pmp->pm_Media = b50->bpbMedia;
477 
478 	/* calculate the ratio of sector size to DEV_BSIZE */
479 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
480 
481 	/* XXX - We should probably check more values here */
482 	if (!pmp->pm_BytesPerSec || !SecPerClust
483 		|| !pmp->pm_Heads
484 #ifdef PC98
485     		|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
486 #else
487 		|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
488 #endif
489 		error = EINVAL;
490 		goto error_exit;
491 	}
492 
493 	if (pmp->pm_Sectors == 0) {
494 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
495 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
496 	} else {
497 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
498 		pmp->pm_HugeSectors = pmp->pm_Sectors;
499 	}
500 #ifndef MSDOSFS_LARGE
501 	if (pmp->pm_HugeSectors > 0xffffffff /
502 	    (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
503 		/*
504 		 * We cannot deal currently with this size of disk
505 		 * due to fileid limitations (see msdosfs_getattr and
506 		 * msdosfs_readdir)
507 		 */
508 		error = EINVAL;
509 		printf("mountmsdosfs(): disk too big, sorry\n");
510 		goto error_exit;
511 	}
512 #endif	/* !MSDOSFS_LARGE */
513 
514 	if (pmp->pm_RootDirEnts == 0) {
515 		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
516 		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
517 		    || pmp->pm_Sectors
518 		    || pmp->pm_FATsecs
519 		    || getushort(b710->bpbFSVers)) {
520 			error = EINVAL;
521 			printf("mountmsdosfs(): bad FAT32 filesystem\n");
522 			goto error_exit;
523 		}
524 		pmp->pm_fatmask = FAT32_MASK;
525 		pmp->pm_fatmult = 4;
526 		pmp->pm_fatdiv = 1;
527 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
528 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
529 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
530 		else
531 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
532 	} else
533 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
534 
535 	/*
536 	 * Check a few values (could do some more):
537 	 * - logical sector size: power of 2, >= block size
538 	 * - sectors per cluster: power of 2, >= 1
539 	 * - number of sectors:   >= 1, <= size of partition
540 	 * - number of FAT sectors: >= 1
541 	 */
542 	if ( (SecPerClust == 0)
543 	  || (SecPerClust & (SecPerClust - 1))
544 	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
545 	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
546 	  || (pmp->pm_HugeSectors == 0)
547 	  || (pmp->pm_FATsecs == 0)
548 	) {
549 		error = EINVAL;
550 		goto error_exit;
551 	}
552 
553 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
554 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
555 	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
556 	SecPerClust         *= pmp->pm_BlkPerSec;
557 
558 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
559 
560 	if (FAT32(pmp)) {
561 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
562 		pmp->pm_firstcluster = pmp->pm_fatblk
563 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
564 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
565 	} else {
566 		pmp->pm_rootdirblk = pmp->pm_fatblk +
567 			(pmp->pm_FATs * pmp->pm_FATsecs);
568 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
569 				       + DEV_BSIZE - 1)
570 			/ DEV_BSIZE; /* in blocks */
571 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
572 	}
573 
574 	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
575 	    SecPerClust + 1;
576 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
577 
578 	if (pmp->pm_fatmask == 0) {
579 		if (pmp->pm_maxcluster
580 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
581 			/*
582 			 * This will usually be a floppy disk. This size makes
583 			 * sure that one fat entry will not be split across
584 			 * multiple blocks.
585 			 */
586 			pmp->pm_fatmask = FAT12_MASK;
587 			pmp->pm_fatmult = 3;
588 			pmp->pm_fatdiv = 2;
589 		} else {
590 			pmp->pm_fatmask = FAT16_MASK;
591 			pmp->pm_fatmult = 2;
592 			pmp->pm_fatdiv = 1;
593 		}
594 	}
595 
596 	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
597 	if (pmp->pm_maxcluster >= clusters) {
598 		printf("Warning: number of clusters (%ld) exceeds FAT "
599 		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
600 		pmp->pm_maxcluster = clusters - 1;
601 	}
602 
603 
604 	if (FAT12(pmp))
605 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
606 	else
607 		pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
608 
609 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
610 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
611 
612 	/*
613 	 * Compute mask and shift value for isolating cluster relative byte
614 	 * offsets and cluster numbers from a file offset.
615 	 */
616 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
617 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
618 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
619 
620 	/*
621 	 * Check for valid cluster size
622 	 * must be a power of 2
623 	 */
624 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
625 		error = EINVAL;
626 		goto error_exit;
627 	}
628 
629 	/*
630 	 * Release the bootsector buffer.
631 	 */
632 	brelse(bp);
633 	bp = NULL;
634 
635 	/*
636 	 * Check FSInfo.
637 	 */
638 	if (pmp->pm_fsinfo) {
639 		struct fsinfo *fp;
640 
641 		if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
642 		    NOCRED, &bp)) != 0)
643 			goto error_exit;
644 		fp = (struct fsinfo *)bp->b_data;
645 		if (!bcmp(fp->fsisig1, "RRaA", 4)
646 		    && !bcmp(fp->fsisig2, "rrAa", 4)
647 		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
648 		    && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) {
649 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
650 			if (pmp->pm_nxtfree == 0xffffffff)
651 				pmp->pm_nxtfree = CLUST_FIRST;
652 		} else
653 			pmp->pm_fsinfo = 0;
654 		brelse(bp);
655 		bp = NULL;
656 	}
657 
658 	/*
659 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?
660 	 */
661 	if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
662 		printf(
663 		"Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
664 		    pmp->pm_nxtfree, pmp->pm_maxcluster);
665 		error = EINVAL;
666 		goto error_exit;
667 	}
668 
669 	/*
670 	 * Allocate memory for the bitmap of allocated clusters, and then
671 	 * fill it in.
672 	 */
673 	pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
674 				  * sizeof(*pmp->pm_inusemap),
675 				  M_MSDOSFSFAT, M_WAITOK);
676 
677 	/*
678 	 * fillinusemap() needs pm_devvp.
679 	 */
680 	pmp->pm_devvp = devvp;
681 
682 	/*
683 	 * Have the inuse map filled in.
684 	 */
685 	if ((error = fillinusemap(pmp)) != 0)
686 		goto error_exit;
687 
688 	/*
689 	 * If they want fat updates to be synchronous then let them suffer
690 	 * the performance degradation in exchange for the on disk copy of
691 	 * the fat being correct just about all the time.  I suppose this
692 	 * would be a good thing to turn on if the kernel is still flakey.
693 	 */
694 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
695 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
696 
697 	/*
698 	 * Finish up.
699 	 */
700 	if (ronly)
701 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
702 	else {
703 		/* Mark the volume dirty while it is mounted read/write. */
704 		if ((error = markvoldirty(pmp, 1)) != 0)
705 			goto error_exit;
706 		pmp->pm_fmod = 1;
707 	}
708 	mp->mnt_data = (qaddr_t) pmp;
709 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
710 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
711 	mp->mnt_flag |= MNT_LOCAL;
712 
713 #ifdef MSDOSFS_LARGE
714 	msdosfs_fileno_init(mp);
715 #endif
716 
717 	return 0;
718 
719 error_exit:
720 	if (bp)
721 		brelse(bp);
722 	if (cp != NULL) {
723 		DROP_GIANT();
724 		g_topology_lock();
725 		g_vfs_close(cp, td);
726 		g_topology_unlock();
727 		PICKUP_GIANT();
728 	}
729 	if (pmp) {
730 		if (pmp->pm_inusemap)
731 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
732 		free(pmp, M_MSDOSFSMNT);
733 		mp->mnt_data = (qaddr_t)0;
734 	}
735 	return (error);
736 }
737 
738 /*
739  * Unmount the filesystem described by mp.
740  */
741 static int
742 msdosfs_unmount(mp, mntflags, td)
743 	struct mount *mp;
744 	int mntflags;
745 	struct thread *td;
746 {
747 	struct msdosfsmount *pmp;
748 	int error, flags;
749 
750 	flags = 0;
751 	if (mntflags & MNT_FORCE)
752 		flags |= FORCECLOSE;
753 	error = vflush(mp, 0, flags, td);
754 	if (error)
755 		return error;
756 	pmp = VFSTOMSDOSFS(mp);
757 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
758 		if (pmp->pm_w2u)
759 			msdosfs_iconv->close(pmp->pm_w2u);
760 		if (pmp->pm_u2w)
761 			msdosfs_iconv->close(pmp->pm_u2w);
762 		if (pmp->pm_d2u)
763 			msdosfs_iconv->close(pmp->pm_d2u);
764 		if (pmp->pm_u2d)
765 			msdosfs_iconv->close(pmp->pm_u2d);
766 	}
767 
768 	/* If the volume was mounted read/write, mark it clean now. */
769 	if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
770 		error = markvoldirty(pmp, 0);
771 		if (error && (flags & FORCECLOSE) == 0)
772 			return (error);
773 	}
774 #ifdef MSDOSFS_DEBUG
775 	{
776 		struct vnode *vp = pmp->pm_devvp;
777 
778 		VI_LOCK(vp);
779 		vn_printf(vp,
780 		    "msdosfs_umount(): just before calling VOP_CLOSE()\n");
781 		printf("freef %p, freeb %p, mount %p\n",
782 		    TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
783 		    vp->v_mount);
784 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
785 		    TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
786 		    TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
787 		    vp->v_bufobj.bo_numoutput, vp->v_type);
788 		VI_UNLOCK(vp);
789 	}
790 #endif
791 	DROP_GIANT();
792 	g_topology_lock();
793 	g_vfs_close(pmp->pm_cp, td);
794 	g_topology_unlock();
795 	PICKUP_GIANT();
796 	vrele(pmp->pm_devvp);
797 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
798 #ifdef MSDOSFS_LARGE
799 	msdosfs_fileno_free(mp);
800 #endif
801 	free(pmp, M_MSDOSFSMNT);
802 	mp->mnt_data = (qaddr_t)0;
803 	mp->mnt_flag &= ~MNT_LOCAL;
804 	return (error);
805 }
806 
807 static int
808 msdosfs_root(mp, flags, vpp, td)
809 	struct mount *mp;
810 	int flags;
811 	struct vnode **vpp;
812 	struct thread *td;
813 {
814 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
815 	struct denode *ndep;
816 	int error;
817 
818 #ifdef MSDOSFS_DEBUG
819 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
820 #endif
821 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
822 	if (error)
823 		return (error);
824 	*vpp = DETOV(ndep);
825 	return (0);
826 }
827 
828 static int
829 msdosfs_statfs(mp, sbp, td)
830 	struct mount *mp;
831 	struct statfs *sbp;
832 	struct thread *td;
833 {
834 	struct msdosfsmount *pmp;
835 
836 	pmp = VFSTOMSDOSFS(mp);
837 	sbp->f_bsize = pmp->pm_bpcluster;
838 	sbp->f_iosize = pmp->pm_bpcluster;
839 	sbp->f_blocks = pmp->pm_maxcluster + 1;
840 	sbp->f_bfree = pmp->pm_freeclustercount;
841 	sbp->f_bavail = pmp->pm_freeclustercount;
842 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
843 	sbp->f_ffree = 0;	/* what to put in here? */
844 	return (0);
845 }
846 
847 static int
848 msdosfs_sync(mp, waitfor, td)
849 	struct mount *mp;
850 	int waitfor;
851 	struct thread *td;
852 {
853 	struct vnode *vp, *nvp;
854 	struct denode *dep;
855 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
856 	int error, allerror = 0;
857 
858 	/*
859 	 * If we ever switch to not updating all of the fats all the time,
860 	 * this would be the place to update them from the first one.
861 	 */
862 	if (pmp->pm_fmod != 0) {
863 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
864 			panic("msdosfs_sync: rofs mod");
865 		else {
866 			/* update fats here */
867 		}
868 	}
869 	/*
870 	 * Write back each (modified) denode.
871 	 */
872 	MNT_ILOCK(mp);
873 loop:
874 	MNT_VNODE_FOREACH(vp, mp, nvp) {
875 		VI_LOCK(vp);
876 		if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) {
877 			VI_UNLOCK(vp);
878 			continue;
879 		}
880 		MNT_IUNLOCK(mp);
881 		dep = VTODE(vp);
882 		if ((dep->de_flag &
883 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
884 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
885 		    waitfor == MNT_LAZY)) {
886 			VI_UNLOCK(vp);
887 			MNT_ILOCK(mp);
888 			continue;
889 		}
890 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
891 		if (error) {
892 			MNT_ILOCK(mp);
893 			if (error == ENOENT)
894 				goto loop;
895 			continue;
896 		}
897 		error = VOP_FSYNC(vp, waitfor, td);
898 		if (error)
899 			allerror = error;
900 		VOP_UNLOCK(vp, 0, td);
901 		vrele(vp);
902 		MNT_ILOCK(mp);
903 	}
904 	MNT_IUNLOCK(mp);
905 
906 	/*
907 	 * Flush filesystem control info.
908 	 */
909 	if (waitfor != MNT_LAZY) {
910 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
911 		error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
912 		if (error)
913 			allerror = error;
914 		VOP_UNLOCK(pmp->pm_devvp, 0, td);
915 	}
916 	return (allerror);
917 }
918 
919 static int
920 msdosfs_fhtovp(mp, fhp, vpp)
921 	struct mount *mp;
922 	struct fid *fhp;
923 	struct vnode **vpp;
924 {
925 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
926 	struct defid *defhp = (struct defid *) fhp;
927 	struct denode *dep;
928 	int error;
929 
930 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
931 	if (error) {
932 		*vpp = NULLVP;
933 		return (error);
934 	}
935 	*vpp = DETOV(dep);
936 	vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
937 	return (0);
938 }
939 
940 static int
941 msdosfs_vptofh(vp, fhp)
942 	struct vnode *vp;
943 	struct fid *fhp;
944 {
945 	struct denode *dep;
946 	struct defid *defhp;
947 
948 	dep = VTODE(vp);
949 	defhp = (struct defid *)fhp;
950 	defhp->defid_len = sizeof(struct defid);
951 	defhp->defid_dirclust = dep->de_dirclust;
952 	defhp->defid_dirofs = dep->de_diroffset;
953 	/* defhp->defid_gen = dep->de_gen; */
954 	return (0);
955 }
956 
957 static struct vfsops msdosfs_vfsops = {
958 	.vfs_fhtovp =		msdosfs_fhtovp,
959 	.vfs_mount =		msdosfs_mount,
960 	.vfs_cmount =		msdosfs_cmount,
961 	.vfs_root =		msdosfs_root,
962 	.vfs_statfs =		msdosfs_statfs,
963 	.vfs_sync =		msdosfs_sync,
964 	.vfs_unmount =		msdosfs_unmount,
965 	.vfs_vptofh =		msdosfs_vptofh,
966 };
967 
968 VFS_SET(msdosfs_vfsops, msdosfs, 0);
969 MODULE_VERSION(msdosfs, 1);
970