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