xref: /netbsd-src/sys/fs/msdosfs/msdosfs_vfsops.c (revision 20e85ad185ab16980f1219a557c42e057edb42ea)
1 /*	$NetBSD: msdosfs_vfsops.c,v 1.23 2005/03/29 02:41:05 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
5  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
6  * All rights reserved.
7  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by TooLs GmbH.
20  * 4. The name of TooLs GmbH may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 /*
35  * Written by Paul Popelka (paulp@uts.amdahl.com)
36  *
37  * You can do anything you want with this software, just don't say you wrote
38  * it, and don't remove this notice.
39  *
40  * This software is provided "as is".
41  *
42  * The author supplies this software to be publicly redistributed on the
43  * understanding that the author is not responsible for the correct
44  * functioning of this software in any circumstances and is not liable for
45  * any damages caused by this software.
46  *
47  * October 1992
48  */
49 
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: msdosfs_vfsops.c,v 1.23 2005/03/29 02:41:05 thorpej Exp $");
52 
53 #if defined(_KERNEL_OPT)
54 #include "opt_quota.h"
55 #include "opt_compat_netbsd.h"
56 #endif
57 
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/sysctl.h>
61 #include <sys/namei.h>
62 #include <sys/proc.h>
63 #include <sys/kernel.h>
64 #include <sys/vnode.h>
65 #include <miscfs/specfs/specdev.h> /* XXX */	/* defines v_rdev */
66 #include <sys/mount.h>
67 #include <sys/buf.h>
68 #include <sys/file.h>
69 #include <sys/device.h>
70 #include <sys/disklabel.h>
71 #include <sys/ioctl.h>
72 #include <sys/malloc.h>
73 #include <sys/dirent.h>
74 #include <sys/stat.h>
75 #include <sys/conf.h>
76 
77 #include <fs/msdosfs/bpb.h>
78 #include <fs/msdosfs/bootsect.h>
79 #include <fs/msdosfs/direntry.h>
80 #include <fs/msdosfs/denode.h>
81 #include <fs/msdosfs/msdosfsmount.h>
82 #include <fs/msdosfs/fat.h>
83 
84 #define MSDOSFS_NAMEMAX(pmp) \
85 	(pmp)->pm_flags & MSDOSFSMNT_LONGNAME ? WIN_MAXLEN : 12
86 
87 int msdosfs_mountroot __P((void));
88 int msdosfs_mount __P((struct mount *, const char *, void *,
89     struct nameidata *, struct proc *));
90 int msdosfs_start __P((struct mount *, int, struct proc *));
91 int msdosfs_unmount __P((struct mount *, int, struct proc *));
92 int msdosfs_root __P((struct mount *, struct vnode **));
93 int msdosfs_quotactl __P((struct mount *, int, uid_t, void *, struct proc *));
94 int msdosfs_statvfs __P((struct mount *, struct statvfs *, struct proc *));
95 int msdosfs_sync __P((struct mount *, int, struct ucred *, struct proc *));
96 int msdosfs_vget __P((struct mount *, ino_t, struct vnode **));
97 int msdosfs_fhtovp __P((struct mount *, struct fid *, struct vnode **));
98 int msdosfs_checkexp __P((struct mount *, struct mbuf *, int *,
99     struct ucred **));
100 int msdosfs_vptofh __P((struct vnode *, struct fid *));
101 
102 int msdosfs_mountfs __P((struct vnode *, struct mount *, struct proc *,
103     struct msdosfs_args *));
104 
105 static int update_mp __P((struct mount *, struct msdosfs_args *));
106 
107 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOS FS mount structure");
108 MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS fat", "MSDOS FS fat table");
109 
110 #define ROOTNAME "root_device"
111 
112 extern const struct vnodeopv_desc msdosfs_vnodeop_opv_desc;
113 
114 const struct vnodeopv_desc * const msdosfs_vnodeopv_descs[] = {
115 	&msdosfs_vnodeop_opv_desc,
116 	NULL,
117 };
118 
119 struct vfsops msdosfs_vfsops = {
120 	MOUNT_MSDOS,
121 	msdosfs_mount,
122 	msdosfs_start,
123 	msdosfs_unmount,
124 	msdosfs_root,
125 	msdosfs_quotactl,
126 	msdosfs_statvfs,
127 	msdosfs_sync,
128 	msdosfs_vget,
129 	msdosfs_fhtovp,
130 	msdosfs_vptofh,
131 	msdosfs_init,
132 	msdosfs_reinit,
133 	msdosfs_done,
134 	NULL,
135 	msdosfs_mountroot,
136 	msdosfs_checkexp,
137 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
138 	vfs_stdextattrctl,
139 	msdosfs_vnodeopv_descs,
140 };
141 VFS_ATTACH(msdosfs_vfsops);
142 
143 static int
144 update_mp(mp, argp)
145 	struct mount *mp;
146 	struct msdosfs_args *argp;
147 {
148 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
149 	int error;
150 
151 	pmp->pm_gid = argp->gid;
152 	pmp->pm_uid = argp->uid;
153 	pmp->pm_mask = argp->mask & ALLPERMS;
154 	pmp->pm_dirmask = argp->dirmask & ALLPERMS;
155 	pmp->pm_gmtoff = argp->gmtoff;
156 	pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
157 
158 	/*
159 	 * GEMDOS knows nothing (yet) about win95
160 	 */
161 	if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
162 		pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
163 
164 	if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
165 		pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
166 	else if (!(pmp->pm_flags &
167 	    (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
168 		struct vnode *rootvp;
169 
170 		/*
171 		 * Try to divine whether to support Win'95 long filenames
172 		 */
173 		if (FAT32(pmp))
174 			pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
175 		else {
176 			if ((error = msdosfs_root(mp, &rootvp)) != 0)
177 				return error;
178 			pmp->pm_flags |= findwin95(VTODE(rootvp))
179 				? MSDOSFSMNT_LONGNAME
180 					: MSDOSFSMNT_SHORTNAME;
181 			vput(rootvp);
182 		}
183 	}
184 
185 	mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
186 
187 	return 0;
188 }
189 
190 int
191 msdosfs_mountroot()
192 {
193 	struct mount *mp;
194 	struct proc *p = curproc;	/* XXX */
195 	int error;
196 	struct msdosfs_args args;
197 
198 	if (root_device->dv_class != DV_DISK)
199 		return (ENODEV);
200 
201 	if ((error = vfs_rootmountalloc(MOUNT_MSDOS, "root_device", &mp))) {
202 		vrele(rootvp);
203 		return (error);
204 	}
205 
206 	args.flags = MSDOSFSMNT_VERSIONED;
207 	args.uid = 0;
208 	args.gid = 0;
209 	args.mask = 0777;
210 	args.version = MSDOSFSMNT_VERSION;
211 	args.dirmask = 0777;
212 
213 	if ((error = msdosfs_mountfs(rootvp, mp, p, &args)) != 0) {
214 		mp->mnt_op->vfs_refcount--;
215 		vfs_unbusy(mp);
216 		free(mp, M_MOUNT);
217 		return (error);
218 	}
219 
220 	if ((error = update_mp(mp, &args)) != 0) {
221 		(void)msdosfs_unmount(mp, 0, p);
222 		vfs_unbusy(mp);
223 		free(mp, M_MOUNT);
224 		vrele(rootvp);
225 		return (error);
226 	}
227 
228 	simple_lock(&mountlist_slock);
229 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
230 	simple_unlock(&mountlist_slock);
231 	(void)msdosfs_statvfs(mp, &mp->mnt_stat, p);
232 	vfs_unbusy(mp);
233 	return (0);
234 }
235 
236 /*
237  * mp - path - addr in user space of mount point (ie /usr or whatever)
238  * data - addr in user space of mount params including the name of the block
239  * special file to treat as a filesystem.
240  */
241 int
242 msdosfs_mount(mp, path, data, ndp, p)
243 	struct mount *mp;
244 	const char *path;
245 	void *data;
246 	struct nameidata *ndp;
247 	struct proc *p;
248 {
249 	struct vnode *devvp;	  /* vnode for blk device to mount */
250 	struct msdosfs_args args; /* will hold data from mount request */
251 	/* msdosfs specific mount control block */
252 	struct msdosfsmount *pmp = NULL;
253 	int error, flags;
254 	mode_t accessmode;
255 
256 	if (mp->mnt_flag & MNT_GETARGS) {
257 		pmp = VFSTOMSDOSFS(mp);
258 		if (pmp == NULL)
259 			return EIO;
260 		args.fspec = NULL;
261 		args.uid = pmp->pm_uid;
262 		args.gid = pmp->pm_gid;
263 		args.mask = pmp->pm_mask;
264 		args.flags = pmp->pm_flags;
265 		args.version = MSDOSFSMNT_VERSION;
266 		args.dirmask = pmp->pm_dirmask;
267 		args.gmtoff = pmp->pm_gmtoff;
268 		vfs_showexport(mp, &args.export, &pmp->pm_export);
269 		return copyout(&args, data, sizeof(args));
270 	}
271 	error = copyin(data, &args, sizeof(struct msdosfs_args));
272 	if (error)
273 		return (error);
274 
275 	/*
276 	 * If not versioned (i.e. using old mount_msdos(8)), fill in
277 	 * the additional structure items with suitable defaults.
278 	 */
279 	if ((args.flags & MSDOSFSMNT_VERSIONED) == 0) {
280 		args.version = 1;
281 		args.dirmask = args.mask;
282 	}
283 
284 	/*
285 	 * Reset GMT offset for pre-v3 mount structure args.
286 	 */
287 	if (args.version < 3)
288 		args.gmtoff = 0;
289 
290 	/*
291 	 * If updating, check whether changing from read-only to
292 	 * read/write; if there is no device name, that's all we do.
293 	 */
294 	if (mp->mnt_flag & MNT_UPDATE) {
295 		pmp = VFSTOMSDOSFS(mp);
296 		error = 0;
297 		if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
298 			flags = WRITECLOSE;
299 			if (mp->mnt_flag & MNT_FORCE)
300 				flags |= FORCECLOSE;
301 			error = vflush(mp, NULLVP, flags);
302 		}
303 		if (!error && (mp->mnt_flag & MNT_RELOAD))
304 			/* not yet implemented */
305 			error = EOPNOTSUPP;
306 		if (error)
307 			return (error);
308 		if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_iflag & IMNT_WANTRDWR)) {
309 			/*
310 			 * If upgrade to read-write by non-root, then verify
311 			 * that user has necessary permissions on the device.
312 			 */
313 			if (p->p_ucred->cr_uid != 0) {
314 				devvp = pmp->pm_devvp;
315 				vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
316 				error = VOP_ACCESS(devvp, VREAD | VWRITE,
317 						   p->p_ucred, p);
318 				VOP_UNLOCK(devvp, 0);
319 				if (error)
320 					return (error);
321 			}
322 			pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
323 		}
324 		if (args.fspec == 0) {
325 #ifdef	__notyet__		/* doesn't work correctly with current mountd	XXX */
326 			if (args.flags & MSDOSFSMNT_MNTOPT) {
327 				pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
328 				pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
329 				if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
330 					pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
331 			}
332 #endif
333 			/*
334 			 * Process export requests.
335 			 */
336 			return (vfs_export(mp, &pmp->pm_export, &args.export));
337 		}
338 	}
339 	/*
340 	 * Not an update, or updating the name: look up the name
341 	 * and verify that it refers to a sensible block device.
342 	 */
343 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
344 	if ((error = namei(ndp)) != 0)
345 		return (error);
346 	devvp = ndp->ni_vp;
347 
348 	if (devvp->v_type != VBLK) {
349 		vrele(devvp);
350 		return (ENOTBLK);
351 	}
352 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
353 		vrele(devvp);
354 		return (ENXIO);
355 	}
356 	/*
357 	 * If mount by non-root, then verify that user has necessary
358 	 * permissions on the device.
359 	 */
360 	if (p->p_ucred->cr_uid != 0) {
361 		accessmode = VREAD;
362 		if ((mp->mnt_flag & MNT_RDONLY) == 0)
363 			accessmode |= VWRITE;
364 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
365 		error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
366 		VOP_UNLOCK(devvp, 0);
367 		if (error) {
368 			vrele(devvp);
369 			return (error);
370 		}
371 	}
372 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
373 		int flags;
374 
375 		/*
376 		 * Disallow multiple mounts of the same device.
377 		 * Disallow mounting of a device that is currently in use
378 		 * (except for root, which might share swap device for
379 		 * miniroot).
380 		 */
381 		error = vfs_mountedon(devvp);
382 		if (error)
383 			goto fail;
384 		if (vcount(devvp) > 1 && devvp != rootvp) {
385 			error = EBUSY;
386 			goto fail;
387 		}
388 		if (mp->mnt_flag & MNT_RDONLY)
389 			flags = FREAD;
390 		else
391 			flags = FREAD|FWRITE;
392 		error = VOP_OPEN(devvp, flags, FSCRED, p);
393 		if (error)
394 			goto fail;
395 		error = msdosfs_mountfs(devvp, mp, p, &args);
396 		if (error) {
397 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
398 			(void) VOP_CLOSE(devvp, flags, NOCRED, p);
399 			VOP_UNLOCK(devvp, 0);
400 			goto fail;
401 		}
402 #ifdef MSDOSFS_DEBUG		/* only needed for the printf below */
403 		pmp = VFSTOMSDOSFS(mp);
404 #endif
405 	} else {
406 		vrele(devvp);
407 		if (devvp != pmp->pm_devvp)
408 			return (EINVAL);	/* needs translation */
409 	}
410 	if ((error = update_mp(mp, &args)) != 0) {
411 		msdosfs_unmount(mp, MNT_FORCE, p);
412 		return error;
413 	}
414 
415 #ifdef MSDOSFS_DEBUG
416 	printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
417 #endif
418 	return set_statvfs_info(path, UIO_USERSPACE, args.fspec, UIO_USERSPACE,
419 	    mp, p);
420 
421 fail:
422 	vrele(devvp);
423 	return (error);
424 }
425 
426 int
427 msdosfs_mountfs(devvp, mp, p, argp)
428 	struct vnode *devvp;
429 	struct mount *mp;
430 	struct proc *p;
431 	struct msdosfs_args *argp;
432 {
433 	struct msdosfsmount *pmp;
434 	struct buf *bp;
435 	dev_t dev = devvp->v_rdev;
436 	struct partinfo dpart;
437 	union bootsector *bsp;
438 	struct byte_bpb33 *b33;
439 	struct byte_bpb50 *b50;
440 	struct byte_bpb710 *b710;
441 	u_int8_t SecPerClust;
442 	int	ronly, error;
443 	int	bsize = 0, dtype = 0, tmp;
444 	u_long	dirsperblk;
445 
446 	/* Flush out any old buffers remaining from a previous use. */
447 	if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
448 		return (error);
449 
450 	ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
451 
452 	bp  = NULL; /* both used in error_exit */
453 	pmp = NULL;
454 
455 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
456 		/*
457 	 	 * We need the disklabel to calculate the size of a FAT entry
458 		 * later on. Also make sure the partition contains a filesystem
459 		 * of type FS_MSDOS. This doesn't work for floppies, so we have
460 		 * to check for them too.
461 	 	 *
462 	 	 * At least some parts of the msdos fs driver seem to assume
463 		 * that the size of a disk block will always be 512 bytes.
464 		 * Let's check it...
465 		 */
466 		error = VOP_IOCTL(devvp, DIOCGPART, &dpart, FREAD, NOCRED, p);
467 		if (error)
468 			goto error_exit;
469 		tmp   = dpart.part->p_fstype;
470 		dtype = dpart.disklab->d_type;
471 		bsize = dpart.disklab->d_secsize;
472 		if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
473 			error = EINVAL;
474 			goto error_exit;
475 		}
476 	}
477 
478 	/*
479 	 * Read the boot sector of the filesystem, and then check the
480 	 * boot signature.  If not a dos boot sector then error out.
481 	 */
482 	if ((error = bread(devvp, 0, 512, NOCRED, &bp)) != 0)
483 		goto error_exit;
484 	bp->b_flags |= B_AGE;
485 	bsp = (union bootsector *)bp->b_data;
486 	b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
487 	b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
488 	b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
489 
490 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
491 		if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
492 		    || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
493 			error = EINVAL;
494 			goto error_exit;
495 		}
496 	}
497 
498 	pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
499 	memset(pmp, 0, sizeof *pmp);
500 	pmp->pm_mountp = mp;
501 
502 	/*
503 	 * Compute several useful quantities from the bpb in the
504 	 * bootsector.  Copy in the dos 5 variant of the bpb then fix up
505 	 * the fields that are different between dos 5 and dos 3.3.
506 	 */
507 	SecPerClust = b50->bpbSecPerClust;
508 	pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
509 	pmp->pm_ResSectors = getushort(b50->bpbResSectors);
510 	pmp->pm_FATs = b50->bpbFATs;
511 	pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
512 	pmp->pm_Sectors = getushort(b50->bpbSectors);
513 	pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
514 	pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
515 	pmp->pm_Heads = getushort(b50->bpbHeads);
516 	pmp->pm_Media = b50->bpbMedia;
517 
518 	if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
519 		/* XXX - We should probably check more values here */
520     		if (!pmp->pm_BytesPerSec || !SecPerClust
521 	    		|| pmp->pm_Heads > 255 || pmp->pm_SecPerTrack > 63) {
522 			error = EINVAL;
523 			goto error_exit;
524 		}
525 	}
526 
527 	if (pmp->pm_Sectors == 0) {
528 		pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
529 		pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
530 	} else {
531 		pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
532 		pmp->pm_HugeSectors = pmp->pm_Sectors;
533 	}
534 	dirsperblk = pmp->pm_BytesPerSec / sizeof(struct direntry);
535 	if (pmp->pm_HugeSectors > 0xffffffff / dirsperblk + 1) {
536 		/*
537 		 * We cannot deal currently with this size of disk
538 		 * due to fileid limitations (see msdosfs_getattr and
539 		 * msdosfs_readdir)
540 		 */
541 		error = EINVAL;
542 		goto error_exit;
543 	}
544 
545 	if (pmp->pm_RootDirEnts == 0) {
546 		if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
547 		    || bsp->bs710.bsBootSectSig3 != BOOTSIG3
548 		    || pmp->pm_Sectors
549 		    || pmp->pm_FATsecs
550 		    || getushort(b710->bpbFSVers)) {
551 			error = EINVAL;
552 			goto error_exit;
553 		}
554 		pmp->pm_fatmask = FAT32_MASK;
555 		pmp->pm_fatmult = 4;
556 		pmp->pm_fatdiv = 1;
557 		pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
558 
559 		/* mirrorring is enabled if the FATMIRROR bit is not set */
560 		if ((getushort(b710->bpbExtFlags) & FATMIRROR) == 0)
561 			pmp->pm_flags |= MSDOSFS_FATMIRROR;
562 		else
563 			pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
564 	} else
565 		pmp->pm_flags |= MSDOSFS_FATMIRROR;
566 
567 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
568 		if (FAT32(pmp)) {
569 			/*
570 			 * GEMDOS doesn't know fat32.
571 			 */
572 			error = EINVAL;
573 			goto error_exit;
574 		}
575 
576 		/*
577 		 * Check a few values (could do some more):
578 		 * - logical sector size: power of 2, >= block size
579 		 * - sectors per cluster: power of 2, >= 1
580 		 * - number of sectors:   >= 1, <= size of partition
581 		 */
582 		if ( (SecPerClust == 0)
583 		  || (SecPerClust & (SecPerClust - 1))
584 		  || (pmp->pm_BytesPerSec < bsize)
585 		  || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
586 		  || (pmp->pm_HugeSectors == 0)
587 		  || (pmp->pm_HugeSectors * (pmp->pm_BytesPerSec / bsize)
588 							> dpart.part->p_size)
589 		   ) {
590 			error = EINVAL;
591 			goto error_exit;
592 		}
593 		/*
594 		 * XXX - Many parts of the msdos fs driver seem to assume that
595 		 * the number of bytes per logical sector (BytesPerSec) will
596 		 * always be the same as the number of bytes per disk block
597 		 * Let's pretend it is.
598 		 */
599 		tmp = pmp->pm_BytesPerSec / bsize;
600 		pmp->pm_BytesPerSec  = bsize;
601 		pmp->pm_HugeSectors *= tmp;
602 		pmp->pm_HiddenSects *= tmp;
603 		pmp->pm_ResSectors  *= tmp;
604 		pmp->pm_Sectors     *= tmp;
605 		pmp->pm_FATsecs     *= tmp;
606 		SecPerClust         *= tmp;
607 	}
608 	pmp->pm_fatblk = pmp->pm_ResSectors;
609 	if (FAT32(pmp)) {
610 		pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
611 		pmp->pm_firstcluster = pmp->pm_fatblk
612 			+ (pmp->pm_FATs * pmp->pm_FATsecs);
613 		pmp->pm_fsinfo = getushort(b710->bpbFSInfo);
614 	} else {
615 		pmp->pm_rootdirblk = pmp->pm_fatblk +
616 			(pmp->pm_FATs * pmp->pm_FATsecs);
617 		pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
618 				       + pmp->pm_BytesPerSec - 1)
619 			/ pmp->pm_BytesPerSec;/* in sectors */
620 		pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
621 	}
622 
623 	pmp->pm_nmbrofclusters = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
624 	    SecPerClust;
625 	pmp->pm_maxcluster = pmp->pm_nmbrofclusters + 1;
626 	pmp->pm_fatsize = pmp->pm_FATsecs * pmp->pm_BytesPerSec;
627 
628 	if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
629 		if (pmp->pm_nmbrofclusters <= (0xff0 - 2)
630 		      && (dtype == DTYPE_FLOPPY
631 			  || (dtype == DTYPE_VND
632 				&& (pmp->pm_Heads == 1 || pmp->pm_Heads == 2)))
633 		    ) {
634 			pmp->pm_fatmask = FAT12_MASK;
635 			pmp->pm_fatmult = 3;
636 			pmp->pm_fatdiv = 2;
637 		} else {
638 			pmp->pm_fatmask = FAT16_MASK;
639 			pmp->pm_fatmult = 2;
640 			pmp->pm_fatdiv = 1;
641 		}
642 	} else if (pmp->pm_fatmask == 0) {
643 		if (pmp->pm_maxcluster
644 		    <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
645 			/*
646 			 * This will usually be a floppy disk. This size makes
647 			 * sure that one fat entry will not be split across
648 			 * multiple blocks.
649 			 */
650 			pmp->pm_fatmask = FAT12_MASK;
651 			pmp->pm_fatmult = 3;
652 			pmp->pm_fatdiv = 2;
653 		} else {
654 			pmp->pm_fatmask = FAT16_MASK;
655 			pmp->pm_fatmult = 2;
656 			pmp->pm_fatdiv = 1;
657 		}
658 	}
659 	if (FAT12(pmp))
660 		pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
661 	else
662 		pmp->pm_fatblocksize = MAXBSIZE;
663 
664 	pmp->pm_fatblocksec = pmp->pm_fatblocksize / pmp->pm_BytesPerSec;
665 	pmp->pm_bnshift = ffs(pmp->pm_BytesPerSec) - 1;
666 
667 	/*
668 	 * Compute mask and shift value for isolating cluster relative byte
669 	 * offsets and cluster numbers from a file offset.
670 	 */
671 	pmp->pm_bpcluster = SecPerClust * pmp->pm_BytesPerSec;
672 	pmp->pm_crbomask = pmp->pm_bpcluster - 1;
673 	pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
674 
675 	/*
676 	 * Check for valid cluster size
677 	 * must be a power of 2
678 	 */
679 	if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
680 		error = EINVAL;
681 		goto error_exit;
682 	}
683 
684 	/*
685 	 * Release the bootsector buffer.
686 	 */
687 	brelse(bp);
688 	bp = NULL;
689 
690 	/*
691 	 * Check FSInfo.
692 	 */
693 	if (pmp->pm_fsinfo) {
694 		struct fsinfo *fp;
695 
696 		if ((error = bread(devvp, pmp->pm_fsinfo, 1024, NOCRED, &bp)) != 0)
697 			goto error_exit;
698 		fp = (struct fsinfo *)bp->b_data;
699 		if (!memcmp(fp->fsisig1, "RRaA", 4)
700 		    && !memcmp(fp->fsisig2, "rrAa", 4)
701 		    && !memcmp(fp->fsisig3, "\0\0\125\252", 4)
702 		    && !memcmp(fp->fsisig4, "\0\0\125\252", 4))
703 			pmp->pm_nxtfree = getulong(fp->fsinxtfree);
704 		else
705 			pmp->pm_fsinfo = 0;
706 		brelse(bp);
707 		bp = NULL;
708 	}
709 
710 	/*
711 	 * Check and validate (or perhaps invalidate?) the fsinfo structure?
712 	 * XXX
713 	 */
714 	if (pmp->pm_fsinfo) {
715 		if (pmp->pm_nxtfree == (u_long)-1)
716 			pmp->pm_fsinfo = 0;
717 	}
718 
719 	/*
720 	 * Allocate memory for the bitmap of allocated clusters, and then
721 	 * fill it in.
722 	 */
723 	pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
724 				   / N_INUSEBITS)
725 				  * sizeof(*pmp->pm_inusemap),
726 				  M_MSDOSFSFAT, M_WAITOK);
727 
728 	/*
729 	 * fillinusemap() needs pm_devvp.
730 	 */
731 	pmp->pm_dev = dev;
732 	pmp->pm_devvp = devvp;
733 
734 	/*
735 	 * Have the inuse map filled in.
736 	 */
737 	if ((error = fillinusemap(pmp)) != 0)
738 		goto error_exit;
739 
740 	/*
741 	 * If they want fat updates to be synchronous then let them suffer
742 	 * the performance degradation in exchange for the on disk copy of
743 	 * the fat being correct just about all the time.  I suppose this
744 	 * would be a good thing to turn on if the kernel is still flakey.
745 	 */
746 	if (mp->mnt_flag & MNT_SYNCHRONOUS)
747 		pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
748 
749 	/*
750 	 * Finish up.
751 	 */
752 	if (ronly)
753 		pmp->pm_flags |= MSDOSFSMNT_RONLY;
754 	else
755 		pmp->pm_fmod = 1;
756 	mp->mnt_data = pmp;
757 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
758 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_MSDOS);
759 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
760 	mp->mnt_stat.f_namemax = MSDOSFS_NAMEMAX(pmp);
761 	mp->mnt_flag |= MNT_LOCAL;
762 	mp->mnt_dev_bshift = pmp->pm_bnshift;
763 	mp->mnt_fs_bshift = pmp->pm_cnshift;
764 
765 #ifdef QUOTA
766 	/*
767 	 * If we ever do quotas for DOS filesystems this would be a place
768 	 * to fill in the info in the msdosfsmount structure. You dolt,
769 	 * quotas on dos filesystems make no sense because files have no
770 	 * owners on dos filesystems. of course there is some empty space
771 	 * in the directory entry where we could put uid's and gid's.
772 	 */
773 #endif
774 	devvp->v_specmountpoint = mp;
775 
776 	return (0);
777 
778 error_exit:;
779 	if (bp)
780 		brelse(bp);
781 	if (pmp) {
782 		if (pmp->pm_inusemap)
783 			free(pmp->pm_inusemap, M_MSDOSFSFAT);
784 		free(pmp, M_MSDOSFSMNT);
785 		mp->mnt_data = NULL;
786 	}
787 	return (error);
788 }
789 
790 int
791 msdosfs_start(mp, flags, p)
792 	struct mount *mp;
793 	int flags;
794 	struct proc *p;
795 {
796 
797 	return (0);
798 }
799 
800 /*
801  * Unmount the filesystem described by mp.
802  */
803 int
804 msdosfs_unmount(mp, mntflags, p)
805 	struct mount *mp;
806 	int mntflags;
807 	struct proc *p;
808 {
809 	struct msdosfsmount *pmp;
810 	int error, flags;
811 
812 	flags = 0;
813 	if (mntflags & MNT_FORCE)
814 		flags |= FORCECLOSE;
815 #ifdef QUOTA
816 #endif
817 	if ((error = vflush(mp, NULLVP, flags)) != 0)
818 		return (error);
819 	pmp = VFSTOMSDOSFS(mp);
820 	if (pmp->pm_devvp->v_type != VBAD)
821 		pmp->pm_devvp->v_specmountpoint = NULL;
822 #ifdef MSDOSFS_DEBUG
823 	{
824 		struct vnode *vp = pmp->pm_devvp;
825 
826 		printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
827 		printf("flag %08x, usecount %d, writecount %ld, holdcnt %ld\n",
828 		    vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
829 		printf("id %lu, mount %p, op %p\n",
830 		    vp->v_id, vp->v_mount, vp->v_op);
831 		printf("freef %p, freeb %p, mount %p\n",
832 		    vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
833 		    vp->v_mount);
834 		printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
835 		    vp->v_cleanblkhd.lh_first,
836 		    vp->v_dirtyblkhd.lh_first,
837 		    vp->v_numoutput, vp->v_type);
838 		printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
839 		    vp->v_socket, vp->v_tag,
840 		    ((u_int *)vp->v_data)[0],
841 		    ((u_int *)vp->v_data)[1]);
842 	}
843 #endif
844 	vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
845 	error = VOP_CLOSE(pmp->pm_devvp,
846 	    pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD|FWRITE, NOCRED, p);
847 	vput(pmp->pm_devvp);
848 	free(pmp->pm_inusemap, M_MSDOSFSFAT);
849 	free(pmp, M_MSDOSFSMNT);
850 	mp->mnt_data = NULL;
851 	mp->mnt_flag &= ~MNT_LOCAL;
852 	return (error);
853 }
854 
855 int
856 msdosfs_root(mp, vpp)
857 	struct mount *mp;
858 	struct vnode **vpp;
859 {
860 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
861 	struct denode *ndep;
862 	int error;
863 
864 #ifdef MSDOSFS_DEBUG
865 	printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
866 #endif
867 	if ((error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep)) != 0)
868 		return (error);
869 	*vpp = DETOV(ndep);
870 	return (0);
871 }
872 
873 int
874 msdosfs_quotactl(mp, cmds, uid, arg, p)
875 	struct mount *mp;
876 	int cmds;
877 	uid_t uid;
878 	void *arg;
879 	struct proc *p;
880 {
881 
882 #ifdef QUOTA
883 	return (EOPNOTSUPP);
884 #else
885 	return (EOPNOTSUPP);
886 #endif
887 }
888 
889 int
890 msdosfs_statvfs(mp, sbp, p)
891 	struct mount *mp;
892 	struct statvfs *sbp;
893 	struct proc *p;
894 {
895 	struct msdosfsmount *pmp;
896 
897 	pmp = VFSTOMSDOSFS(mp);
898 	sbp->f_bsize = pmp->pm_bpcluster;
899 	sbp->f_frsize = sbp->f_bsize;
900 	sbp->f_iosize = pmp->pm_bpcluster;
901 	sbp->f_blocks = pmp->pm_nmbrofclusters;
902 	sbp->f_bfree = pmp->pm_freeclustercount;
903 	sbp->f_bavail = pmp->pm_freeclustercount;
904 	sbp->f_bresvd = 0;
905 	sbp->f_files = pmp->pm_RootDirEnts;			/* XXX */
906 	sbp->f_ffree = 0;	/* what to put in here? */
907 	sbp->f_favail = 0;	/* what to put in here? */
908 	sbp->f_fresvd = 0;
909 	copy_statvfs_info(sbp, mp);
910 	return (0);
911 }
912 
913 int
914 msdosfs_sync(mp, waitfor, cred, p)
915 	struct mount *mp;
916 	int waitfor;
917 	struct ucred *cred;
918 	struct proc *p;
919 {
920 	struct vnode *vp, *nvp;
921 	struct denode *dep;
922 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
923 	int error, allerror = 0;
924 
925 	/*
926 	 * If we ever switch to not updating all of the fats all the time,
927 	 * this would be the place to update them from the first one.
928 	 */
929 	if (pmp->pm_fmod != 0) {
930 		if (pmp->pm_flags & MSDOSFSMNT_RONLY)
931 			panic("msdosfs_sync: rofs mod");
932 		else {
933 			/* update fats here */
934 		}
935 	}
936 	/*
937 	 * Write back each (modified) denode.
938 	 */
939 	simple_lock(&mntvnode_slock);
940 loop:
941 	for (vp = mp->mnt_vnodelist.lh_first; vp != NULL; vp = nvp) {
942 		/*
943 		 * If the vnode that we are about to sync is no longer
944 		 * assoicated with this mount point, start over.
945 		 */
946 		if (vp->v_mount != mp)
947 			goto loop;
948 		simple_lock(&vp->v_interlock);
949 		nvp = vp->v_mntvnodes.le_next;
950 		dep = VTODE(vp);
951 		if (waitfor == MNT_LAZY || vp->v_type == VNON ||
952 		    (((dep->de_flag &
953 		    (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0) &&
954 		     (LIST_EMPTY(&vp->v_dirtyblkhd) &&
955 		      vp->v_uobj.uo_npages == 0))) {
956 			simple_unlock(&vp->v_interlock);
957 			continue;
958 		}
959 		simple_unlock(&mntvnode_slock);
960 		error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
961 		if (error) {
962 			simple_lock(&mntvnode_slock);
963 			if (error == ENOENT)
964 				goto loop;
965 			continue;
966 		}
967 		if ((error = VOP_FSYNC(vp, cred,
968 		    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
969 			allerror = error;
970 		vput(vp);
971 		simple_lock(&mntvnode_slock);
972 	}
973 	simple_unlock(&mntvnode_slock);
974 	/*
975 	 * Force stale file system control information to be flushed.
976 	 */
977 	if ((error = VOP_FSYNC(pmp->pm_devvp, cred,
978 	    waitfor == MNT_WAIT ? FSYNC_WAIT : 0, 0, 0, p)) != 0)
979 		allerror = error;
980 #ifdef QUOTA
981 	/* qsync(mp); */
982 #endif
983 	return (allerror);
984 }
985 
986 int
987 msdosfs_fhtovp(mp, fhp, vpp)
988 	struct mount *mp;
989 	struct fid *fhp;
990 	struct vnode **vpp;
991 {
992 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
993 	struct defid *defhp = (struct defid *) fhp;
994 	struct denode *dep;
995 	int error;
996 
997 	error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
998 	if (error) {
999 		*vpp = NULLVP;
1000 		return (error);
1001 	}
1002 	*vpp = DETOV(dep);
1003 	return (0);
1004 }
1005 
1006 int
1007 msdosfs_checkexp(mp, nam, exflagsp, credanonp)
1008 	struct mount *mp;
1009 	struct mbuf *nam;
1010 	int *exflagsp;
1011 	struct ucred **credanonp;
1012 {
1013 	struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1014 	struct netcred *np;
1015 
1016 	np = vfs_export_lookup(mp, &pmp->pm_export, nam);
1017 	if (np == NULL)
1018 		return (EACCES);
1019 	*exflagsp = np->netc_exflags;
1020 	*credanonp = &np->netc_anon;
1021 	return (0);
1022 }
1023 
1024 int
1025 msdosfs_vptofh(vp, fhp)
1026 	struct vnode *vp;
1027 	struct fid *fhp;
1028 {
1029 	struct denode *dep;
1030 	struct defid *defhp;
1031 
1032 	dep = VTODE(vp);
1033 	defhp = (struct defid *)fhp;
1034 	defhp->defid_len = sizeof(struct defid);
1035 	defhp->defid_dirclust = dep->de_dirclust;
1036 	defhp->defid_dirofs = dep->de_diroffset;
1037 	/* defhp->defid_gen = dep->de_gen; */
1038 	return (0);
1039 }
1040 
1041 int
1042 msdosfs_vget(mp, ino, vpp)
1043 	struct mount *mp;
1044 	ino_t ino;
1045 	struct vnode **vpp;
1046 {
1047 
1048 	return (EOPNOTSUPP);
1049 }
1050 
1051 SYSCTL_SETUP(sysctl_vfs_msdosfs_setup, "sysctl vfs.msdosfs subtree setup")
1052 {
1053 
1054 	sysctl_createv(clog, 0, NULL, NULL,
1055 		       CTLFLAG_PERMANENT,
1056 		       CTLTYPE_NODE, "vfs", NULL,
1057 		       NULL, 0, NULL, 0,
1058 		       CTL_VFS, CTL_EOL);
1059 	sysctl_createv(clog, 0, NULL, NULL,
1060 		       CTLFLAG_PERMANENT,
1061 		       CTLTYPE_NODE, "msdosfs",
1062 		       SYSCTL_DESCR("MS-DOS file system"),
1063 		       NULL, 0, NULL, 0,
1064 		       CTL_VFS, 4, CTL_EOL);
1065 	/*
1066 	 * XXX the "4" above could be dynamic, thereby eliminating one
1067 	 * more instance of the "number to vfs" mapping problem, but
1068 	 * "4" is the order as taken from sys/mount.h
1069 	 */
1070 }
1071