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/10.4/sys/fs/msdosfs/msdosfs_vfsops.c 308552 2016-11-11 20:08:45Z kib $ */
    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/buf.h>
   54 #include <sys/conf.h>
   55 #include <sys/fcntl.h>
   56 #include <sys/iconv.h>
   57 #include <sys/kernel.h>
   58 #include <sys/lock.h>
   59 #include <sys/malloc.h>
   60 #include <sys/mount.h>
   61 #include <sys/mutex.h>
   62 #include <sys/namei.h>
   63 #include <sys/priv.h>
   64 #include <sys/proc.h>
   65 #include <sys/stat.h>
   66 #include <sys/vnode.h>
   67 
   68 #include <geom/geom.h>
   69 #include <geom/geom_vfs.h>
   70 
   71 #include <fs/msdosfs/bootsect.h>
   72 #include <fs/msdosfs/bpb.h>
   73 #include <fs/msdosfs/direntry.h>
   74 #include <fs/msdosfs/denode.h>
   75 #include <fs/msdosfs/fat.h>
   76 #include <fs/msdosfs/msdosfsmount.h>
   77 
   78 static const char msdosfs_lock_msg[] = "fatlk";
   79 
   80 /* Mount options that we support. */
   81 static const char *msdosfs_opts[] = {
   82         "async", "noatime", "noclusterr", "noclusterw",
   83         "export", "force", "from", "sync",
   84         "cs_dos", "cs_local", "cs_win", "dirmask",
   85         "gid", "kiconv", "large", "longname",
   86         "longnames", "mask", "shortname", "shortnames",
   87         "uid", "win95", "nowin95",
   88         NULL
   89 };
   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;
  106 
  107 static int      update_mp(struct mount *mp, struct thread *td);
  108 static int      mountmsdosfs(struct vnode *devvp, struct mount *mp);
  109 static vfs_fhtovp_t     msdosfs_fhtovp;
  110 static vfs_mount_t      msdosfs_mount;
  111 static vfs_root_t       msdosfs_root;
  112 static vfs_statfs_t     msdosfs_statfs;
  113 static vfs_sync_t       msdosfs_sync;
  114 static vfs_unmount_t    msdosfs_unmount;
  115 
  116 /* Maximum length of a character set name (arbitrary). */
  117 #define MAXCSLEN        64
  118 
  119 static int
  120 update_mp(struct mount *mp, struct thread *td)
  121 {
  122         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  123         void *dos, *win, *local;
  124         int error, v;
  125 
  126         if (!vfs_getopt(mp->mnt_optnew, "kiconv", NULL, NULL)) {
  127                 if (msdosfs_iconv != NULL) {
  128                         error = vfs_getopt(mp->mnt_optnew,
  129                             "cs_win", &win, NULL);
  130                         if (!error)
  131                                 error = vfs_getopt(mp->mnt_optnew,
  132                                     "cs_local", &local, NULL);
  133                         if (!error)
  134                                 error = vfs_getopt(mp->mnt_optnew,
  135                                     "cs_dos", &dos, NULL);
  136                         if (!error) {
  137                                 msdosfs_iconv->open(win, local, &pmp->pm_u2w);
  138                                 msdosfs_iconv->open(local, win, &pmp->pm_w2u);
  139                                 msdosfs_iconv->open(dos, local, &pmp->pm_u2d);
  140                                 msdosfs_iconv->open(local, dos, &pmp->pm_d2u);
  141                         }
  142                         if (error != 0)
  143                                 return (error);
  144                 } else {
  145                         pmp->pm_w2u = NULL;
  146                         pmp->pm_u2w = NULL;
  147                         pmp->pm_d2u = NULL;
  148                         pmp->pm_u2d = NULL;
  149                 }
  150         }
  151 
  152         if (vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v) == 1)
  153                 pmp->pm_gid = v;
  154         if (vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v) == 1)
  155                 pmp->pm_uid = v;
  156         if (vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v) == 1)
  157                 pmp->pm_mask = v & ALLPERMS;
  158         if (vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v) == 1)
  159                 pmp->pm_dirmask = v & ALLPERMS;
  160         vfs_flagopt(mp->mnt_optnew, "shortname",
  161             &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
  162         vfs_flagopt(mp->mnt_optnew, "shortnames",
  163             &pmp->pm_flags, MSDOSFSMNT_SHORTNAME);
  164         vfs_flagopt(mp->mnt_optnew, "longname",
  165             &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
  166         vfs_flagopt(mp->mnt_optnew, "longnames",
  167             &pmp->pm_flags, MSDOSFSMNT_LONGNAME);
  168         vfs_flagopt(mp->mnt_optnew, "kiconv",
  169             &pmp->pm_flags, MSDOSFSMNT_KICONV);
  170 
  171         if (vfs_getopt(mp->mnt_optnew, "nowin95", NULL, NULL) == 0)
  172                 pmp->pm_flags |= MSDOSFSMNT_NOWIN95;
  173         else
  174                 pmp->pm_flags &= ~MSDOSFSMNT_NOWIN95;
  175 
  176         if (pmp->pm_flags & MSDOSFSMNT_NOWIN95)
  177                 pmp->pm_flags |= MSDOSFSMNT_SHORTNAME;
  178         else
  179                 pmp->pm_flags |= MSDOSFSMNT_LONGNAME;
  180         return 0;
  181 }
  182 
  183 static int
  184 msdosfs_cmount(struct mntarg *ma, void *data, uint64_t flags)
  185 {
  186         struct msdosfs_args args;
  187         struct export_args exp;
  188         int error;
  189 
  190         if (data == NULL)
  191                 return (EINVAL);
  192         error = copyin(data, &args, sizeof args);
  193         if (error)
  194                 return (error);
  195         vfs_oexport_conv(&args.export, &exp);
  196 
  197         ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
  198         ma = mount_arg(ma, "export", &exp, sizeof(exp));
  199         ma = mount_argf(ma, "uid", "%d", args.uid);
  200         ma = mount_argf(ma, "gid", "%d", args.gid);
  201         ma = mount_argf(ma, "mask", "%d", args.mask);
  202         ma = mount_argf(ma, "dirmask", "%d", args.dirmask);
  203 
  204         ma = mount_argb(ma, args.flags & MSDOSFSMNT_SHORTNAME, "noshortname");
  205         ma = mount_argb(ma, args.flags & MSDOSFSMNT_LONGNAME, "nolongname");
  206         ma = mount_argb(ma, !(args.flags & MSDOSFSMNT_NOWIN95), "nowin95");
  207         ma = mount_argb(ma, args.flags & MSDOSFSMNT_KICONV, "nokiconv");
  208 
  209         ma = mount_argsu(ma, "cs_win", args.cs_win, MAXCSLEN);
  210         ma = mount_argsu(ma, "cs_dos", args.cs_dos, MAXCSLEN);
  211         ma = mount_argsu(ma, "cs_local", args.cs_local, MAXCSLEN);
  212 
  213         error = kernel_mount(ma, flags);
  214 
  215         return (error);
  216 }
  217 
  218 /*
  219  * mp - path - addr in user space of mount point (ie /usr or whatever)
  220  * data - addr in user space of mount params including the name of the block
  221  * special file to treat as a filesystem.
  222  */
  223 static int
  224 msdosfs_mount(struct mount *mp)
  225 {
  226         struct vnode *devvp;      /* vnode for blk device to mount */
  227         struct thread *td;
  228         /* msdosfs specific mount control block */
  229         struct msdosfsmount *pmp = NULL;
  230         struct nameidata ndp;
  231         int error, flags;
  232         accmode_t accmode;
  233         char *from;
  234 
  235         td = curthread;
  236         if (vfs_filteropt(mp->mnt_optnew, msdosfs_opts))
  237                 return (EINVAL);
  238 
  239         /*
  240          * If updating, check whether changing from read-only to
  241          * read/write; if there is no device name, that's all we do.
  242          */
  243         if (mp->mnt_flag & MNT_UPDATE) {
  244                 pmp = VFSTOMSDOSFS(mp);
  245                 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0)) {
  246                         /*
  247                          * Forbid export requests if filesystem has
  248                          * MSDOSFS_LARGEFS flag set.
  249                          */
  250                         if ((pmp->pm_flags & MSDOSFS_LARGEFS) != 0) {
  251                                 vfs_mount_error(mp,
  252                                     "MSDOSFS_LARGEFS flag set, cannot export");
  253                                 return (EOPNOTSUPP);
  254                         }
  255                 }
  256                 if (!(pmp->pm_flags & MSDOSFSMNT_RONLY) &&
  257                     vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
  258                         error = VFS_SYNC(mp, MNT_WAIT);
  259                         if (error)
  260                                 return (error);
  261                         flags = WRITECLOSE;
  262                         if (mp->mnt_flag & MNT_FORCE)
  263                                 flags |= FORCECLOSE;
  264                         error = vflush(mp, 0, flags, td);
  265                         if (error)
  266                                 return (error);
  267 
  268                         /*
  269                          * Now the volume is clean.  Mark it so while the
  270                          * device is still rw.
  271                          */
  272                         error = markvoldirty(pmp, 0);
  273                         if (error) {
  274                                 (void)markvoldirty(pmp, 1);
  275                                 return (error);
  276                         }
  277 
  278                         /* Downgrade the device from rw to ro. */
  279                         DROP_GIANT();
  280                         g_topology_lock();
  281                         error = g_access(pmp->pm_cp, 0, -1, 0);
  282                         g_topology_unlock();
  283                         PICKUP_GIANT();
  284                         if (error) {
  285                                 (void)markvoldirty(pmp, 1);
  286                                 return (error);
  287                         }
  288 
  289                         /*
  290                          * Backing out after an error was painful in the
  291                          * above.  Now we are committed to succeeding.
  292                          */
  293                         pmp->pm_fmod = 0;
  294                         pmp->pm_flags |= MSDOSFSMNT_RONLY;
  295                         MNT_ILOCK(mp);
  296                         mp->mnt_flag |= MNT_RDONLY;
  297                         MNT_IUNLOCK(mp);
  298                 } else if ((pmp->pm_flags & MSDOSFSMNT_RONLY) &&
  299                     !vfs_flagopt(mp->mnt_optnew, "ro", NULL, 0)) {
  300                         /*
  301                          * If upgrade to read-write by non-root, then verify
  302                          * that user has necessary permissions on the device.
  303                          */
  304                         devvp = pmp->pm_devvp;
  305                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
  306                         error = VOP_ACCESS(devvp, VREAD | VWRITE,
  307                             td->td_ucred, td);
  308                         if (error)
  309                                 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
  310                         if (error) {
  311                                 VOP_UNLOCK(devvp, 0);
  312                                 return (error);
  313                         }
  314                         VOP_UNLOCK(devvp, 0);
  315                         DROP_GIANT();
  316                         g_topology_lock();
  317                         error = g_access(pmp->pm_cp, 0, 1, 0);
  318                         g_topology_unlock();
  319                         PICKUP_GIANT();
  320                         if (error)
  321                                 return (error);
  322 
  323                         pmp->pm_fmod = 1;
  324                         pmp->pm_flags &= ~MSDOSFSMNT_RONLY;
  325                         MNT_ILOCK(mp);
  326                         mp->mnt_flag &= ~MNT_RDONLY;
  327                         MNT_IUNLOCK(mp);
  328 
  329                         /* Now that the volume is modifiable, mark it dirty. */
  330                         error = markvoldirty(pmp, 1);
  331                         if (error)
  332                                 return (error); 
  333                 }
  334         }
  335         /*
  336          * Not an update, or updating the name: look up the name
  337          * and verify that it refers to a sensible disk device.
  338          */
  339         if (vfs_getopt(mp->mnt_optnew, "from", (void **)&from, NULL))
  340                 return (EINVAL);
  341         NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, from, td);
  342         error = namei(&ndp);
  343         if (error)
  344                 return (error);
  345         devvp = ndp.ni_vp;
  346         NDFREE(&ndp, NDF_ONLY_PNBUF);
  347 
  348         if (!vn_isdisk(devvp, &error)) {
  349                 vput(devvp);
  350                 return (error);
  351         }
  352         /*
  353          * If mount by non-root, then verify that user has necessary
  354          * permissions on the device.
  355          */
  356         accmode = VREAD;
  357         if ((mp->mnt_flag & MNT_RDONLY) == 0)
  358                 accmode |= VWRITE;
  359         error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
  360         if (error)
  361                 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
  362         if (error) {
  363                 vput(devvp);
  364                 return (error);
  365         }
  366         if ((mp->mnt_flag & MNT_UPDATE) == 0) {
  367                 error = mountmsdosfs(devvp, mp);
  368 #ifdef MSDOSFS_DEBUG            /* only needed for the printf below */
  369                 pmp = VFSTOMSDOSFS(mp);
  370 #endif
  371         } else {
  372                 vput(devvp);
  373                 if (devvp != pmp->pm_devvp)
  374                         return (EINVAL);        /* XXX needs translation */
  375         }
  376         if (error) {
  377                 vrele(devvp);
  378                 return (error);
  379         }
  380 
  381         error = update_mp(mp, td);
  382         if (error) {
  383                 if ((mp->mnt_flag & MNT_UPDATE) == 0)
  384                         msdosfs_unmount(mp, MNT_FORCE);
  385                 return error;
  386         }
  387 
  388         if (devvp->v_type == VCHR && devvp->v_rdev != NULL)
  389                 devvp->v_rdev->si_mountpt = mp;
  390         vfs_mountedfrom(mp, from);
  391 #ifdef MSDOSFS_DEBUG
  392         printf("msdosfs_mount(): mp %p, pmp %p, inusemap %p\n", mp, pmp, pmp->pm_inusemap);
  393 #endif
  394         return (0);
  395 }
  396 
  397 static int
  398 mountmsdosfs(struct vnode *devvp, struct mount *mp)
  399 {
  400         struct msdosfsmount *pmp;
  401         struct buf *bp;
  402         struct cdev *dev;
  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         bp = NULL;              /* This and pmp both used in error_exit. */
  414         pmp = NULL;
  415         ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
  416 
  417         dev = devvp->v_rdev;
  418         dev_ref(dev);
  419         DROP_GIANT();
  420         g_topology_lock();
  421         error = g_vfs_open(devvp, &cp, "msdosfs", ronly ? 0 : 1);
  422         g_topology_unlock();
  423         PICKUP_GIANT();
  424         VOP_UNLOCK(devvp, 0);
  425         if (error)
  426                 goto error_exit;
  427 
  428         bo = &devvp->v_bufobj;
  429 
  430         /*
  431          * Read the boot sector of the filesystem, and then check the
  432          * boot signature.  If not a dos boot sector then error out.
  433          *
  434          * NOTE: 8192 is a magic size that works for ffs.
  435          */
  436         error = bread(devvp, 0, 8192, NOCRED, &bp);
  437         if (error)
  438                 goto error_exit;
  439         bp->b_flags |= B_AGE;
  440         bsp = (union bootsector *)bp->b_data;
  441         b33 = (struct byte_bpb33 *)bsp->bs33.bsBPB;
  442         b50 = (struct byte_bpb50 *)bsp->bs50.bsBPB;
  443         b710 = (struct byte_bpb710 *)bsp->bs710.bsBPB;
  444 
  445 #ifndef MSDOSFS_NOCHECKSIG
  446         if (bsp->bs50.bsBootSectSig0 != BOOTSIG0
  447             || bsp->bs50.bsBootSectSig1 != BOOTSIG1) {
  448                 error = EINVAL;
  449                 goto error_exit;
  450         }
  451 #endif
  452 
  453         pmp = malloc(sizeof *pmp, M_MSDOSFSMNT, M_WAITOK | M_ZERO);
  454         pmp->pm_mountp = mp;
  455         pmp->pm_cp = cp;
  456         pmp->pm_bo = bo;
  457 
  458         lockinit(&pmp->pm_fatlock, 0, msdosfs_lock_msg, 0, 0);
  459 
  460         /*
  461          * Initialize ownerships and permissions, since nothing else will
  462          * initialize them iff we are mounting root.
  463          */
  464         pmp->pm_uid = UID_ROOT;
  465         pmp->pm_gid = GID_WHEEL;
  466         pmp->pm_mask = pmp->pm_dirmask = S_IXUSR | S_IXGRP | S_IXOTH |
  467             S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR;
  468 
  469         /*
  470          * Experimental support for large MS-DOS filesystems.
  471          * WARNING: This uses at least 32 bytes of kernel memory (which is not
  472          * reclaimed until the FS is unmounted) for each file on disk to map
  473          * between the 32-bit inode numbers used by VFS and the 64-bit
  474          * pseudo-inode numbers used internally by msdosfs. This is only
  475          * safe to use in certain controlled situations (e.g. read-only FS
  476          * with less than 1 million files).
  477          * Since the mappings do not persist across unmounts (or reboots), these
  478          * filesystems are not suitable for exporting through NFS, or any other
  479          * application that requires fixed inode numbers.
  480          */
  481         vfs_flagopt(mp->mnt_optnew, "large", &pmp->pm_flags, MSDOSFS_LARGEFS);
  482 
  483         /*
  484          * Compute several useful quantities from the bpb in the
  485          * bootsector.  Copy in the dos 5 variant of the bpb then fix up
  486          * the fields that are different between dos 5 and dos 3.3.
  487          */
  488         SecPerClust = b50->bpbSecPerClust;
  489         pmp->pm_BytesPerSec = getushort(b50->bpbBytesPerSec);
  490         if (pmp->pm_BytesPerSec < DEV_BSIZE) {
  491                 error = EINVAL;
  492                 goto error_exit;
  493         }
  494         pmp->pm_ResSectors = getushort(b50->bpbResSectors);
  495         pmp->pm_FATs = b50->bpbFATs;
  496         pmp->pm_RootDirEnts = getushort(b50->bpbRootDirEnts);
  497         pmp->pm_Sectors = getushort(b50->bpbSectors);
  498         pmp->pm_FATsecs = getushort(b50->bpbFATsecs);
  499         pmp->pm_SecPerTrack = getushort(b50->bpbSecPerTrack);
  500         pmp->pm_Heads = getushort(b50->bpbHeads);
  501         pmp->pm_Media = b50->bpbMedia;
  502 
  503         /* calculate the ratio of sector size to DEV_BSIZE */
  504         pmp->pm_BlkPerSec = pmp->pm_BytesPerSec / DEV_BSIZE;
  505 
  506         /*
  507          * We don't check pm_Heads nor pm_SecPerTrack, because
  508          * these may not be set for EFI file systems. We don't
  509          * use these anyway, so we're unaffected if they are
  510          * invalid.
  511          */
  512         if (!pmp->pm_BytesPerSec || !SecPerClust) {
  513                 error = EINVAL;
  514                 goto error_exit;
  515         }
  516 
  517         if (pmp->pm_Sectors == 0) {
  518                 pmp->pm_HiddenSects = getulong(b50->bpbHiddenSecs);
  519                 pmp->pm_HugeSectors = getulong(b50->bpbHugeSectors);
  520         } else {
  521                 pmp->pm_HiddenSects = getushort(b33->bpbHiddenSecs);
  522                 pmp->pm_HugeSectors = pmp->pm_Sectors;
  523         }
  524         if (!(pmp->pm_flags & MSDOSFS_LARGEFS)) {
  525                 if (pmp->pm_HugeSectors > 0xffffffff /
  526                     (pmp->pm_BytesPerSec / sizeof(struct direntry)) + 1) {
  527                         /*
  528                          * We cannot deal currently with this size of disk
  529                          * due to fileid limitations (see msdosfs_getattr and
  530                          * msdosfs_readdir)
  531                          */
  532                         error = EINVAL;
  533                         vfs_mount_error(mp,
  534                             "Disk too big, try '-o large' mount option");
  535                         goto error_exit;
  536                 }
  537         }
  538 
  539         if (pmp->pm_RootDirEnts == 0) {
  540                 if (pmp->pm_FATsecs
  541                     || getushort(b710->bpbFSVers)) {
  542                         error = EINVAL;
  543 #ifdef MSDOSFS_DEBUG
  544                         printf("mountmsdosfs(): bad FAT32 filesystem\n");
  545 #endif
  546                         goto error_exit;
  547                 }
  548                 pmp->pm_fatmask = FAT32_MASK;
  549                 pmp->pm_fatmult = 4;
  550                 pmp->pm_fatdiv = 1;
  551                 pmp->pm_FATsecs = getulong(b710->bpbBigFATsecs);
  552                 if (getushort(b710->bpbExtFlags) & FATMIRROR)
  553                         pmp->pm_curfat = getushort(b710->bpbExtFlags) & FATNUM;
  554                 else
  555                         pmp->pm_flags |= MSDOSFS_FATMIRROR;
  556         } else
  557                 pmp->pm_flags |= MSDOSFS_FATMIRROR;
  558 
  559         /*
  560          * Check a few values (could do some more):
  561          * - logical sector size: power of 2, >= block size
  562          * - sectors per cluster: power of 2, >= 1
  563          * - number of sectors:   >= 1, <= size of partition
  564          * - number of FAT sectors: >= 1
  565          */
  566         if ( (SecPerClust == 0)
  567           || (SecPerClust & (SecPerClust - 1))
  568           || (pmp->pm_BytesPerSec < DEV_BSIZE)
  569           || (pmp->pm_BytesPerSec & (pmp->pm_BytesPerSec - 1))
  570           || (pmp->pm_HugeSectors == 0)
  571           || (pmp->pm_FATsecs == 0)
  572           || (SecPerClust * pmp->pm_BlkPerSec > MAXBSIZE / DEV_BSIZE)
  573         ) {
  574                 error = EINVAL;
  575                 goto error_exit;
  576         }
  577 
  578         pmp->pm_HugeSectors *= pmp->pm_BlkPerSec;
  579         pmp->pm_HiddenSects *= pmp->pm_BlkPerSec;       /* XXX not used? */
  580         pmp->pm_FATsecs     *= pmp->pm_BlkPerSec;
  581         SecPerClust         *= pmp->pm_BlkPerSec;
  582 
  583         pmp->pm_fatblk = pmp->pm_ResSectors * pmp->pm_BlkPerSec;
  584 
  585         if (FAT32(pmp)) {
  586                 pmp->pm_rootdirblk = getulong(b710->bpbRootClust);
  587                 pmp->pm_firstcluster = pmp->pm_fatblk
  588                         + (pmp->pm_FATs * pmp->pm_FATsecs);
  589                 pmp->pm_fsinfo = getushort(b710->bpbFSInfo) * pmp->pm_BlkPerSec;
  590         } else {
  591                 pmp->pm_rootdirblk = pmp->pm_fatblk +
  592                         (pmp->pm_FATs * pmp->pm_FATsecs);
  593                 pmp->pm_rootdirsize = (pmp->pm_RootDirEnts * sizeof(struct direntry)
  594                                        + DEV_BSIZE - 1)
  595                         / DEV_BSIZE; /* in blocks */
  596                 pmp->pm_firstcluster = pmp->pm_rootdirblk + pmp->pm_rootdirsize;
  597         }
  598 
  599         pmp->pm_maxcluster = (pmp->pm_HugeSectors - pmp->pm_firstcluster) /
  600             SecPerClust + 1;
  601         pmp->pm_fatsize = pmp->pm_FATsecs * DEV_BSIZE;  /* XXX not used? */
  602 
  603         if (pmp->pm_fatmask == 0) {
  604                 if (pmp->pm_maxcluster
  605                     <= ((CLUST_RSRVD - CLUST_FIRST) & FAT12_MASK)) {
  606                         /*
  607                          * This will usually be a floppy disk. This size makes
  608                          * sure that one fat entry will not be split across
  609                          * multiple blocks.
  610                          */
  611                         pmp->pm_fatmask = FAT12_MASK;
  612                         pmp->pm_fatmult = 3;
  613                         pmp->pm_fatdiv = 2;
  614                 } else {
  615                         pmp->pm_fatmask = FAT16_MASK;
  616                         pmp->pm_fatmult = 2;
  617                         pmp->pm_fatdiv = 1;
  618                 }
  619         }
  620 
  621         clusters = (pmp->pm_fatsize / pmp->pm_fatmult) * pmp->pm_fatdiv;
  622         if (pmp->pm_maxcluster >= clusters) {
  623 #ifdef MSDOSFS_DEBUG
  624                 printf("Warning: number of clusters (%ld) exceeds FAT "
  625                     "capacity (%ld)\n", pmp->pm_maxcluster + 1, clusters);
  626 #endif
  627                 pmp->pm_maxcluster = clusters - 1;
  628         }
  629 
  630         if (FAT12(pmp))
  631                 pmp->pm_fatblocksize = 3 * 512;
  632         else
  633                 pmp->pm_fatblocksize = PAGE_SIZE;
  634         pmp->pm_fatblocksize = roundup(pmp->pm_fatblocksize,
  635             pmp->pm_BytesPerSec);
  636         pmp->pm_fatblocksec = pmp->pm_fatblocksize / DEV_BSIZE;
  637         pmp->pm_bnshift = ffs(DEV_BSIZE) - 1;
  638 
  639         /*
  640          * Compute mask and shift value for isolating cluster relative byte
  641          * offsets and cluster numbers from a file offset.
  642          */
  643         pmp->pm_bpcluster = SecPerClust * DEV_BSIZE;
  644         pmp->pm_crbomask = pmp->pm_bpcluster - 1;
  645         pmp->pm_cnshift = ffs(pmp->pm_bpcluster) - 1;
  646 
  647         /*
  648          * Check for valid cluster size
  649          * must be a power of 2
  650          */
  651         if (pmp->pm_bpcluster ^ (1 << pmp->pm_cnshift)) {
  652                 error = EINVAL;
  653                 goto error_exit;
  654         }
  655 
  656         /*
  657          * Release the bootsector buffer.
  658          */
  659         brelse(bp);
  660         bp = NULL;
  661 
  662         /*
  663          * Check the fsinfo sector if we have one.  Silently fix up our
  664          * in-core copy of fp->fsinxtfree if it is unknown (0xffffffff)
  665          * or too large.  Ignore fp->fsinfree for now, since we need to
  666          * read the entire FAT anyway to fill the inuse map.
  667          */
  668         if (pmp->pm_fsinfo) {
  669                 struct fsinfo *fp;
  670 
  671                 if ((error = bread(devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
  672                     NOCRED, &bp)) != 0)
  673                         goto error_exit;
  674                 fp = (struct fsinfo *)bp->b_data;
  675                 if (!bcmp(fp->fsisig1, "RRaA", 4)
  676                     && !bcmp(fp->fsisig2, "rrAa", 4)
  677                     && !bcmp(fp->fsisig3, "\0\0\125\252", 4)) {
  678                         pmp->pm_nxtfree = getulong(fp->fsinxtfree);
  679                         if (pmp->pm_nxtfree > pmp->pm_maxcluster)
  680                                 pmp->pm_nxtfree = CLUST_FIRST;
  681                 } else
  682                         pmp->pm_fsinfo = 0;
  683                 brelse(bp);
  684                 bp = NULL;
  685         }
  686 
  687         /*
  688          * Finish initializing pmp->pm_nxtfree (just in case the first few
  689          * sectors aren't properly reserved in the FAT).  This completes
  690          * the fixup for fp->fsinxtfree, and fixes up the zero-initialized
  691          * value if there is no fsinfo.  We will use pmp->pm_nxtfree
  692          * internally even if there is no fsinfo.
  693          */
  694         if (pmp->pm_nxtfree < CLUST_FIRST)
  695                 pmp->pm_nxtfree = CLUST_FIRST;
  696 
  697         /*
  698          * Allocate memory for the bitmap of allocated clusters, and then
  699          * fill it in.
  700          */
  701         pmp->pm_inusemap = malloc(howmany(pmp->pm_maxcluster + 1, N_INUSEBITS)
  702                                   * sizeof(*pmp->pm_inusemap),
  703                                   M_MSDOSFSFAT, M_WAITOK);
  704 
  705         /*
  706          * fillinusemap() needs pm_devvp.
  707          */
  708         pmp->pm_devvp = devvp;
  709         pmp->pm_dev = dev;
  710 
  711         /*
  712          * Have the inuse map filled in.
  713          */
  714         MSDOSFS_LOCK_MP(pmp);
  715         error = fillinusemap(pmp);
  716         MSDOSFS_UNLOCK_MP(pmp);
  717         if (error != 0)
  718                 goto error_exit;
  719 
  720         /*
  721          * If they want fat updates to be synchronous then let them suffer
  722          * the performance degradation in exchange for the on disk copy of
  723          * the fat being correct just about all the time.  I suppose this
  724          * would be a good thing to turn on if the kernel is still flakey.
  725          */
  726         if (mp->mnt_flag & MNT_SYNCHRONOUS)
  727                 pmp->pm_flags |= MSDOSFSMNT_WAITONFAT;
  728 
  729         /*
  730          * Finish up.
  731          */
  732         if (ronly)
  733                 pmp->pm_flags |= MSDOSFSMNT_RONLY;
  734         else {
  735                 if ((error = markvoldirty(pmp, 1)) != 0) {
  736                         (void)markvoldirty(pmp, 0);
  737                         goto error_exit;
  738                 }
  739                 pmp->pm_fmod = 1;
  740         }
  741         mp->mnt_data =  pmp;
  742         mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
  743         mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
  744         MNT_ILOCK(mp);
  745         mp->mnt_flag |= MNT_LOCAL;
  746         mp->mnt_kern_flag |= MNTK_USES_BCACHE | MNTK_NO_IOPF;
  747         MNT_IUNLOCK(mp);
  748 
  749         if (pmp->pm_flags & MSDOSFS_LARGEFS)
  750                 msdosfs_fileno_init(mp);
  751 
  752         return 0;
  753 
  754 error_exit:
  755         if (bp)
  756                 brelse(bp);
  757         if (cp != NULL) {
  758                 DROP_GIANT();
  759                 g_topology_lock();
  760                 g_vfs_close(cp);
  761                 g_topology_unlock();
  762                 PICKUP_GIANT();
  763         }
  764         if (pmp) {
  765                 lockdestroy(&pmp->pm_fatlock);
  766                 free(pmp->pm_inusemap, M_MSDOSFSFAT);
  767                 free(pmp, M_MSDOSFSMNT);
  768                 mp->mnt_data = NULL;
  769         }
  770         dev_rel(dev);
  771         return (error);
  772 }
  773 
  774 /*
  775  * Unmount the filesystem described by mp.
  776  */
  777 static int
  778 msdosfs_unmount(struct mount *mp, int mntflags)
  779 {
  780         struct msdosfsmount *pmp;
  781         int error, flags;
  782 
  783         error = flags = 0;
  784         pmp = VFSTOMSDOSFS(mp);
  785         if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0)
  786                 error = msdosfs_sync(mp, MNT_WAIT);
  787         if ((mntflags & MNT_FORCE) != 0)
  788                 flags |= FORCECLOSE;
  789         else if (error != 0)
  790                 return (error);
  791         error = vflush(mp, 0, flags, curthread);
  792         if (error != 0 && error != ENXIO)
  793                 return (error);
  794         if ((pmp->pm_flags & MSDOSFSMNT_RONLY) == 0) {
  795                 error = markvoldirty(pmp, 0);
  796                 if (error && error != ENXIO) {
  797                         (void)markvoldirty(pmp, 1);
  798                         return (error);
  799                 }
  800         }
  801         if (pmp->pm_flags & MSDOSFSMNT_KICONV && msdosfs_iconv) {
  802                 if (pmp->pm_w2u)
  803                         msdosfs_iconv->close(pmp->pm_w2u);
  804                 if (pmp->pm_u2w)
  805                         msdosfs_iconv->close(pmp->pm_u2w);
  806                 if (pmp->pm_d2u)
  807                         msdosfs_iconv->close(pmp->pm_d2u);
  808                 if (pmp->pm_u2d)
  809                         msdosfs_iconv->close(pmp->pm_u2d);
  810         }
  811 
  812 #ifdef MSDOSFS_DEBUG
  813         {
  814                 struct vnode *vp = pmp->pm_devvp;
  815                 struct bufobj *bo;
  816 
  817                 bo = &vp->v_bufobj;
  818                 BO_LOCK(bo);
  819                 VI_LOCK(vp);
  820                 vn_printf(vp,
  821                     "msdosfs_umount(): just before calling VOP_CLOSE()\n");
  822                 printf("freef %p, freeb %p, mount %p\n",
  823                     TAILQ_NEXT(vp, v_actfreelist), vp->v_actfreelist.tqe_prev,
  824                     vp->v_mount);
  825                 printf("cleanblkhd %p, dirtyblkhd %p, numoutput %ld, type %d\n",
  826                     TAILQ_FIRST(&vp->v_bufobj.bo_clean.bv_hd),
  827                     TAILQ_FIRST(&vp->v_bufobj.bo_dirty.bv_hd),
  828                     vp->v_bufobj.bo_numoutput, vp->v_type);
  829                 VI_UNLOCK(vp);
  830                 BO_UNLOCK(bo);
  831         }
  832 #endif
  833         DROP_GIANT();
  834         if (pmp->pm_devvp->v_type == VCHR && pmp->pm_devvp->v_rdev != NULL)
  835                 pmp->pm_devvp->v_rdev->si_mountpt = NULL;
  836         g_topology_lock();
  837         g_vfs_close(pmp->pm_cp);
  838         g_topology_unlock();
  839         PICKUP_GIANT();
  840         vrele(pmp->pm_devvp);
  841         dev_rel(pmp->pm_dev);
  842         free(pmp->pm_inusemap, M_MSDOSFSFAT);
  843         if (pmp->pm_flags & MSDOSFS_LARGEFS)
  844                 msdosfs_fileno_free(mp);
  845         lockdestroy(&pmp->pm_fatlock);
  846         free(pmp, M_MSDOSFSMNT);
  847         mp->mnt_data = NULL;
  848         MNT_ILOCK(mp);
  849         mp->mnt_flag &= ~MNT_LOCAL;
  850         MNT_IUNLOCK(mp);
  851         return (error);
  852 }
  853 
  854 static int
  855 msdosfs_root(struct mount *mp, int flags, struct vnode **vpp)
  856 {
  857         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  858         struct denode *ndep;
  859         int error;
  860 
  861 #ifdef MSDOSFS_DEBUG
  862         printf("msdosfs_root(); mp %p, pmp %p\n", mp, pmp);
  863 #endif
  864         error = deget(pmp, MSDOSFSROOT, MSDOSFSROOT_OFS, &ndep);
  865         if (error)
  866                 return (error);
  867         *vpp = DETOV(ndep);
  868         return (0);
  869 }
  870 
  871 static int
  872 msdosfs_statfs(struct mount *mp, struct statfs *sbp)
  873 {
  874         struct msdosfsmount *pmp;
  875 
  876         pmp = VFSTOMSDOSFS(mp);
  877         sbp->f_bsize = pmp->pm_bpcluster;
  878         sbp->f_iosize = pmp->pm_bpcluster;
  879         sbp->f_blocks = pmp->pm_maxcluster + 1;
  880         sbp->f_bfree = pmp->pm_freeclustercount;
  881         sbp->f_bavail = pmp->pm_freeclustercount;
  882         sbp->f_files = pmp->pm_RootDirEnts;     /* XXX */
  883         sbp->f_ffree = 0;       /* what to put in here? */
  884         return (0);
  885 }
  886 
  887 /*
  888  * If we have an FSInfo block, update it.
  889  */
  890 static int
  891 msdosfs_fsiflush(struct msdosfsmount *pmp, int waitfor)
  892 {
  893         struct fsinfo *fp;
  894         struct buf *bp;
  895         int error;
  896 
  897         MSDOSFS_LOCK_MP(pmp);
  898         if (pmp->pm_fsinfo == 0 || (pmp->pm_flags & MSDOSFS_FSIMOD) == 0) {
  899                 error = 0;
  900                 goto unlock;
  901         }
  902         error = bread(pmp->pm_devvp, pmp->pm_fsinfo, pmp->pm_BytesPerSec,
  903             NOCRED, &bp);
  904         if (error != 0) {
  905                 brelse(bp);
  906                 goto unlock;
  907         }
  908         fp = (struct fsinfo *)bp->b_data;
  909         putulong(fp->fsinfree, pmp->pm_freeclustercount);
  910         putulong(fp->fsinxtfree, pmp->pm_nxtfree);
  911         pmp->pm_flags &= ~MSDOSFS_FSIMOD;
  912         if (waitfor == MNT_WAIT)
  913                 error = bwrite(bp);
  914         else
  915                 bawrite(bp);
  916 unlock:
  917         MSDOSFS_UNLOCK_MP(pmp);
  918         return (error);
  919 }
  920 
  921 static int
  922 msdosfs_sync(struct mount *mp, int waitfor)
  923 {
  924         struct vnode *vp, *nvp;
  925         struct thread *td;
  926         struct denode *dep;
  927         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  928         int error, allerror = 0;
  929 
  930         td = curthread;
  931 
  932         /*
  933          * If we ever switch to not updating all of the fats all the time,
  934          * this would be the place to update them from the first one.
  935          */
  936         if (pmp->pm_fmod != 0) {
  937                 if (pmp->pm_flags & MSDOSFSMNT_RONLY)
  938                         panic("msdosfs_sync: rofs mod");
  939                 else {
  940                         /* update fats here */
  941                 }
  942         }
  943         /*
  944          * Write back each (modified) denode.
  945          */
  946 loop:
  947         MNT_VNODE_FOREACH_ALL(vp, mp, nvp) {
  948                 if (vp->v_type == VNON) {
  949                         VI_UNLOCK(vp);
  950                         continue;
  951                 }
  952                 dep = VTODE(vp);
  953                 if ((dep->de_flag &
  954                     (DE_ACCESS | DE_CREATE | DE_UPDATE | DE_MODIFIED)) == 0 &&
  955                     (vp->v_bufobj.bo_dirty.bv_cnt == 0 ||
  956                     waitfor == MNT_LAZY)) {
  957                         VI_UNLOCK(vp);
  958                         continue;
  959                 }
  960                 error = vget(vp, LK_EXCLUSIVE | LK_NOWAIT | LK_INTERLOCK, td);
  961                 if (error) {
  962                         if (error == ENOENT)
  963                                 goto loop;
  964                         continue;
  965                 }
  966                 error = VOP_FSYNC(vp, waitfor, td);
  967                 if (error)
  968                         allerror = error;
  969                 VOP_UNLOCK(vp, 0);
  970                 vrele(vp);
  971         }
  972 
  973         /*
  974          * Flush filesystem control info.
  975          */
  976         if (waitfor != MNT_LAZY) {
  977                 vn_lock(pmp->pm_devvp, LK_EXCLUSIVE | LK_RETRY);
  978                 error = VOP_FSYNC(pmp->pm_devvp, waitfor, td);
  979                 if (error)
  980                         allerror = error;
  981                 VOP_UNLOCK(pmp->pm_devvp, 0);
  982         }
  983 
  984         error = msdosfs_fsiflush(pmp, waitfor);
  985         if (error != 0)
  986                 allerror = error;
  987         return (allerror);
  988 }
  989 
  990 static int
  991 msdosfs_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
  992 {
  993         struct msdosfsmount *pmp = VFSTOMSDOSFS(mp);
  994         struct defid *defhp = (struct defid *) fhp;
  995         struct denode *dep;
  996         int error;
  997 
  998         error = deget(pmp, defhp->defid_dirclust, defhp->defid_dirofs, &dep);
  999         if (error) {
 1000                 *vpp = NULLVP;
 1001                 return (error);
 1002         }
 1003         *vpp = DETOV(dep);
 1004         vnode_create_vobject(*vpp, dep->de_FileSize, curthread);
 1005         return (0);
 1006 }
 1007 
 1008 static struct vfsops msdosfs_vfsops = {
 1009         .vfs_fhtovp =           msdosfs_fhtovp,
 1010         .vfs_mount =            msdosfs_mount,
 1011         .vfs_cmount =           msdosfs_cmount,
 1012         .vfs_root =             msdosfs_root,
 1013         .vfs_statfs =           msdosfs_statfs,
 1014         .vfs_sync =             msdosfs_sync,
 1015         .vfs_unmount =          msdosfs_unmount,
 1016 };
 1017 
 1018 VFS_SET(msdosfs_vfsops, msdosfs, 0);
 1019 MODULE_VERSION(msdosfs, 1);

Cache object: cacd02f72f4d34de7e4421350d2a47b2


[ 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.