freebsd-dev/sys/fs/msdosfs/msdosfs_vfsops.c

933 lines
25 KiB
C
Raw Normal View History

1999-08-28 01:08:13 +00:00
/* $FreeBSD$ */
/* $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $ */
1994-09-19 15:41:57 +00:00
/*-
* Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
* Copyright (C) 1994, 1995, 1997 TooLs GmbH.
1994-09-19 15:41:57 +00:00
* All rights reserved.
* Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by TooLs GmbH.
* 4. The name of TooLs GmbH may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Written by Paul Popelka (paulp@uts.amdahl.com)
1995-05-30 08:16:23 +00:00
*
1994-09-19 15:41:57 +00:00
* You can do anything you want with this software, just don't say you wrote
* it, and don't remove this notice.
1995-05-30 08:16:23 +00:00
*
1994-09-19 15:41:57 +00:00
* This software is provided "as is".
1995-05-30 08:16:23 +00:00
*
1994-09-19 15:41:57 +00:00
* The author supplies this software to be publicly redistributed on the
* understanding that the author is not responsible for the correct
* functioning of this software in any circumstances and is not liable for
* any damages caused by this software.
1995-05-30 08:16:23 +00:00
*
1994-09-19 15:41:57 +00:00
* October 1992
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
1994-09-19 15:41:57 +00:00
#include <sys/namei.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/bio.h>
1994-09-19 15:41:57 +00:00
#include <sys/buf.h>
#include <sys/fcntl.h>
1994-09-19 15:41:57 +00:00
#include <sys/malloc.h>
#include <sys/stat.h> /* defines ALLPERMS */
#include <sys/iconv.h>
#include <sys/mutex.h>
#include <fs/msdosfs/bpb.h>
#include <fs/msdosfs/bootsect.h>
#include <fs/msdosfs/msdosfsmount.h>
#include <fs/msdosfs/direntry.h>
#include <fs/msdosfs/denode.h>
#include <fs/msdosfs/fat.h>
1994-09-19 15:41:57 +00:00
#include <geom/geom.h>
#include <geom/geom_vfs.h>
#include "opt_msdosfs.h"
#define MSDOSFS_DFLTBSIZE 4096
#if 1 /*def PC98*/
/*
* XXX - The boot signature formatted by NEC PC-98 DOS looks like a
* garbage or a random value :-{
* If you want to use that broken-signatured media, define the
* following symbol even though PC/AT.
* (ex. mount PC-98 DOS formatted FD on PC/AT)
*/
#define MSDOSFS_NOCHECKSIG
#endif
MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
struct iconv_functions *msdosfs_iconv = NULL;
static int update_mp(struct mount *mp, struct msdosfs_args *argp,
struct thread *td);
2002-03-19 22:20:14 +00:00
static int mountmsdosfs(struct vnode *devvp, struct mount *mp,
struct thread *td, struct msdosfs_args *argp);
static vfs_fhtovp_t msdosfs_fhtovp;
static vfs_omount_t msdosfs_omount;
static vfs_root_t msdosfs_root;
static vfs_statfs_t msdosfs_statfs;
static vfs_sync_t msdosfs_sync;
static vfs_unmount_t msdosfs_unmount;
static vfs_vptofh_t msdosfs_vptofh;
/* Maximum length of a character set name (arbitrary). */
#define MAXCSLEN 64
static int
update_mp(mp, argp, td)
struct mount *mp;
struct msdosfs_args *argp;
struct thread *td;
{
struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
char *dos, *win, *local;
int error;
if (argp->flags & MSDOSFSMNT_KICONV) {
if (msdosfs_iconv) {
win = malloc(MAXCSLEN, M_TEMP, M_WAITOK);
local = malloc(MAXCSLEN, M_TEMP, M_WAITOK);
dos = malloc(MAXCSLEN, M_TEMP, M_WAITOK);
if ((error = copyinstr(argp->cs_win, win, MAXCSLEN,
NULL)) != 0)
goto iconvdone;
if ((error = copyinstr(argp->cs_local, local, MAXCSLEN,
NULL)) != 0)
goto iconvdone;
if ((error = copyinstr(argp->cs_dos, dos, MAXCSLEN,
NULL)) != 0)
goto iconvdone;
msdosfs_iconv->open(win, local, &pmp->pm_u2w);
msdosfs_iconv->open(local, win, &pmp->pm_w2u);
msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
iconvdone: free(win, M_TEMP);
free(local, M_TEMP);
free(dos, M_TEMP);
if (error != 0)
return (error);
} else {
pmp->pm_w2u = NULL;
pmp->pm_u2w = NULL;
pmp->pm_d2u = NULL;
pmp->pm_u2d = NULL;
}
}
pmp->pm_gid = argp->gid;
pmp->pm_uid = argp->uid;
pmp->pm_mask = argp->mask & ALLPERMS;
pmp->pm_dirmask = argp->dirmask & ALLPERMS;
pmp->pm_flags |= argp->flags & MSDOSFSMNT_MNTOPT;
if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
else if (!(pmp->pm_flags &
(MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
struct vnode *rootvp;
/*
* Try to divine whether to support Win'95 long filenames
*/
if (FAT32(pmp))
pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
else {
if ((error = msdosfs_root(mp, &rootvp, td)) != 0)
return error;
pmp->pm_flags |= findwin95(VTODE(rootvp))
? MSDOSFSMNT_LONGNAME
: MSDOSFSMNT_SHORTNAME;
vput(rootvp);
}
}
return 0;
}
1994-09-19 15:41:57 +00:00
/*
1995-05-30 08:16:23 +00:00
* mp - path - addr in user space of mount point (ie /usr or whatever)
1994-09-19 15:41:57 +00:00
* data - addr in user space of mount params including the name of the block
1995-05-30 08:16:23 +00:00
* special file to treat as a filesystem.
1994-09-19 15:41:57 +00:00
*/
1995-11-07 14:06:45 +00:00
static int
msdosfs_omount(mp, path, data, td)
1994-09-19 15:41:57 +00:00
struct mount *mp;
char *path;
caddr_t data;
struct thread *td;
1994-09-19 15:41:57 +00:00
{
struct vnode *devvp; /* vnode for blk device to mount */
struct msdosfs_args args; /* will hold data from mount request */
/* msdosfs specific mount control block */
struct msdosfsmount *pmp = NULL;
struct nameidata ndp;
size_t size;
1994-09-19 15:41:57 +00:00
int error, flags;
mode_t accessmode;
1994-09-19 15:41:57 +00:00
error = copyin(data, (caddr_t)&args, sizeof(struct msdosfs_args));
if (error)
return (error);
if (args.magic != MSDOSFS_ARGSMAGIC)
args.flags = 0;
1994-09-19 15:41:57 +00:00
/*
* If updating, check whether changing from read-only to
* read/write; if there is no device name, that's all we do.
1994-09-19 15:41:57 +00:00
*/
if (mp->mnt_flag & MNT_UPDATE) {
pmp = VFSTOMSDOSFS(mp);
1994-09-19 15:41:57 +00:00
error = 0;
if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
(mp->mnt_flag & MNT_RDONLY)) {
error = VFS_SYNC(mp, MNT_WAIT, td->td_ucred, td);
if (error)
return (error);
1994-09-19 15:41:57 +00:00
flags = WRITECLOSE;
if (mp->mnt_flag & MNT_FORCE)
flags |= FORCECLOSE;
error = vflush(mp, 0, flags, td);
DROP_GIANT();
g_topology_lock();
g_access(pmp->pm_cp, 0, -1, 0);
g_topology_unlock();
PICKUP_GIANT();
1994-09-19 15:41:57 +00:00
}
if (!error && (mp->mnt_flag & MNT_RELOAD))
/* not yet implemented */
error = EOPNOTSUPP;
1994-09-19 15:41:57 +00:00
if (error)
return (error);
if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
(mp->mnt_kern_flag & MNTK_WANTRDWR)) {
/*
* If upgrade to read-write by non-root, then verify
* that user has necessary permissions on the device.
*/
if (suser(td)) {
devvp = pmp->pm_devvp;
vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
error = VOP_ACCESS(devvp, VREAD | VWRITE,
td->td_ucred, td);
if (error) {
VOP_UNLOCK(devvp, 0, td);
return (error);
}
VOP_UNLOCK(devvp, 0, td);
}
DROP_GIANT();
g_topology_lock();
error = g_access(pmp->pm_cp, 0, 1, 0);
g_topology_unlock();
PICKUP_GIANT();
if (error)
return (error);
pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
/* Now that the volume is modifiable, mark it dirty. */
error = markvoldirty(pmp, 1);
if (error)
return (error);
}
1994-09-19 15:41:57 +00:00
if (args.fspec == 0) {
#ifdef __notyet__ /* doesn't work correctly with current mountd XXX */
if (args.flags & MSDOSFSMNT_MNTOPT) {
pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
}
#endif
1994-09-19 15:41:57 +00:00
/*
* Process export requests.
*/
if ((args.export.ex_flags & MNT_EXPORTED) != 0 &&
(pmp->pm_flags & MSDOSFS_LARGEFS) != 0)
return (EOPNOTSUPP);
return (vfs_export(mp, &args.export));
1994-09-19 15:41:57 +00:00
}
}
/*
* Not an update, or updating the name: look up the name
* and verify that it refers to a sensible disk device.
1994-09-19 15:41:57 +00:00
*/
NDINIT(&ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
error = namei(&ndp);
if (error)
return (error);
devvp = ndp.ni_vp;
NDFREE(&ndp, NDF_ONLY_PNBUF);
if (!vn_isdisk(devvp, &error)) {
1994-09-19 15:41:57 +00:00
vrele(devvp);
return (error);
1994-09-19 15:41:57 +00:00
}
/*
* If mount by non-root, then verify that user has necessary
* permissions on the device.
1994-09-19 15:41:57 +00:00
*/
if (suser(td)) {
accessmode = VREAD;
if ((mp->mnt_flag & MNT_RDONLY) == 0)
accessmode |= VWRITE;
vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
if (error) {
vput(devvp);
return (error);
}
VOP_UNLOCK(devvp, 0, td);
}
if ((mp->mnt_flag & MNT_UPDATE) == 0) {
error = mountmsdosfs(devvp, mp, td, &args);
#ifdef MSDOSFS_DEBUG /* only needed for the printf below */
pmp = VFSTOMSDOSFS(mp);
#endif
} else {
1994-09-19 15:41:57 +00:00
if (devvp != pmp->pm_devvp)
error = EINVAL; /* XXX needs translation */
1994-09-19 15:41:57 +00:00
else
vrele(devvp);
}
if (error) {
vrele(devvp);
return (error);
}
error = update_mp(mp, &args, td);
if (error) {
if ((mp->mnt_flag & MNT_UPDATE) == 0)
msdosfs_unmount(mp, MNT_FORCE, td);
1994-09-19 15:41:57 +00:00
return error;
}
(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
&size);
bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
(void) msdosfs_statfs(mp, &mp->mnt_stat, td);
1994-09-19 15:41:57 +00:00
#ifdef MSDOSFS_DEBUG
printf("msdosfs_omount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
1994-09-19 15:41:57 +00:00
#endif
return (0);
1994-09-19 15:41:57 +00:00
}
1995-11-07 14:06:45 +00:00
static int
mountmsdosfs(devvp, mp, td, argp)
1994-09-19 15:41:57 +00:00
struct vnode *devvp;
struct mount *mp;
struct thread *td;
struct msdosfs_args *argp;
1994-09-19 15:41:57 +00:00
{
struct msdosfsmount *pmp;
struct buf *bp;
struct cdev *dev = devvp->v_rdev;
1994-09-19 15:41:57 +00:00
union bootsector *bsp;
struct byte_bpb33 *b33;
struct byte_bpb50 *b50;
struct byte_bpb710 *b710;
u_int8_t SecPerClust;
u_long clusters;
int ronly, error;
struct g_consumer *cp;
struct bufobj *bo;
1994-09-19 15:41:57 +00:00
if (mp->mnt_flag & MNT_ROOTFS)
return (EOPNOTSUPP);
ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
/* XXX: use VOP_ACCESS to check FS perms */
DROP_GIANT();
g_topology_lock();
error = g_vfs_open(devvp, &cp, "msdos", ronly ? 0 : 1);
g_topology_unlock();
PICKUP_GIANT();
VOP_UNLOCK(devvp, 0, td);
if (error)
return (error);
bo = &devvp->v_bufobj;
bp = NULL; /* both used in error_exit */
pmp = NULL;
1994-09-19 15:41:57 +00:00
/*
* Read the boot sector of the filesystem, and then check the
* boot signature. If not a dos boot sector then error out.
*
* NOTE: 2048 is a maximum sector size in current...
1994-09-19 15:41:57 +00:00
*/
error = bread(devvp, 0, 2048, NOCRED, &bp);
if (error)
1994-09-19 15:41:57 +00:00
goto error_exit;
bp->b_flags |= B_AGE;
bsp = (union bootsector *)bp->b_data;
b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
#ifndef MSDOSFS_NOCHECKSIG
if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
|| bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
error = EINVAL;
goto error_exit;
}
#endif
1994-09-19 15:41:57 +00:00
pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
1994-09-19 15:41:57 +00:00
pmp->pm_mountp = mp;
pmp->pm_cp = cp;
pmp->pm_bo = bo;
1994-09-19 15:41:57 +00:00
/*
* Compute several useful quantities from the bpb in the
* bootsector. Copy in the dos 5 variant of the bpb then fix up
* the fields that are different between dos 5 and dos 3.3.
*/
SecPerClust = b50->bpbSecPerClust;
1994-09-19 15:41:57 +00:00
pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
if (pmp->pm_BytesPerSec < DEV_BSIZE) {
error = EINVAL;
goto error_exit;
}
1994-09-19 15:41:57 +00:00
pmp->pm_ResSectors = getushort(b50->bpbResSectors);
pmp->pm_FATs = b50->bpbFATs;
pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
pmp->pm_Sectors = getushort(b50->bpbSectors);
pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
pmp->pm_Heads = getushort(b50->bpbHeads);
pmp->pm_Media = b50->bpbMedia;
1994-09-19 15:41:57 +00:00
/* calculate the ratio of sector size to DEV_BSIZE */
pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
/* XXX - We should probably check more values here */
if (!pmp->pm_BytesPerSec || !SecPerClust
|| !pmp->pm_Heads
#ifdef PC98
|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
#else
|| !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
#endif
error = EINVAL;
goto error_exit;
}
1994-09-19 15:41:57 +00:00
if (pmp->pm_Sectors == 0) {
pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
} else {
pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
pmp->pm_HugeSectors = pmp->pm_Sectors;
}
#ifndef MSDOSFS_LARGE
if (pmp->pm_HugeSectors > 0xffffffff /
(pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
/*
* We cannot deal currently with this size of disk
* due to fileid limitations (see msdosfs_getattr and
* msdosfs_readdir)
*/
error = EINVAL;
printf("mountmsdosfs(): disk too big, sorry\n");
goto error_exit;
}
#endif /* !MSDOSFS_LARGE */
if (pmp->pm_RootDirEnts == 0) {
if (bsp->bs710.bsBootSectSig2 != BOOTSIG2
|| bsp->bs710.bsBootSectSig3 != BOOTSIG3
|| pmp->pm_Sectors
|| pmp->pm_FATsecs
|| getushort(b710->bpbFSVers)) {
error = EINVAL;
printf("mountmsdosfs(): bad FAT32 filesystem\n");
goto error_exit;
}
pmp->pm_fatmask = FAT32_MASK;
pmp->pm_fatmult = 4;
pmp->pm_fatdiv = 1;
pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
if (getushort(b710->bpbExtFlags) & FATMIRROR)
pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
else
pmp->pm_flags |= MSDOSFS_FATMIRROR;
} else
pmp->pm_flags |= MSDOSFS_FATMIRROR;
/*
* Check a few values (could do some more):
* - logical sector size: power of 2, >= block size
* - sectors per cluster: power of 2, >= 1
* - number of sectors: >= 1, <= size of partition
* - number of FAT sectors: >= 1
*/
if ( (SecPerClust == 0)
|| (SecPerClust & (SecPerClust - 1))
|| (pmp->pm_BytesPerSec < DEV_BSIZE)
|| (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
|| (pmp->pm_HugeSectors == 0)
|| (pmp->pm_FATsecs == 0)
) {
error = EINVAL;
goto error_exit;
}
pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
pmp->pm_FATsecs *= pmp->pm_BlkPerSec;
SecPerClust *= pmp->pm_BlkPerSec;
pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
if (FAT32(pmp)) {
pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
pmp->pm_firstcluster = pmp->pm_fatblk
+ (pmp->pm_FATs * pmp->pm_FATsecs);
pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
} else {
pmp->pm_rootdirblk = pmp->pm_fatblk +
(pmp->pm_FATs * pmp->pm_FATsecs);
pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
+ DEV_BSIZE - 1)
/ DEV_BSIZE; /* in blocks */
pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
}
pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
SecPerClust + 1;
pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
if (pmp->pm_fatmask == 0) {
if (pmp->pm_maxcluster
<= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
/*
* This will usually be a floppy disk. This size makes
* sure that one fat entry will not be split across
* multiple blocks.
*/
pmp->pm_fatmask = FAT12_MASK;
pmp->pm_fatmult = 3;
pmp->pm_fatdiv = 2;
} else {
pmp->pm_fatmask = FAT16_MASK;
pmp->pm_fatmult = 2;
pmp->pm_fatdiv = 1;
}
}
clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
if (pmp->pm_maxcluster >= clusters) {
printf("Warning: number of clusters (%ld) exceeds FAT "
"capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
pmp->pm_maxcluster = clusters - 1;
}
1994-09-19 15:41:57 +00:00
if (FAT12(pmp))
pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
else
pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
1994-09-19 15:41:57 +00:00
pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
1994-09-19 15:41:57 +00:00
/*
* Compute mask and shift value for isolating cluster relative byte
* offsets and cluster numbers from a file offset.
*/
pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
pmp->pm_crbomask = pmp->pm_bpcluster - 1;
pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
/*
* Check for valid cluster size
* must be a power of 2
*/
if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
1994-09-19 15:41:57 +00:00
error = EINVAL;
goto error_exit;
}
/*
* Release the bootsector buffer.
*/
brelse(bp);
bp = NULL;
/*
* Check FSInfo.
*/
if (pmp->pm_fsinfo) {
struct fsinfo *fp;
if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
NOCRED, &bp)) != 0)
goto error_exit;
fp = (struct fsinfo *)bp->b_data;
if (!bcmp(fp->fsisig1, "RRaA", 4)
&& !bcmp(fp->fsisig2, "rrAa", 4)
&& !bcmp(fp->fsisig3, "\0\0\125\252", 4)
&& !bcmp(fp->fsisig4, "\0\0\125\252", 4)) {
pmp->pm_nxtfree = getulong(fp->fsinxtfree);
if (pmp->pm_nxtfree == 0xffffffff)
pmp->pm_nxtfree = CLUST_FIRST;
} else
pmp->pm_fsinfo = 0;
brelse(bp);
bp = NULL;
}
1994-09-19 15:41:57 +00:00
/*
* Check and validate (or perhaps invalidate?) the fsinfo structure?
1994-09-19 15:41:57 +00:00
*/
if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
printf(
"Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
pmp->pm_nxtfree, pmp->pm_maxcluster);
error = EINVAL;
goto error_exit;
}
1994-09-19 15:41:57 +00:00
/*
* Allocate memory for the bitmap of allocated clusters, and then
* fill it in.
*/
pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
1994-09-19 15:41:57 +00:00
* sizeof(*pmp->pm_inusemap),
M_MSDOSFSFAT, M_WAITOK);
1994-09-19 15:41:57 +00:00
/*
* fillinusemap() needs pm_devvp.
*/
pmp->pm_dev = dev;
pmp->pm_devvp = devvp;
/*
* Have the inuse map filled in.
*/
if ((error = fillinusemap(pmp)) != 0)
1994-09-19 15:41:57 +00:00
goto error_exit;
/*
* If they want fat updates to be synchronous then let them suffer
* the performance degradation in exchange for the on disk copy of
* the fat being correct just about all the time. I suppose this
* would be a good thing to turn on if the kernel is still flakey.
*/
if (mp->mnt_flag & MNT_SYNCHRONOUS)
pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
1994-09-19 15:41:57 +00:00
/*
* Finish up.
*/
if (ronly)
pmp->pm_flags |= MSDOSFSMNT_RONLY;
else {
/* Mark the volume dirty while it is mounted read/write. */
if ((error = markvoldirty(pmp, 1)) != 0)
goto error_exit;
1994-09-19 15:41:57 +00:00
pmp->pm_fmod = 1;
}
1994-09-19 15:41:57 +00:00
mp->mnt_data = (qaddr_t) pmp;
mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
mp->mnt_flag |= MNT_LOCAL;
1994-09-19 15:41:57 +00:00
#ifdef MSDOSFS_LARGE
msdosfs_fileno_init(mp);
#endif
1994-09-19 15:41:57 +00:00
return 0;
error_exit:
if (bp)
brelse(bp);
if (cp != NULL) {
DROP_GIANT();
g_topology_lock();
g_wither_geom_close(cp->geom, ENXIO);
g_topology_unlock();
PICKUP_GIANT();
}
1994-09-19 15:41:57 +00:00
if (pmp) {
if (pmp->pm_inusemap)
free(pmp->pm_inusemap, M_MSDOSFSFAT);
free(pmp, M_MSDOSFSMNT);
mp->mnt_data = (qaddr_t)0;
1994-09-19 15:41:57 +00:00
}
return (error);
1994-09-19 15:41:57 +00:00
}
/*
* Unmount the filesystem described by mp.
*/
1995-11-07 14:06:45 +00:00
static int
msdosfs_unmount(mp, mntflags, td)
1994-09-19 15:41:57 +00:00
struct mount *mp;
int mntflags;
struct thread *td;
1994-09-19 15:41:57 +00:00
{
struct msdosfsmount *pmp;
int error, flags;
1994-09-19 15:41:57 +00:00
flags = 0;
if (mntflags & MNT_FORCE)
1994-09-19 15:41:57 +00:00
flags |= FORCECLOSE;
error = vflush(mp, 0, flags, td);
if (error)
1994-09-19 15:41:57 +00:00
return error;
pmp = VFSTOMSDOSFS(mp);
if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
if (pmp->pm_w2u)
msdosfs_iconv->close(pmp->pm_w2u);
if (pmp->pm_u2w)
msdosfs_iconv->close(pmp->pm_u2w);
if (pmp->pm_d2u)
msdosfs_iconv->close(pmp->pm_d2u);
if (pmp->pm_u2d)
msdosfs_iconv->close(pmp->pm_u2d);
}
/* If the volume was mounted read/write, mark it clean now. */
if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
error = markvoldirty(pmp, 0);
if (error && (flags & FORCECLOSE) == 0)
return (error);
}
#ifdef MSDOSFS_DEBUG
{
struct vnode *vp = pmp->pm_devvp;
VI_LOCK(vp);
printf("msdosfs_umount(): just before calling VOP_CLOSE()\n");
printf("iflag %08lx, usecount %d, writecount %d, holdcnt %ld\n",
vp->vi_flag, vp->v_usecount, vp->v_writecount,
vp->v_holdcnt);
printf("id %lu, mount %p, op %p\n",
vp->v_id, vp->v_mount, vp->v_op);
printf("freef %p, freeb %p, mount %p\n",
TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
vp->v_mount);
printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
vp->v_bufobj.bo_numoutput, vp->v_type);
printf("union %p, tag %s, data[0] %08x, data[1] %08x\n",
vp->v_socket, vp->v_tag,
((u_int *)vp->v_data)[0],
((u_int *)vp->v_data)[1]);
VI_UNLOCK(vp);
}
#endif
DROP_GIANT();
g_topology_lock();
g_wither_geom_close(pmp->pm_cp->geom, ENXIO);
g_topology_unlock();
PICKUP_GIANT();
1994-09-19 15:41:57 +00:00
vrele(pmp->pm_devvp);
free(pmp->pm_inusemap, M_MSDOSFSFAT);
#ifdef MSDOSFS_LARGE
msdosfs_fileno_free(mp);
#endif
free(pmp, M_MSDOSFSMNT);
mp->mnt_data = (qaddr_t)0;
mp->mnt_flag &= ~MNT_LOCAL;
return (error);
1994-09-19 15:41:57 +00:00
}
1995-11-07 14:06:45 +00:00
static int
msdosfs_root(mp, vpp, td)
1994-09-19 15:41:57 +00:00
struct mount *mp;
struct vnode **vpp;
struct thread *td;
1994-09-19 15:41:57 +00:00
{
struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1994-09-19 15:41:57 +00:00
struct denode *ndep;
int error;
#ifdef MSDOSFS_DEBUG
printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
1994-09-19 15:41:57 +00:00
#endif
error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
if (error)
return (error);
*vpp = DETOV(ndep);
return (0);
1994-09-19 15:41:57 +00:00
}
1995-11-07 14:06:45 +00:00
static int
msdosfs_statfs(mp, sbp, td)
1994-09-19 15:41:57 +00:00
struct mount *mp;
struct statfs *sbp;
struct thread *td;
1994-09-19 15:41:57 +00:00
{
struct msdosfsmount *pmp;
1994-09-19 15:41:57 +00:00
pmp = VFSTOMSDOSFS(mp);
1994-09-19 15:41:57 +00:00
sbp->f_bsize = pmp->pm_bpcluster;
sbp->f_iosize = pmp->pm_bpcluster;
sbp->f_blocks = pmp->pm_maxcluster + 1;
1994-09-19 15:41:57 +00:00
sbp->f_bfree = pmp->pm_freeclustercount;
sbp->f_bavail = pmp->pm_freeclustercount;
sbp->f_files = pmp->pm_RootDirEnts; /* XXX */
sbp->f_ffree = 0; /* what to put in here? */
return (0);
1994-09-19 15:41:57 +00:00
}
1995-11-07 14:06:45 +00:00
static int
msdosfs_sync(mp, waitfor, cred, td)
1994-09-19 15:41:57 +00:00
struct mount *mp;
int waitfor;
struct ucred *cred;
struct thread *td;
1994-09-19 15:41:57 +00:00
{
struct vnode *vp, *nvp;
1994-09-19 15:41:57 +00:00
struct denode *dep;
struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
int error, allerror = 0;
1994-09-19 15:41:57 +00:00
/*
* If we ever switch to not updating all of the fats all the time,
* this would be the place to update them from the first one.
*/
if (pmp->pm_fmod != 0) {
if (pmp->pm_flags & MSDOSFSMNT_RONLY)
1994-09-19 15:41:57 +00:00
panic("msdosfs_sync: rofs mod");
else {
/* update fats here */
}
}
1994-09-19 15:41:57 +00:00
/*
* Write back each (modified) denode.
1994-09-19 15:41:57 +00:00
*/
MNT_ILOCK(mp);
1994-09-19 15:41:57 +00:00
loop:
When we traverse the vnodes on a mountpoint we need to look out for our cached 'next vnode' being removed from this mountpoint. If we find that it was recycled, we restart our traversal from the start of the list. Code to do that is in all local disk filesystems (and a few other places) and looks roughly like this: MNT_ILOCK(mp); loop: for (vp = TAILQ_FIRST(&mp...); (vp = nvp) != NULL; nvp = TAILQ_NEXT(vp,...)) { if (vp->v_mount != mp) goto loop; MNT_IUNLOCK(mp); ... MNT_ILOCK(mp); } MNT_IUNLOCK(mp); The code which takes vnodes off a mountpoint looks like this: MNT_ILOCK(vp->v_mount); ... TAILQ_REMOVE(&vp->v_mount->mnt_nvnodelist, vp, v_nmntvnodes); ... MNT_IUNLOCK(vp->v_mount); ... vp->v_mount = something; (Take a moment and try to spot the locking error before you read on.) On a SMP system, one CPU could have removed nvp from our mountlist but not yet gotten to assign a new value to vp->v_mount while another CPU simultaneously get to the top of the traversal loop where it finds that (vp->v_mount != mp) is not true despite the fact that the vnode has indeed been removed from our mountpoint. Fix: Introduce the macro MNT_VNODE_FOREACH() to traverse the list of vnodes on a mountpoint while taking into account that vnodes may be removed from the list as we go. This saves approx 65 lines of duplicated code. Split the insmntque() which potentially moves a vnode from one mount point to another into delmntque() and insmntque() which does just what the names say. Fix delmntque() to set vp->v_mount to NULL while holding the mountpoint lock.
2004-07-04 08:52:35 +00:00
MNT_VNODE_FOREACH(vp, mp, nvp) {
VI_LOCK(vp);
if (vp->v_type == VNON || (vp->v_iflag & VI_XLOCK)) {
VI_UNLOCK(vp);
continue;
}
MNT_IUNLOCK(mp);
1994-09-19 15:41:57 +00:00
dep = VTODE(vp);
if ((dep->de_flag &
(DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
(vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
waitfor == MNT_LAZY)) {
VI_UNLOCK(vp);
MNT_ILOCK(mp);
1994-09-19 15:41:57 +00:00
continue;
}
error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
if (error) {
MNT_ILOCK(mp);
if (error == ENOENT)
goto loop;
continue;
}
error = VOP_FSYNC(vp, cred, waitfor, td);
if (error)
1994-09-19 15:41:57 +00:00
allerror = error;
VOP_UNLOCK(vp, 0, td);
vrele(vp);
MNT_ILOCK(mp);
1994-09-19 15:41:57 +00:00
}
MNT_IUNLOCK(mp);
1994-09-19 15:41:57 +00:00
/*
* Flush filesystem control info.
*/
if (waitfor != MNT_LAZY) {
vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
error = VOP_FSYNC(pmp->pm_devvp, cred, waitfor, td);
if (error)
allerror = error;
VOP_UNLOCK(pmp->pm_devvp, 0, td);
}
return (allerror);
1994-09-19 15:41:57 +00:00
}
1995-11-07 14:06:45 +00:00
static int
msdosfs_fhtovp(mp, fhp, vpp)
1994-09-19 15:41:57 +00:00
struct mount *mp;
struct fid *fhp;
struct vnode **vpp;
{
struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
1994-09-19 15:41:57 +00:00
struct defid *defhp = (struct defid *) fhp;
struct denode *dep;
int error;
error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
1994-09-19 15:41:57 +00:00
if (error) {
*vpp = NULLVP;
return (error);
1994-09-19 15:41:57 +00:00
}
*vpp = DETOV(dep);
return (0);
}
1995-11-07 14:06:45 +00:00
static int
1994-09-19 15:41:57 +00:00
msdosfs_vptofh(vp, fhp)
struct vnode *vp;
struct fid *fhp;
{
struct denode *dep;
struct defid *defhp;
1994-09-19 15:41:57 +00:00
dep = VTODE(vp);
defhp = (struct defid *)fhp;
1994-09-19 15:41:57 +00:00
defhp->defid_len = sizeof(struct defid);
defhp->defid_dirclust = dep->de_dirclust;
defhp->defid_dirofs = dep->de_diroffset;
/* defhp->defid_gen = dep->de_gen; */
return (0);
1994-09-19 15:41:57 +00:00
}
1995-11-07 14:10:19 +00:00
static struct vfsops msdosfs_vfsops = {
.vfs_fhtovp = msdosfs_fhtovp,
.vfs_init = msdosfs_init,
.vfs_omount = msdosfs_omount,
.vfs_root = msdosfs_root,
.vfs_statfs = msdosfs_statfs,
.vfs_sync = msdosfs_sync,
.vfs_uninit = msdosfs_uninit,
.vfs_unmount = msdosfs_unmount,
.vfs_vptofh = msdosfs_vptofh,
1994-09-19 15:41:57 +00:00
};
VFS_SET(msdosfs_vfsops, msdosfs, 0);
MODULE_VERSION(msdosfs, 1);