2 /* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */
5 * SPDX-License-Identifier: BSD-4-Clause
7 * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8 * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
10 * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by TooLs GmbH.
23 * 4. The name of TooLs GmbH may not be used to endorse or promote products
24 * derived from this software without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * Written by Paul Popelka (paulp@uts.amdahl.com)
40 * You can do anything you want with this software, just don't say you wrote
41 * it, and don't remove this notice.
43 * This software is provided "as is".
45 * The author supplies this software to be publicly redistributed on the
46 * understanding that the author is not responsible for the correct
47 * functioning of this software in any circumstances and is not liable for
48 * any damages caused by this software.
53 #include <sys/param.h>
54 #include <sys/systm.h>
57 #include <sys/fcntl.h>
58 #include <sys/iconv.h>
59 #include <sys/kernel.h>
61 #include <sys/malloc.h>
62 #include <sys/mount.h>
63 #include <sys/mutex.h>
64 #include <sys/namei.h>
68 #include <sys/vnode.h>
70 #include <geom/geom.h>
71 #include <geom/geom_vfs.h>
73 #include <fs/msdosfs/bootsect.h>
74 #include <fs/msdosfs/bpb.h>
75 #include <fs/msdosfs/direntry.h>
76 #include <fs/msdosfs/denode.h>
77 #include <fs/msdosfs/fat.h>
78 #include <fs/msdosfs/msdosfsmount.h>
81 #include <sys/rwlock.h>
84 static const char msdosfs_lock_msg[] = "fatlk";
86 /* Mount options that we support. */
87 static const char *msdosfs_opts[] = {
88 "async", "noatime", "noclusterr", "noclusterw",
89 "export", "force", "from", "sync",
90 "cs_dos", "cs_local", "cs_win", "dirmask",
91 "gid", "kiconv", "longname",
92 "longnames", "mask", "shortname", "shortnames",
93 "uid", "win95", "nowin95",
99 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
100 * garbage or a random value :-{
101 * If you want to use that broken-signatured media, define the
102 * following symbol even though PC/AT.
103 * (ex. mount PC-98 DOS formatted FD on PC/AT)
105 #define MSDOSFS_NOCHECKSIG
108 MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure");
109 static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table");
111 struct iconv_functions *msdosfs_iconv;
113 static int update_mp(struct mount *mp, struct thread *td);
114 static int mountmsdosfs(struct vnode *devvp, struct mount *mp);
115 static vfs_fhtovp_t msdosfs_fhtovp;
116 static vfs_mount_t msdosfs_mount;
117 static vfs_root_t msdosfs_root;
118 static vfs_statfs_t msdosfs_statfs;
119 static vfs_sync_t msdosfs_sync;
120 static vfs_unmount_t msdosfs_unmount;
122 /* Maximum length of a character set name (arbitrary). */
126 update_mp(struct mount *mp, struct thread *td)
128 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
129 void *dos, *win, *local;
132 if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
133 if (msdosfs_iconv != NULL) {
134 error = vfs_getopt(mp->mnt_optnew,
135 "cs_win", &win, NULL);
137 error = vfs_getopt(mp->mnt_optnew,
138 "cs_local", &local, NULL);
140 error = vfs_getopt(mp->mnt_optnew,
141 "cs_dos", &dos, NULL);
143 msdosfs_iconv->open(win, local, &pmp->pm_u2w);
144 msdosfs_iconv->open(local, win, &pmp->pm_w2u);
145 msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
146 msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
158 if (vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v) == 1)
160 if (vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v) == 1)
162 if (vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v) == 1)
163 pmp->pm_mask = v & ALLPERMS;
164 if (vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v) == 1)
165 pmp->pm_dirmask = v & ALLPERMS;
166 vfs_flagopt(mp->mnt_optnew, "shortname",
167 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
168 vfs_flagopt(mp->mnt_optnew, "shortnames",
169 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
170 vfs_flagopt(mp->mnt_optnew, "longname",
171 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
172 vfs_flagopt(mp->mnt_optnew, "longnames",
173 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
174 vfs_flagopt(mp->mnt_optnew, "kiconv",
175 &pmp->pm_flags, MSDOSFSMNT_KICONV);
177 if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
178 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
180 pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
182 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
183 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
185 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
190 msdosfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
192 struct msdosfs_args args;
193 struct export_args exp;
198 error = copyin(data, &args, sizeof args);
201 vfs_oexport_conv(&args.export, &exp);
203 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
204 ma = mount_arg(ma, "export", &exp, sizeof(exp));
205 ma = mount_argf(ma, "uid", "%d", args.uid);
206 ma = mount_argf(ma, "gid", "%d", args.gid);
207 ma = mount_argf(ma, "mask", "%d", args.mask);
208 ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
210 ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
211 ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
212 ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
213 ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
215 ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
216 ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
217 ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
219 error = kernel_mount(ma, flags);
225 * mp - path - addr in user space of mount point (ie /usr or whatever)
226 * data - addr in user space of mount params including the name of the block
227 * special file to treat as a filesystem.
230 msdosfs_mount(struct mount *mp)
232 struct vnode *devvp; /* vnode for blk device to mount */
234 /* msdosfs specific mount control block */
235 struct msdosfsmount *pmp = NULL;
236 struct nameidata ndp;
242 if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
246 * If updating, check whether changing from read-only to
247 * read/write; if there is no device name, that's all we do.
249 if (mp->mnt_flag & MNT_UPDATE) {
250 pmp = VFSTOMSDOSFS(mp);
251 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
252 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
253 error = VFS_SYNC(mp, MNT_WAIT);
257 if (mp->mnt_flag & MNT_FORCE)
259 error = vflush(mp, 0, flags, td);
264 * Now the volume is clean. Mark it so while the
265 * device is still rw.
267 error = markvoldirty(pmp, 0);
269 (void)markvoldirty(pmp, 1);
273 /* Downgrade the device from rw to ro. */
275 error = g_access(pmp->pm_cp, 0, -1, 0);
278 (void)markvoldirty(pmp, 1);
283 * Backing out after an error was painful in the
284 * above. Now we are committed to succeeding.
287 pmp->pm_flags |= MSDOSFSMNT_RONLY;
289 mp->mnt_flag |= MNT_RDONLY;
291 } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
292 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
294 * If upgrade to read-write by non-root, then verify
295 * that user has necessary permissions on the device.
297 devvp = pmp->pm_devvp;
298 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
299 error = VOP_ACCESS(devvp, VREAD | VWRITE,
302 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
304 VOP_UNLOCK(devvp, 0);
307 VOP_UNLOCK(devvp, 0);
309 error = g_access(pmp->pm_cp, 0, 1, 0);
315 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
317 mp->mnt_flag &= ~MNT_RDONLY;
320 /* Now that the volume is modifiable, mark it dirty. */
321 error = markvoldirty(pmp, 1);
327 * Not an update, or updating the name: look up the name
328 * and verify that it refers to a sensible disk device.
330 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
332 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
337 NDFREE(&ndp, NDF_ONLY_PNBUF);
339 if (!vn_isdisk(devvp, &error)) {
344 * If mount by non-root, then verify that user has necessary
345 * permissions on the device.
348 if ((mp->mnt_flag & MNT_RDONLY) == 0)
350 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
352 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
357 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
358 error = mountmsdosfs(devvp, mp);
359 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */
360 pmp = VFSTOMSDOSFS(mp);
364 if (devvp != pmp->pm_devvp)
365 return (EINVAL); /* XXX needs translation */
372 error = update_mp(mp, td);
374 if ((mp->mnt_flag & MNT_UPDATE) == 0)
375 msdosfs_unmount(mp, MNT_FORCE);
379 vfs_mountedfrom(mp, from);
381 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
387 mountmsdosfs(struct vnode *devvp, struct mount *mp)
389 struct msdosfsmount *pmp;
392 union bootsector *bsp;
393 struct byte_bpb33 *b33;
394 struct byte_bpb50 *b50;
395 struct byte_bpb710 *b710;
399 struct g_consumer *cp;
402 bp = NULL; /* This and pmp both used in error_exit. */
404 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
407 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
408 (uintptr_t)mp) == 0) {
409 VOP_UNLOCK(devvp, 0);
413 error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
416 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
417 VOP_UNLOCK(devvp, 0);
421 bo = &devvp->v_bufobj;
422 VOP_UNLOCK(devvp, 0);
423 if (dev->si_iosize_max != 0)
424 mp->mnt_iosize_max = dev->si_iosize_max;
425 if (mp->mnt_iosize_max > MAXPHYS)
426 mp->mnt_iosize_max = MAXPHYS;
429 * Read the boot sector of the filesystem, and then check the
430 * boot signature. If not a dos boot sector then error out.
432 * NOTE: 8192 is a magic size that works for ffs.
434 error = bread(devvp, 0, 8192, NOCRED, &bp);
437 bp->b_flags |= B_AGE;
438 bsp = (union bootsector *)bp->b_data;
439 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
440 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
441 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
443 #ifndef MSDOSFS_NOCHECKSIG
444 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
445 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
451 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
456 lockinit(&pmp->pm_fatlock, 0, msdosfs_lock_msg, 0, 0);
459 * Initialize ownerships and permissions, since nothing else will
460 * initialize them iff we are mounting root.
462 pmp->pm_uid = UID_ROOT;
463 pmp->pm_gid = GID_WHEEL;
464 pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
465 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
468 * Compute several useful quantities from the bpb in the
469 * bootsector. Copy in the dos 5 variant of the bpb then fix up
470 * the fields that are different between dos 5 and dos 3.3.
472 SecPerClust = b50->bpbSecPerClust;
473 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
474 if (pmp->pm_BytesPerSec < DEV_BSIZE) {
478 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
479 pmp->pm_FATs = b50->bpbFATs;
480 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
481 pmp->pm_Sectors = getushort(b50->bpbSectors);
482 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
483 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
484 pmp->pm_Heads = getushort(b50->bpbHeads);
485 pmp->pm_Media = b50->bpbMedia;
487 /* calculate the ratio of sector size to DEV_BSIZE */
488 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
491 * We don't check pm_Heads nor pm_SecPerTrack, because
492 * these may not be set for EFI file systems. We don't
493 * use these anyway, so we're unaffected if they are
496 if (!pmp->pm_BytesPerSec || !SecPerClust) {
501 if (pmp->pm_Sectors == 0) {
502 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
503 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
505 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
506 pmp->pm_HugeSectors = pmp->pm_Sectors;
509 if (pmp->pm_RootDirEnts == 0) {
511 || getushort(b710->bpbFSVers)) {
514 printf("mountmsdosfs(): bad FAT32 filesystem\n");
518 pmp->pm_fatmask = FAT32_MASK;
521 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
522 if (getushort(b710->bpbExtFlags) & FATMIRROR)
523 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
525 pmp->pm_flags |= MSDOSFS_FATMIRROR;
527 pmp->pm_flags |= MSDOSFS_FATMIRROR;
530 * Check a few values (could do some more):
531 * - logical sector size: power of 2, >= block size
532 * - sectors per cluster: power of 2, >= 1
533 * - number of sectors: >= 1, <= size of partition
534 * - number of FAT sectors: >= 1
536 if ( (SecPerClust == 0)
537 || (SecPerClust & (SecPerClust - 1))
538 || (pmp->pm_BytesPerSec < DEV_BSIZE)
539 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
540 || (pmp->pm_HugeSectors == 0)
541 || (pmp->pm_FATsecs == 0)
542 || (SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE)
548 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
549 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
550 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
551 SecPerClust *= pmp->pm_BlkPerSec;
553 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
556 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
557 pmp->pm_firstcluster = pmp->pm_fatblk
558 + (pmp->pm_FATs * pmp->pm_FATsecs);
559 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
561 pmp->pm_rootdirblk = pmp->pm_fatblk +
562 (pmp->pm_FATs * pmp->pm_FATsecs);
563 pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts *
564 sizeof(struct direntry), DEV_BSIZE); /* in blocks */
565 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
568 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
570 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
572 if (pmp->pm_fatmask == 0) {
573 if (pmp->pm_maxcluster
574 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
576 * This will usually be a floppy disk. This size makes
577 * sure that one FAT entry will not be split across
580 pmp->pm_fatmask = FAT12_MASK;
584 pmp->pm_fatmask = FAT16_MASK;
590 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
591 if (pmp->pm_maxcluster >= clusters) {
593 printf("Warning: number of clusters (%ld) exceeds FAT "
594 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
596 pmp->pm_maxcluster = clusters - 1;
600 pmp->pm_fatblocksize = 3 * 512;
602 pmp->pm_fatblocksize = PAGE_SIZE;
603 pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
604 pmp->pm_BytesPerSec);
605 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
606 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
609 * Compute mask and shift value for isolating cluster relative byte
610 * offsets and cluster numbers from a file offset.
612 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
613 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
614 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
617 * Check for valid cluster size
618 * must be a power of 2
620 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
626 * Release the bootsector buffer.
632 * Check the fsinfo sector if we have one. Silently fix up our
633 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
634 * or too large. Ignore fp->fsinfree for now, since we need to
635 * read the entire FAT anyway to fill the inuse map.
637 if (pmp->pm_fsinfo) {
640 if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
643 fp = (struct fsinfo *)bp->b_data;
644 if (!bcmp(fp->fsisig1, "RRaA", 4)
645 && !bcmp(fp->fsisig2, "rrAa", 4)
646 && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
647 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
648 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
649 pmp->pm_nxtfree = CLUST_FIRST;
657 * Finish initializing pmp->pm_nxtfree (just in case the first few
658 * sectors aren't properly reserved in the FAT). This completes
659 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
660 * value if there is no fsinfo. We will use pmp->pm_nxtfree
661 * internally even if there is no fsinfo.
663 if (pmp->pm_nxtfree < CLUST_FIRST)
664 pmp->pm_nxtfree = CLUST_FIRST;
667 * Allocate memory for the bitmap of allocated clusters, and then
670 pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
671 * sizeof(*pmp->pm_inusemap),
672 M_MSDOSFSFAT, M_WAITOK);
675 * fillinusemap() needs pm_devvp.
677 pmp->pm_devvp = devvp;
681 * Have the inuse map filled in.
683 MSDOSFS_LOCK_MP(pmp);
684 error = fillinusemap(pmp);
685 MSDOSFS_UNLOCK_MP(pmp);
690 * If they want FAT updates to be synchronous then let them suffer
691 * the performance degradation in exchange for the on disk copy of
692 * the FAT being correct just about all the time. I suppose this
693 * would be a good thing to turn on if the kernel is still flakey.
695 if (mp->mnt_flag & MNT_SYNCHRONOUS)
696 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
702 pmp->pm_flags |= MSDOSFSMNT_RONLY;
704 if ((error = markvoldirty(pmp, 1)) != 0) {
705 (void)markvoldirty(pmp, 0);
711 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
712 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
714 mp->mnt_flag |= MNT_LOCAL;
715 mp->mnt_kern_flag |= MNTK_USES_BCACHE | MNTK_NO_IOPF;
729 lockdestroy(&pmp->pm_fatlock);
730 free(pmp->pm_inusemap, M_MSDOSFSFAT);
731 free(pmp, M_MSDOSFSMNT);
734 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
740 * Unmount the filesystem described by mp.
743 msdosfs_unmount(struct mount *mp, int mntflags)
745 struct msdosfsmount *pmp;
749 pmp = VFSTOMSDOSFS(mp);
750 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0)
751 error = msdosfs_sync(mp, MNT_WAIT);
752 if ((mntflags & MNT_FORCE) != 0)
756 error = vflush(mp, 0, flags, curthread);
757 if (error != 0 && error != ENXIO)
759 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
760 error = markvoldirty(pmp, 0);
761 if (error && error != ENXIO) {
762 (void)markvoldirty(pmp, 1);
766 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
768 msdosfs_iconv->close(pmp->pm_w2u);
770 msdosfs_iconv->close(pmp->pm_u2w);
772 msdosfs_iconv->close(pmp->pm_d2u);
774 msdosfs_iconv->close(pmp->pm_u2d);
779 struct vnode *vp = pmp->pm_devvp;
786 "msdosfs_umount(): just before calling VOP_CLOSE()\n");
787 printf("freef %p, freeb %p, mount %p\n",
788 TAILQ_NEXT(vp, v_actfreelist), vp->v_actfreelist.tqe_prev,
790 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
791 TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
792 TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
793 vp->v_bufobj.bo_numoutput, vp->v_type);
799 g_vfs_close(pmp->pm_cp);
801 atomic_store_rel_ptr((uintptr_t *)&pmp->pm_dev->si_mountpt, 0);
802 vrele(pmp->pm_devvp);
803 dev_rel(pmp->pm_dev);
804 free(pmp->pm_inusemap, M_MSDOSFSFAT);
805 lockdestroy(&pmp->pm_fatlock);
806 free(pmp, M_MSDOSFSMNT);
809 mp->mnt_flag &= ~MNT_LOCAL;
815 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp)
817 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
822 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
824 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
832 msdosfs_statfs(struct mount *mp, struct statfs *sbp)
834 struct msdosfsmount *pmp;
836 pmp = VFSTOMSDOSFS(mp);
837 sbp->f_bsize = pmp->pm_bpcluster;
838 sbp->f_iosize = pmp->pm_bpcluster;
839 sbp->f_blocks = pmp->pm_maxcluster + 1;
840 sbp->f_bfree = pmp->pm_freeclustercount;
841 sbp->f_bavail = pmp->pm_freeclustercount;
842 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
843 sbp->f_ffree = 0; /* what to put in here? */
848 * If we have an FSInfo block, update it.
851 msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
857 MSDOSFS_LOCK_MP(pmp);
858 if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
862 error = bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
868 fp = (struct fsinfo *)bp->b_data;
869 putulong(fp->fsinfree, pmp->pm_freeclustercount);
870 putulong(fp->fsinxtfree, pmp->pm_nxtfree);
871 pmp->pm_flags &= ~MSDOSFS_FSIMOD;
872 if (waitfor == MNT_WAIT)
877 MSDOSFS_UNLOCK_MP(pmp);
882 msdosfs_sync(struct mount *mp, int waitfor)
884 struct vnode *vp, *nvp;
887 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
888 int error, allerror = 0;
893 * If we ever switch to not updating all of the FATs all the time,
894 * this would be the place to update them from the first one.
896 if (pmp->pm_fmod != 0) {
897 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
898 panic("msdosfs_sync: rofs mod");
900 /* update FATs here */
904 * Write back each (modified) denode.
907 MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
908 if (vp->v_type == VNON) {
914 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
915 (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
916 waitfor == MNT_LAZY)) {
920 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
926 error = VOP_FSYNC(vp, waitfor, td);
934 * Flush filesystem control info.
936 if (waitfor != MNT_LAZY) {
937 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
938 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
941 VOP_UNLOCK(pmp->pm_devvp, 0);
944 error = msdosfs_fsiflush(pmp, waitfor);
951 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
953 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
954 struct defid *defhp = (struct defid *) fhp;
958 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
964 vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
968 static struct vfsops msdosfs_vfsops = {
969 .vfs_fhtovp = msdosfs_fhtovp,
970 .vfs_mount = msdosfs_mount,
971 .vfs_cmount = msdosfs_cmount,
972 .vfs_root = msdosfs_root,
973 .vfs_statfs = msdosfs_statfs,
974 .vfs_sync = msdosfs_sync,
975 .vfs_unmount = msdosfs_unmount,
978 VFS_SET(msdosfs_vfsops, msdosfs, 0);
979 MODULE_VERSION(msdosfs, 1);