1 /* $FreeBSD: src/sys/msdosfs/msdosfs_vfsops.c,v 1.60.2.6 2002/09/12 21:33:38 trhodes Exp $ */
2 /* $DragonFly: src/sys/vfs/msdosfs/msdosfs_vfsops.c,v 1.3 2003/06/25 03:56:01 dillon Exp $ */
3 /* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */
6 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
7 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by TooLs GmbH.
22 * 4. The name of TooLs GmbH may not be used to endorse or promote products
23 * derived from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
31 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
33 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 * Written by Paul Popelka (paulp@uts.amdahl.com)
39 * You can do anything you want with this software, just don't say you wrote
40 * it, and don't remove this notice.
42 * This software is provided "as is".
44 * The author supplies this software to be publicly redistributed on the
45 * understanding that the author is not responsible for the correct
46 * functioning of this software in any circumstances and is not liable for
47 * any damages caused by this software.
52 #include <sys/param.h>
53 #include <sys/systm.h>
56 #include <sys/namei.h>
57 #include <sys/kernel.h>
58 #include <sys/vnode.h>
59 #include <sys/mount.h>
61 #include <sys/fcntl.h>
62 #include <sys/malloc.h>
63 #include <sys/stat.h> /* defines ALLPERMS */
64 #include <vm/vm_zone.h>
66 #include <msdosfs/bpb.h>
67 #include <msdosfs/bootsect.h>
68 #include <msdosfs/direntry.h>
69 #include <msdosfs/denode.h>
70 #include <msdosfs/msdosfsmount.h>
71 #include <msdosfs/fat.h>
73 #define MSDOSFS_DFLTBSIZE 4096
77 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
78 * garbage or a random value :-{
79 * If you want to use that broken-signatured media, define the
80 * following symbol even though PC/AT.
81 * (ex. mount PC-98 DOS formatted FD on PC/AT)
83 #define MSDOSFS_NOCHECKSIG
86 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
87 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
89 static int update_mp __P((struct mount *mp, struct msdosfs_args *argp));
90 static int mountmsdosfs __P((struct vnode *devvp, struct mount *mp,
91 struct thread *td, struct msdosfs_args *argp));
92 static int msdosfs_fhtovp __P((struct mount *, struct fid *,
94 static int msdosfs_checkexp __P((struct mount *, struct sockaddr *,
95 int *, struct ucred **));
96 static int msdosfs_mount __P((struct mount *, char *, caddr_t,
97 struct nameidata *, struct thread *));
98 static int msdosfs_root __P((struct mount *, struct vnode **));
99 static int msdosfs_statfs __P((struct mount *, struct statfs *,
101 static int msdosfs_sync __P((struct mount *, int, struct ucred *,
103 static int msdosfs_unmount __P((struct mount *, int, struct thread *));
104 static int msdosfs_vptofh __P((struct vnode *, struct fid *));
109 struct msdosfs_args *argp;
111 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
114 pmp->pm_gid = argp->gid;
115 pmp->pm_uid = argp->uid;
116 pmp->pm_mask = argp->mask & ALLPERMS;
117 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
118 if (pmp->pm_flags & MSDOSFSMNT_U2WTABLE) {
119 bcopy(argp->u2w, pmp->pm_u2w, sizeof(pmp->pm_u2w));
120 bcopy(argp->d2u, pmp->pm_d2u, sizeof(pmp->pm_d2u));
121 bcopy(argp->u2d, pmp->pm_u2d, sizeof(pmp->pm_u2d));
123 if (pmp->pm_flags & MSDOSFSMNT_ULTABLE) {
124 bcopy(argp->ul, pmp->pm_ul, sizeof(pmp->pm_ul));
125 bcopy(argp->lu, pmp->pm_lu, sizeof(pmp->pm_lu));
130 * GEMDOS knows nothing (yet) about win95
132 if (pmp->pm_flags & MSDOSFSMNT_GEMDOSFS)
133 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
136 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
137 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
138 else if (!(pmp->pm_flags &
139 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
140 struct vnode *rootvp;
143 * Try to divine whether to support Win'95 long filenames
146 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
148 if ((error = msdosfs_root(mp, &rootvp)) != 0)
150 pmp->pm_flags |= findwin95(VTODE(rootvp))
151 ? MSDOSFSMNT_LONGNAME
152 : MSDOSFSMNT_SHORTNAME;
163 register struct mount *mp;
164 struct thread *td = curthread; /* XXX */
167 struct msdosfs_args args;
169 if (root_device->dv_class != DV_DISK)
173 * Get vnodes for swapdev and rootdev.
175 if (bdevvp(rootdev, &rootvp))
176 panic("msdosfs_mountroot: can't setup rootvp");
178 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
179 bzero((char *)mp, (u_long)sizeof(struct mount));
180 mp->mnt_op = &msdosfs_vfsops;
182 TAILQ_INIT(&mp->mnt_nvnodelist);
183 TAILQ_INIT(&mp->mnt_reservedvnlist);
190 if ((error = mountmsdosfs(rootvp, mp, p, &args)) != 0) {
195 if ((error = update_mp(mp, &args)) != 0) {
196 (void)msdosfs_unmount(mp, 0, p);
201 if ((error = vfs_lock(mp)) != 0) {
202 (void)msdosfs_unmount(mp, 0, p);
207 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
208 mp->mnt_vnodecovered = NULLVP;
209 (void) copystr("/", mp->mnt_stat.f_mntonname, MNAMELEN - 1,
211 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
212 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
214 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
215 (void)msdosfs_statfs(mp, &mp->mnt_stat, p);
222 * mp - path - addr in user space of mount point (ie /usr or whatever)
223 * data - addr in user space of mount params including the name of the block
224 * special file to treat as a filesystem.
227 msdosfs_mount(mp, path, data, ndp, td)
231 struct nameidata *ndp;
234 struct vnode *devvp; /* vnode for blk device to mount */
235 struct msdosfs_args args; /* will hold data from mount request */
236 /* msdosfs specific mount control block */
237 struct msdosfsmount *pmp = NULL;
241 struct proc *p = td->td_proc;
245 error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
248 if (args.magic != MSDOSFS_ARGSMAGIC)
251 * If updating, check whether changing from read-only to
252 * read/write; if there is no device name, that's all we do.
254 if (mp->mnt_flag & MNT_UPDATE) {
255 pmp = VFSTOMSDOSFS(mp);
257 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_flag & MNT_RDONLY)) {
259 if (mp->mnt_flag & MNT_FORCE)
261 error = vflush(mp, 0, flags);
263 if (!error && (mp->mnt_flag & MNT_RELOAD))
264 /* not yet implemented */
268 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
270 * If upgrade to read-write by non-root, then verify
271 * that user has necessary permissions on the device.
273 if (p->p_ucred->cr_uid != 0) {
274 devvp = pmp->pm_devvp;
275 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
276 error = VOP_ACCESS(devvp, VREAD | VWRITE,
279 VOP_UNLOCK(devvp, 0, td);
282 VOP_UNLOCK(devvp, 0, td);
284 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
286 if (args.fspec == 0) {
287 #ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
288 if (args.flags & MSDOSFSMNT_MNTOPT) {
289 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
290 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
291 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
292 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
296 * Process export requests.
298 return (vfs_export(mp, &pmp->pm_export, &args.export));
302 * Not an update, or updating the name: look up the name
303 * and verify that it refers to a sensible block device.
305 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
310 NDFREE(ndp, NDF_ONLY_PNBUF);
312 if (!vn_isdisk(devvp, &error)) {
317 * If mount by non-root, then verify that user has necessary
318 * permissions on the device.
320 if (p->p_ucred->cr_uid != 0) {
322 if ((mp->mnt_flag & MNT_RDONLY) == 0)
323 accessmode |= VWRITE;
324 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
325 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, td);
330 VOP_UNLOCK(devvp, 0, td);
332 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
333 error = mountmsdosfs(devvp, mp, td, &args);
334 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */
335 pmp = VFSTOMSDOSFS(mp);
338 if (devvp != pmp->pm_devvp)
339 error = EINVAL; /* XXX needs translation */
348 error = update_mp(mp, &args);
350 msdosfs_unmount(mp, MNT_FORCE, td);
354 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
355 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
356 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
358 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
359 (void) msdosfs_statfs(mp, &mp->mnt_stat, td);
361 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
367 mountmsdosfs(devvp, mp, td, argp)
371 struct msdosfs_args *argp;
373 struct msdosfsmount *pmp;
375 dev_t dev = devvp->v_rdev;
377 struct partinfo dpart;
378 int bsize = 0, dtype = 0, tmp;
380 union bootsector *bsp;
381 struct byte_bpb33 *b33;
382 struct byte_bpb50 *b50;
383 struct byte_bpb710 *b710;
384 u_int8_t SecPerClust;
387 struct proc *p = td->td_proc;
392 * Disallow multiple mounts of the same device.
393 * Disallow mounting of a device that is currently in use
394 * (except for root, which might share swap device for miniroot).
395 * Flush out any old buffers remaining from a previous use.
397 error = vfs_mountedon(devvp);
400 if (vcount(devvp) > 1 && devvp != rootvp)
402 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
403 error = vinvalbuf(devvp, V_SAVE, p->p_ucred, td, 0, 0);
404 VOP_UNLOCK(devvp, 0, td);
408 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
409 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
410 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, td);
411 VOP_UNLOCK(devvp, 0, td);
415 bp = NULL; /* both used in error_exit */
419 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
421 * We need the disklabel to calculate the size of a FAT entry
422 * later on. Also make sure the partition contains a filesystem
423 * of type FS_MSDOS. This doesn't work for floppies, so we have
424 * to check for them too.
426 * At least some parts of the msdos fs driver seem to assume
427 * that the size of a disk block will always be 512 bytes.
430 error = VOP_IOCTL(devvp, DIOCGPART, (caddr_t)&dpart,
434 tmp = dpart.part->p_fstype;
435 dtype = dpart.disklab->d_type;
436 bsize = dpart.disklab->d_secsize;
437 if (bsize != 512 || (dtype!=DTYPE_FLOPPY && tmp!=FS_MSDOS)) {
445 * Read the boot sector of the filesystem, and then check the
446 * boot signature. If not a dos boot sector then error out.
448 * NOTE: 2048 is a maximum sector size in current...
450 error = bread(devvp, 0, 2048, NOCRED, &bp);
453 bp->b_flags |= B_AGE;
454 bsp = (union bootsector *)bp->b_data;
455 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
456 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
457 b710 = (struct byte_bpb710 *)bsp->bs710.bsPBP;
460 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
462 #ifndef MSDOSFS_NOCHECKSIG
463 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
464 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
473 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK);
474 bzero((caddr_t)pmp, sizeof *pmp);
478 * Compute several useful quantities from the bpb in the
479 * bootsector. Copy in the dos 5 variant of the bpb then fix up
480 * the fields that are different between dos 5 and dos 3.3.
482 SecPerClust = b50->bpbSecPerClust;
483 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
484 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
485 pmp->pm_FATs = b50->bpbFATs;
486 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
487 pmp->pm_Sectors = getushort(b50->bpbSectors);
488 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
489 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
490 pmp->pm_Heads = getushort(b50->bpbHeads);
491 pmp->pm_Media = b50->bpbMedia;
493 /* calculate the ratio of sector size to DEV_BSIZE */
494 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
497 if (!(argp->flags & MSDOSFSMNT_GEMDOSFS)) {
499 /* XXX - We should probably check more values here */
500 if (!pmp->pm_BytesPerSec || !SecPerClust
501 || !pmp->pm_Heads || pmp->pm_Heads > 255
503 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
505 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
514 if (pmp->pm_Sectors == 0) {
515 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
516 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
518 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
519 pmp->pm_HugeSectors = pmp->pm_Sectors;
521 if (pmp->pm_HugeSectors > 0xffffffff /
522 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
524 * We cannot deal currently with this size of disk
525 * due to fileid limitations (see msdosfs_getattr and
529 printf("mountmsdosfs(): disk too big, sorry\n");
533 if (pmp->pm_RootDirEnts == 0) {
534 if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
535 || bsp->bs710.bsBootSectSig3 != BOOTSIG3
538 || getushort(b710->bpbFSVers)) {
540 printf("mountmsdosfs(): bad FAT32 filesystem\n");
543 pmp->pm_fatmask = FAT32_MASK;
546 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
547 if (getushort(b710->bpbExtFlags) & FATMIRROR)
548 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
550 pmp->pm_flags |= MSDOSFS_FATMIRROR;
552 pmp->pm_flags |= MSDOSFS_FATMIRROR;
555 * Check a few values (could do some more):
556 * - logical sector size: power of 2, >= block size
557 * - sectors per cluster: power of 2, >= 1
558 * - number of sectors: >= 1, <= size of partition
560 if ( (SecPerClust == 0)
561 || (SecPerClust & (SecPerClust - 1))
562 || (pmp->pm_BytesPerSec < DEV_BSIZE)
563 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
564 || (pmp->pm_HugeSectors == 0)
570 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
571 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
572 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
573 SecPerClust *= pmp->pm_BlkPerSec;
575 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
578 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
579 pmp->pm_firstcluster = pmp->pm_fatblk
580 + (pmp->pm_FATs * pmp->pm_FATsecs);
581 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
583 pmp->pm_rootdirblk = pmp->pm_fatblk +
584 (pmp->pm_FATs * pmp->pm_FATsecs);
585 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
587 / DEV_BSIZE; /* in blocks */
588 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
591 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
593 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
596 if (argp->flags & MSDOSFSMNT_GEMDOSFS) {
597 if ((pmp->pm_maxcluster <= (0xff0 - 2))
598 && ((dtype == DTYPE_FLOPPY) || ((dtype == DTYPE_VNODE)
599 && ((pmp->pm_Heads == 1) || (pmp->pm_Heads == 2))))
601 pmp->pm_fatmask = FAT12_MASK;
605 pmp->pm_fatmask = FAT16_MASK;
611 if (pmp->pm_fatmask == 0) {
612 if (pmp->pm_maxcluster
613 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
615 * This will usually be a floppy disk. This size makes
616 * sure that one fat entry will not be split across
619 pmp->pm_fatmask = FAT12_MASK;
623 pmp->pm_fatmask = FAT16_MASK;
629 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
630 if (pmp->pm_maxcluster >= clusters) {
631 printf("Warning: number of clusters (%ld) exceeds FAT "
632 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
633 pmp->pm_maxcluster = clusters - 1;
638 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
640 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
642 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
643 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
646 * Compute mask and shift value for isolating cluster relative byte
647 * offsets and cluster numbers from a file offset.
649 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
650 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
651 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
654 * Check for valid cluster size
655 * must be a power of 2
657 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
663 * Release the bootsector buffer.
671 if (pmp->pm_fsinfo) {
674 if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
677 fp = (struct fsinfo *)bp->b_data;
678 if (!bcmp(fp->fsisig1, "RRaA", 4)
679 && !bcmp(fp->fsisig2, "rrAa", 4)
680 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
681 && !bcmp(fp->fsisig4, "\0\0\125\252", 4))
682 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
690 * Check and validate (or perhaps invalidate?) the fsinfo structure?
692 if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
694 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
695 pmp->pm_nxtfree, pmp->pm_maxcluster);
701 * Allocate memory for the bitmap of allocated clusters, and then
704 pmp->pm_inusemap = malloc(((pmp->pm_maxcluster + N_INUSEBITS - 1)
706 * sizeof(*pmp->pm_inusemap),
707 M_MSDOSFSFAT, M_WAITOK);
710 * fillinusemap() needs pm_devvp.
713 pmp->pm_devvp = devvp;
716 * Have the inuse map filled in.
718 if ((error = fillinusemap(pmp)) != 0)
722 * If they want fat updates to be synchronous then let them suffer
723 * the performance degradation in exchange for the on disk copy of
724 * the fat being correct just about all the time. I suppose this
725 * would be a good thing to turn on if the kernel is still flakey.
727 if (mp->mnt_flag & MNT_SYNCHRONOUS)
728 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
734 pmp->pm_flags |= MSDOSFSMNT_RONLY;
737 mp->mnt_data = (qaddr_t) pmp;
738 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
739 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
740 mp->mnt_flag |= MNT_LOCAL;
741 devvp->v_specmountpoint = mp;
748 (void) VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NOCRED, td);
750 if (pmp->pm_inusemap)
751 free(pmp->pm_inusemap, M_MSDOSFSFAT);
752 free(pmp, M_MSDOSFSMNT);
753 mp->mnt_data = (qaddr_t)0;
759 * Unmount the filesystem described by mp.
762 msdosfs_unmount(mp, mntflags, td)
767 struct msdosfsmount *pmp;
771 if (mntflags & MNT_FORCE)
773 error = vflush(mp, 0, flags);
776 pmp = VFSTOMSDOSFS(mp);
777 pmp->pm_devvp->v_specmountpoint = NULL;
780 struct vnode *vp = pmp->pm_devvp;
782 printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
783 printf("flag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
784 vp->v_flag, vp->v_usecount, vp->v_writecount, vp->v_holdcnt);
785 printf("id %lu, mount %p, op %p\n",
786 vp->v_id, vp->v_mount, vp->v_op);
787 printf("freef %p, freeb %p, mount %p\n",
788 vp->v_freelist.tqe_next, vp->v_freelist.tqe_prev,
790 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
791 TAILQ_FIRST(&vp->v_cleanblkhd),
792 TAILQ_FIRST(&vp->v_dirtyblkhd),
793 vp->v_numoutput, vp->v_type);
794 printf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
795 vp->v_socket, vp->v_tag,
796 ((u_int *)vp->v_data)[0],
797 ((u_int *)vp->v_data)[1]);
800 error = VOP_CLOSE(pmp->pm_devvp,
801 (pmp->pm_flags&MSDOSFSMNT_RONLY) ? FREAD : FREAD | FWRITE,
803 vrele(pmp->pm_devvp);
804 free(pmp->pm_inusemap, M_MSDOSFSFAT);
805 free(pmp, M_MSDOSFSMNT);
806 mp->mnt_data = (qaddr_t)0;
807 mp->mnt_flag &= ~MNT_LOCAL;
812 msdosfs_root(mp, vpp)
816 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
821 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
823 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
831 msdosfs_statfs(mp, sbp, td)
836 struct msdosfsmount *pmp;
838 pmp = VFSTOMSDOSFS(mp);
839 sbp->f_bsize = pmp->pm_bpcluster;
840 sbp->f_iosize = pmp->pm_bpcluster;
841 sbp->f_blocks = pmp->pm_maxcluster + 1;
842 sbp->f_bfree = pmp->pm_freeclustercount;
843 sbp->f_bavail = pmp->pm_freeclustercount;
844 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
845 sbp->f_ffree = 0; /* what to put in here? */
846 if (sbp != &mp->mnt_stat) {
847 sbp->f_type = mp->mnt_vfc->vfc_typenum;
848 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
849 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
851 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
856 msdosfs_sync(mp, waitfor, cred, td)
862 struct vnode *vp, *nvp;
864 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
865 int error, allerror = 0;
868 * If we ever switch to not updating all of the fats all the time,
869 * this would be the place to update them from the first one.
871 if (pmp->pm_fmod != 0) {
872 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
873 panic("msdosfs_sync: rofs mod");
875 /* update fats here */
879 * Write back each (modified) denode.
881 simple_lock(&mntvnode_slock);
883 for (vp = TAILQ_FIRST(&mp->mnt_nvnodelist); vp != NULL; vp = nvp) {
885 * If the vnode that we are about to sync is no longer
886 * associated with this mount point, start over.
888 if (vp->v_mount != mp)
891 simple_lock(&vp->v_interlock);
892 nvp = TAILQ_NEXT(vp, v_nmntvnodes);
894 if (vp->v_type == VNON ||
896 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
897 (TAILQ_EMPTY(&vp->v_dirtyblkhd) || waitfor == MNT_LAZY))) {
898 simple_unlock(&vp->v_interlock);
901 simple_unlock(&mntvnode_slock);
902 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
904 simple_lock(&mntvnode_slock);
909 error = VOP_FSYNC(vp, cred, waitfor, td);
912 VOP_UNLOCK(vp, 0, td);
914 simple_lock(&mntvnode_slock);
916 simple_unlock(&mntvnode_slock);
919 * Flush filesystem control info.
921 if (waitfor != MNT_LAZY) {
922 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
923 error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, td);
926 VOP_UNLOCK(pmp->pm_devvp, 0, td);
932 msdosfs_fhtovp(mp, fhp, vpp)
937 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
938 struct defid *defhp = (struct defid *) fhp;
942 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
952 msdosfs_checkexp(mp, nam, exflagsp, credanonp)
954 struct sockaddr *nam;
956 struct ucred **credanonp;
958 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
961 np = vfs_export_lookup(mp, &pmp->pm_export, nam);
964 *exflagsp = np->netc_exflags;
965 *credanonp = &np->netc_anon;
970 msdosfs_vptofh(vp, fhp)
978 defhp = (struct defid *)fhp;
979 defhp->defid_len = sizeof(struct defid);
980 defhp->defid_dirclust = dep->de_dirclust;
981 defhp->defid_dirofs = dep->de_diroffset;
982 /* defhp->defid_gen = dep->de_gen; */
986 static struct vfsops msdosfs_vfsops = {
1003 VFS_SET(msdosfs_vfsops, msdos, 0);