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 /* Mount options that we support. */
78 static const char *msdosfs_opts[] = {
79 "async", "noatime", "noclusterr", "noclusterw",
80 "export", "force", "from", "sync",
81 "cs_dos", "cs_local", "cs_win", "dirmask",
82 "gid", "kiconv", "large", "longname",
83 "longnames", "mask", "shortname", "shortnames",
84 "uid", "win95", "nowin95",
90 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
91 * garbage or a random value :-{
92 * If you want to use that broken-signatured media, define the
93 * following symbol even though PC/AT.
94 * (ex. mount PC-98 DOS formatted FD on PC/AT)
96 #define MSDOSFS_NOCHECKSIG
99 MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure");
100 static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table");
102 struct iconv_functions *msdosfs_iconv;
104 static int update_mp(struct mount *mp, struct thread *td);
105 static int mountmsdosfs(struct vnode *devvp, struct mount *mp,
107 static vfs_fhtovp_t msdosfs_fhtovp;
108 static vfs_mount_t msdosfs_mount;
109 static vfs_root_t msdosfs_root;
110 static vfs_statfs_t msdosfs_statfs;
111 static vfs_sync_t msdosfs_sync;
112 static vfs_unmount_t msdosfs_unmount;
114 /* Maximum length of a character set name (arbitrary). */
118 update_mp(struct mount *mp, struct thread *td)
120 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
121 void *dos, *win, *local;
124 if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
125 if (msdosfs_iconv != NULL) {
126 error = vfs_getopt(mp->mnt_optnew,
127 "cs_win", &win, NULL);
129 error = vfs_getopt(mp->mnt_optnew,
130 "cs_local", &local, NULL);
132 error = vfs_getopt(mp->mnt_optnew,
133 "cs_dos", &dos, NULL);
135 msdosfs_iconv->open(win, local, &pmp->pm_u2w);
136 msdosfs_iconv->open(local, win, &pmp->pm_w2u);
137 msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
138 msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
150 if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
152 if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
154 if (1 == vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v))
155 pmp->pm_mask = v & ALLPERMS;
156 if (1 == vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v))
157 pmp->pm_dirmask = v & ALLPERMS;
158 vfs_flagopt(mp->mnt_optnew, "shortname",
159 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
160 vfs_flagopt(mp->mnt_optnew, "shortnames",
161 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
162 vfs_flagopt(mp->mnt_optnew, "longname",
163 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
164 vfs_flagopt(mp->mnt_optnew, "longnames",
165 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
166 vfs_flagopt(mp->mnt_optnew, "kiconv",
167 &pmp->pm_flags, MSDOSFSMNT_KICONV);
169 if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
170 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
172 pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
174 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
175 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
176 else if (!(pmp->pm_flags &
177 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
178 struct vnode *rootvp;
181 * Try to divine whether to support Win'95 long filenames
184 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
187 msdosfs_root(mp, LK_EXCLUSIVE, &rootvp, td)) != 0)
189 pmp->pm_flags |= findwin95(VTODE(rootvp)) ?
190 MSDOSFSMNT_LONGNAME : MSDOSFSMNT_SHORTNAME;
198 msdosfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
200 struct msdosfs_args args;
205 error = copyin(data, &args, sizeof args);
209 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
210 ma = mount_arg(ma, "export", &args.export, sizeof args.export);
211 ma = mount_argf(ma, "uid", "%d", args.uid);
212 ma = mount_argf(ma, "gid", "%d", args.gid);
213 ma = mount_argf(ma, "mask", "%d", args.mask);
214 ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
216 ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
217 ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
218 ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
219 ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
221 ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
222 ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
223 ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
225 error = kernel_mount(ma, flags);
231 * mp - path - addr in user space of mount point (ie /usr or whatever)
232 * data - addr in user space of mount params including the name of the block
233 * special file to treat as a filesystem.
236 msdosfs_mount(struct mount *mp, struct thread *td)
238 struct vnode *devvp; /* vnode for blk device to mount */
239 /* msdosfs specific mount control block */
240 struct msdosfsmount *pmp = NULL;
241 struct nameidata ndp;
246 if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
250 * If updating, check whether changing from read-only to
251 * read/write; if there is no device name, that's all we do.
253 if (mp->mnt_flag & MNT_UPDATE) {
254 pmp = VFSTOMSDOSFS(mp);
255 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) {
257 * Forbid export requests if filesystem has
258 * MSDOSFS_LARGEFS flag set.
260 if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0) {
262 "MSDOSFS_LARGEFS flag set, cannot export");
266 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
267 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
268 error = VFS_SYNC(mp, MNT_WAIT, td);
272 if (mp->mnt_flag & MNT_FORCE)
274 error = vflush(mp, 0, flags, td);
279 * Now the volume is clean. Mark it so while the
280 * device is still rw.
282 error = markvoldirty(pmp, 0);
284 (void)markvoldirty(pmp, 1);
288 /* Downgrade the device from rw to ro. */
291 error = g_access(pmp->pm_cp, 0, -1, 0);
295 (void)markvoldirty(pmp, 1);
300 * Backing out after an error was painful in the
301 * above. Now we are committed to succeeding.
304 pmp->pm_flags |= MSDOSFSMNT_RONLY;
306 mp->mnt_flag |= MNT_RDONLY;
308 } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
309 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
311 * If upgrade to read-write by non-root, then verify
312 * that user has necessary permissions on the device.
314 devvp = pmp->pm_devvp;
315 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
316 error = VOP_ACCESS(devvp, VREAD | VWRITE,
319 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
321 VOP_UNLOCK(devvp, 0, td);
324 VOP_UNLOCK(devvp, 0, td);
327 error = g_access(pmp->pm_cp, 0, 1, 0);
334 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
336 mp->mnt_flag &= ~MNT_RDONLY;
339 /* Now that the volume is modifiable, mark it dirty. */
340 error = markvoldirty(pmp, 1);
346 * Not an update, or updating the name: look up the name
347 * and verify that it refers to a sensible disk device.
349 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
351 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
356 NDFREE(&ndp, NDF_ONLY_PNBUF);
358 if (!vn_isdisk(devvp, &error)) {
363 * If mount by non-root, then verify that user has necessary
364 * permissions on the device.
367 if ((mp->mnt_flag & MNT_RDONLY) == 0)
368 accessmode |= VWRITE;
369 error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
371 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
376 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
377 error = mountmsdosfs(devvp, mp, td);
378 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */
379 pmp = VFSTOMSDOSFS(mp);
382 if (devvp != pmp->pm_devvp)
383 error = EINVAL; /* XXX needs translation */
392 error = update_mp(mp, td);
394 if ((mp->mnt_flag & MNT_UPDATE) == 0)
395 msdosfs_unmount(mp, MNT_FORCE, td);
399 vfs_mountedfrom(mp, from);
401 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
407 mountmsdosfs(struct vnode *devvp, struct mount *mp, struct thread *td)
409 struct msdosfsmount *pmp;
411 struct cdev *dev = devvp->v_rdev;
412 union bootsector *bsp;
413 struct byte_bpb33 *b33;
414 struct byte_bpb50 *b50;
415 struct byte_bpb710 *b710;
416 u_int8_t SecPerClust;
419 struct g_consumer *cp;
422 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
423 /* XXX: use VOP_ACCESS to check FS perms */
426 error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
429 VOP_UNLOCK(devvp, 0, td);
433 bo = &devvp->v_bufobj;
434 bp = NULL; /* This and pmp both used in error_exit. */
438 * Read the boot sector of the filesystem, and then check the
439 * boot signature. If not a dos boot sector then error out.
441 * NOTE: 8192 is a magic size that works for ffs.
443 error = bread(devvp, 0, 8192, NOCRED, &bp);
446 bp->b_flags |= B_AGE;
447 bsp = (union bootsector *)bp->b_data;
448 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
449 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
450 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
452 #ifndef MSDOSFS_NOCHECKSIG
453 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
454 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
460 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
466 * Initialize ownerships and permissions, since nothing else will
467 * initialize them iff we are mounting root.
469 pmp->pm_uid = UID_ROOT;
470 pmp->pm_gid = GID_WHEEL;
471 pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
472 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
475 * Experimental support for large MS-DOS filesystems.
476 * WARNING: This uses at least 32 bytes of kernel memory (which is not
477 * reclaimed until the FS is unmounted) for each file on disk to map
478 * between the 32-bit inode numbers used by VFS and the 64-bit
479 * pseudo-inode numbers used internally by msdosfs. This is only
480 * safe to use in certain controlled situations (e.g. read-only FS
481 * with less than 1 million files).
482 * Since the mappings do not persist across unmounts (or reboots), these
483 * filesystems are not suitable for exporting through NFS, or any other
484 * application that requires fixed inode numbers.
486 vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags, MSDOSFS_LARGEFS);
489 * Compute several useful quantities from the bpb in the
490 * bootsector. Copy in the dos 5 variant of the bpb then fix up
491 * the fields that are different between dos 5 and dos 3.3.
493 SecPerClust = b50->bpbSecPerClust;
494 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
495 if (pmp->pm_BytesPerSec < DEV_BSIZE) {
499 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
500 pmp->pm_FATs = b50->bpbFATs;
501 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
502 pmp->pm_Sectors = getushort(b50->bpbSectors);
503 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
504 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
505 pmp->pm_Heads = getushort(b50->bpbHeads);
506 pmp->pm_Media = b50->bpbMedia;
508 /* calculate the ratio of sector size to DEV_BSIZE */
509 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
511 /* XXX - We should probably check more values here */
512 if (!pmp->pm_BytesPerSec || !SecPerClust
515 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
517 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
523 if (pmp->pm_Sectors == 0) {
524 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
525 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
527 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
528 pmp->pm_HugeSectors = pmp->pm_Sectors;
530 if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) {
531 if (pmp->pm_HugeSectors > 0xffffffff /
532 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
534 * We cannot deal currently with this size of disk
535 * due to fileid limitations (see msdosfs_getattr and
540 "Disk too big, try '-o large' mount option");
545 if (pmp->pm_RootDirEnts == 0) {
548 || getushort(b710->bpbFSVers)) {
550 printf("mountmsdosfs(): bad FAT32 filesystem\n");
553 pmp->pm_fatmask = FAT32_MASK;
556 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
557 if (getushort(b710->bpbExtFlags) & FATMIRROR)
558 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
560 pmp->pm_flags |= MSDOSFS_FATMIRROR;
562 pmp->pm_flags |= MSDOSFS_FATMIRROR;
565 * Check a few values (could do some more):
566 * - logical sector size: power of 2, >= block size
567 * - sectors per cluster: power of 2, >= 1
568 * - number of sectors: >= 1, <= size of partition
569 * - number of FAT sectors: >= 1
571 if ( (SecPerClust == 0)
572 || (SecPerClust & (SecPerClust - 1))
573 || (pmp->pm_BytesPerSec < DEV_BSIZE)
574 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
575 || (pmp->pm_HugeSectors == 0)
576 || (pmp->pm_FATsecs == 0)
582 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
583 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
584 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
585 SecPerClust *= pmp->pm_BlkPerSec;
587 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
590 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
591 pmp->pm_firstcluster = pmp->pm_fatblk
592 + (pmp->pm_FATs * pmp->pm_FATsecs);
593 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
595 pmp->pm_rootdirblk = pmp->pm_fatblk +
596 (pmp->pm_FATs * pmp->pm_FATsecs);
597 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
599 / DEV_BSIZE; /* in blocks */
600 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
603 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
605 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
607 if (pmp->pm_fatmask == 0) {
608 if (pmp->pm_maxcluster
609 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
611 * This will usually be a floppy disk. This size makes
612 * sure that one fat entry will not be split across
615 pmp->pm_fatmask = FAT12_MASK;
619 pmp->pm_fatmask = FAT16_MASK;
625 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
626 if (pmp->pm_maxcluster >= clusters) {
627 printf("Warning: number of clusters (%ld) exceeds FAT "
628 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
629 pmp->pm_maxcluster = clusters - 1;
633 pmp->pm_fatblocksize = 3 * 512;
635 pmp->pm_fatblocksize = PAGE_SIZE;
636 pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
637 pmp->pm_BytesPerSec);
638 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
639 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
642 * Compute mask and shift value for isolating cluster relative byte
643 * offsets and cluster numbers from a file offset.
645 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
646 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
647 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
650 * Check for valid cluster size
651 * must be a power of 2
653 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
659 * Release the bootsector buffer.
665 * Check the fsinfo sector if we have one. Silently fix up our
666 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
667 * or too large. Ignore fp->fsinfree for now, since we need to
668 * read the entire FAT anyway to fill the inuse map.
670 if (pmp->pm_fsinfo) {
673 if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
676 fp = (struct fsinfo *)bp->b_data;
677 if (!bcmp(fp->fsisig1, "RRaA", 4)
678 && !bcmp(fp->fsisig2, "rrAa", 4)
679 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
680 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
681 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
682 pmp->pm_nxtfree = CLUST_FIRST;
690 * Finish initializing pmp->pm_nxtfree (just in case the first few
691 * sectors aren't properly reserved in the FAT). This completes
692 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
693 * value if there is no fsinfo. We will use pmp->pm_nxtfree
694 * internally even if there is no fsinfo.
696 if (pmp->pm_nxtfree < CLUST_FIRST)
697 pmp->pm_nxtfree = CLUST_FIRST;
700 * Allocate memory for the bitmap of allocated clusters, and then
703 pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
704 * sizeof(*pmp->pm_inusemap),
705 M_MSDOSFSFAT, M_WAITOK);
708 * fillinusemap() needs pm_devvp.
710 pmp->pm_devvp = devvp;
713 * Have the inuse map filled in.
715 if ((error = fillinusemap(pmp)) != 0)
719 * If they want fat updates to be synchronous then let them suffer
720 * the performance degradation in exchange for the on disk copy of
721 * the fat being correct just about all the time. I suppose this
722 * would be a good thing to turn on if the kernel is still flakey.
724 if (mp->mnt_flag & MNT_SYNCHRONOUS)
725 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
731 pmp->pm_flags |= MSDOSFSMNT_RONLY;
733 if ((error = markvoldirty(pmp, 1)) != 0) {
734 (void)markvoldirty(pmp, 0);
740 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
741 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
743 mp->mnt_flag |= MNT_LOCAL;
746 if (pmp->pm_flags & MSDOSFS_LARGEFS)
747 msdosfs_fileno_init(mp);
762 if (pmp->pm_inusemap)
763 free(pmp->pm_inusemap, M_MSDOSFSFAT);
764 free(pmp, M_MSDOSFSMNT);
771 * Unmount the filesystem described by mp.
774 msdosfs_unmount(struct mount *mp, int mntflags, struct thread *td)
776 struct msdosfsmount *pmp;
780 if (mntflags & MNT_FORCE)
782 error = vflush(mp, 0, flags, td);
785 pmp = VFSTOMSDOSFS(mp);
786 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
787 error = markvoldirty(pmp, 0);
789 (void)markvoldirty(pmp, 1);
793 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
795 msdosfs_iconv->close(pmp->pm_w2u);
797 msdosfs_iconv->close(pmp->pm_u2w);
799 msdosfs_iconv->close(pmp->pm_d2u);
801 msdosfs_iconv->close(pmp->pm_u2d);
806 struct vnode *vp = pmp->pm_devvp;
810 "msdosfs_umount(): just before calling VOP_CLOSE()\n");
811 printf("freef %p, freeb %p, mount %p\n",
812 TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
814 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
815 TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
816 TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
817 vp->v_bufobj.bo_numoutput, vp->v_type);
823 g_vfs_close(pmp->pm_cp, td);
826 vrele(pmp->pm_devvp);
827 free(pmp->pm_inusemap, M_MSDOSFSFAT);
828 if (pmp->pm_flags & MSDOSFS_LARGEFS)
829 msdosfs_fileno_free(mp);
830 free(pmp, M_MSDOSFSMNT);
833 mp->mnt_flag &= ~MNT_LOCAL;
839 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp, struct thread *td)
841 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
846 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
848 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
856 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
858 struct msdosfsmount *pmp;
860 pmp = VFSTOMSDOSFS(mp);
861 sbp->f_bsize = pmp->pm_bpcluster;
862 sbp->f_iosize = pmp->pm_bpcluster;
863 sbp->f_blocks = pmp->pm_maxcluster + 1;
864 sbp->f_bfree = pmp->pm_freeclustercount;
865 sbp->f_bavail = pmp->pm_freeclustercount;
866 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
867 sbp->f_ffree = 0; /* what to put in here? */
872 msdosfs_sync(struct mount *mp, int waitfor, struct thread *td)
874 struct vnode *vp, *nvp;
876 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
877 int error, allerror = 0;
880 * If we ever switch to not updating all of the fats all the time,
881 * this would be the place to update them from the first one.
883 if (pmp->pm_fmod != 0) {
884 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
885 panic("msdosfs_sync: rofs mod");
887 /* update fats here */
891 * Write back each (modified) denode.
895 MNT_VNODE_FOREACH(vp, mp, nvp) {
897 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) {
904 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
905 (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
906 waitfor == MNT_LAZY)) {
911 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
918 error = VOP_FSYNC(vp, waitfor, td);
921 VOP_UNLOCK(vp, 0, td);
928 * Flush filesystem control info.
930 if (waitfor != MNT_LAZY) {
931 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
932 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
935 VOP_UNLOCK(pmp->pm_devvp, 0, td);
941 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
943 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
944 struct defid *defhp = (struct defid *) fhp;
948 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
954 vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
958 static struct vfsops msdosfs_vfsops = {
959 .vfs_fhtovp = msdosfs_fhtovp,
960 .vfs_mount = msdosfs_mount,
961 .vfs_cmount = msdosfs_cmount,
962 .vfs_root = msdosfs_root,
963 .vfs_statfs = msdosfs_statfs,
964 .vfs_sync = msdosfs_sync,
965 .vfs_unmount = msdosfs_unmount,
968 VFS_SET(msdosfs_vfsops, msdosfs, 0);
969 MODULE_VERSION(msdosfs, 1);