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