xref: /freebsd/sys/fs/msdosfs/msdosfs_vfsops.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
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/buf.h>
54 #include <sys/conf.h>
55 #include <sys/fcntl.h>
56 #include <sys/iconv.h>
57 #include <sys/kernel.h>
58 #include <sys/lock.h>
59 #include <sys/malloc.h>
60 #include <sys/mount.h>
61 #include <sys/mutex.h>
62 #include <sys/namei.h>
63 #include <sys/priv.h>
64 #include <sys/proc.h>
65 #include <sys/stat.h>
66 #include <sys/vnode.h>
67 
68 #include <geom/geom.h>
69 #include <geom/geom_vfs.h>
70 
71 #include <fs/msdosfs/bootsect.h>
72 #include <fs/msdosfs/bpb.h>
73 #include <fs/msdosfs/direntry.h>
74 #include <fs/msdosfs/denode.h>
75 #include <fs/msdosfs/fat.h>
76 #include <fs/msdosfs/msdosfsmount.h>
77 
78 static const char msdosfs_lock_msg[] = "fatlk";
79 
80 /* Mount options that we support. */
81 static const char *msdosfs_opts[] = {
82 	"async", "noatime", "noclusterr", "noclusterw",
83 	"export", "force", "from", "sync",
84 	"cs_dos", "cs_local", "cs_win", "dirmask",
85 	"gid", "kiconv", "large", "longname",
86 	"longnames", "mask", "shortname", "shortnames",
87 	"uid", "win95", "nowin95",
88 	NULL
89 };
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;
106 
107 static int	update_mp(struct mount *mp, struct thread *td);
108 static int	mountmsdosfs(struct vnode *devvp, struct mount *mp);
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 (vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v) == 1)
153 		pmp->pm_gid = v;
154 	if (vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v) == 1)
155 		pmp->pm_uid = v;
156 	if (vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v) == 1)
157 		pmp->pm_mask = v & ALLPERMS;
158 	if (vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v) == 1)
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
179 		pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
180 	return 0;
181 }
182 
183 static int
184 msdosfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
185 {
186 	struct msdosfs_args args;
187 	struct export_args exp;
188 	int error;
189 
190 	if (data == NULL)
191 		return (EINVAL);
192 	error = copyin(data, &args, sizeof args);
193 	if (error)
194 		return (error);
195 	vfs_oexport_conv(&args.export, &exp);
196 
197 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
198 	ma = mount_arg(ma, "export", &exp, sizeof(exp));
199 	ma = mount_argf(ma, "uid", "%d", args.uid);
200 	ma = mount_argf(ma, "gid", "%d", args.gid);
201 	ma = mount_argf(ma, "mask", "%d", args.mask);
202 	ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
203 
204 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
205 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
206 	ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
207 	ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
208 
209 	ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
210 	ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
211 	ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
212 
213 	error = kernel_mount(ma, flags);
214 
215 	return (error);
216 }
217 
218 /*
219  * mp - path - addr in user space of mount point (ie /usr or whatever)
220  * data - addr in user space of mount params including the name of the block
221  * special file to treat as a filesystem.
222  */
223 static int
224 msdosfs_mount(struct mount *mp)
225 {
226 	struct vnode *devvp;	  /* vnode for blk device to mount */
227 	struct thread *td;
228 	/* msdosfs specific mount control block */
229 	struct msdosfsmount *pmp = NULL;
230 	struct nameidata ndp;
231 	int error, flags;
232 	accmode_t accmode;
233 	char *from;
234 
235 	td = curthread;
236 	if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
237 		return (EINVAL);
238 
239 	/*
240 	 * If updating, check whether changing from read-only to
241 	 * read/write; if there is no device name, that's all we do.
242 	 */
243 	if (mp->mnt_flag & MNT_UPDATE) {
244 		pmp = VFSTOMSDOSFS(mp);
245 		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) {
246 			/*
247 			 * Forbid export requests if filesystem has
248 			 * MSDOSFS_LARGEFS flag set.
249 			 */
250 			if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0) {
251 				vfs_mount_error(mp,
252 				    "MSDOSFS_LARGEFS flag set, cannot export");
253 				return (EOPNOTSUPP);
254 			}
255 		}
256 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
257 		    vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
258 			error = VFS_SYNC(mp, MNT_WAIT);
259 			if (error)
260 				return (error);
261 			flags = WRITECLOSE;
262 			if (mp->mnt_flag & MNT_FORCE)
263 				flags |= FORCECLOSE;
264 			error = vflush(mp, 0, flags, td);
265 			if (error)
266 				return (error);
267 
268 			/*
269 			 * Now the volume is clean.  Mark it so while the
270 			 * device is still rw.
271 			 */
272 			error = markvoldirty(pmp, 0);
273 			if (error) {
274 				(void)markvoldirty(pmp, 1);
275 				return (error);
276 			}
277 
278 			/* Downgrade the device from rw to ro. */
279 			g_topology_lock();
280 			error = g_access(pmp->pm_cp, 0, -1, 0);
281 			g_topology_unlock();
282 			if (error) {
283 				(void)markvoldirty(pmp, 1);
284 				return (error);
285 			}
286 
287 			/*
288 			 * Backing out after an error was painful in the
289 			 * above.  Now we are committed to succeeding.
290 			 */
291 			pmp->pm_fmod = 0;
292 			pmp->pm_flags |= MSDOSFSMNT_RONLY;
293 			MNT_ILOCK(mp);
294 			mp->mnt_flag |= MNT_RDONLY;
295 			MNT_IUNLOCK(mp);
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);
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);
310 				return (error);
311 			}
312 			VOP_UNLOCK(devvp, 0);
313 			g_topology_lock();
314 			error = g_access(pmp->pm_cp, 0, 1, 0);
315 			g_topology_unlock();
316 			if (error)
317 				return (error);
318 
319 			pmp->pm_fmod = 1;
320 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
321 			MNT_ILOCK(mp);
322 			mp->mnt_flag &= ~MNT_RDONLY;
323 			MNT_IUNLOCK(mp);
324 
325 			/* Now that the volume is modifiable, mark it dirty. */
326 			error = markvoldirty(pmp, 1);
327 			if (error)
328 				return (error);
329 		}
330 	}
331 	/*
332 	 * Not an update, or updating the name: look up the name
333 	 * and verify that it refers to a sensible disk device.
334 	 */
335 	if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
336 		return (EINVAL);
337 	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
338 	error = namei(&ndp);
339 	if (error)
340 		return (error);
341 	devvp = ndp.ni_vp;
342 	NDFREE(&ndp, NDF_ONLY_PNBUF);
343 
344 	if (!vn_isdisk(devvp, &error)) {
345 		vput(devvp);
346 		return (error);
347 	}
348 	/*
349 	 * If mount by non-root, then verify that user has necessary
350 	 * permissions on the device.
351 	 */
352 	accmode = VREAD;
353 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
354 		accmode |= VWRITE;
355 	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
356 	if (error)
357 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
358 	if (error) {
359 		vput(devvp);
360 		return (error);
361 	}
362 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
363 		error = mountmsdosfs(devvp, mp);
364 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
365 		pmp = VFSTOMSDOSFS(mp);
366 #endif
367 	} else {
368 		vput(devvp);
369 		if (devvp != pmp->pm_devvp)
370 			return (EINVAL);	/* XXX needs translation */
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);
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)
393 {
394 	struct msdosfsmount *pmp;
395 	struct buf *bp;
396 	struct cdev *dev;
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 	bp = NULL;		/* This and pmp both used in error_exit. */
408 	pmp = NULL;
409 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
410 
411 	dev = devvp->v_rdev;
412 	if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
413 	    (uintptr_t)mp) == 0) {
414 		VOP_UNLOCK(devvp, 0);
415 		return (EBUSY);
416 	}
417 	g_topology_lock();
418 	error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
419 	g_topology_unlock();
420 	if (error != 0) {
421 		atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
422 		VOP_UNLOCK(devvp, 0);
423 		return (error);
424 	}
425 	dev_ref(dev);
426 	VOP_UNLOCK(devvp, 0);
427 
428 	bo = &devvp->v_bufobj;
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: 8192 is a magic size that works for ffs.
435 	 */
436 	error = bread(devvp, 0, 8192, 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 	lockinit(&pmp->pm_fatlock, 0, msdosfs_lock_msg, 0, 0);
459 
460 	/*
461 	 * Initialize ownerships and permissions, since nothing else will
462 	 * initialize them iff we are mounting root.
463 	 */
464 	pmp->pm_uid = UID_ROOT;
465 	pmp->pm_gid = GID_WHEEL;
466 	pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
467 	    S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
468 
469 	/*
470 	 * Experimental support for large MS-DOS filesystems.
471 	 * WARNING: This uses at least 32 bytes of kernel memory (which is not
472 	 * reclaimed until the FS is unmounted) for each file on disk to map
473 	 * between the 32-bit inode numbers used by VFS and the 64-bit
474 	 * pseudo-inode numbers used internally by msdosfs. This is only
475 	 * safe to use in certain controlled situations (e.g. read-only FS
476 	 * with less than 1 million files).
477 	 * Since the mappings do not persist across unmounts (or reboots), these
478 	 * filesystems are not suitable for exporting through NFS, or any other
479 	 * application that requires fixed inode numbers.
480 	 */
481 	vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags, MSDOSFS_LARGEFS);
482 
483 	/*
484 	 * Compute several useful quantities from the bpb in the
485 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
486 	 * the fields that are different between dos 5 and dos 3.3.
487 	 */
488 	SecPerClust = b50->bpbSecPerClust;
489 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
490 	if (pmp->pm_BytesPerSec < DEV_BSIZE) {
491 		error = EINVAL;
492 		goto error_exit;
493 	}
494 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
495 	pmp->pm_FATs = b50->bpbFATs;
496 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
497 	pmp->pm_Sectors = getushort(b50->bpbSectors);
498 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
499 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
500 	pmp->pm_Heads = getushort(b50->bpbHeads);
501 	pmp->pm_Media = b50->bpbMedia;
502 
503 	/* calculate the ratio of sector size to DEV_BSIZE */
504 	pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
505 
506 	/*
507 	 * We don't check pm_Heads nor pm_SecPerTrack, because
508 	 * these may not be set for EFI file systems. We don't
509 	 * use these anyway, so we're unaffected if they are
510 	 * invalid.
511 	 */
512 	if (!pmp->pm_BytesPerSec || !SecPerClust) {
513 		error = EINVAL;
514 		goto error_exit;
515 	}
516 
517 	if (pmp->pm_Sectors == 0) {
518 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
519 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
520 	} else {
521 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
522 		pmp->pm_HugeSectors = pmp->pm_Sectors;
523 	}
524 	if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) {
525 		if (pmp->pm_HugeSectors > 0xffffffff /
526 		    (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
527 			/*
528 			 * We cannot deal currently with this size of disk
529 			 * due to fileid limitations (see msdosfs_getattr and
530 			 * msdosfs_readdir)
531 			 */
532 			error = EINVAL;
533 			vfs_mount_error(mp,
534 			    "Disk too big, try '-o large' mount option");
535 			goto error_exit;
536 		}
537 	}
538 
539 	if (pmp->pm_RootDirEnts == 0) {
540 		if (pmp->pm_FATsecs
541 		    || getushort(b710->bpbFSVers)) {
542 			error = EINVAL;
543 #ifdef MSDOSFS_DEBUG
544 			printf("mountmsdosfs(): bad FAT32 filesystem\n");
545 #endif
546 			goto error_exit;
547 		}
548 		pmp->pm_fatmask = FAT32_MASK;
549 		pmp->pm_fatmult = 4;
550 		pmp->pm_fatdiv = 1;
551 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
552 		if (getushort(b710->bpbExtFlags) & FATMIRROR)
553 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
554 		else
555 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
556 	} else
557 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
558 
559 	/*
560 	 * Check a few values (could do some more):
561 	 * - logical sector size: power of 2, >= block size
562 	 * - sectors per cluster: power of 2, >= 1
563 	 * - number of sectors:   >= 1, <= size of partition
564 	 * - number of FAT sectors: >= 1
565 	 */
566 	if ( (SecPerClust == 0)
567 	  || (SecPerClust & (SecPerClust - 1))
568 	  || (pmp->pm_BytesPerSec < DEV_BSIZE)
569 	  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
570 	  || (pmp->pm_HugeSectors == 0)
571 	  || (pmp->pm_FATsecs == 0)
572 	  || (SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE)
573 	) {
574 		error = EINVAL;
575 		goto error_exit;
576 	}
577 
578 	pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
579 	pmp->pm_HiddenSects *= pmp->pm_BlkPerSec;	/* XXX not used? */
580 	pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
581 	SecPerClust         *= pmp->pm_BlkPerSec;
582 
583 	pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
584 
585 	if (FAT32(pmp)) {
586 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
587 		pmp->pm_firstcluster = pmp->pm_fatblk
588 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
589 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
590 	} else {
591 		pmp->pm_rootdirblk = pmp->pm_fatblk +
592 			(pmp->pm_FATs * pmp->pm_FATsecs);
593 		pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts *
594 			sizeof(struct direntry), DEV_BSIZE); /* in blocks */
595 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
596 	}
597 
598 	pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
599 	    SecPerClust + 1;
600 	pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE;	/* XXX not used? */
601 
602 	if (pmp->pm_fatmask == 0) {
603 		if (pmp->pm_maxcluster
604 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
605 			/*
606 			 * This will usually be a floppy disk. This size makes
607 			 * sure that one fat entry will not be split across
608 			 * multiple blocks.
609 			 */
610 			pmp->pm_fatmask = FAT12_MASK;
611 			pmp->pm_fatmult = 3;
612 			pmp->pm_fatdiv = 2;
613 		} else {
614 			pmp->pm_fatmask = FAT16_MASK;
615 			pmp->pm_fatmult = 2;
616 			pmp->pm_fatdiv = 1;
617 		}
618 	}
619 
620 	clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
621 	if (pmp->pm_maxcluster >= clusters) {
622 #ifdef MSDOSFS_DEBUG
623 		printf("Warning: number of clusters (%ld) exceeds FAT "
624 		    "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
625 #endif
626 		pmp->pm_maxcluster = clusters - 1;
627 	}
628 
629 	if (FAT12(pmp))
630 		pmp->pm_fatblocksize = 3 * 512;
631 	else
632 		pmp->pm_fatblocksize = PAGE_SIZE;
633 	pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
634 	    pmp->pm_BytesPerSec);
635 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
636 	pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
637 
638 	/*
639 	 * Compute mask and shift value for isolating cluster relative byte
640 	 * offsets and cluster numbers from a file offset.
641 	 */
642 	pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
643 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
644 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
645 
646 	/*
647 	 * Check for valid cluster size
648 	 * must be a power of 2
649 	 */
650 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
651 		error = EINVAL;
652 		goto error_exit;
653 	}
654 
655 	/*
656 	 * Release the bootsector buffer.
657 	 */
658 	brelse(bp);
659 	bp = NULL;
660 
661 	/*
662 	 * Check the fsinfo sector if we have one.  Silently fix up our
663 	 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
664 	 * or too large.  Ignore fp->fsinfree for now, since we need to
665 	 * read the entire FAT anyway to fill the inuse map.
666 	 */
667 	if (pmp->pm_fsinfo) {
668 		struct fsinfo *fp;
669 
670 		if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
671 		    NOCRED, &bp)) != 0)
672 			goto error_exit;
673 		fp = (struct fsinfo *)bp->b_data;
674 		if (!bcmp(fp->fsisig1, "RRaA", 4)
675 		    && !bcmp(fp->fsisig2, "rrAa", 4)
676 		    && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
677 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
678 			if (pmp->pm_nxtfree > pmp->pm_maxcluster)
679 				pmp->pm_nxtfree = CLUST_FIRST;
680 		} else
681 			pmp->pm_fsinfo = 0;
682 		brelse(bp);
683 		bp = NULL;
684 	}
685 
686 	/*
687 	 * Finish initializing pmp->pm_nxtfree (just in case the first few
688 	 * sectors aren't properly reserved in the FAT).  This completes
689 	 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
690 	 * value if there is no fsinfo.  We will use pmp->pm_nxtfree
691 	 * internally even if there is no fsinfo.
692 	 */
693 	if (pmp->pm_nxtfree < CLUST_FIRST)
694 		pmp->pm_nxtfree = CLUST_FIRST;
695 
696 	/*
697 	 * Allocate memory for the bitmap of allocated clusters, and then
698 	 * fill it in.
699 	 */
700 	pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
701 				  * sizeof(*pmp->pm_inusemap),
702 				  M_MSDOSFSFAT, M_WAITOK);
703 
704 	/*
705 	 * fillinusemap() needs pm_devvp.
706 	 */
707 	pmp->pm_devvp = devvp;
708 	pmp->pm_dev = dev;
709 
710 	/*
711 	 * Have the inuse map filled in.
712 	 */
713 	MSDOSFS_LOCK_MP(pmp);
714 	error = fillinusemap(pmp);
715 	MSDOSFS_UNLOCK_MP(pmp);
716 	if (error != 0)
717 		goto error_exit;
718 
719 	/*
720 	 * If they want fat updates to be synchronous then let them suffer
721 	 * the performance degradation in exchange for the on disk copy of
722 	 * the fat being correct just about all the time.  I suppose this
723 	 * would be a good thing to turn on if the kernel is still flakey.
724 	 */
725 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
726 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
727 
728 	/*
729 	 * Finish up.
730 	 */
731 	if (ronly)
732 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
733 	else {
734 		if ((error = markvoldirty(pmp, 1)) != 0) {
735 			(void)markvoldirty(pmp, 0);
736 			goto error_exit;
737 		}
738 		pmp->pm_fmod = 1;
739 	}
740 	mp->mnt_data =  pmp;
741 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
742 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
743 	MNT_ILOCK(mp);
744 	mp->mnt_flag |= MNT_LOCAL;
745 	mp->mnt_kern_flag |= MNTK_USES_BCACHE | MNTK_NO_IOPF;
746 	MNT_IUNLOCK(mp);
747 
748 	if (pmp->pm_flags & MSDOSFS_LARGEFS)
749 		msdosfs_fileno_init(mp);
750 
751 	return 0;
752 
753 error_exit:
754 	if (bp)
755 		brelse(bp);
756 	if (cp != NULL) {
757 		g_topology_lock();
758 		g_vfs_close(cp);
759 		g_topology_unlock();
760 	}
761 	if (pmp) {
762 		lockdestroy(&pmp->pm_fatlock);
763 		free(pmp->pm_inusemap, M_MSDOSFSFAT);
764 		free(pmp, M_MSDOSFSMNT);
765 		mp->mnt_data = NULL;
766 	}
767 	atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
768 	dev_rel(dev);
769 	return (error);
770 }
771 
772 /*
773  * Unmount the filesystem described by mp.
774  */
775 static int
776 msdosfs_unmount(struct mount *mp, int mntflags)
777 {
778 	struct msdosfsmount *pmp;
779 	int error, flags;
780 
781 	error = flags = 0;
782 	pmp = VFSTOMSDOSFS(mp);
783 	if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0)
784 		error = msdosfs_sync(mp, MNT_WAIT);
785 	if ((mntflags & MNT_FORCE) != 0)
786 		flags |= FORCECLOSE;
787 	else if (error != 0)
788 		return (error);
789 	error = vflush(mp, 0, flags, curthread);
790 	if (error != 0 && error != ENXIO)
791 		return (error);
792 	if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
793 		error = markvoldirty(pmp, 0);
794 		if (error && error != ENXIO) {
795 			(void)markvoldirty(pmp, 1);
796 			return (error);
797 		}
798 	}
799 	if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
800 		if (pmp->pm_w2u)
801 			msdosfs_iconv->close(pmp->pm_w2u);
802 		if (pmp->pm_u2w)
803 			msdosfs_iconv->close(pmp->pm_u2w);
804 		if (pmp->pm_d2u)
805 			msdosfs_iconv->close(pmp->pm_d2u);
806 		if (pmp->pm_u2d)
807 			msdosfs_iconv->close(pmp->pm_u2d);
808 	}
809 
810 #ifdef MSDOSFS_DEBUG
811 	{
812 		struct vnode *vp = pmp->pm_devvp;
813 		struct bufobj *bo;
814 
815 		bo = &vp->v_bufobj;
816 		BO_LOCK(bo);
817 		VI_LOCK(vp);
818 		vn_printf(vp,
819 		    "msdosfs_umount(): just before calling VOP_CLOSE()\n");
820 		printf("freef %p, freeb %p, mount %p\n",
821 		    TAILQ_NEXT(vp, v_actfreelist), vp->v_actfreelist.tqe_prev,
822 		    vp->v_mount);
823 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
824 		    TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
825 		    TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
826 		    vp->v_bufobj.bo_numoutput, vp->v_type);
827 		VI_UNLOCK(vp);
828 		BO_UNLOCK(bo);
829 	}
830 #endif
831 	g_topology_lock();
832 	g_vfs_close(pmp->pm_cp);
833 	g_topology_unlock();
834 	atomic_store_rel_ptr((uintptr_t *)&pmp->pm_dev->si_mountpt, 0);
835 	vrele(pmp->pm_devvp);
836 	dev_rel(pmp->pm_dev);
837 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
838 	if (pmp->pm_flags & MSDOSFS_LARGEFS)
839 		msdosfs_fileno_free(mp);
840 	lockdestroy(&pmp->pm_fatlock);
841 	free(pmp, M_MSDOSFSMNT);
842 	mp->mnt_data = NULL;
843 	MNT_ILOCK(mp);
844 	mp->mnt_flag &= ~MNT_LOCAL;
845 	MNT_IUNLOCK(mp);
846 	return (error);
847 }
848 
849 static int
850 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp)
851 {
852 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
853 	struct denode *ndep;
854 	int error;
855 
856 #ifdef MSDOSFS_DEBUG
857 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
858 #endif
859 	error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
860 	if (error)
861 		return (error);
862 	*vpp = DETOV(ndep);
863 	return (0);
864 }
865 
866 static int
867 msdosfs_statfs(struct mount *mp, struct statfs *sbp)
868 {
869 	struct msdosfsmount *pmp;
870 
871 	pmp = VFSTOMSDOSFS(mp);
872 	sbp->f_bsize = pmp->pm_bpcluster;
873 	sbp->f_iosize = pmp->pm_bpcluster;
874 	sbp->f_blocks = pmp->pm_maxcluster + 1;
875 	sbp->f_bfree = pmp->pm_freeclustercount;
876 	sbp->f_bavail = pmp->pm_freeclustercount;
877 	sbp->f_files = pmp->pm_RootDirEnts;	/* XXX */
878 	sbp->f_ffree = 0;	/* what to put in here? */
879 	return (0);
880 }
881 
882 /*
883  * If we have an FSInfo block, update it.
884  */
885 static int
886 msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
887 {
888 	struct fsinfo *fp;
889 	struct buf *bp;
890 	int error;
891 
892 	MSDOSFS_LOCK_MP(pmp);
893 	if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
894 		error = 0;
895 		goto unlock;
896 	}
897 	error = bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
898 	    NOCRED, &bp);
899 	if (error != 0) {
900 		brelse(bp);
901 		goto unlock;
902 	}
903 	fp = (struct fsinfo *)bp->b_data;
904 	putulong(fp->fsinfree, pmp->pm_freeclustercount);
905 	putulong(fp->fsinxtfree, pmp->pm_nxtfree);
906 	pmp->pm_flags &= ~MSDOSFS_FSIMOD;
907 	if (waitfor == MNT_WAIT)
908 		error = bwrite(bp);
909 	else
910 		bawrite(bp);
911 unlock:
912 	MSDOSFS_UNLOCK_MP(pmp);
913 	return (error);
914 }
915 
916 static int
917 msdosfs_sync(struct mount *mp, int waitfor)
918 {
919 	struct vnode *vp, *nvp;
920 	struct thread *td;
921 	struct denode *dep;
922 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
923 	int error, allerror = 0;
924 
925 	td = curthread;
926 
927 	/*
928 	 * If we ever switch to not updating all of the fats all the time,
929 	 * this would be the place to update them from the first one.
930 	 */
931 	if (pmp->pm_fmod != 0) {
932 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
933 			panic("msdosfs_sync: rofs mod");
934 		else {
935 			/* update fats here */
936 		}
937 	}
938 	/*
939 	 * Write back each (modified) denode.
940 	 */
941 loop:
942 	MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
943 		if (vp->v_type == VNON) {
944 			VI_UNLOCK(vp);
945 			continue;
946 		}
947 		dep = VTODE(vp);
948 		if ((dep->de_flag &
949 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
950 		    (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
951 		    waitfor == MNT_LAZY)) {
952 			VI_UNLOCK(vp);
953 			continue;
954 		}
955 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
956 		if (error) {
957 			if (error == ENOENT)
958 				goto loop;
959 			continue;
960 		}
961 		error = VOP_FSYNC(vp, waitfor, td);
962 		if (error)
963 			allerror = error;
964 		VOP_UNLOCK(vp, 0);
965 		vrele(vp);
966 	}
967 
968 	/*
969 	 * Flush filesystem control info.
970 	 */
971 	if (waitfor != MNT_LAZY) {
972 		vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
973 		error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
974 		if (error)
975 			allerror = error;
976 		VOP_UNLOCK(pmp->pm_devvp, 0);
977 	}
978 
979 	error = msdosfs_fsiflush(pmp, waitfor);
980 	if (error != 0)
981 		allerror = error;
982 	return (allerror);
983 }
984 
985 static int
986 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
987 {
988 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
989 	struct defid *defhp = (struct defid *) fhp;
990 	struct denode *dep;
991 	int error;
992 
993 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
994 	if (error) {
995 		*vpp = NULLVP;
996 		return (error);
997 	}
998 	*vpp = DETOV(dep);
999 	vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
1000 	return (0);
1001 }
1002 
1003 static struct vfsops msdosfs_vfsops = {
1004 	.vfs_fhtovp =		msdosfs_fhtovp,
1005 	.vfs_mount =		msdosfs_mount,
1006 	.vfs_cmount =		msdosfs_cmount,
1007 	.vfs_root =		msdosfs_root,
1008 	.vfs_statfs =		msdosfs_statfs,
1009 	.vfs_sync =		msdosfs_sync,
1010 	.vfs_unmount =		msdosfs_unmount,
1011 };
1012 
1013 VFS_SET(msdosfs_vfsops, msdosfs, 0);
1014 MODULE_VERSION(msdosfs, 1);
1015