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