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>
54 #include <sys/namei.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/mount.h>
61 #include <sys/fcntl.h>
62 #include <sys/malloc.h>
63 #include <sys/stat.h> /* defines ALLPERMS */
64 #include <sys/iconv.h>
65 #include <sys/mutex.h>
67 #include <fs/msdosfs/bpb.h>
68 #include <fs/msdosfs/bootsect.h>
69 #include <fs/msdosfs/msdosfsmount.h>
70 #include <fs/msdosfs/direntry.h>
71 #include <fs/msdosfs/denode.h>
72 #include <fs/msdosfs/fat.h>
74 #include <geom/geom.h>
75 #include <geom/geom_vfs.h>
77 #include "opt_msdosfs.h"
79 /* List of mount options we support */
80 static const char *msdosfs_opts[] = {
83 "uid", "gid", "mask", "dirmask",
84 "shortname", "shortnames", "longname", "longnames", "nowin95", "win95",
85 "kiconv", "cs_win", "cs_dos", "cs_local",
89 #define MSDOSFS_DFLTBSIZE 4096
93 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
94 * garbage or a random value :-{
95 * If you want to use that broken-signatured media, define the
96 * following symbol even though PC/AT.
97 * (ex. mount PC-98 DOS formatted FD on PC/AT)
99 #define MSDOSFS_NOCHECKSIG
102 MALLOC_DEFINE(M_MSDOSFSMNT, "msdosfs_mount", "MSDOSFS mount structure");
103 static MALLOC_DEFINE(M_MSDOSFSFAT, "msdosfs_fat", "MSDOSFS file allocation table");
105 struct iconv_functions *msdosfs_iconv = NULL;
107 static int update_mp(struct mount *mp, struct thread *td);
108 static int mountmsdosfs(struct vnode *devvp, struct mount *mp,
110 static vfs_fhtovp_t msdosfs_fhtovp;
111 static vfs_mount_t msdosfs_mount;
112 static vfs_root_t msdosfs_root;
113 static vfs_statfs_t msdosfs_statfs;
114 static vfs_sync_t msdosfs_sync;
115 static vfs_unmount_t msdosfs_unmount;
116 static vfs_vptofh_t msdosfs_vptofh;
118 /* Maximum length of a character set name (arbitrary). */
126 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
127 void *dos, *win, *local;
130 if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
131 if (msdosfs_iconv != NULL) {
132 error = vfs_getopt(mp->mnt_optnew,
133 "cs_win", &win, NULL);
135 error = vfs_getopt(mp->mnt_optnew,
136 "cs_local", &local, NULL);
138 error = vfs_getopt(mp->mnt_optnew,
139 "cs_dos", &dos, NULL);
141 msdosfs_iconv->open(win, local, &pmp->pm_u2w);
142 msdosfs_iconv->open(local, win, &pmp->pm_w2u);
143 msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
144 msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
156 if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
158 if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
160 if (1 == vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v))
161 pmp->pm_mask = v & ALLPERMS;
162 if (1 == vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v))
163 pmp->pm_dirmask = v & ALLPERMS;
164 vfs_flagopt(mp->mnt_optnew, "shortname",
165 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
166 vfs_flagopt(mp->mnt_optnew, "shortnames",
167 &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
168 vfs_flagopt(mp->mnt_optnew, "longname",
169 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
170 vfs_flagopt(mp->mnt_optnew, "longnames",
171 &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
172 vfs_flagopt(mp->mnt_optnew, "kiconv",
173 &pmp->pm_flags, MSDOSFSMNT_KICONV);
175 if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
176 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
178 pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
180 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
181 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
182 else if (!(pmp->pm_flags &
183 (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
184 struct vnode *rootvp;
187 * Try to divine whether to support Win'95 long filenames
190 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
193 msdosfs_root(mp, LK_EXCLUSIVE, &rootvp, td)) != 0)
195 pmp->pm_flags |= findwin95(VTODE(rootvp))
196 ? MSDOSFSMNT_LONGNAME
197 : MSDOSFSMNT_SHORTNAME;
205 msdosfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
207 struct msdosfs_args args;
212 error = copyin(data, &args, sizeof args);
216 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
217 ma = mount_arg(ma, "export", &args.export, sizeof args.export);
218 ma = mount_argf(ma, "uid", "%d", args.uid);
219 ma = mount_argf(ma, "gid", "%d", args.gid);
220 ma = mount_argf(ma, "mask", "%d", args.mask);
221 ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
223 ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
224 ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
225 ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
226 ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
228 ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
229 ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
230 ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
232 error = kernel_mount(ma, flags);
238 * mp - path - addr in user space of mount point (ie /usr or whatever)
239 * data - addr in user space of mount params including the name of the block
240 * special file to treat as a filesystem.
243 msdosfs_mount(struct mount *mp, struct thread *td)
245 struct vnode *devvp; /* vnode for blk device to mount */
246 struct export_args export;
247 /* msdosfs specific mount control block */
248 struct msdosfsmount *pmp = NULL;
249 struct nameidata ndp;
254 if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
258 * If updating, check whether changing from read-only to
259 * read/write; if there is no device name, that's all we do.
261 if (mp->mnt_flag & MNT_UPDATE) {
262 pmp = VFSTOMSDOSFS(mp);
264 error = vfs_copyopt(mp->mnt_optnew, "export",
265 &export, sizeof export);
266 if (error == 0 && export.ex_flags != 0) {
268 * Process export requests.
270 if ((export.ex_flags & MNT_EXPORTED) != 0 &&
271 (pmp->pm_flags & MSDOSFS_LARGEFS) != 0)
273 return (vfs_export(mp, &export));
275 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
276 vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
277 error = VFS_SYNC(mp, MNT_WAIT, td);
281 if (mp->mnt_flag & MNT_FORCE)
283 error = vflush(mp, 0, flags, td);
288 g_access(pmp->pm_cp, 0, -1, 0);
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.
298 devvp = pmp->pm_devvp;
299 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
300 error = VOP_ACCESS(devvp, VREAD | VWRITE,
303 VOP_UNLOCK(devvp, 0, td);
306 VOP_UNLOCK(devvp, 0, td);
310 error = g_access(pmp->pm_cp, 0, 1, 0);
316 /* Now that the volume is modifiable, mark it dirty. */
317 error = markvoldirty(pmp, 1);
321 vfs_flagopt(mp->mnt_optnew, "ro",
322 &pmp->pm_flags, MSDOSFSMNT_RONLY);
323 vfs_flagopt(mp->mnt_optnew, "ro",
324 &mp->mnt_flag, MNT_RDONLY);
325 if (vfs_getopt(mp->mnt_optnew, "from", NULL, NULL)) {
326 #ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
327 if (args.flags & MSDOSFSMNT_MNTOPT) {
328 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
329 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
330 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
331 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
337 * Not an update, or updating the name: look up the name
338 * and verify that it refers to a sensible disk device.
340 if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
342 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
347 NDFREE(&ndp, NDF_ONLY_PNBUF);
349 if (!vn_isdisk(devvp, &error)) {
354 * If mount by non-root, then verify that user has necessary
355 * permissions on the device.
359 if ((mp->mnt_flag & MNT_RDONLY) == 0)
360 accessmode |= VWRITE;
361 error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
367 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
368 error = mountmsdosfs(devvp, mp, td);
369 #ifdef MSDOSFS_DEBUG /* only needed for the printf below */
370 pmp = VFSTOMSDOSFS(mp);
373 if (devvp != pmp->pm_devvp)
374 error = EINVAL; /* XXX needs translation */
383 error = update_mp(mp, td);
385 if ((mp->mnt_flag & MNT_UPDATE) == 0)
386 msdosfs_unmount(mp, MNT_FORCE, td);
390 vfs_mountedfrom(mp, from);
392 printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
398 mountmsdosfs(devvp, mp, td)
403 struct msdosfsmount *pmp;
405 struct cdev *dev = devvp->v_rdev;
406 union bootsector *bsp;
407 struct byte_bpb33 *b33;
408 struct byte_bpb50 *b50;
409 struct byte_bpb710 *b710;
410 u_int8_t SecPerClust;
413 struct g_consumer *cp;
416 ronly = !vfs_getopt(mp->mnt_optnew, "ro", NULL, NULL);
417 /* XXX: use VOP_ACCESS to check FS perms */
420 error = g_vfs_open(devvp, &cp, "msdos", ronly ? 0 : 1);
423 VOP_UNLOCK(devvp, 0, td);
427 bo = &devvp->v_bufobj;
428 bp = NULL; /* both used in error_exit */
432 * Read the boot sector of the filesystem, and then check the
433 * boot signature. If not a dos boot sector then error out.
435 * NOTE: 2048 is a maximum sector size in current...
437 error = bread(devvp, 0, 2048, NOCRED, &bp);
440 bp->b_flags |= B_AGE;
441 bsp = (union bootsector *)bp->b_data;
442 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
443 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
444 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
446 #ifndef MSDOSFS_NOCHECKSIG
447 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
448 || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
454 pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
460 * Compute several useful quantities from the bpb in the
461 * bootsector. Copy in the dos 5 variant of the bpb then fix up
462 * the fields that are different between dos 5 and dos 3.3.
464 SecPerClust = b50->bpbSecPerClust;
465 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
466 if (pmp->pm_BytesPerSec < DEV_BSIZE) {
470 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
471 pmp->pm_FATs = b50->bpbFATs;
472 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
473 pmp->pm_Sectors = getushort(b50->bpbSectors);
474 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
475 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
476 pmp->pm_Heads = getushort(b50->bpbHeads);
477 pmp->pm_Media = b50->bpbMedia;
479 /* calculate the ratio of sector size to DEV_BSIZE */
480 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
482 /* XXX - We should probably check more values here */
483 if (!pmp->pm_BytesPerSec || !SecPerClust
486 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
488 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
494 if (pmp->pm_Sectors == 0) {
495 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
496 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
498 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
499 pmp->pm_HugeSectors = pmp->pm_Sectors;
501 #ifndef MSDOSFS_LARGE
502 if (pmp->pm_HugeSectors > 0xffffffff /
503 (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
505 * We cannot deal currently with this size of disk
506 * due to fileid limitations (see msdosfs_getattr and
510 printf("mountmsdosfs(): disk too big, sorry\n");
513 #endif /* !MSDOSFS_LARGE */
515 if (pmp->pm_RootDirEnts == 0) {
518 || getushort(b710->bpbFSVers)) {
520 printf("mountmsdosfs(): bad FAT32 filesystem\n");
523 pmp->pm_fatmask = FAT32_MASK;
526 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
527 if (getushort(b710->bpbExtFlags) & FATMIRROR)
528 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
530 pmp->pm_flags |= MSDOSFS_FATMIRROR;
532 pmp->pm_flags |= MSDOSFS_FATMIRROR;
535 * Check a few values (could do some more):
536 * - logical sector size: power of 2, >= block size
537 * - sectors per cluster: power of 2, >= 1
538 * - number of sectors: >= 1, <= size of partition
539 * - number of FAT sectors: >= 1
541 if ( (SecPerClust == 0)
542 || (SecPerClust & (SecPerClust - 1))
543 || (pmp->pm_BytesPerSec < DEV_BSIZE)
544 || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
545 || (pmp->pm_HugeSectors == 0)
546 || (pmp->pm_FATsecs == 0)
552 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
553 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
554 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
555 SecPerClust *= pmp->pm_BlkPerSec;
557 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
560 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
561 pmp->pm_firstcluster = pmp->pm_fatblk
562 + (pmp->pm_FATs * pmp->pm_FATsecs);
563 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
565 pmp->pm_rootdirblk = pmp->pm_fatblk +
566 (pmp->pm_FATs * pmp->pm_FATsecs);
567 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
569 / DEV_BSIZE; /* in blocks */
570 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
573 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
575 pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
577 if (pmp->pm_fatmask == 0) {
578 if (pmp->pm_maxcluster
579 <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
581 * This will usually be a floppy disk. This size makes
582 * sure that one fat entry will not be split across
585 pmp->pm_fatmask = FAT12_MASK;
589 pmp->pm_fatmask = FAT16_MASK;
595 clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
596 if (pmp->pm_maxcluster >= clusters) {
597 printf("Warning: number of clusters (%ld) exceeds FAT "
598 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
599 pmp->pm_maxcluster = clusters - 1;
604 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
606 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
608 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
609 pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
612 * Compute mask and shift value for isolating cluster relative byte
613 * offsets and cluster numbers from a file offset.
615 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
616 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
617 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
620 * Check for valid cluster size
621 * must be a power of 2
623 if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
629 * Release the bootsector buffer.
637 if (pmp->pm_fsinfo) {
640 if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
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 && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) {
648 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
649 if (pmp->pm_nxtfree == 0xffffffff)
650 pmp->pm_nxtfree = CLUST_FIRST;
658 * Check and validate (or perhaps invalidate?) the fsinfo structure?
660 if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
662 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
663 pmp->pm_nxtfree, pmp->pm_maxcluster);
669 * Allocate memory for the bitmap of allocated clusters, and then
672 pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
673 * sizeof(*pmp->pm_inusemap),
674 M_MSDOSFSFAT, M_WAITOK);
677 * fillinusemap() needs pm_devvp.
679 pmp->pm_devvp = devvp;
682 * Have the inuse map filled in.
684 if ((error = fillinusemap(pmp)) != 0)
688 * If they want fat updates to be synchronous then let them suffer
689 * the performance degradation in exchange for the on disk copy of
690 * the fat being correct just about all the time. I suppose this
691 * would be a good thing to turn on if the kernel is still flakey.
693 if (mp->mnt_flag & MNT_SYNCHRONOUS)
694 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
700 pmp->pm_flags |= MSDOSFSMNT_RONLY;
702 /* Mark the volume dirty while it is mounted read/write. */
703 if ((error = markvoldirty(pmp, 1)) != 0)
707 mp->mnt_data = (qaddr_t) pmp;
708 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
709 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
710 mp->mnt_flag |= MNT_LOCAL;
713 msdosfs_fileno_init(mp);
729 if (pmp->pm_inusemap)
730 free(pmp->pm_inusemap, M_MSDOSFSFAT);
731 free(pmp, M_MSDOSFSMNT);
732 mp->mnt_data = (qaddr_t)0;
738 * Unmount the filesystem described by mp.
741 msdosfs_unmount(mp, mntflags, td)
746 struct msdosfsmount *pmp;
750 if (mntflags & MNT_FORCE)
752 error = vflush(mp, 0, flags, td);
755 pmp = VFSTOMSDOSFS(mp);
756 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
758 msdosfs_iconv->close(pmp->pm_w2u);
760 msdosfs_iconv->close(pmp->pm_u2w);
762 msdosfs_iconv->close(pmp->pm_d2u);
764 msdosfs_iconv->close(pmp->pm_u2d);
767 /* If the volume was mounted read/write, mark it clean now. */
768 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
769 error = markvoldirty(pmp, 0);
770 if (error && (flags & FORCECLOSE) == 0)
775 struct vnode *vp = pmp->pm_devvp;
779 "msdosfs_umount(): just before calling VOP_CLOSE()\n");
780 printf("freef %p, freeb %p, mount %p\n",
781 TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
783 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
784 TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
785 TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
786 vp->v_bufobj.bo_numoutput, vp->v_type);
792 g_vfs_close(pmp->pm_cp, td);
795 vrele(pmp->pm_devvp);
796 free(pmp->pm_inusemap, M_MSDOSFSFAT);
798 msdosfs_fileno_free(mp);
800 free(pmp, M_MSDOSFSMNT);
801 mp->mnt_data = (qaddr_t)0;
802 mp->mnt_flag &= ~MNT_LOCAL;
807 msdosfs_root(mp, flags, vpp, td)
813 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
818 printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
820 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
828 msdosfs_statfs(mp, sbp, td)
833 struct msdosfsmount *pmp;
835 pmp = VFSTOMSDOSFS(mp);
836 sbp->f_bsize = pmp->pm_bpcluster;
837 sbp->f_iosize = pmp->pm_bpcluster;
838 sbp->f_blocks = pmp->pm_maxcluster + 1;
839 sbp->f_bfree = pmp->pm_freeclustercount;
840 sbp->f_bavail = pmp->pm_freeclustercount;
841 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
842 sbp->f_ffree = 0; /* what to put in here? */
847 msdosfs_sync(mp, waitfor, td)
852 struct vnode *vp, *nvp;
854 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
855 int error, allerror = 0;
858 * If we ever switch to not updating all of the fats all the time,
859 * this would be the place to update them from the first one.
861 if (pmp->pm_fmod != 0) {
862 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
863 panic("msdosfs_sync: rofs mod");
865 /* update fats here */
869 * Write back each (modified) denode.
873 MNT_VNODE_FOREACH(vp, mp, nvp) {
875 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) {
882 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
883 (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
884 waitfor == MNT_LAZY)) {
889 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
896 error = VOP_FSYNC(vp, waitfor, td);
899 VOP_UNLOCK(vp, 0, td);
906 * Flush filesystem control info.
908 if (waitfor != MNT_LAZY) {
909 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
910 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
913 VOP_UNLOCK(pmp->pm_devvp, 0, td);
919 msdosfs_fhtovp(mp, fhp, vpp)
924 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
925 struct defid *defhp = (struct defid *) fhp;
929 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
935 vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
940 msdosfs_vptofh(vp, fhp)
948 defhp = (struct defid *)fhp;
949 defhp->defid_len = sizeof(struct defid);
950 defhp->defid_dirclust = dep->de_dirclust;
951 defhp->defid_dirofs = dep->de_diroffset;
952 /* defhp->defid_gen = dep->de_gen; */
956 static struct vfsops msdosfs_vfsops = {
957 .vfs_fhtovp = msdosfs_fhtovp,
958 .vfs_mount = msdosfs_mount,
959 .vfs_cmount = msdosfs_cmount,
960 .vfs_root = msdosfs_root,
961 .vfs_statfs = msdosfs_statfs,
962 .vfs_sync = msdosfs_sync,
963 .vfs_unmount = msdosfs_unmount,
964 .vfs_vptofh = msdosfs_vptofh,
967 VFS_SET(msdosfs_vfsops, msdosfs, 0);
968 MODULE_VERSION(msdosfs, 1);