The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/fs/msdosfs/msdosfs_vfsops.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* $FreeBSD: releng/6.0/sys/fs/msdosfs/msdosfs_vfsops.c 150909 2005-10-04 13:13:57Z peadar $ */
    2 /*      $NetBSD: msdosfs_vfsops.c,v 1.51 1997/11/17 15:36:58 ws Exp $   */
    3 
    4 /*-
    5  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
    6  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
    7  * All rights reserved.
    8  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   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.
   23  *
   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.
   34  */
   35 /*-
   36  * Written by Paul Popelka (paulp@uts.amdahl.com)
   37  *
   38  * You can do anything you want with this software, just don't say you wrote
   39  * it, and don't remove this notice.
   40  *
   41  * This software is provided "as is".
   42  *
   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.
   47  *
   48  * October 1992
   49  */
   50 
   51 #include <sys/param.h>
   52 #include <sys/systm.h>
   53 #include <sys/conf.h>
   54 #include <sys/namei.h>
   55 #include <sys/proc.h>
   56 #include <sys/kernel.h>
   57 #include <sys/vnode.h>
   58 #include <sys/mount.h>
   59 #include <sys/bio.h>
   60 #include <sys/buf.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>
   66 
   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>
   73 
   74 #include <geom/geom.h>
   75 #include <geom/geom_vfs.h>
   76 
   77 #include "opt_msdosfs.h"
   78 
   79 /* List of mount options we support */
   80 static const char *msdosfs_opts[] = {
   81         "from",
   82         "export",
   83         "uid", "gid", "mask", "dirmask",
   84         "shortname", "longname", "win95",
   85         "kiconv", "cs_win", "cs_dos", "cs_local",
   86         NULL
   87 };
   88 
   89 #define MSDOSFS_DFLTBSIZE       4096
   90 
   91 #if 1 /*def PC98*/
   92 /*
   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)
   98  */
   99 #define MSDOSFS_NOCHECKSIG
  100 #endif
  101 
  102 MALLOC_DEFINE(M_MSDOSFSMNT, "MSDOSFS mount", "MSDOSFS mount structure");
  103 static MALLOC_DEFINE(M_MSDOSFSFAT, "MSDOSFS FAT", "MSDOSFS file allocation table");
  104 
  105 struct iconv_functions *msdosfs_iconv = NULL;
  106 
  107 static int      update_mp(struct mount *mp, struct thread *td);
  108 static int      mountmsdosfs(struct vnode *devvp, struct mount *mp,
  109                     struct thread *td);
  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;
  117 
  118 /* Maximum length of a character set name (arbitrary). */
  119 #define MAXCSLEN        64
  120 
  121 static int
  122 update_mp(mp, td)
  123         struct mount *mp;
  124         struct thread *td;
  125 {
  126         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  127         void *dos, *win, *local;
  128         int error, v;
  129 
  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);
  134                         if (!error)
  135                                 error = vfs_getopt(mp->mnt_optnew,
  136                                     "cs_local", &local, NULL);
  137                         if (!error)
  138                                 error = vfs_getopt(mp->mnt_optnew,
  139                                     "cs_dos", &dos, NULL);
  140                         if (!error) {
  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);
  145                         }
  146                         if (error != 0)
  147                                 return (error);
  148                 } else {
  149                         pmp->pm_w2u = NULL;
  150                         pmp->pm_u2w = NULL;
  151                         pmp->pm_d2u = NULL;
  152                         pmp->pm_u2d = NULL;
  153                 }
  154         }
  155 
  156         if (1 == vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v))
  157                 pmp->pm_gid = v;
  158         if (1 == vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v))
  159                 pmp->pm_uid = 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, "longname",
  167             &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
  168         vfs_flagopt(mp->mnt_optnew, "kiconv",
  169             &pmp->pm_flags, MSDOSFSMNT_KICONV);
  170 
  171         /* XXX: Can't use flagopt due to negative option */
  172         if (!vfs_getopt(mp->mnt_optnew, "win95", NULL, NULL))
  173                 pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
  174         else
  175                 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
  176 
  177         if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
  178                 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
  179         else if (!(pmp->pm_flags &
  180             (MSDOSFSMNT_SHORTNAME | MSDOSFSMNT_LONGNAME))) {
  181                 struct vnode *rootvp;
  182 
  183                 /*
  184                  * Try to divine whether to support Win'95 long filenames
  185                  */
  186                 if (FAT32(pmp))
  187                         pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
  188                 else {
  189                         if ((error =
  190                             msdosfs_root(mp, LK_EXCLUSIVE, &rootvp, td)) != 0)
  191                                 return error;
  192                         pmp->pm_flags |= findwin95(VTODE(rootvp))
  193                                 ? MSDOSFSMNT_LONGNAME
  194                                         : MSDOSFSMNT_SHORTNAME;
  195                         vput(rootvp);
  196                 }
  197         }
  198         return 0;
  199 }
  200 
  201 static int
  202 msdosfs_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
  203 {
  204         struct msdosfs_args args;
  205         int error;
  206 
  207         if (data == NULL)
  208                 return (EINVAL);
  209         error = copyin(data, &args, sizeof args);
  210         if (error)
  211                 return (error);
  212 
  213         ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
  214         ma = mount_arg(ma, "export", &args.export, sizeof args.export);
  215         ma = mount_argf(ma, "uid", "%d", args.uid);
  216         ma = mount_argf(ma, "gid", "%d", args.gid);
  217         ma = mount_argf(ma, "mask", "%d", args.mask);
  218         ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
  219 
  220         ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
  221         ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
  222         ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
  223         ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
  224 
  225         ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
  226         ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
  227         ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
  228 
  229         error = kernel_mount(ma, flags);
  230 
  231         return (error);
  232 }
  233 
  234 /*
  235  * mp - path - addr in user space of mount point (ie /usr or whatever)
  236  * data - addr in user space of mount params including the name of the block
  237  * special file to treat as a filesystem.
  238  */
  239 static int
  240 msdosfs_mount(struct mount *mp, struct thread *td)
  241 {
  242         struct vnode *devvp;      /* vnode for blk device to mount */
  243         struct export_args export;
  244         /* msdosfs specific mount control block */
  245         struct msdosfsmount *pmp = NULL;
  246         struct nameidata ndp;
  247         int error, flags;
  248         mode_t accessmode;
  249         char *from;
  250 
  251         if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
  252                 return (EINVAL);
  253 
  254         /*
  255          * If updating, check whether changing from read-only to
  256          * read/write; if there is no device name, that's all we do.
  257          */
  258         if (mp->mnt_flag & MNT_UPDATE) {
  259                 pmp = VFSTOMSDOSFS(mp);
  260 
  261                 error = vfs_copyopt(mp->mnt_optnew, "export",
  262                     &export, sizeof export);
  263                 if (error == 0 && export.ex_flags != 0) {
  264                         /*
  265                          * Process export requests.
  266                          */
  267                         if ((export.ex_flags & MNT_EXPORTED) != 0 &&
  268                             (pmp->pm_flags & MSDOSFS_LARGEFS) != 0)
  269                                 return (EOPNOTSUPP);
  270                         return (vfs_export(mp, &export));
  271                 }
  272                 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
  273                     vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
  274                         error = VFS_SYNC(mp, MNT_WAIT, td);
  275                         if (error)
  276                                 return (error);
  277                         flags = WRITECLOSE;
  278                         if (mp->mnt_flag & MNT_FORCE)
  279                                 flags |= FORCECLOSE;
  280                         error = vflush(mp, 0, flags, td);
  281                         if (error)
  282                                 return (error);
  283                         DROP_GIANT();
  284                         g_topology_lock();
  285                         g_access(pmp->pm_cp, 0, -1, 0);
  286                         g_topology_unlock();
  287                         PICKUP_GIANT();
  288                 } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
  289                     !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
  290                         /*
  291                          * If upgrade to read-write by non-root, then verify
  292                          * that user has necessary permissions on the device.
  293                          */
  294                         if (suser(td)) {
  295                                 devvp = pmp->pm_devvp;
  296                                 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
  297                                 error = VOP_ACCESS(devvp, VREAD | VWRITE,
  298                                                    td->td_ucred, td);
  299                                 if (error) {
  300                                         VOP_UNLOCK(devvp, 0, td);
  301                                         return (error);
  302                                 }
  303                                 VOP_UNLOCK(devvp, 0, td);
  304                         }
  305                         DROP_GIANT();
  306                         g_topology_lock();
  307                         error = g_access(pmp->pm_cp, 0, 1, 0);
  308                         g_topology_unlock();
  309                         PICKUP_GIANT();
  310                         if (error)
  311                                 return (error);
  312 
  313                         /* Now that the volume is modifiable, mark it dirty. */
  314                         error = markvoldirty(pmp, 1);
  315                         if (error)
  316                                 return (error);
  317                 }
  318                 vfs_flagopt(mp->mnt_optnew, "ro",
  319                     &pmp->pm_flags, MSDOSFSMNT_RONLY);
  320                 vfs_flagopt(mp->mnt_optnew, "ro",
  321                     &mp->mnt_flag, MNT_RDONLY);
  322                 if (vfs_getopt(mp->mnt_optnew, "from", NULL, NULL)) {
  323 #ifdef  __notyet__      /* doesn't work correctly with current mountd   XXX */
  324                         if (args.flags & MSDOSFSMNT_MNTOPT) {
  325                                 pmp->pm_flags &= ~MSDOSFSMNT_MNTOPT;
  326                                 pmp->pm_flags |= args.flags & MSDOSFSMNT_MNTOPT;
  327                                 if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
  328                                         pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
  329                         }
  330 #endif
  331                 }
  332         }
  333         /*
  334          * Not an update, or updating the name: look up the name
  335          * and verify that it refers to a sensible disk device.
  336          */
  337         if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
  338                 return (EINVAL);
  339         NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
  340         error = namei(&ndp);
  341         if (error)
  342                 return (error);
  343         devvp = ndp.ni_vp;
  344         NDFREE(&ndp, NDF_ONLY_PNBUF);
  345 
  346         if (!vn_isdisk(devvp, &error)) {
  347                 vput(devvp);
  348                 return (error);
  349         }
  350         /*
  351          * If mount by non-root, then verify that user has necessary
  352          * permissions on the device.
  353          */
  354         if (suser(td)) {
  355                 accessmode = VREAD;
  356                 if ((mp->mnt_flag & MNT_RDONLY) == 0)
  357                         accessmode |= VWRITE;
  358                 error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
  359                 if (error) {
  360                         vput(devvp);
  361                         return (error);
  362                 }
  363         }
  364         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
  365                 error = mountmsdosfs(devvp, mp, td);
  366 #ifdef MSDOSFS_DEBUG            /* only needed for the printf below */
  367                 pmp = VFSTOMSDOSFS(mp);
  368 #endif
  369         } else {
  370                 if (devvp != pmp->pm_devvp)
  371                         error = EINVAL; /* XXX needs translation */
  372                 else
  373                         vput(devvp);
  374         }
  375         if (error) {
  376                 vrele(devvp);
  377                 return (error);
  378         }
  379 
  380         error = update_mp(mp, td);
  381         if (error) {
  382                 if ((mp->mnt_flag & MNT_UPDATE) == 0)
  383                         msdosfs_unmount(mp, MNT_FORCE, td);
  384                 return error;
  385         }
  386         
  387         vfs_mountedfrom(mp, from);
  388 #ifdef MSDOSFS_DEBUG
  389         printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
  390 #endif
  391         return (0);
  392 }
  393 
  394 static int
  395 mountmsdosfs(devvp, mp, td)
  396         struct vnode *devvp;
  397         struct mount *mp;
  398         struct thread *td;
  399 {
  400         struct msdosfsmount *pmp;
  401         struct buf *bp;
  402         struct cdev *dev = devvp->v_rdev;
  403         union bootsector *bsp;
  404         struct byte_bpb33 *b33;
  405         struct byte_bpb50 *b50;
  406         struct byte_bpb710 *b710;
  407         u_int8_t SecPerClust;
  408         u_long clusters;
  409         int     ronly, error;
  410         struct g_consumer *cp;
  411         struct bufobj *bo;
  412 
  413         ronly = !vfs_getopt(mp->mnt_optnew, "ro", NULL, NULL);
  414         /* XXX: use VOP_ACCESS to check FS perms */
  415         DROP_GIANT();
  416         g_topology_lock();
  417         error = g_vfs_open(devvp, &cp, "msdos", ronly ? 0 : 1);
  418         g_topology_unlock();
  419         PICKUP_GIANT();
  420         VOP_UNLOCK(devvp, 0, td);
  421         if (error)
  422                 return (error);
  423 
  424         bo = &devvp->v_bufobj;
  425         bp  = NULL; /* both used in error_exit */
  426         pmp = NULL;
  427 
  428         /*
  429          * Read the boot sector of the filesystem, and then check the
  430          * boot signature.  If not a dos boot sector then error out.
  431          *
  432          * NOTE: 2048 is a maximum sector size in current...
  433          */
  434         error = bread(devvp, 0, 2048, NOCRED, &bp);
  435         if (error)
  436                 goto error_exit;
  437         bp->b_flags |= B_AGE;
  438         bsp = (union bootsector *)bp->b_data;
  439         b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
  440         b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
  441         b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
  442 
  443 #ifndef MSDOSFS_NOCHECKSIG
  444         if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
  445             || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
  446                 error = EINVAL;
  447                 goto error_exit;
  448         }
  449 #endif
  450 
  451         pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
  452         pmp->pm_mountp = mp;
  453         pmp->pm_cp = cp;
  454         pmp->pm_bo = bo;
  455 
  456         /*
  457          * Compute several useful quantities from the bpb in the
  458          * bootsector.  Copy in the dos 5 variant of the bpb then fix up
  459          * the fields that are different between dos 5 and dos 3.3.
  460          */
  461         SecPerClust = b50->bpbSecPerClust;
  462         pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
  463         if (pmp->pm_BytesPerSec < DEV_BSIZE) {
  464                 error = EINVAL;
  465                 goto error_exit;
  466         }
  467         pmp->pm_ResSectors = getushort(b50->bpbResSectors);
  468         pmp->pm_FATs = b50->bpbFATs;
  469         pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
  470         pmp->pm_Sectors = getushort(b50->bpbSectors);
  471         pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
  472         pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
  473         pmp->pm_Heads = getushort(b50->bpbHeads);
  474         pmp->pm_Media = b50->bpbMedia;
  475 
  476         /* calculate the ratio of sector size to DEV_BSIZE */
  477         pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
  478 
  479         /* XXX - We should probably check more values here */
  480         if (!pmp->pm_BytesPerSec || !SecPerClust
  481                 || !pmp->pm_Heads
  482 #ifdef PC98
  483                 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 255) {
  484 #else
  485                 || !pmp->pm_SecPerTrack || pmp->pm_SecPerTrack > 63) {
  486 #endif
  487                 error = EINVAL;
  488                 goto error_exit;
  489         }
  490 
  491         if (pmp->pm_Sectors == 0) {
  492                 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
  493                 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
  494         } else {
  495                 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
  496                 pmp->pm_HugeSectors = pmp->pm_Sectors;
  497         }
  498 #ifndef MSDOSFS_LARGE
  499         if (pmp->pm_HugeSectors > 0xffffffff / 
  500             (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
  501                 /*
  502                  * We cannot deal currently with this size of disk
  503                  * due to fileid limitations (see msdosfs_getattr and
  504                  * msdosfs_readdir)
  505                  */
  506                 error = EINVAL;
  507                 printf("mountmsdosfs(): disk too big, sorry\n");
  508                 goto error_exit;
  509         }
  510 #endif  /* !MSDOSFS_LARGE */
  511 
  512         if (pmp->pm_RootDirEnts == 0) {
  513                 if (pmp->pm_Sectors
  514                     || pmp->pm_FATsecs
  515                     || getushort(b710->bpbFSVers)) {
  516                         error = EINVAL;
  517                         printf("mountmsdosfs(): bad FAT32 filesystem\n");
  518                         goto error_exit;
  519                 }
  520                 pmp->pm_fatmask = FAT32_MASK;
  521                 pmp->pm_fatmult = 4;
  522                 pmp->pm_fatdiv = 1;
  523                 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
  524                 if (getushort(b710->bpbExtFlags) & FATMIRROR)
  525                         pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
  526                 else
  527                         pmp->pm_flags |= MSDOSFS_FATMIRROR;
  528         } else
  529                 pmp->pm_flags |= MSDOSFS_FATMIRROR;
  530 
  531         /*
  532          * Check a few values (could do some more):
  533          * - logical sector size: power of 2, >= block size
  534          * - sectors per cluster: power of 2, >= 1
  535          * - number of sectors:   >= 1, <= size of partition
  536          * - number of FAT sectors: >= 1
  537          */
  538         if ( (SecPerClust == 0)
  539           || (SecPerClust & (SecPerClust - 1))
  540           || (pmp->pm_BytesPerSec < DEV_BSIZE)
  541           || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
  542           || (pmp->pm_HugeSectors == 0)
  543           || (pmp->pm_FATsecs == 0)
  544         ) {
  545                 error = EINVAL;
  546                 goto error_exit;
  547         }
  548 
  549         pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
  550         pmp->pm_HiddenSects *= pmp->pm_BlkPerSec; /* XXX not used? */
  551         pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
  552         SecPerClust         *= pmp->pm_BlkPerSec;
  553 
  554         pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
  555 
  556         if (FAT32(pmp)) {
  557                 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
  558                 pmp->pm_firstcluster = pmp->pm_fatblk
  559                         + (pmp->pm_FATs * pmp->pm_FATsecs);
  560                 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
  561         } else {
  562                 pmp->pm_rootdirblk = pmp->pm_fatblk +
  563                         (pmp->pm_FATs * pmp->pm_FATsecs);
  564                 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
  565                                        + DEV_BSIZE - 1)
  566                         / DEV_BSIZE; /* in blocks */
  567                 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
  568         }
  569 
  570         pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
  571             SecPerClust + 1;
  572         pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE; /* XXX not used? */
  573 
  574         if (pmp->pm_fatmask == 0) {
  575                 if (pmp->pm_maxcluster
  576                     <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
  577                         /*
  578                          * This will usually be a floppy disk. This size makes
  579                          * sure that one fat entry will not be split across
  580                          * multiple blocks.
  581                          */
  582                         pmp->pm_fatmask = FAT12_MASK;
  583                         pmp->pm_fatmult = 3;
  584                         pmp->pm_fatdiv = 2;
  585                 } else {
  586                         pmp->pm_fatmask = FAT16_MASK;
  587                         pmp->pm_fatmult = 2;
  588                         pmp->pm_fatdiv = 1;
  589                 }
  590         }
  591 
  592         clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
  593         if (pmp->pm_maxcluster >= clusters) {
  594                 printf("Warning: number of clusters (%ld) exceeds FAT "
  595                     "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
  596                 pmp->pm_maxcluster = clusters - 1;
  597         }
  598 
  599 
  600         if (FAT12(pmp))
  601                 pmp->pm_fatblocksize = 3 * pmp->pm_BytesPerSec;
  602         else
  603                 pmp->pm_fatblocksize = MSDOSFS_DFLTBSIZE;
  604 
  605         pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
  606         pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
  607 
  608         /*
  609          * Compute mask and shift value for isolating cluster relative byte
  610          * offsets and cluster numbers from a file offset.
  611          */
  612         pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
  613         pmp->pm_crbomask = pmp->pm_bpcluster - 1;
  614         pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
  615 
  616         /*
  617          * Check for valid cluster size
  618          * must be a power of 2
  619          */
  620         if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
  621                 error = EINVAL;
  622                 goto error_exit;
  623         }
  624 
  625         /*
  626          * Release the bootsector buffer.
  627          */
  628         brelse(bp);
  629         bp = NULL;
  630 
  631         /*
  632          * Check FSInfo.
  633          */
  634         if (pmp->pm_fsinfo) {
  635                 struct fsinfo *fp;
  636 
  637                 if ((error = bread(devvp, pmp->pm_fsinfo, fsi_size(pmp),
  638                     NOCRED, &bp)) != 0)
  639                         goto error_exit;
  640                 fp = (struct fsinfo *)bp->b_data;
  641                 if (!bcmp(fp->fsisig1, "RRaA", 4)
  642                     && !bcmp(fp->fsisig2, "rrAa", 4)
  643                     && !bcmp(fp->fsisig3, "\0\0\125\252", 4)
  644                     && !bcmp(fp->fsisig4, "\0\0\125\252", 4)) {
  645                         pmp->pm_nxtfree = getulong(fp->fsinxtfree);
  646                         if (pmp->pm_nxtfree == 0xffffffff)
  647                                 pmp->pm_nxtfree = CLUST_FIRST;
  648                 } else
  649                         pmp->pm_fsinfo = 0;
  650                 brelse(bp);
  651                 bp = NULL;
  652         }
  653 
  654         /*
  655          * Check and validate (or perhaps invalidate?) the fsinfo structure?
  656          */
  657         if (pmp->pm_fsinfo && pmp->pm_nxtfree > pmp->pm_maxcluster) {
  658                 printf(
  659                 "Next free cluster in FSInfo (%lu) exceeds maxcluster (%lu)\n",
  660                     pmp->pm_nxtfree, pmp->pm_maxcluster);
  661                 error = EINVAL;
  662                 goto error_exit;
  663         }
  664 
  665         /*
  666          * Allocate memory for the bitmap of allocated clusters, and then
  667          * fill it in.
  668          */
  669         pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
  670                                   * sizeof(*pmp->pm_inusemap),
  671                                   M_MSDOSFSFAT, M_WAITOK);
  672 
  673         /*
  674          * fillinusemap() needs pm_devvp.
  675          */
  676         pmp->pm_devvp = devvp;
  677 
  678         /*
  679          * Have the inuse map filled in.
  680          */
  681         if ((error = fillinusemap(pmp)) != 0)
  682                 goto error_exit;
  683 
  684         /*
  685          * If they want fat updates to be synchronous then let them suffer
  686          * the performance degradation in exchange for the on disk copy of
  687          * the fat being correct just about all the time.  I suppose this
  688          * would be a good thing to turn on if the kernel is still flakey.
  689          */
  690         if (mp->mnt_flag & MNT_SYNCHRONOUS)
  691                 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
  692 
  693         /*
  694          * Finish up.
  695          */
  696         if (ronly)
  697                 pmp->pm_flags |= MSDOSFSMNT_RONLY;
  698         else {
  699                 /* Mark the volume dirty while it is mounted read/write. */
  700                 if ((error = markvoldirty(pmp, 1)) != 0)
  701                         goto error_exit;
  702                 pmp->pm_fmod = 1;
  703         }
  704         mp->mnt_data = (qaddr_t) pmp;
  705         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
  706         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
  707         mp->mnt_flag |= MNT_LOCAL;
  708 
  709 #ifdef MSDOSFS_LARGE
  710         msdosfs_fileno_init(mp);
  711 #endif
  712 
  713         return 0;
  714 
  715 error_exit:
  716         if (bp)
  717                 brelse(bp);
  718         if (cp != NULL) {
  719                 DROP_GIANT();
  720                 g_topology_lock();
  721                 g_vfs_close(cp, td);
  722                 g_topology_unlock();
  723                 PICKUP_GIANT();
  724         }
  725         if (pmp) {
  726                 if (pmp->pm_inusemap)
  727                         free(pmp->pm_inusemap, M_MSDOSFSFAT);
  728                 free(pmp, M_MSDOSFSMNT);
  729                 mp->mnt_data = (qaddr_t)0;
  730         }
  731         return (error);
  732 }
  733 
  734 /*
  735  * Unmount the filesystem described by mp.
  736  */
  737 static int
  738 msdosfs_unmount(mp, mntflags, td)
  739         struct mount *mp;
  740         int mntflags;
  741         struct thread *td;
  742 {
  743         struct msdosfsmount *pmp;
  744         int error, flags;
  745 
  746         flags = 0;
  747         if (mntflags & MNT_FORCE)
  748                 flags |= FORCECLOSE;
  749         error = vflush(mp, 0, flags, td);
  750         if (error)
  751                 return error;
  752         pmp = VFSTOMSDOSFS(mp);
  753         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
  754                 if (pmp->pm_w2u)
  755                         msdosfs_iconv->close(pmp->pm_w2u);
  756                 if (pmp->pm_u2w)
  757                         msdosfs_iconv->close(pmp->pm_u2w);
  758                 if (pmp->pm_d2u)
  759                         msdosfs_iconv->close(pmp->pm_d2u);
  760                 if (pmp->pm_u2d)
  761                         msdosfs_iconv->close(pmp->pm_u2d);
  762         }
  763 
  764         /* If the volume was mounted read/write, mark it clean now. */
  765         if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
  766                 error = markvoldirty(pmp, 0);
  767                 if (error && (flags & FORCECLOSE) == 0)
  768                         return (error);
  769         }
  770 #ifdef MSDOSFS_DEBUG
  771         {
  772                 struct vnode *vp = pmp->pm_devvp;
  773 
  774                 VI_LOCK(vp);
  775                 vn_printf(vp,
  776                     "msdosfs_umount(): just before calling VOP_CLOSE()\n");
  777                 printf("freef %p, freeb %p, mount %p\n",
  778                     TAILQ_NEXT(vp, v_freelist), vp->v_freelist.tqe_prev,
  779                     vp->v_mount);
  780                 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
  781                     TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
  782                     TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
  783                     vp->v_bufobj.bo_numoutput, vp->v_type);
  784                 VI_UNLOCK(vp);
  785         }
  786 #endif
  787         DROP_GIANT();
  788         g_topology_lock();
  789         g_vfs_close(pmp->pm_cp, td);
  790         g_topology_unlock();
  791         PICKUP_GIANT();
  792         vrele(pmp->pm_devvp);
  793         free(pmp->pm_inusemap, M_MSDOSFSFAT);
  794 #ifdef MSDOSFS_LARGE
  795         msdosfs_fileno_free(mp);
  796 #endif
  797         free(pmp, M_MSDOSFSMNT);
  798         mp->mnt_data = (qaddr_t)0;
  799         mp->mnt_flag &= ~MNT_LOCAL;
  800         return (error);
  801 }
  802 
  803 static int
  804 msdosfs_root(mp, flags, vpp, td)
  805         struct mount *mp;
  806         int flags;
  807         struct vnode **vpp;
  808         struct thread *td;
  809 {
  810         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  811         struct denode *ndep;
  812         int error;
  813 
  814 #ifdef MSDOSFS_DEBUG
  815         printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
  816 #endif
  817         error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
  818         if (error)
  819                 return (error);
  820         *vpp = DETOV(ndep);
  821         return (0);
  822 }
  823 
  824 static int
  825 msdosfs_statfs(mp, sbp, td)
  826         struct mount *mp;
  827         struct statfs *sbp;
  828         struct thread *td;
  829 {
  830         struct msdosfsmount *pmp;
  831 
  832         pmp = VFSTOMSDOSFS(mp);
  833         sbp->f_bsize = pmp->pm_bpcluster;
  834         sbp->f_iosize = pmp->pm_bpcluster;
  835         sbp->f_blocks = pmp->pm_maxcluster + 1;
  836         sbp->f_bfree = pmp->pm_freeclustercount;
  837         sbp->f_bavail = pmp->pm_freeclustercount;
  838         sbp->f_files = pmp->pm_RootDirEnts;                     /* XXX */
  839         sbp->f_ffree = 0;       /* what to put in here? */
  840         return (0);
  841 }
  842 
  843 static int
  844 msdosfs_sync(mp, waitfor, td)
  845         struct mount *mp;
  846         int waitfor;
  847         struct thread *td;
  848 {
  849         struct vnode *vp, *nvp;
  850         struct denode *dep;
  851         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  852         int error, allerror = 0;
  853 
  854         /*
  855          * If we ever switch to not updating all of the fats all the time,
  856          * this would be the place to update them from the first one.
  857          */
  858         if (pmp->pm_fmod != 0) {
  859                 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
  860                         panic("msdosfs_sync: rofs mod");
  861                 else {
  862                         /* update fats here */
  863                 }
  864         }
  865         /*
  866          * Write back each (modified) denode.
  867          */
  868         MNT_ILOCK(mp);
  869 loop:
  870         MNT_VNODE_FOREACH(vp, mp, nvp) {
  871                 VI_LOCK(vp);
  872                 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED)) {
  873                         VI_UNLOCK(vp);
  874                         continue;
  875                 }
  876                 MNT_IUNLOCK(mp);
  877                 dep = VTODE(vp);
  878                 if ((dep->de_flag &
  879                     (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
  880                     (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
  881                     waitfor == MNT_LAZY)) {
  882                         VI_UNLOCK(vp);
  883                         MNT_ILOCK(mp);
  884                         continue;
  885                 }
  886                 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
  887                 if (error) {
  888                         MNT_ILOCK(mp);
  889                         if (error == ENOENT)
  890                                 goto loop;
  891                         continue;
  892                 }
  893                 error = VOP_FSYNC(vp, waitfor, td);
  894                 if (error)
  895                         allerror = error;
  896                 VOP_UNLOCK(vp, 0, td);
  897                 vrele(vp);
  898                 MNT_ILOCK(mp);
  899         }
  900         MNT_IUNLOCK(mp);
  901 
  902         /*
  903          * Flush filesystem control info.
  904          */
  905         if (waitfor != MNT_LAZY) {
  906                 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY, td);
  907                 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
  908                 if (error)
  909                         allerror = error;
  910                 VOP_UNLOCK(pmp->pm_devvp, 0, td);
  911         }
  912         return (allerror);
  913 }
  914 
  915 static int
  916 msdosfs_fhtovp(mp, fhp, vpp)
  917         struct mount *mp;
  918         struct fid *fhp;
  919         struct vnode **vpp;
  920 {
  921         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  922         struct defid *defhp = (struct defid *) fhp;
  923         struct denode *dep;
  924         int error;
  925 
  926         error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
  927         if (error) {
  928                 *vpp = NULLVP;
  929                 return (error);
  930         }
  931         *vpp = DETOV(dep);
  932         vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
  933         return (0);
  934 }
  935 
  936 static int
  937 msdosfs_vptofh(vp, fhp)
  938         struct vnode *vp;
  939         struct fid *fhp;
  940 {
  941         struct denode *dep;
  942         struct defid *defhp;
  943 
  944         dep = VTODE(vp);
  945         defhp = (struct defid *)fhp;
  946         defhp->defid_len = sizeof(struct defid);
  947         defhp->defid_dirclust = dep->de_dirclust;
  948         defhp->defid_dirofs = dep->de_diroffset;
  949         /* defhp->defid_gen = dep->de_gen; */
  950         return (0);
  951 }
  952 
  953 static struct vfsops msdosfs_vfsops = {
  954         .vfs_fhtovp =           msdosfs_fhtovp,
  955         .vfs_mount =            msdosfs_mount,
  956         .vfs_cmount =           msdosfs_cmount,
  957         .vfs_root =             msdosfs_root,
  958         .vfs_statfs =           msdosfs_statfs,
  959         .vfs_sync =             msdosfs_sync,
  960         .vfs_unmount =          msdosfs_unmount,
  961         .vfs_vptofh =           msdosfs_vptofh,
  962 };
  963 
  964 VFS_SET(msdosfs_vfsops, msdosfs, 0);
  965 MODULE_VERSION(msdosfs, 1);

Cache object: bff8fe4e36258aae5307eda3de729562


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.