2 /* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */
5 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
6 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
8 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
36 * Written by Paul Popelka (paulp@uts.amdahl.com)
38 * You can do anything you want with this software, just don't say you wrote
39 * it, and don't remove this notice.
41 * This software is provided "as is".
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.
51 #include <sys/param.h>
52 #include <sys/systm.h>
55 #include <sys/iconv.h>
56 #include <sys/kernel.h>
58 #include <sys/malloc.h>
59 #include <sys/mount.h>
60 #include <sys/mutex.h>
61 #include <sys/namei.h>
65 #include <sys/vnode.h>
67 #include <geom/geom.h>
68 #include <geom/geom_vfs.h>
70 #include <fs/msdosfs/bootsect.h>
71 #include <fs/msdosfs/bpb.h>
72 #include <fs/msdosfs/direntry.h>
73 #include <fs/msdosfs/denode.h>
74 #include <fs/msdosfs/fat.h>
75 #include <fs/msdosfs/msdosfsmount.h>
77 /* List of mount options we support */
78 static const char *msdosfs_opts[] = {
80 "atime", "export", "force", "sync",
81 "uid", "gid", "mask", "dirmask",
82 "shortname", "shortnames", "longname", "longnames", "nowin95", "win95",
83 "kiconv", "cs_win", "cs_dos", "cs_local", "large",
89 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
90 * garbage or a random value :-{
91 * If you want to use that broken-signatured media, define the
92 * following symbol even though PC/AT.
93 * (ex. mount PC-98 DOS formatted FD on PC/AT)
95 #define MSDOSFS_NOCHECKSIG
98 MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure");
99 static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table");
101 struct iconv_functions *msdosfs_iconv;
103 static int update_mp(struct mount *mp, struct thread *td);
104 static int mountmsdosfs(struct vnode *devvp, struct mount *mp,
106 static vfs_fhtovp_t msdosfs_fhtovp;
107 static vfs_mount_t msdosfs_mount;
108 static vfs_root_t msdosfs_root;
109 static vfs_statfs_t msdosfs_statfs;
110 static vfs_sync_t msdosfs_sync;
111 static vfs_unmount_t msdosfs_unmount;
113 /* Maximum length of a character set name (arbitrary). */
117 update_mp(struct mount *mp, struct thread *td)
119 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
120 void *dos, *win, *local;
123 if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
124 if (msdosfs_iconv != NULL) {
125 error = vfs_getopt(mp->mnt_optnew,
126 "cs_win", &win, NULL);
128 error = vfs_getopt(mp->mnt_optnew,
129 "cs_local", &local, NULL);
131 error = vfs_getopt(mp->mnt_optnew,
132 "cs_dos", &dos, NULL);
134 msdosfs_iconv->open(win, local, &pmp->pm_u2w);
135 msdosfs_iconv->open(local, win, &pmp->pm_w2u);
136 msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
137 msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
149 if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
151 if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
153 if (1 == vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v))
154 pmp->pm_mask = v & ALLPERMS;
155 if (1 == vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v))
156 pmp->pm_dirmask = v & ALLPERMS;
157 vfs_flagopt(mp->mnt_optnew, "shortname",
158 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
159 vfs_flagopt(mp->mnt_optnew, "shortnames",
160 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
161 vfs_flagopt(mp->mnt_optnew, "longname",
162 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
163 vfs_flagopt(mp->mnt_optnew, "longnames",
164 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
165 vfs_flagopt(mp->mnt_optnew, "kiconv",
166 &pmp->pm_flags, MSDOSFSMNT_KICONV);
168 if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
169 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
171 pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
173 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
174 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
175 else if (!(pmp->pm_flags &
176 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
177 struct vnode *rootvp;
180 * Try to divine whether to support Win'95 long filenames
183 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
186 msdosfs_root(mp, LK_EXCLUSIVE, &rootvp, td)) != 0)
188 pmp->pm_flags |= findwin95(VTODE(rootvp)) ?
189 MSDOSFSMNT_LONGNAME : MSDOSFSMNT_SHORTNAME;
197 msdosfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
199 struct msdosfs_args args;
204 error = copyin(data, &args, sizeof args);
208 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
209 ma = mount_arg(ma, "export", &args.export, sizeof args.export);
210 ma = mount_argf(ma, "uid", "%d", args.uid);
211 ma = mount_argf(ma, "gid", "%d", args.gid);
212 ma = mount_argf(ma, "mask", "%d", args.mask);
213 ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
215 ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
216 ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
217 ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
218 ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
220 ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
221 ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
222 ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
224 error = kernel_mount(ma, flags);
230 * mp - path - addr in user space of mount point (ie /usr or whatever)
231 * data - addr in user space of mount params including the name of the block
232 * special file to treat as a filesystem.
235 msdosfs_mount(struct mount *mp, struct thread *td)
237 struct vnode *devvp; /* vnode for blk device to mount */
238 /* msdosfs specific mount control block */
239 struct msdosfsmount *pmp = NULL;
240 struct nameidata ndp;
245 if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
249 * If updating, check whether changing from read-only to
250 * read/write; if there is no device name, that's all we do.
252 if (mp->mnt_flag & MNT_UPDATE) {
255 pmp = VFSTOMSDOSFS(mp);
256 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) {
258 * Forbid export requests if filesystem has
259 * MSDOSFS_LARGEFS flag set.
261 if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0) {
263 "MSDOSFS_LARGEFS flag set, cannot export");
267 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
268 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
269 error = VFS_SYNC(mp, MNT_WAIT, td);
273 if (mp->mnt_flag & MNT_FORCE)
275 error = vflush(mp, 0, flags, td);
280 error = g_access(pmp->pm_cp, 0, -1, 0);
286 /* Now the volume is clean. Mark it. */
287 error = markvoldirty(pmp, 0);
288 if (error && (flags & FORCECLOSE) == 0)
290 } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
291 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
293 * If upgrade to read-write by non-root, then verify
294 * that user has necessary permissions on the device.
296 devvp = pmp->pm_devvp;
297 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
298 error = VOP_ACCESS(devvp, VREAD | VWRITE,
301 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
303 VOP_UNLOCK(devvp, 0, td);
306 VOP_UNLOCK(devvp, 0, td);
309 error = g_access(pmp->pm_cp, 0, 1, 0);
317 vfs_flagopt(mp->mnt_optnew, "ro",
318 &pmp->pm_flags, MSDOSFSMNT_RONLY);
319 vfs_flagopt(mp->mnt_optnew, "ro",
320 &mp->mnt_flag, MNT_RDONLY);
323 /* Now that the volume is modifiable, mark it dirty. */
324 error = markvoldirty(pmp, 1);
330 * Not an update, or updating the name: look up the name
331 * and verify that it refers to a sensible disk device.
333 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
335 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
340 NDFREE(&ndp, NDF_ONLY_PNBUF);
342 if (!vn_isdisk(devvp, &error)) {
347 * If mount by non-root, then verify that user has necessary
348 * permissions on the device.
351 if ((mp->mnt_flag & MNT_RDONLY) == 0)
352 accessmode |= VWRITE;
353 error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
355 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
360 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
361 error = mountmsdosfs(devvp, mp, td);
362 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */
363 pmp = VFSTOMSDOSFS(mp);
366 if (devvp != pmp->pm_devvp)
367 error = EINVAL; /* XXX needs translation */
376 error = update_mp(mp, td);
378 if ((mp->mnt_flag & MNT_UPDATE) == 0)
379 msdosfs_unmount(mp, MNT_FORCE, td);
383 vfs_mountedfrom(mp, from);
385 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
391 mountmsdosfs(struct vnode *devvp, struct mount *mp, struct thread *td)
393 struct msdosfsmount *pmp;
395 struct cdev *dev = devvp->v_rdev;
396 union bootsector *bsp;
397 struct byte_bpb33 *b33;
398 struct byte_bpb50 *b50;
399 struct byte_bpb710 *b710;
400 u_int8_t SecPerClust;
403 struct g_consumer *cp;
406 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
407 /* XXX: use VOP_ACCESS to check FS perms */
410 error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
413 VOP_UNLOCK(devvp, 0, td);
417 bo = &devvp->v_bufobj;
418 bp = NULL; /* This and pmp both used in error_exit. */
422 * Read the boot sector of the filesystem, and then check the
423 * boot signature. If not a dos boot sector then error out.
425 * NOTE: 8192 is a magic size that works for ffs.
427 error = bread(devvp, 0, 8192, NOCRED, &bp);
430 bp->b_flags |= B_AGE;
431 bsp = (union bootsector *)bp->b_data;
432 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
433 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
434 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
436 #ifndef MSDOSFS_NOCHECKSIG
437 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
438 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
444 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
450 * Initialize ownerships and permissions, since nothing else will
451 * initialize them iff we are mounting root.
453 pmp->pm_uid = UID_ROOT;
454 pmp->pm_gid = GID_WHEEL;
455 pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
456 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
459 * Experimental support for large MS-DOS filesystems.
460 * WARNING: This uses at least 32 bytes of kernel memory (which is not
461 * reclaimed until the FS is unmounted) for each file on disk to map
462 * between the 32-bit inode numbers used by VFS and the 64-bit
463 * pseudo-inode numbers used internally by msdosfs. This is only
464 * safe to use in certain controlled situations (e.g. read-only FS
465 * with less than 1 million files).
466 * Since the mappings do not persist across unmounts (or reboots), these
467 * filesystems are not suitable for exporting through NFS, or any other
468 * application that requires fixed inode numbers.
470 vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags, MSDOSFS_LARGEFS);
473 * Compute several useful quantities from the bpb in the
474 * bootsector. Copy in the dos 5 variant of the bpb then fix up
475 * the fields that are different between dos 5 and dos 3.3.
477 SecPerClust = b50->bpbSecPerClust;
478 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
479 if (pmp->pm_BytesPerSec < DEV_BSIZE) {
483 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
484 pmp->pm_FATs = b50->bpbFATs;
485 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
486 pmp->pm_Sectors = getushort(b50->bpbSectors);
487 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
488 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
489 pmp->pm_Heads = getushort(b50->bpbHeads);
490 pmp->pm_Media = b50->bpbMedia;
492 /* calculate the ratio of sector size to DEV_BSIZE */
493 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
495 /* XXX - We should probably check more values here */
496 if (!pmp->pm_BytesPerSec || !SecPerClust
499 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
501 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
507 if (pmp->pm_Sectors == 0) {
508 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
509 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
511 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
512 pmp->pm_HugeSectors = pmp->pm_Sectors;
514 if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) {
515 if (pmp->pm_HugeSectors > 0xffffffff /
516 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
518 * We cannot deal currently with this size of disk
519 * due to fileid limitations (see msdosfs_getattr and
524 "Disk too big, try '-o large' mount option");
529 if (pmp->pm_RootDirEnts == 0) {
532 || getushort(b710->bpbFSVers)) {
534 printf("mountmsdosfs(): bad FAT32 filesystem\n");
537 pmp->pm_fatmask = FAT32_MASK;
540 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
541 if (getushort(b710->bpbExtFlags) & FATMIRROR)
542 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
544 pmp->pm_flags |= MSDOSFS_FATMIRROR;
546 pmp->pm_flags |= MSDOSFS_FATMIRROR;
549 * Check a few values (could do some more):
550 * - logical sector size: power of 2, >= block size
551 * - sectors per cluster: power of 2, >= 1
552 * - number of sectors: >= 1, <= size of partition
553 * - number of FAT sectors: >= 1
555 if ( (SecPerClust == 0)
556 || (SecPerClust & (SecPerClust - 1))
557 || (pmp->pm_BytesPerSec < DEV_BSIZE)
558 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
559 || (pmp->pm_HugeSectors == 0)
560 || (pmp->pm_FATsecs == 0)
566 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
567 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
568 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
569 SecPerClust *= pmp->pm_BlkPerSec;
571 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
574 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
575 pmp->pm_firstcluster = pmp->pm_fatblk
576 + (pmp->pm_FATs * pmp->pm_FATsecs);
577 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
579 pmp->pm_rootdirblk = pmp->pm_fatblk +
580 (pmp->pm_FATs * pmp->pm_FATsecs);
581 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
583 / DEV_BSIZE; /* in blocks */
584 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
587 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
589 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
591 if (pmp->pm_fatmask == 0) {
592 if (pmp->pm_maxcluster
593 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
595 * This will usually be a floppy disk. This size makes
596 * sure that one fat entry will not be split across
599 pmp->pm_fatmask = FAT12_MASK;
603 pmp->pm_fatmask = FAT16_MASK;
609 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
610 if (pmp->pm_maxcluster >= clusters) {
611 printf("Warning: number of clusters (%ld) exceeds FAT "
612 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
613 pmp->pm_maxcluster = clusters - 1;
617 pmp->pm_fatblocksize = 3 * 512;
619 pmp->pm_fatblocksize = PAGE_SIZE;
620 pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
621 pmp->pm_BytesPerSec);
622 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
623 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
626 * Compute mask and shift value for isolating cluster relative byte
627 * offsets and cluster numbers from a file offset.
629 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
630 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
631 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
634 * Check for valid cluster size
635 * must be a power of 2
637 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
643 * Release the bootsector buffer.
649 * Check the fsinfo sector if we have one. Silently fix up our
650 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
651 * or too large. Ignore fp->fsinfree for now, since we need to
652 * read the entire FAT anyway to fill the inuse map.
654 if (pmp->pm_fsinfo) {
657 if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
660 fp = (struct fsinfo *)bp->b_data;
661 if (!bcmp(fp->fsisig1, "RRaA", 4)
662 && !bcmp(fp->fsisig2, "rrAa", 4)
663 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
664 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
665 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
666 pmp->pm_nxtfree = CLUST_FIRST;
674 * Finish initializing pmp->pm_nxtfree (just in case the first few
675 * sectors aren't properly reserved in the FAT). This completes
676 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
677 * value if there is no fsinfo. We will use pmp->pm_nxtfree
678 * internally even if there is no fsinfo.
680 if (pmp->pm_nxtfree < CLUST_FIRST)
681 pmp->pm_nxtfree = CLUST_FIRST;
684 * Allocate memory for the bitmap of allocated clusters, and then
687 pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
688 * sizeof(*pmp->pm_inusemap),
689 M_MSDOSFSFAT, M_WAITOK);
692 * fillinusemap() needs pm_devvp.
694 pmp->pm_devvp = devvp;
697 * Have the inuse map filled in.
699 if ((error = fillinusemap(pmp)) != 0)
703 * If they want fat updates to be synchronous then let them suffer
704 * the performance degradation in exchange for the on disk copy of
705 * the fat being correct just about all the time. I suppose this
706 * would be a good thing to turn on if the kernel is still flakey.
708 if (mp->mnt_flag & MNT_SYNCHRONOUS)
709 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
715 pmp->pm_flags |= MSDOSFSMNT_RONLY;
717 /* Mark the volume dirty while it is mounted read/write. */
718 if ((error = markvoldirty(pmp, 1)) != 0)
722 mp->mnt_data = (qaddr_t) pmp;
723 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
724 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
726 mp->mnt_flag |= MNT_LOCAL;
729 if (pmp->pm_flags & MSDOSFS_LARGEFS)
730 msdosfs_fileno_init(mp);
745 if (pmp->pm_inusemap)
746 free(pmp->pm_inusemap, M_MSDOSFSFAT);
747 free(pmp, M_MSDOSFSMNT);
748 mp->mnt_data = (qaddr_t)0;
754 * Unmount the filesystem described by mp.
757 msdosfs_unmount(struct mount *mp, int mntflags, struct thread *td)
759 struct msdosfsmount *pmp;
763 if (mntflags & MNT_FORCE)
765 error = vflush(mp, 0, flags, td);
768 pmp = VFSTOMSDOSFS(mp);
769 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
771 msdosfs_iconv->close(pmp->pm_w2u);
773 msdosfs_iconv->close(pmp->pm_u2w);
775 msdosfs_iconv->close(pmp->pm_d2u);
777 msdosfs_iconv->close(pmp->pm_u2d);
780 /* If the volume was mounted read/write, mark it clean now. */
781 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
782 error = markvoldirty(pmp, 0);
783 if (error && (flags & FORCECLOSE) == 0)
788 struct vnode *vp = pmp->pm_devvp;
792 "msdosfs_umount(): just before calling VOP_CLOSE()\n");
793 printf("freef %p, freeb %p, mount %p\n",
794 TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
796 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
797 TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
798 TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
799 vp->v_bufobj.bo_numoutput, vp->v_type);
805 g_vfs_close(pmp->pm_cp, td);
808 vrele(pmp->pm_devvp);
809 free(pmp->pm_inusemap, M_MSDOSFSFAT);
810 if (pmp->pm_flags & MSDOSFS_LARGEFS) {
811 msdosfs_fileno_free(mp);
813 free(pmp, M_MSDOSFSMNT);
814 mp->mnt_data = (qaddr_t)0;
816 mp->mnt_flag &= ~MNT_LOCAL;
822 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
824 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
829 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
831 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
839 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
841 struct msdosfsmount *pmp;
843 pmp = VFSTOMSDOSFS(mp);
844 sbp->f_bsize = pmp->pm_bpcluster;
845 sbp->f_iosize = pmp->pm_bpcluster;
846 sbp->f_blocks = pmp->pm_maxcluster + 1;
847 sbp->f_bfree = pmp->pm_freeclustercount;
848 sbp->f_bavail = pmp->pm_freeclustercount;
849 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
850 sbp->f_ffree = 0; /* what to put in here? */
855 msdosfs_sync(struct mount *mp, int waitfor, struct thread *td)
857 struct vnode *vp, *nvp;
859 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
860 int error, allerror = 0;
863 * If we ever switch to not updating all of the fats all the time,
864 * this would be the place to update them from the first one.
866 if (pmp->pm_fmod != 0) {
867 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
868 panic("msdosfs_sync: rofs mod");
870 /* update fats here */
874 * Write back each (modified) denode.
878 MNT_VNODE_FOREACH(vp, mp, nvp) {
880 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) {
887 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
888 (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
889 waitfor == MNT_LAZY)) {
894 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
901 error = VOP_FSYNC(vp, waitfor, td);
904 VOP_UNLOCK(vp, 0, td);
911 * Flush filesystem control info.
913 if (waitfor != MNT_LAZY) {
914 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
915 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
918 VOP_UNLOCK(pmp->pm_devvp, 0, td);
924 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
926 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
927 struct defid *defhp = (struct defid *) fhp;
931 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
937 vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
941 static struct vfsops msdosfs_vfsops = {
942 .vfs_fhtovp = msdosfs_fhtovp,
943 .vfs_mount = msdosfs_mount,
944 .vfs_cmount = msdosfs_cmount,
945 .vfs_root = msdosfs_root,
946 .vfs_statfs = msdosfs_statfs,
947 .vfs_sync = msdosfs_sync,
948 .vfs_unmount = msdosfs_unmount,
951 VFS_SET(msdosfs_vfsops, msdosfs, 0);
952 MODULE_VERSION(msdosfs, 1);