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>
56 #include <sys/bufobj.h>
58 #include <sys/fcntl.h>
59 #include <sys/iconv.h>
60 #include <sys/kernel.h>
62 #include <sys/malloc.h>
63 #include <sys/mount.h>
64 #include <sys/mutex.h>
65 #include <sys/namei.h>
68 #include <sys/rwlock.h>
70 #include <sys/taskqueue.h>
71 #include <sys/vnode.h>
73 #include <geom/geom.h>
74 #include <geom/geom_vfs.h>
76 #include <fs/msdosfs/bootsect.h>
77 #include <fs/msdosfs/bpb.h>
78 #include <fs/msdosfs/direntry.h>
79 #include <fs/msdosfs/denode.h>
80 #include <fs/msdosfs/fat.h>
81 #include <fs/msdosfs/msdosfsmount.h>
84 #include <sys/rwlock.h>
87 static const char msdosfs_lock_msg[] = "fatlk";
89 /* Mount options that we support. */
90 static const char *msdosfs_opts[] = {
91 "async", "noatime", "noclusterr", "noclusterw",
92 "export", "force", "from", "sync",
93 "cs_dos", "cs_local", "cs_win", "dirmask",
94 "gid", "kiconv", "longname",
95 "longnames", "mask", "shortname", "shortnames",
96 "uid", "win95", "nowin95",
102 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
103 * garbage or a random value :-{
104 * If you want to use that broken-signatured media, define the
105 * following symbol even though PC/AT.
106 * (ex. mount PC-98 DOS formatted FD on PC/AT)
108 #define MSDOSFS_NOCHECKSIG
111 MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure");
112 static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table");
114 struct iconv_functions *msdosfs_iconv;
116 static int update_mp(struct mount *mp, struct thread *td);
117 static int mountmsdosfs(struct vnode *devvp, struct mount *mp);
118 static void msdosfs_remount_ro(void *arg, int pending);
119 static vfs_fhtovp_t msdosfs_fhtovp;
120 static vfs_mount_t msdosfs_mount;
121 static vfs_root_t msdosfs_root;
122 static vfs_statfs_t msdosfs_statfs;
123 static vfs_sync_t msdosfs_sync;
124 static vfs_unmount_t msdosfs_unmount;
126 /* Maximum length of a character set name (arbitrary). */
130 update_mp(struct mount *mp, struct thread *td)
132 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
133 void *dos, *win, *local;
136 if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
137 if (msdosfs_iconv != NULL) {
138 error = vfs_getopt(mp->mnt_optnew,
139 "cs_win", &win, NULL);
141 error = vfs_getopt(mp->mnt_optnew,
142 "cs_local", &local, NULL);
144 error = vfs_getopt(mp->mnt_optnew,
145 "cs_dos", &dos, NULL);
147 msdosfs_iconv->open(win, local, &pmp->pm_u2w);
148 msdosfs_iconv->open(local, win, &pmp->pm_w2u);
149 msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
150 msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
162 if (vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v) == 1)
164 if (vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v) == 1)
166 if (vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v) == 1)
167 pmp->pm_mask = v & ALLPERMS;
168 if (vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v) == 1)
169 pmp->pm_dirmask = v & ALLPERMS;
170 vfs_flagopt(mp->mnt_optnew, "shortname",
171 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
172 vfs_flagopt(mp->mnt_optnew, "shortnames",
173 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
174 vfs_flagopt(mp->mnt_optnew, "longname",
175 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
176 vfs_flagopt(mp->mnt_optnew, "longnames",
177 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
178 vfs_flagopt(mp->mnt_optnew, "kiconv",
179 &pmp->pm_flags, MSDOSFSMNT_KICONV);
181 if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
182 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
184 pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
186 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
187 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
189 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
194 msdosfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
196 struct msdosfs_args args;
201 error = copyin(data, &args, sizeof args);
205 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
206 ma = mount_arg(ma, "export", &args.export, sizeof(args.export));
207 ma = mount_argf(ma, "uid", "%d", args.uid);
208 ma = mount_argf(ma, "gid", "%d", args.gid);
209 ma = mount_argf(ma, "mask", "%d", args.mask);
210 ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
212 ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
213 ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
214 ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
215 ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
217 ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
218 ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
219 ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
221 error = kernel_mount(ma, flags);
227 * mp - path - addr in user space of mount point (ie /usr or whatever)
228 * data - addr in user space of mount params including the name of the block
229 * special file to treat as a filesystem.
232 msdosfs_mount(struct mount *mp)
234 struct vnode *devvp, *odevvp; /* vnode for blk device to mount */
236 /* msdosfs specific mount control block */
237 struct msdosfsmount *pmp = NULL;
238 struct nameidata ndp;
244 if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
248 * If updating, check whether changing from read-only to
249 * read/write; if there is no device name, that's all we do.
251 if (mp->mnt_flag & MNT_UPDATE) {
252 pmp = VFSTOMSDOSFS(mp);
253 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
254 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
255 if ((error = vn_start_write(NULL, &mp, V_WAIT)) != 0)
257 error = vfs_write_suspend_umnt(mp);
262 if (mp->mnt_flag & MNT_FORCE)
264 error = vflush(mp, 0, flags, td);
266 vfs_write_resume(mp, 0);
271 * Now the volume is clean. Mark it so while the
272 * device is still rw.
274 error = markvoldirty(pmp, 0);
276 vfs_write_resume(mp, 0);
277 (void)markvoldirty(pmp, 1);
281 /* Downgrade the device from rw to ro. */
283 error = g_access(pmp->pm_cp, 0, -1, 0);
286 vfs_write_resume(mp, 0);
287 (void)markvoldirty(pmp, 1);
292 * Backing out after an error was painful in the
293 * above. Now we are committed to succeeding.
296 pmp->pm_flags |= MSDOSFSMNT_RONLY;
298 mp->mnt_flag |= MNT_RDONLY;
300 vfs_write_resume(mp, 0);
301 } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
302 !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
304 * If upgrade to read-write by non-root, then verify
305 * that user has necessary permissions on the device.
307 odevvp = pmp->pm_odevvp;
308 vn_lock(odevvp, LK_EXCLUSIVE | LK_RETRY);
309 error = VOP_ACCESS(odevvp, VREAD | VWRITE,
312 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
319 error = g_access(pmp->pm_cp, 0, 1, 0);
324 /* Now that the volume is modifiable, mark it dirty. */
325 error = markvoldirty_upgrade(pmp, true, true);
328 * If dirtying the superblock failed, drop GEOM
329 * 'w' refs (we're still RO).
332 (void)g_access(pmp->pm_cp, 0, -1, 0);
339 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
341 mp->mnt_flag &= ~MNT_RDONLY;
346 * Avoid namei() below. The "from" option is not set.
347 * Update of the devvp is pointless for this case.
349 if ((pmp->pm_flags & MSDOSFS_ERR_RO) != 0)
353 * Not an update, or updating the name: look up the name
354 * and verify that it refers to a sensible disk device.
356 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
358 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from);
365 if (!vn_isdisk_error(devvp, &error)) {
370 * If mount by non-root, then verify that user has necessary
371 * permissions on the device.
374 if ((mp->mnt_flag & MNT_RDONLY) == 0)
376 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
378 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
383 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
384 error = mountmsdosfs(devvp, mp);
385 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */
386 pmp = VFSTOMSDOSFS(mp);
390 if (devvp != pmp->pm_odevvp)
391 return (EINVAL); /* XXX needs translation */
398 error = update_mp(mp, td);
400 if ((mp->mnt_flag & MNT_UPDATE) == 0)
401 msdosfs_unmount(mp, MNT_FORCE);
405 vfs_mountedfrom(mp, from);
407 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
413 mountmsdosfs(struct vnode *odevvp, struct mount *mp)
415 struct msdosfsmount *pmp;
419 union bootsector *bsp;
420 struct byte_bpb33 *b33;
421 struct byte_bpb50 *b50;
422 struct byte_bpb710 *b710;
426 struct g_consumer *cp;
429 bp = NULL; /* This and pmp both used in error_exit. */
431 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
433 devvp = mntfs_allocvp(mp, odevvp);
435 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
437 if (atomic_cmpset_acq_ptr((uintptr_t *)&dev->si_mountpt, 0,
438 (uintptr_t)mp) == 0) {
443 error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
446 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
451 bo = &devvp->v_bufobj;
452 BO_LOCK(&odevvp->v_bufobj);
453 odevvp->v_bufobj.bo_flag |= BO_NOBUFS;
454 BO_UNLOCK(&odevvp->v_bufobj);
456 if (dev->si_iosize_max != 0)
457 mp->mnt_iosize_max = dev->si_iosize_max;
458 if (mp->mnt_iosize_max > maxphys)
459 mp->mnt_iosize_max = maxphys;
462 * Read the boot sector of the filesystem, and then check the
463 * boot signature. If not a dos boot sector then error out.
465 * NOTE: 8192 is a magic size that works for ffs.
467 error = bread(devvp, 0, 8192, NOCRED, &bp);
470 bp->b_flags |= B_AGE;
471 bsp = (union bootsector *)bp->b_data;
472 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
473 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
474 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
476 #ifndef MSDOSFS_NOCHECKSIG
477 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 ||
478 bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
484 pmp = malloc(sizeof(*pmp), M_MSDOSFSMNT, M_WAITOK | M_ZERO);
489 lockinit(&pmp->pm_fatlock, 0, msdosfs_lock_msg, 0, 0);
490 lockinit(&pmp->pm_checkpath_lock, 0, "msdoscp", 0, 0);
492 TASK_INIT(&pmp->pm_rw2ro_task, 0, msdosfs_remount_ro, pmp);
495 * Initialize ownerships and permissions, since nothing else will
496 * initialize them iff we are mounting root.
498 pmp->pm_uid = UID_ROOT;
499 pmp->pm_gid = GID_WHEEL;
500 pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
501 S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
504 * Compute several useful quantities from the bpb in the
505 * bootsector. Copy in the dos 5 variant of the bpb then fix up
506 * the fields that are different between dos 5 and dos 3.3.
508 SecPerClust = b50->bpbSecPerClust;
509 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
510 if (pmp->pm_BytesPerSec < DEV_BSIZE) {
514 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
515 pmp->pm_FATs = b50->bpbFATs;
516 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
517 pmp->pm_Sectors = getushort(b50->bpbSectors);
518 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
519 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
520 pmp->pm_Heads = getushort(b50->bpbHeads);
521 pmp->pm_Media = b50->bpbMedia;
523 /* calculate the ratio of sector size to DEV_BSIZE */
524 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
527 * We don't check pm_Heads nor pm_SecPerTrack, because
528 * these may not be set for EFI file systems. We don't
529 * use these anyway, so we're unaffected if they are
532 if (pmp->pm_BytesPerSec == 0 || SecPerClust == 0) {
537 if (pmp->pm_Sectors == 0) {
538 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
539 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
541 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
542 pmp->pm_HugeSectors = pmp->pm_Sectors;
545 if (pmp->pm_RootDirEnts == 0) {
546 if (pmp->pm_FATsecs != 0 || getushort(b710->bpbFSVers) != 0) {
549 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) != 0)
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 || (SecPerClust & (SecPerClust - 1)) != 0 ||
572 pmp->pm_BytesPerSec < DEV_BSIZE ||
573 (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) != 0 ||
574 pmp->pm_HugeSectors == 0 || pmp->pm_FATsecs == 0 ||
575 SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE) {
580 if ((off_t)pmp->pm_HugeSectors * pmp->pm_BytesPerSec <
581 pmp->pm_HugeSectors /* overflow */ ||
582 (off_t)pmp->pm_HugeSectors * pmp->pm_BytesPerSec >
583 cp->provider->mediasize /* past end of vol */) {
588 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
589 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
590 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
591 SecPerClust *= pmp->pm_BlkPerSec;
593 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
596 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
597 pmp->pm_firstcluster = pmp->pm_fatblk +
598 pmp->pm_FATs * pmp->pm_FATsecs;
599 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
601 pmp->pm_rootdirblk = pmp->pm_fatblk +
602 pmp->pm_FATs * pmp->pm_FATsecs;
603 pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts *
604 sizeof(struct direntry), DEV_BSIZE); /* in blocks */
605 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
608 if (pmp->pm_HugeSectors <= pmp->pm_firstcluster) {
612 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
614 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
616 if (pmp->pm_fatmask == 0) {
617 if (pmp->pm_maxcluster <= ((CLUST_RSRVD - CLUST_FIRST) &
620 * This will usually be a floppy disk. This size makes
621 * sure that one FAT entry will not be split across
624 pmp->pm_fatmask = FAT12_MASK;
628 pmp->pm_fatmask = FAT16_MASK;
634 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
635 if (pmp->pm_maxcluster >= clusters) {
637 printf("Warning: number of clusters (%ld) exceeds FAT "
638 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
640 pmp->pm_maxcluster = clusters - 1;
644 pmp->pm_fatblocksize = 3 * 512;
646 pmp->pm_fatblocksize = PAGE_SIZE;
647 pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
648 pmp->pm_BytesPerSec);
649 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
650 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
653 * Compute mask and shift value for isolating cluster relative byte
654 * offsets and cluster numbers from a file offset.
656 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
657 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
658 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
661 * Check for valid cluster size
662 * must be a power of 2
664 if ((pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) != 0) {
670 * Release the bootsector buffer.
676 * Check the fsinfo sector if we have one. Silently fix up our
677 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
678 * or too large. Ignore fp->fsinfree for now, since we need to
679 * read the entire FAT anyway to fill the inuse map.
681 if (pmp->pm_fsinfo) {
684 if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
687 fp = (struct fsinfo *)bp->b_data;
688 if (!bcmp(fp->fsisig1, "RRaA", 4) &&
689 !bcmp(fp->fsisig2, "rrAa", 4) &&
690 !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
691 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
692 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
693 pmp->pm_nxtfree = CLUST_FIRST;
701 * Finish initializing pmp->pm_nxtfree (just in case the first few
702 * sectors aren't properly reserved in the FAT). This completes
703 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
704 * value if there is no fsinfo. We will use pmp->pm_nxtfree
705 * internally even if there is no fsinfo.
707 if (pmp->pm_nxtfree < CLUST_FIRST)
708 pmp->pm_nxtfree = CLUST_FIRST;
711 * Allocate memory for the bitmap of allocated clusters, and then
714 pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1,
715 N_INUSEBITS) * sizeof(*pmp->pm_inusemap), M_MSDOSFSFAT, M_WAITOK);
718 * fillinusemap() needs pm_devvp.
720 pmp->pm_devvp = devvp;
721 pmp->pm_odevvp = odevvp;
725 * Have the inuse map filled in.
727 MSDOSFS_LOCK_MP(pmp);
728 error = fillinusemap(pmp);
729 MSDOSFS_UNLOCK_MP(pmp);
734 * If they want FAT updates to be synchronous then let them suffer
735 * the performance degradation in exchange for the on disk copy of
736 * the FAT being correct just about all the time. I suppose this
737 * would be a good thing to turn on if the kernel is still flakey.
739 if (mp->mnt_flag & MNT_SYNCHRONOUS)
740 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
746 pmp->pm_flags |= MSDOSFSMNT_RONLY;
748 if ((error = markvoldirty(pmp, 1)) != 0)
753 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
754 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
756 mp->mnt_flag |= MNT_LOCAL;
757 mp->mnt_kern_flag |= MNTK_USES_BCACHE | MNTK_NO_IOPF;
771 lockdestroy(&pmp->pm_fatlock);
772 lockdestroy(&pmp->pm_checkpath_lock);
773 free(pmp->pm_inusemap, M_MSDOSFSFAT);
774 free(pmp, M_MSDOSFSMNT);
777 BO_LOCK(&odevvp->v_bufobj);
778 odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
779 BO_UNLOCK(&odevvp->v_bufobj);
780 atomic_store_rel_ptr((uintptr_t *)&dev->si_mountpt, 0);
781 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
788 * Unmount the filesystem described by mp.
791 msdosfs_unmount(struct mount *mp, int mntflags)
793 struct msdosfsmount *pmp;
798 pmp = VFSTOMSDOSFS(mp);
799 susp = (pmp->pm_flags & MSDOSFSMNT_RONLY) == 0;
802 error = vfs_write_suspend_umnt(mp);
807 if ((mntflags & MNT_FORCE) != 0)
809 error = vflush(mp, 0, flags, curthread);
810 if (error != 0 && error != ENXIO) {
812 vfs_write_resume(mp, VR_START_WRITE);
816 error = markvoldirty(pmp, 0);
817 if (error != 0 && error != ENXIO) {
819 vfs_write_resume(mp, VR_START_WRITE);
820 (void)markvoldirty(pmp, 1);
824 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
826 msdosfs_iconv->close(pmp->pm_w2u);
828 msdosfs_iconv->close(pmp->pm_u2w);
830 msdosfs_iconv->close(pmp->pm_d2u);
832 msdosfs_iconv->close(pmp->pm_u2d);
837 struct vnode *vp = pmp->pm_devvp;
844 "msdosfs_umount(): just before calling VOP_CLOSE()\n");
845 printf("freef %p, freeb %p, mount %p\n",
846 TAILQ_NEXT(vp, v_vnodelist), vp->v_vnodelist.tqe_prev,
848 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
849 TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
850 TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
851 vp->v_bufobj.bo_numoutput, vp->v_type);
857 vfs_write_resume(mp, VR_START_WRITE);
859 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
861 g_vfs_close(pmp->pm_cp);
863 BO_LOCK(&pmp->pm_odevvp->v_bufobj);
864 pmp->pm_odevvp->v_bufobj.bo_flag &= ~BO_NOBUFS;
865 BO_UNLOCK(&pmp->pm_odevvp->v_bufobj);
866 atomic_store_rel_ptr((uintptr_t *)&pmp->pm_dev->si_mountpt, 0);
867 mntfs_freevp(pmp->pm_devvp);
868 vrele(pmp->pm_odevvp);
869 dev_rel(pmp->pm_dev);
870 free(pmp->pm_inusemap, M_MSDOSFSFAT);
871 lockdestroy(&pmp->pm_fatlock);
872 lockdestroy(&pmp->pm_checkpath_lock);
873 free(pmp, M_MSDOSFSMNT);
876 mp->mnt_flag &= ~MNT_LOCAL;
882 msdosfs_remount_ro(void *arg, int pending)
884 struct msdosfsmount *pmp;
889 MSDOSFS_LOCK_MP(pmp);
890 if ((pmp->pm_flags & MSDOSFS_ERR_RO) != 0) {
891 while ((pmp->pm_flags & MSDOSFS_ERR_RO) != 0)
892 msleep(&pmp->pm_flags, &pmp->pm_fatlock, PVFS,
894 } else if ((pmp->pm_mountp->mnt_flag & MNT_RDONLY) == 0) {
895 pmp->pm_flags |= MSDOSFS_ERR_RO;
896 MSDOSFS_UNLOCK_MP(pmp);
897 printf("%s: remounting read-only due to corruption\n",
898 pmp->pm_mountp->mnt_stat.f_mntfromname);
899 error = vfs_remount_ro(pmp->pm_mountp);
901 printf("%s: remounting read-only failed: error %d\n",
902 pmp->pm_mountp->mnt_stat.f_mntfromname, error);
904 printf("remounted %s read-only\n",
905 pmp->pm_mountp->mnt_stat.f_mntfromname);
906 MSDOSFS_LOCK_MP(pmp);
907 pmp->pm_flags &= ~MSDOSFS_ERR_RO;
908 wakeup(&pmp->pm_flags);
910 MSDOSFS_UNLOCK_MP(pmp);
912 vfs_unbusy(pmp->pm_mountp);
916 msdosfs_integrity_error(struct msdosfsmount *pmp)
920 error = vfs_busy(pmp->pm_mountp, MBF_NOWAIT);
922 taskqueue_enqueue(taskqueue_thread, &pmp->pm_rw2ro_task);
924 printf("%s: integrity error busying failed, error %d\n",
925 pmp->pm_mountp->mnt_stat.f_mntfromname, error);
929 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp)
931 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
936 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
938 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, LK_EXCLUSIVE, &ndep);
946 msdosfs_statfs(struct mount *mp, struct statfs *sbp)
948 struct msdosfsmount *pmp;
950 pmp = VFSTOMSDOSFS(mp);
951 sbp->f_bsize = pmp->pm_bpcluster;
952 sbp->f_iosize = pmp->pm_bpcluster;
953 sbp->f_blocks = pmp->pm_maxcluster + 1;
954 sbp->f_bfree = pmp->pm_freeclustercount;
955 sbp->f_bavail = pmp->pm_freeclustercount;
956 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
957 sbp->f_ffree = 0; /* what to put in here? */
962 * If we have an FSInfo block, update it.
965 msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
971 MSDOSFS_LOCK_MP(pmp);
972 if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
976 error = bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
981 fp = (struct fsinfo *)bp->b_data;
982 putulong(fp->fsinfree, pmp->pm_freeclustercount);
983 putulong(fp->fsinxtfree, pmp->pm_nxtfree);
984 pmp->pm_flags &= ~MSDOSFS_FSIMOD;
985 if (waitfor == MNT_WAIT)
990 MSDOSFS_UNLOCK_MP(pmp);
995 msdosfs_sync(struct mount *mp, int waitfor)
997 struct vnode *vp, *nvp;
1000 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1001 int error, allerror = 0;
1006 * If we ever switch to not updating all of the FATs all the time,
1007 * this would be the place to update them from the first one.
1009 if (pmp->pm_fmod != 0) {
1010 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1011 panic("msdosfs_sync: rofs mod");
1013 /* update FATs here */
1017 * Write back each (modified) denode.
1020 MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
1021 if (vp->v_type == VNON) {
1027 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
1028 (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
1029 waitfor == MNT_LAZY)) {
1033 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK);
1035 if (error == ENOENT) {
1036 MNT_VNODE_FOREACH_ALL_ABORT(mp, nvp);
1041 error = VOP_FSYNC(vp, waitfor, td);
1049 * Flush filesystem control info.
1051 if (waitfor != MNT_LAZY) {
1052 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
1053 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
1056 VOP_UNLOCK(pmp->pm_devvp);
1059 error = msdosfs_fsiflush(pmp, waitfor);
1063 if (allerror == 0 && waitfor == MNT_SUSPEND) {
1065 mp->mnt_kern_flag |= MNTK_SUSPEND2 | MNTK_SUSPENDED;
1072 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
1074 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1075 struct defid *defhp = (struct defid *) fhp;
1079 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs,
1080 LK_EXCLUSIVE, &dep);
1086 vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
1090 static struct vfsops msdosfs_vfsops = {
1091 .vfs_fhtovp = msdosfs_fhtovp,
1092 .vfs_mount = msdosfs_mount,
1093 .vfs_cmount = msdosfs_cmount,
1094 .vfs_root = msdosfs_root,
1095 .vfs_statfs = msdosfs_statfs,
1096 .vfs_sync = msdosfs_sync,
1097 .vfs_unmount = msdosfs_unmount,
1100 VFS_SET(msdosfs_vfsops, msdosfs, 0);
1101 MODULE_VERSION(msdosfs, 1);