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/nlookup.h>
58 #include <sys/kernel.h>
59 #include <sys/vnode.h>
60 #include <sys/iconv.h>
61 #include <sys/mount.h>
63 #include <sys/fcntl.h>
64 #include <sys/malloc.h>
66 #include <vm/vm_zone.h>
70 #include <vfs/msdosfs/bootsect.h>
71 #include <vfs/msdosfs/bpb.h>
72 #include <vfs/msdosfs/direntry.h>
73 #include <vfs/msdosfs/denode.h>
74 #include <vfs/msdosfs/fat.h>
75 #include <vfs/msdosfs/msdosfsmount.h>
77 extern struct vop_ops msdosfs_vnode_vops;
78 struct iconv_functions *msdosfs_iconv;
80 #define ENCODING_UNICODE "UTF-16BE"
83 * XXX - The boot signature formatted by NEC PC-98 DOS looks like a
84 * garbage or a random value :-{
85 * If you want to use that broken-signatured media, define the
86 * following symbol even though PC/AT.
87 * (ex. mount PC-98 DOS formatted FD on PC/AT)
89 #define MSDOSFS_NOCHECKSIG
92 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
93 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
95 static int mountmsdosfs(struct vnode *devvp, struct mount *mp,
96 struct msdosfs_args *argp);
97 static int msdosfs_root(struct mount *, struct vnode **);
98 static int msdosfs_statfs(struct mount *, struct statfs *, struct ucred *);
99 static int msdosfs_unmount(struct mount *, int);
102 update_mp(struct mount *mp, struct msdosfs_args *argp)
104 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
106 char cs_local[ICONV_CSNMAXLEN];
107 char cs_dos[ICONV_CSNMAXLEN];
109 pmp->pm_gid = argp->gid;
110 pmp->pm_uid = argp->uid;
111 pmp->pm_mask = argp->mask & ALLPERMS;
112 pmp->pm_dirmask = argp->dirmask & ALLPERMS;
113 pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
115 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
116 memcpy(cs_local, argp->cs_local, sizeof(cs_local));
117 memcpy(cs_dos, argp->cs_dos, sizeof(cs_dos));
118 kprintf("local: %s dos: %s\n",argp->cs_local, argp->cs_dos);
119 error = msdosfs_iconv->open(cs_local, ENCODING_UNICODE,
123 error = msdosfs_iconv->open(ENCODING_UNICODE, cs_local,
127 error = msdosfs_iconv->open(cs_dos, cs_local, &pmp->pm_u2d);
130 error = msdosfs_iconv->open(cs_local, cs_dos, &pmp->pm_d2u);
135 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
136 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
138 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
143 * mp - path - addr in user space of mount point (ie /usr or whatever)
144 * data - addr in user space of mount params including the name of the block
145 * special file to treat as a filesystem.
148 msdosfs_mount(struct mount *mp, char *path, caddr_t data, struct ucred *cred)
150 struct vnode *devvp; /* vnode for blk device to mount */
151 struct msdosfs_args args; /* will hold data from mount request */
152 /* msdosfs specific mount control block */
153 struct msdosfsmount *pmp = NULL;
157 struct nlookupdata nd;
159 error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
163 * If updating, check whether changing from read-only to
164 * read/write; if there is no device name, that's all we do.
166 if (mp->mnt_flag & MNT_UPDATE) {
167 pmp = VFSTOMSDOSFS(mp);
169 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
170 (mp->mnt_flag & MNT_RDONLY)) {
172 if (mp->mnt_flag & MNT_FORCE)
174 error = vflush(mp, 0, flags);
176 devvp = pmp->pm_devvp;
177 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
178 VOP_OPEN(devvp, FREAD, FSCRED, NULL);
179 VOP_CLOSE(devvp, FREAD|FWRITE, NULL);
181 pmp->pm_flags |= MSDOSFSMNT_RONLY;
184 if (!error && (mp->mnt_flag & MNT_RELOAD))
185 error = EOPNOTSUPP; /* not yet implemented */
188 if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
189 (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
191 * If upgrade to read-write by non-root, then verify
192 * that user has necessary permissions on the device.
194 devvp = pmp->pm_devvp;
195 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
196 if (cred->cr_uid != 0) {
197 error = VOP_EACCESS(devvp, VREAD | VWRITE,
204 VOP_OPEN(devvp, FREAD|FWRITE, FSCRED, NULL);
205 VOP_CLOSE(devvp, FREAD, NULL);
207 pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
209 if (args.fspec == NULL) {
210 #ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
211 if (args.flags & MSDOSFSMNT_MNTOPT) {
212 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
213 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
214 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
215 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
219 * Process export requests.
221 return (vfs_export(mp, &pmp->pm_export, &args.export));
225 * Not an update, or updating the name: look up the name
226 * and verify that it refers to a sensible disk device.
229 error = nlookup_init(&nd, args.fspec, UIO_USERSPACE, NLC_FOLLOW);
231 error = nlookup(&nd);
233 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
238 if (!vn_isdisk(devvp, &error)) {
243 * If mount by non-root, then verify that user has necessary
244 * permissions on the device.
246 if (cred->cr_uid != 0) {
248 if ((mp->mnt_flag & MNT_RDONLY) == 0)
249 accessmode |= VWRITE;
250 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
251 error = VOP_EACCESS(devvp, accessmode, cred);
258 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
259 error = mountmsdosfs(devvp, mp, &args);
260 #ifdef MSDOSFS_DEBUG /* only needed for the kprintf below */
261 pmp = VFSTOMSDOSFS(mp);
264 if (devvp != pmp->pm_devvp)
265 error = EINVAL; /* XXX needs translation */
274 error = update_mp(mp, &args);
276 if ((mp->mnt_flag & MNT_UPDATE) == 0)
277 msdosfs_unmount(mp, MNT_FORCE);
281 copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
282 memset(mp->mnt_stat.f_mntfromname + size, 0, MNAMELEN - size);
283 msdosfs_statfs(mp, &mp->mnt_stat, cred);
285 kprintf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n",
286 mp, pmp, pmp->pm_inusemap);
292 mountmsdosfs(struct vnode *devvp, struct mount *mp, struct msdosfs_args *argp)
294 struct msdosfsmount *pmp;
297 union bootsector *bsp;
298 struct byte_bpb33 *b33;
299 struct byte_bpb50 *b50;
300 struct byte_bpb710 *b710;
306 * Disallow multiple mounts of the same device.
307 * Disallow mounting of a device that is currently in use
308 * Flush out any old buffers remaining from a previous use.
310 error = vfs_mountedon(devvp);
313 if (vcount(devvp) > 0)
315 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
316 error = vinvalbuf(devvp, V_SAVE, 0, 0);
321 ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
322 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
323 error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, NULL);
328 bp = NULL; /* both used in error_exit */
332 * Read the boot sector of the filesystem, and then check the
333 * boot signature. If not a dos boot sector then error out.
335 * NOTE: 8192 is a magic size that works for ffs.
337 error = bread(devvp, 0, 8192, &bp);
340 bp->b_flags |= B_AGE;
341 bsp = (union bootsector *)bp->b_data;
342 b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
343 b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
344 b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
346 #ifndef MSDOSFS_NOCHECKSIG
347 if (bsp->bs50.bsBootSectSig0 != BOOTSIG0 ||
348 bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
354 pmp = kmalloc(sizeof(*pmp), M_MSDOSFSMNT, M_WAITOK | M_ZERO);
358 * Compute several useful quantities from the bpb in the
359 * bootsector. Copy in the dos 5 variant of the bpb then fix up
360 * the fields that are different between dos 5 and dos 3.3.
362 SecPerClust = b50->bpbSecPerClust;
363 pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
364 if (pmp->pm_BytesPerSec < DEV_BSIZE) {
368 pmp->pm_ResSectors = getushort(b50->bpbResSectors);
369 pmp->pm_FATs = b50->bpbFATs;
370 pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
371 pmp->pm_Sectors = getushort(b50->bpbSectors);
372 pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
373 pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
374 pmp->pm_Heads = getushort(b50->bpbHeads);
375 pmp->pm_Media = b50->bpbMedia;
377 /* calculate the ratio of sector size to DEV_BSIZE */
378 pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
381 * We don't check pm_Heads nor pm_SecPerTrack, because
382 * these may not be set for EFI file systems. We don't
383 * use these anyway, so we're unaffected if they are
386 if (pmp->pm_BytesPerSec == 0 || SecPerClust == 0) {
391 if (pmp->pm_Sectors == 0) {
392 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
393 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
395 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
396 pmp->pm_HugeSectors = pmp->pm_Sectors;
399 if (pmp->pm_RootDirEnts == 0) {
400 if (pmp->pm_FATsecs != 0 || getushort(b710->bpbFSVers) != 0) {
402 kprintf("mountmsdosfs(): bad FAT32 filesystem\n");
405 pmp->pm_fatmask = FAT32_MASK;
408 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
409 if ((getushort(b710->bpbExtFlags) & FATMIRROR) != 0)
410 pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
412 pmp->pm_flags |= MSDOSFS_FATMIRROR;
414 pmp->pm_flags |= MSDOSFS_FATMIRROR;
417 * Check a few values (could do some more):
418 * - logical sector size: power of 2, >= block size
419 * - sectors per cluster: power of 2, >= 1
420 * - number of sectors: >= 1, <= size of partition
421 * - number of FAT sectors: >= 1
423 if (SecPerClust == 0 || (SecPerClust & (SecPerClust - 1)) != 0 ||
424 pmp->pm_BytesPerSec < DEV_BSIZE ||
425 (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1)) != 0 ||
426 pmp->pm_HugeSectors == 0 || pmp->pm_FATsecs == 0 ||
427 SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE) {
432 if ((off_t)pmp->pm_HugeSectors * pmp->pm_BytesPerSec <
433 pmp->pm_HugeSectors /* overflow */) {
434 /* XXX FreeBSD also checks media size in above */
439 pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
440 pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
441 pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
442 SecPerClust *= pmp->pm_BlkPerSec;
444 pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
447 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
448 pmp->pm_firstcluster = pmp->pm_fatblk +
449 pmp->pm_FATs * pmp->pm_FATsecs;
450 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
452 pmp->pm_rootdirblk = pmp->pm_fatblk +
453 pmp->pm_FATs * pmp->pm_FATsecs;
454 pmp->pm_rootdirsize = howmany(pmp->pm_RootDirEnts *
455 sizeof(struct direntry), DEV_BSIZE); /* in blocks */
456 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
459 if (pmp->pm_HugeSectors <= pmp->pm_firstcluster) {
463 pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
466 if (pmp->pm_fatmask == 0) {
467 if (pmp->pm_maxcluster <= ((CLUST_RSRVD - CLUST_FIRST) &
470 * This will usually be a floppy disk. This size makes
471 * sure that one FAT entry will not be split across
474 pmp->pm_fatmask = FAT12_MASK;
478 pmp->pm_fatmask = FAT16_MASK;
484 clusters = ((pmp->pm_FATsecs * DEV_BSIZE) / pmp->pm_fatmult) *
486 if (pmp->pm_maxcluster >= clusters) {
487 kprintf("Warning: number of clusters (%ld) exceeds FAT "
488 "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
489 pmp->pm_maxcluster = clusters - 1;
493 pmp->pm_fatblocksize = 3 * 512;
495 pmp->pm_fatblocksize = PAGE_SIZE;
496 pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
497 pmp->pm_BytesPerSec);
498 pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
499 pmp->pm_bnshift = DEV_BSHIFT;
502 * Compute mask and shift value for isolating cluster relative byte
503 * offsets and cluster numbers from a file offset.
505 pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
506 pmp->pm_crbomask = pmp->pm_bpcluster - 1;
507 pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
510 * Check for valid cluster size
511 * must be a power of 2
513 if ((pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) != 0) {
519 * Release the bootsector buffer.
521 bp->b_flags |= B_RELBUF;
526 * Check the fsinfo sector if we have one. Silently fix up our
527 * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
528 * or too large. Ignore fp->fsinfree for now, since we need to
529 * read the entire FAT anyway to fill the inuse map.
531 if (pmp->pm_fsinfo) {
534 if ((error = bread(devvp, de_bn2doff(pmp, pmp->pm_fsinfo),
535 pmp->pm_BytesPerSec, &bp)) != 0)
537 fp = (struct fsinfo *)bp->b_data;
538 if (!memcmp(fp->fsisig1, "RRaA", 4) &&
539 !memcmp(fp->fsisig2, "rrAa", 4) &&
540 !memcmp(fp->fsisig3, "\0\0\125\252", 4)) {
541 pmp->pm_nxtfree = getulong(fp->fsinxtfree);
542 if (pmp->pm_nxtfree > pmp->pm_maxcluster)
543 pmp->pm_nxtfree = CLUST_FIRST;
546 bp->b_flags |= B_RELBUF;
552 * Finish initializing pmp->pm_nxtfree (just in case the first few
553 * sectors aren't properly reserved in the FAT). This completes
554 * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
555 * value if there is no fsinfo. We will use pmp->pm_nxtfree
556 * internally even if there is no fsinfo.
558 if (pmp->pm_nxtfree < CLUST_FIRST)
559 pmp->pm_nxtfree = CLUST_FIRST;
562 * Allocate memory for the bitmap of allocated clusters, and then
565 pmp->pm_inusemap = kmalloc(howmany(pmp->pm_maxcluster + 1,
566 N_INUSEBITS) * sizeof(*pmp->pm_inusemap), M_MSDOSFSFAT, M_WAITOK);
569 * fillinusemap() needs pm_devvp.
571 pmp->pm_devvp = devvp;
575 * Have the inuse map filled in.
577 MSDOSFS_LOCK_MP(pmp);
578 error = fillinusemap(pmp);
579 MSDOSFS_UNLOCK_MP(pmp);
584 * If they want FAT updates to be synchronous then let them suffer
585 * the performance degradation in exchange for the on disk copy of
586 * the FAT being correct just about all the time. I suppose this
587 * would be a good thing to turn on if the kernel is still flakey.
589 if (mp->mnt_flag & MNT_SYNCHRONOUS)
590 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
596 pmp->pm_flags |= MSDOSFSMNT_RONLY;
599 mp->mnt_data = (qaddr_t) pmp;
600 mp->mnt_stat.f_fsid.val[0] = devid_from_dev(dev);
601 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
602 mp->mnt_flag |= MNT_LOCAL;
603 vfs_add_vnodeops(mp, &msdosfs_vnode_vops, &mp->mnt_vn_norm_ops);
604 dev->si_mountpoint = mp;
610 bp->b_flags |= B_RELBUF;
613 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
614 VOP_CLOSE(devvp, ronly ? FREAD : FREAD | FWRITE, NULL);
617 if (pmp->pm_inusemap)
618 kfree(pmp->pm_inusemap, M_MSDOSFSFAT);
619 kfree(pmp, M_MSDOSFSMNT);
620 mp->mnt_data = (qaddr_t)0;
626 * Unmount the filesystem described by mp.
629 msdosfs_unmount(struct mount *mp, int mntflags)
631 struct msdosfsmount *pmp;
637 if (mntflags & MNT_FORCE)
639 error = vflush(mp, 0, flags);
643 pmp = VFSTOMSDOSFS(mp);
644 pmp->pm_devvp->v_rdev->si_mountpoint = NULL;
646 if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
648 msdosfs_iconv->close(pmp->pm_w2u);
650 msdosfs_iconv->close(pmp->pm_u2w);
652 msdosfs_iconv->close(pmp->pm_d2u);
654 msdosfs_iconv->close(pmp->pm_u2d);
659 kprintf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
660 kprintf("flag %08x, refcnt 0x%08x, writecount %d, auxrefs 0x%08x\n",
661 vp->v_flag, vp->v_refcnt, vp->v_writecount, vp->v_auxrefs);
662 kprintf("mount %p, op %p\n", vp->v_mount, vp->v_ops);
663 kprintf("mount %p\n", vp->v_mount);
664 kprintf("cleanblkhd %p, dirtyblkhd %p, numoutput %d, type %d\n",
665 RB_ROOT(&vp->v_rbclean_tree), RB_ROOT(&vp->v_rbdirty_tree),
666 bio_track_active(&vp->v_track_write), vp->v_type);
667 kprintf("union %p, tag %d, data[0] %08x, data[1] %08x\n",
668 vp->v_socket, vp->v_tag, ((u_int *)vp->v_data)[0],
669 ((u_int *)vp->v_data)[1]);
672 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
673 error = VOP_CLOSE(pmp->pm_devvp,
674 pmp->pm_flags & MSDOSFSMNT_RONLY ? FREAD : FREAD | FWRITE,
676 vn_unlock(pmp->pm_devvp);
677 vrele(pmp->pm_devvp);
678 kfree(pmp->pm_inusemap, M_MSDOSFSFAT);
679 kfree(pmp, M_MSDOSFSMNT);
680 mp->mnt_data = (qaddr_t)0;
681 mp->mnt_flag &= ~MNT_LOCAL;
686 msdosfs_root(struct mount *mp, struct vnode **vpp)
688 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
692 mprintf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
693 error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
701 msdosfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
703 struct msdosfsmount *pmp;
705 pmp = VFSTOMSDOSFS(mp);
706 sbp->f_bsize = pmp->pm_bpcluster;
707 sbp->f_iosize = pmp->pm_bpcluster;
708 sbp->f_blocks = pmp->pm_maxcluster + 1;
709 sbp->f_bfree = pmp->pm_freeclustercount;
710 sbp->f_bavail = pmp->pm_freeclustercount;
711 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
712 sbp->f_ffree = 0; /* what to put in here? */
713 if (sbp != &mp->mnt_stat) {
714 sbp->f_type = mp->mnt_vfc->vfc_typenum;
715 memcpy(sbp->f_mntfromname, mp->mnt_stat.f_mntfromname, MNAMELEN);
717 strncpy(sbp->f_fstypename, mp->mnt_vfc->vfc_name, MFSNAMELEN);
722 msdosfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
724 struct msdosfsmount *pmp;
726 pmp = VFSTOMSDOSFS(mp);
727 sbp->f_bsize = pmp->pm_bpcluster;
728 sbp->f_frsize = pmp->pm_bpcluster;
729 sbp->f_blocks = pmp->pm_maxcluster + 1;
730 sbp->f_bfree = pmp->pm_freeclustercount;
731 sbp->f_bavail = pmp->pm_freeclustercount;
732 sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
733 sbp->f_ffree = 0; /* what to put in here? */
734 if (sbp != &mp->mnt_vstat) {
735 sbp->f_type = mp->mnt_vfc->vfc_typenum;
746 static int msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data);
749 * If we have an FSInfo block, update it.
752 msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
758 MSDOSFS_LOCK_MP(pmp);
759 if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
763 error = bread(pmp->pm_devvp, de_bn2doff(pmp, pmp->pm_fsinfo),
764 pmp->pm_BytesPerSec, &bp);
770 fp = (struct fsinfo *)bp->b_data;
771 putulong(fp->fsinfree, pmp->pm_freeclustercount);
772 putulong(fp->fsinxtfree, pmp->pm_nxtfree);
773 pmp->pm_flags &= ~MSDOSFS_FSIMOD;
774 if (waitfor == MNT_WAIT)
779 MSDOSFS_UNLOCK_MP(pmp);
784 msdosfs_sync(struct mount *mp, int waitfor)
786 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
787 struct scaninfo scaninfo;
791 * If we ever switch to not updating all of the FATs all the time,
792 * this would be the place to update them from the first one.
794 if (pmp->pm_fmod != 0) {
795 if (pmp->pm_flags & MSDOSFSMNT_RONLY) {
796 panic("msdosfs_sync: rofs mod");
798 /* update FATs here */
802 * Write back each (modified) denode.
804 scaninfo.allerror = 0;
806 while (scaninfo.rescan) {
808 vmntvnodescan(mp, VMSC_GETVP|VMSC_NOWAIT, NULL,
809 msdosfs_sync_scan, &scaninfo);
813 * Flush filesystem control info.
815 if ((waitfor & MNT_LAZY) == 0) {
816 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
817 if ((error = VOP_FSYNC(pmp->pm_devvp, waitfor, 0)) != 0)
818 scaninfo.allerror = error;
819 vn_unlock(pmp->pm_devvp);
822 error = msdosfs_fsiflush(pmp, waitfor);
824 scaninfo.allerror = error;
825 return (scaninfo.allerror);
829 msdosfs_sync_scan(struct mount *mp, struct vnode *vp, void *data)
831 struct scaninfo *info = data;
836 if (vp->v_type == VNON || vp->v_type == VBAD ||
838 (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
839 (RB_EMPTY(&vp->v_rbdirty_tree) || (info->waitfor & MNT_LAZY)))) {
842 if ((error = VOP_FSYNC(vp, info->waitfor, 0)) != 0)
843 info->allerror = error;
848 msdosfs_fhtovp(struct mount *mp, struct vnode *rootvp,
849 struct fid *fhp, struct vnode **vpp)
851 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
852 struct defid *defhp = (struct defid *) fhp;
856 error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
866 msdosfs_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
867 struct ucred **credanonp)
869 struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
872 np = vfs_export_lookup(mp, &pmp->pm_export, nam);
875 *exflagsp = np->netc_exflags;
876 *credanonp = &np->netc_anon;
881 msdosfs_vptofh(struct vnode *vp, struct fid *fhp)
887 defhp = (struct defid *)fhp;
888 defhp->defid_len = sizeof(struct defid);
889 defhp->defid_dirclust = dep->de_dirclust;
890 defhp->defid_dirofs = dep->de_diroffset;
891 /* defhp->defid_gen = dep->de_gen; */
895 static struct vfsops msdosfs_vfsops = {
897 .vfs_mount = msdosfs_mount,
898 .vfs_root = msdosfs_root,
899 .vfs_statfs = msdosfs_statfs,
900 .vfs_statvfs = msdosfs_statvfs,
901 .vfs_sync = msdosfs_sync,
902 .vfs_fhtovp = msdosfs_fhtovp,
903 .vfs_checkexp = msdosfs_checkexp,
904 .vfs_vptofh = msdosfs_vptofh,
905 .vfs_init = msdosfs_init,
906 .vfs_uninit = msdosfs_uninit,
907 .vfs_unmount = msdosfs_unmount,
910 VFS_SET(msdosfs_vfsops, msdos, 0);
911 MODULE_VERSION(msdos, 1);