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_lookup.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$ */
    2 /*      $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $   */
    3 
    4 /*-
    5  * SPDX-License-Identifier: BSD-4-Clause
    6  *
    7  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
    8  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
    9  * All rights reserved.
   10  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
   11  *
   12  * Redistribution and use in source and binary forms, with or without
   13  * modification, are permitted provided that the following conditions
   14  * are met:
   15  * 1. Redistributions of source code must retain the above copyright
   16  *    notice, this list of conditions and the following disclaimer.
   17  * 2. Redistributions in binary form must reproduce the above copyright
   18  *    notice, this list of conditions and the following disclaimer in the
   19  *    documentation and/or other materials provided with the distribution.
   20  * 3. All advertising materials mentioning features or use of this software
   21  *    must display the following acknowledgement:
   22  *      This product includes software developed by TooLs GmbH.
   23  * 4. The name of TooLs GmbH may not be used to endorse or promote products
   24  *    derived from this software without specific prior written permission.
   25  *
   26  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
   27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   29  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
   32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
   34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
   35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   36  */
   37 /*-
   38  * Written by Paul Popelka (paulp@uts.amdahl.com)
   39  *
   40  * You can do anything you want with this software, just don't say you wrote
   41  * it, and don't remove this notice.
   42  *
   43  * This software is provided "as is".
   44  *
   45  * The author supplies this software to be publicly redistributed on the
   46  * understanding that the author is not responsible for the correct
   47  * functioning of this software in any circumstances and is not liable for
   48  * any damages caused by this software.
   49  *
   50  * October 1992
   51  */
   52 
   53 #include <sys/param.h>
   54 #include <sys/systm.h>
   55 #include <sys/buf.h>
   56 #include <sys/mount.h>
   57 #include <sys/namei.h>
   58 #include <sys/vnode.h>
   59 
   60 #include <fs/msdosfs/bpb.h>
   61 #include <fs/msdosfs/direntry.h>
   62 #include <fs/msdosfs/denode.h>
   63 #include <fs/msdosfs/fat.h>
   64 #include <fs/msdosfs/msdosfsmount.h>
   65 
   66 static int
   67 msdosfs_lookup_checker(struct msdosfsmount *pmp, struct vnode *dvp,
   68     struct denode *tdp, struct vnode **vpp)
   69 {
   70         struct vnode *vp;
   71 
   72         vp = DETOV(tdp);
   73 
   74         /*
   75          * Lookup assumes that directory cannot be hardlinked.
   76          * Corrupted msdosfs filesystem could break this assumption.
   77          */
   78         if (vp == dvp) {
   79                 vput(vp);
   80                 msdosfs_integrity_error(pmp);
   81                 *vpp = NULL;
   82                 return (EBADF);
   83         }
   84 
   85         *vpp = vp;
   86         return (0);
   87 }
   88 
   89 int
   90 msdosfs_lookup(struct vop_cachedlookup_args *ap)
   91 {
   92 
   93         return (msdosfs_lookup_ino(ap->a_dvp, ap->a_vpp, ap->a_cnp, NULL,
   94             NULL));
   95 }
   96 
   97 struct deget_dotdot {
   98         u_long cluster;
   99         int blkoff;
  100 };
  101 
  102 static int
  103 msdosfs_deget_dotdot(struct mount *mp, void *arg, int lkflags,
  104     struct vnode **rvp)
  105 {
  106         struct deget_dotdot *dd_arg;
  107         struct denode *rdp;
  108         struct msdosfsmount *pmp;
  109         int error;
  110 
  111         pmp = VFSTOMSDOSFS(mp);
  112         dd_arg = arg;
  113         error = deget(pmp, dd_arg->cluster, dd_arg->blkoff,
  114             LK_EXCLUSIVE, &rdp);
  115         if (error == 0)
  116                 *rvp = DETOV(rdp);
  117         return (error);
  118 }
  119 
  120 /*
  121  * When we search a directory the blocks containing directory entries are
  122  * read and examined.  The directory entries contain information that would
  123  * normally be in the inode of a unix filesystem.  This means that some of
  124  * a directory's contents may also be in memory resident denodes (sort of
  125  * an inode).  This can cause problems if we are searching while some other
  126  * process is modifying a directory.  To prevent one process from accessing
  127  * incompletely modified directory information we depend upon being the
  128  * sole owner of a directory block.  bread/brelse provide this service.
  129  * This being the case, when a process modifies a directory it must first
  130  * acquire the disk block that contains the directory entry to be modified.
  131  * Then update the disk block and the denode, and then write the disk block
  132  * out to disk.  This way disk blocks containing directory entries and in
  133  * memory denode's will be in synch.
  134  */
  135 int
  136 msdosfs_lookup_ino(struct vnode *vdp, struct vnode **vpp, struct componentname
  137     *cnp, daddr_t *scnp, u_long *blkoffp)
  138 {
  139         struct mbnambuf nb;
  140         daddr_t bn;
  141         int error;
  142         int slotcount;
  143         int slotoffset = 0;
  144         int frcn;
  145         u_long cluster;
  146         u_long blkoff;
  147         int diroff;
  148         int blsize;
  149         int isadir;             /* ~0 if found direntry is a directory   */
  150         daddr_t scn;            /* starting cluster number               */
  151         struct vnode *pdp;
  152         struct denode *dp;
  153         struct denode *tdp;
  154         struct msdosfsmount *pmp;
  155         struct buf *bp = NULL;
  156         struct direntry *dep = NULL;
  157         struct deget_dotdot dd_arg;
  158         u_char dosfilename[12];
  159         int flags = cnp->cn_flags;
  160         int nameiop = cnp->cn_nameiop;
  161         int unlen;
  162         uint64_t inode1;
  163 
  164         int wincnt = 1;
  165         int chksum = -1, chksum_ok;
  166         int olddos = 1;
  167 
  168 #ifdef MSDOSFS_DEBUG
  169         printf("msdosfs_lookup(): looking for %s\n", cnp->cn_nameptr);
  170 #endif
  171         dp = VTODE(vdp);
  172         pmp = dp->de_pmp;
  173 #ifdef MSDOSFS_DEBUG
  174         printf("msdosfs_lookup(): vdp %p, dp %p, Attr %02x\n",
  175             vdp, dp, dp->de_Attributes);
  176 #endif
  177 
  178  restart:
  179         if (vpp != NULL)
  180                 *vpp = NULL;
  181         /*
  182          * If they are going after the . or .. entry in the root directory,
  183          * they won't find it.  DOS filesystems don't have them in the root
  184          * directory.  So, we fake it. deget() is in on this scam too.
  185          */
  186         if ((vdp->v_vflag & VV_ROOT) && cnp->cn_nameptr[0] == '.' &&
  187             (cnp->cn_namelen == 1 ||
  188                 (cnp->cn_namelen == 2 && cnp->cn_nameptr[1] == '.'))) {
  189                 isadir = ATTR_DIRECTORY;
  190                 scn = MSDOSFSROOT;
  191 #ifdef MSDOSFS_DEBUG
  192                 printf("msdosfs_lookup(): looking for . or .. in root directory\n");
  193 #endif
  194                 cluster = MSDOSFSROOT;
  195                 blkoff = MSDOSFSROOT_OFS;
  196                 goto foundroot;
  197         }
  198 
  199         switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
  200             cnp->cn_namelen, 0, pmp)) {
  201         case 0:
  202                 return (EINVAL);
  203         case 1:
  204                 break;
  205         case 2:
  206                 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
  207                     cnp->cn_namelen, pmp) + 1;
  208                 break;
  209         case 3:
  210                 olddos = 0;
  211                 wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
  212                     cnp->cn_namelen, pmp) + 1;
  213                 break;
  214         }
  215         if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME) {
  216                 wincnt = 1;
  217                 olddos = 1;
  218         }
  219         unlen = winLenFixup(cnp->cn_nameptr, cnp->cn_namelen);
  220 
  221         /*
  222          * Suppress search for slots unless creating
  223          * file and at end of pathname, in which case
  224          * we watch for a place to put the new file in
  225          * case it doesn't already exist.
  226          */
  227         slotcount = wincnt;
  228         if ((nameiop == CREATE || nameiop == RENAME) &&
  229             (flags & ISLASTCN))
  230                 slotcount = 0;
  231 
  232 #ifdef MSDOSFS_DEBUG
  233         printf("msdosfs_lookup(): dos version of filename %s, length %ld\n",
  234             dosfilename, cnp->cn_namelen);
  235 #endif
  236         /*
  237          * Search the directory pointed at by vdp for the name pointed at
  238          * by cnp->cn_nameptr.
  239          */
  240         tdp = NULL;
  241         mbnambuf_init(&nb);
  242         /*
  243          * The outer loop ranges over the clusters that make up the
  244          * directory.  Note that the root directory is different from all
  245          * other directories.  It has a fixed number of blocks that are not
  246          * part of the pool of allocatable clusters.  So, we treat it a
  247          * little differently. The root directory starts at "cluster" 0.
  248          */
  249         diroff = 0;
  250         for (frcn = 0;; frcn++) {
  251                 error = pcbmap(dp, frcn, &bn, &cluster, &blsize);
  252                 if (error) {
  253                         if (error == E2BIG)
  254                                 break;
  255                         return (error);
  256                 }
  257                 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
  258                 if (error) {
  259                         return (error);
  260                 }
  261                 for (blkoff = 0; blkoff < blsize;
  262                      blkoff += sizeof(struct direntry),
  263                      diroff += sizeof(struct direntry)) {
  264                         dep = (struct direntry *)(bp->b_data + blkoff);
  265                         /*
  266                          * If the slot is empty and we are still looking
  267                          * for an empty then remember this one.  If the
  268                          * slot is not empty then check to see if it
  269                          * matches what we are looking for.  If the slot
  270                          * has never been filled with anything, then the
  271                          * remainder of the directory has never been used,
  272                          * so there is no point in searching it.
  273                          */
  274                         if (dep->deName[0] == SLOT_EMPTY ||
  275                             dep->deName[0] == SLOT_DELETED) {
  276                                 /*
  277                                  * Drop memory of previous long matches
  278                                  */
  279                                 chksum = -1;
  280                                 mbnambuf_init(&nb);
  281 
  282                                 if (slotcount < wincnt) {
  283                                         slotcount++;
  284                                         slotoffset = diroff;
  285                                 }
  286                                 if (dep->deName[0] == SLOT_EMPTY) {
  287                                         brelse(bp);
  288                                         goto notfound;
  289                                 }
  290                         } else {
  291                                 /*
  292                                  * If there wasn't enough space for our winentries,
  293                                  * forget about the empty space
  294                                  */
  295                                 if (slotcount < wincnt)
  296                                         slotcount = 0;
  297 
  298                                 /*
  299                                  * Check for Win95 long filename entry
  300                                  */
  301                                 if (dep->deAttributes == ATTR_WIN95) {
  302                                         if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
  303                                                 continue;
  304 
  305                                         chksum = win2unixfn(&nb,
  306                                             (struct winentry *)dep, chksum,
  307                                             pmp);
  308                                         continue;
  309                                 }
  310 
  311                                 chksum = winChkName(&nb,
  312                                     (const u_char *)cnp->cn_nameptr, unlen,
  313                                     chksum, pmp);
  314                                 if (chksum == -2) {
  315                                         chksum = -1;
  316                                         continue;
  317                                 }
  318 
  319                                 /*
  320                                  * Ignore volume labels (anywhere, not just
  321                                  * the root directory).
  322                                  */
  323                                 if (dep->deAttributes & ATTR_VOLUME) {
  324                                         chksum = -1;
  325                                         continue;
  326                                 }
  327 
  328                                 /*
  329                                  * Check for a checksum or name match
  330                                  */
  331                                 chksum_ok = (chksum == winChksum(dep->deName));
  332                                 if (!chksum_ok
  333                                     && (!olddos || bcmp(dosfilename, dep->deName, 11))) {
  334                                         chksum = -1;
  335                                         continue;
  336                                 }
  337 #ifdef MSDOSFS_DEBUG
  338                                 printf("msdosfs_lookup(): match blkoff %d, diroff %d\n",
  339                                     blkoff, diroff);
  340 #endif
  341                                 /*
  342                                  * Remember where this directory
  343                                  * entry came from for whoever did
  344                                  * this lookup.
  345                                  */
  346                                 dp->de_fndoffset = diroff;
  347                                 if (chksum_ok && nameiop == RENAME) {
  348                                         /*
  349                                          * Target had correct long name
  350                                          * directory entries, reuse them
  351                                          * as needed.
  352                                          */
  353                                         dp->de_fndcnt = wincnt - 1;
  354                                 } else {
  355                                         /*
  356                                          * Long name directory entries
  357                                          * not present or corrupt, can only
  358                                          * reuse dos directory entry.
  359                                          */
  360                                         dp->de_fndcnt = 0;
  361                                 }
  362 
  363                                 goto found;
  364                         }
  365                 }       /* for (blkoff = 0; .... */
  366                 /*
  367                  * Release the buffer holding the directory cluster just
  368                  * searched.
  369                  */
  370                 brelse(bp);
  371         }       /* for (frcn = 0; ; frcn++) */
  372 
  373 notfound:
  374         /*
  375          * We hold no disk buffers at this point.
  376          */
  377 
  378         /*
  379          * Fixup the slot description to point to the place where
  380          * we might put the new DOS direntry (putting the Win95
  381          * long name entries before that)
  382          */
  383         if (!slotcount) {
  384                 slotcount = 1;
  385                 slotoffset = diroff;
  386         }
  387         if (wincnt > slotcount)
  388                 slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
  389 
  390         /*
  391          * If we get here we didn't find the entry we were looking for. But
  392          * that's ok if we are creating or renaming and are at the end of
  393          * the pathname and the directory hasn't been removed.
  394          */
  395 #ifdef MSDOSFS_DEBUG
  396         printf("msdosfs_lookup(): op %d, refcnt %ld\n",
  397             nameiop, dp->de_refcnt);
  398         printf("               slotcount %d, slotoffset %d\n",
  399                slotcount, slotoffset);
  400 #endif
  401         if ((nameiop == CREATE || nameiop == RENAME) &&
  402             (flags & ISLASTCN) && dp->de_refcnt != 0) {
  403                 /*
  404                  * Access for write is interpreted as allowing
  405                  * creation of files in the directory.
  406                  */
  407                 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
  408                 if (error)
  409                         return (error);
  410                 /*
  411                  * Return an indication of where the new directory
  412                  * entry should be put.
  413                  */
  414                 dp->de_fndoffset = slotoffset;
  415                 dp->de_fndcnt = wincnt - 1;
  416 
  417                 /*
  418                  * We return with the directory locked, so that
  419                  * the parameters we set up above will still be
  420                  * valid if we actually decide to do a direnter().
  421                  * We return ni_vp == NULL to indicate that the entry
  422                  * does not currently exist; we leave a pointer to
  423                  * the (locked) directory inode in ndp->ni_dvp.
  424                  * The pathname buffer is saved so that the name
  425                  * can be obtained later.
  426                  *
  427                  * NB - if the directory is unlocked, then this
  428                  * information cannot be used.
  429                  */
  430                 cnp->cn_flags |= SAVENAME;
  431                 return (EJUSTRETURN);
  432         }
  433 #if 0
  434         /*
  435          * Insert name into cache (as non-existent) if appropriate.
  436          *
  437          * XXX Negative caching is broken for msdosfs because the name
  438          * cache doesn't understand peculiarities such as case insensitivity
  439          * and 8.3 filenames.  Hence, it may not invalidate all negative
  440          * entries if a file with this name is later created.
  441          */
  442         if ((cnp->cn_flags & MAKEENTRY) != 0)
  443                 cache_enter(vdp, *vpp, cnp);
  444 #endif
  445         return (ENOENT);
  446 
  447 found:
  448         /*
  449          * NOTE:  We still have the buffer with matched directory entry at
  450          * this point.
  451          */
  452         isadir = dep->deAttributes & ATTR_DIRECTORY;
  453         scn = getushort(dep->deStartCluster);
  454         if (FAT32(pmp)) {
  455                 scn |= getushort(dep->deHighClust) << 16;
  456                 if (scn == pmp->pm_rootdirblk) {
  457                         /*
  458                          * There should actually be 0 here.
  459                          * Just ignore the error.
  460                          */
  461                         scn = MSDOSFSROOT;
  462                 }
  463         }
  464 
  465         if (isadir) {
  466                 cluster = scn;
  467                 if (cluster == MSDOSFSROOT)
  468                         blkoff = MSDOSFSROOT_OFS;
  469                 else
  470                         blkoff = 0;
  471         } else if (cluster == MSDOSFSROOT)
  472                 blkoff = diroff;
  473 
  474         /*
  475          * Now release buf to allow deget to read the entry again.
  476          * Reserving it here and giving it to deget could result
  477          * in a deadlock.
  478          */
  479         brelse(bp);
  480         bp = NULL;
  481 
  482 foundroot:
  483         /*
  484          * If we entered at foundroot, then we are looking for the . or ..
  485          * entry of the filesystems root directory.  isadir and scn were
  486          * setup before jumping here.  And, bp is already null.
  487          */
  488         if (FAT32(pmp) && scn == MSDOSFSROOT)
  489                 scn = pmp->pm_rootdirblk;
  490 
  491         if (scnp != NULL) {
  492                 *scnp = cluster;
  493                 *blkoffp = blkoff;
  494                 return (0);
  495         }
  496 
  497         /*
  498          * If deleting, and at end of pathname, return
  499          * parameters which can be used to remove file.
  500          */
  501         if (nameiop == DELETE && (flags & ISLASTCN)) {
  502                 /*
  503                  * Don't allow deleting the root.
  504                  */
  505                 if (blkoff == MSDOSFSROOT_OFS)
  506                         return (EBUSY);
  507 
  508                 /*
  509                  * Write access to directory required to delete files.
  510                  */
  511                 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
  512                 if (error)
  513                         return (error);
  514 
  515                 /*
  516                  * Return pointer to current entry in dp->i_offset.
  517                  * Save directory inode pointer in ndp->ni_dvp for dirremove().
  518                  */
  519                 if (dp->de_StartCluster == scn && isadir) {     /* "." */
  520                         VREF(vdp);
  521                         *vpp = vdp;
  522                         return (0);
  523                 }
  524                 error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE, &tdp);
  525                 if (error)
  526                         return (error);
  527                 return (msdosfs_lookup_checker(pmp, vdp, tdp, vpp));
  528         }
  529 
  530         /*
  531          * If rewriting (RENAME), return the inode and the
  532          * information required to rewrite the present directory
  533          * Must get inode of directory entry to verify it's a
  534          * regular file, or empty directory.
  535          */
  536         if (nameiop == RENAME && (flags & ISLASTCN)) {
  537                 if (blkoff == MSDOSFSROOT_OFS)
  538                         return (EBUSY);
  539 
  540                 error = VOP_ACCESS(vdp, VWRITE, cnp->cn_cred, cnp->cn_thread);
  541                 if (error)
  542                         return (error);
  543 
  544                 /*
  545                  * Careful about locking second inode.
  546                  * This can only occur if the target is ".".
  547                  */
  548                 if (dp->de_StartCluster == scn && isadir)
  549                         return (EISDIR);
  550 
  551                 if ((error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE,
  552                     &tdp)) != 0)
  553                         return (error);
  554                 if ((error = msdosfs_lookup_checker(pmp, vdp, tdp, vpp))
  555                     != 0)
  556                         return (error);
  557                 cnp->cn_flags |= SAVENAME;
  558                 return (0);
  559         }
  560 
  561         /*
  562          * Step through the translation in the name.  We do not `vput' the
  563          * directory because we may need it again if a symbolic link
  564          * is relative to the current directory.  Instead we save it
  565          * unlocked as "pdp".  We must get the target inode before unlocking
  566          * the directory to insure that the inode will not be removed
  567          * before we get it.  We prevent deadlock by always fetching
  568          * inodes from the root, moving down the directory tree. Thus
  569          * when following backward pointers ".." we must unlock the
  570          * parent directory before getting the requested directory.
  571          */
  572         pdp = vdp;
  573         if (flags & ISDOTDOT) {
  574                 dd_arg.cluster = cluster;
  575                 dd_arg.blkoff = blkoff;
  576                 error = vn_vget_ino_gen(vdp, msdosfs_deget_dotdot,
  577                     &dd_arg, cnp->cn_lkflags, vpp);
  578                 if (error != 0) {
  579                         *vpp = NULL;
  580                         return (error);
  581                 }
  582                 /*
  583                  * Recheck that ".." still points to the inode we
  584                  * looked up before pdp lock was dropped.
  585                  */
  586                 error = msdosfs_lookup_ino(pdp, NULL, cnp, &scn, &blkoff);
  587                 if (error) {
  588                         vput(*vpp);
  589                         *vpp = NULL;
  590                         return (error);
  591                 }
  592                 if (FAT32(pmp) && scn == MSDOSFSROOT)
  593                         scn = pmp->pm_rootdirblk;
  594                 inode1 = scn * pmp->pm_bpcluster + blkoff;
  595                 if (VTODE(*vpp)->de_inode != inode1) {
  596                         vput(*vpp);
  597                         goto restart;
  598                 }
  599                 error = msdosfs_lookup_checker(pmp, vdp, VTODE(*vpp), vpp);
  600                 if (error != 0)
  601                         return (error);
  602         } else if (dp->de_StartCluster == scn && isadir) {
  603                 if (cnp->cn_namelen != 1 || cnp->cn_nameptr[0] != '.') {
  604                         /* fs is corrupted, non-dot lookup returned dvp */
  605                         msdosfs_integrity_error(pmp);
  606                         return (EBADF);
  607                 }
  608                 VREF(vdp);      /* we want ourself, ie "." */
  609                 *vpp = vdp;
  610         } else {
  611                 if ((error = deget(pmp, cluster, blkoff, LK_EXCLUSIVE,
  612                     &tdp)) != 0)
  613                         return (error);
  614                 if ((error = msdosfs_lookup_checker(pmp, vdp, tdp, vpp)) != 0)
  615                         return (error);
  616         }
  617 
  618         /*
  619          * Insert name into cache if appropriate.
  620          */
  621         if (cnp->cn_flags & MAKEENTRY)
  622                 cache_enter(vdp, *vpp, cnp);
  623         return (0);
  624 }
  625 
  626 /*
  627  * dep  - directory entry to copy into the directory
  628  * ddep - directory to add to
  629  * depp - return the address of the denode for the created directory entry
  630  *        if depp != 0
  631  * cnp  - componentname needed for Win95 long filenames
  632  */
  633 int
  634 createde(struct denode *dep, struct denode *ddep, struct denode **depp,
  635     struct componentname *cnp)
  636 {
  637         int error;
  638         u_long dirclust, diroffset;
  639         struct direntry *ndep;
  640         struct msdosfsmount *pmp = ddep->de_pmp;
  641         struct buf *bp;
  642         daddr_t bn;
  643         int blsize;
  644 
  645 #ifdef MSDOSFS_DEBUG
  646         printf("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
  647             dep, ddep, depp, cnp);
  648 #endif
  649 
  650         /*
  651          * If no space left in the directory then allocate another cluster
  652          * and chain it onto the end of the file.  There is one exception
  653          * to this.  That is, if the root directory has no more space it
  654          * can NOT be expanded.  extendfile() checks for and fails attempts
  655          * to extend the root directory.  We just return an error in that
  656          * case.
  657          */
  658         if (ddep->de_fndoffset >= ddep->de_FileSize) {
  659                 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
  660                     - ddep->de_FileSize;
  661                 dirclust = de_clcount(pmp, diroffset);
  662                 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
  663                 if (error) {
  664                         (void)detrunc(ddep, ddep->de_FileSize, 0, NOCRED);
  665                         return error;
  666                 }
  667 
  668                 /*
  669                  * Update the size of the directory
  670                  */
  671                 ddep->de_FileSize += de_cn2off(pmp, dirclust);
  672         }
  673 
  674         /*
  675          * We just read in the cluster with space.  Copy the new directory
  676          * entry in.  Then write it to disk. NOTE:  DOS directories
  677          * do not get smaller as clusters are emptied.
  678          */
  679         error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
  680                        &bn, &dirclust, &blsize);
  681         if (error)
  682                 return error;
  683         diroffset = ddep->de_fndoffset;
  684         if (dirclust != MSDOSFSROOT)
  685                 diroffset &= pmp->pm_crbomask;
  686         if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
  687                 brelse(bp);
  688                 return error;
  689         }
  690         ndep = bptoep(pmp, bp, ddep->de_fndoffset);
  691 
  692         DE_EXTERNALIZE(ndep, dep);
  693 
  694         /*
  695          * Now write the Win95 long name
  696          */
  697         if (ddep->de_fndcnt > 0) {
  698                 uint8_t chksum = winChksum(ndep->deName);
  699                 const u_char *un = (const u_char *)cnp->cn_nameptr;
  700                 int unlen = cnp->cn_namelen;
  701                 int cnt = 1;
  702 
  703                 while (--ddep->de_fndcnt >= 0) {
  704                         if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
  705                                 if (DOINGASYNC(DETOV(ddep)))
  706                                         bdwrite(bp);
  707                                 else if ((error = bwrite(bp)) != 0)
  708                                         return error;
  709 
  710                                 ddep->de_fndoffset -= sizeof(struct direntry);
  711                                 error = pcbmap(ddep,
  712                                                de_cluster(pmp,
  713                                                           ddep->de_fndoffset),
  714                                                &bn, 0, &blsize);
  715                                 if (error)
  716                                         return error;
  717 
  718                                 error = bread(pmp->pm_devvp, bn, blsize,
  719                                               NOCRED, &bp);
  720                                 if (error) {
  721                                         return error;
  722                                 }
  723                                 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
  724                         } else {
  725                                 ndep--;
  726                                 ddep->de_fndoffset -= sizeof(struct direntry);
  727                         }
  728                         if (!unix2winfn(un, unlen, (struct winentry *)ndep,
  729                                         cnt++, chksum, pmp))
  730                                 break;
  731                 }
  732         }
  733 
  734         if (DOINGASYNC(DETOV(ddep)))
  735                 bdwrite(bp);
  736         else if ((error = bwrite(bp)) != 0)
  737                 return error;
  738 
  739         /*
  740          * If they want us to return with the denode gotten.
  741          */
  742         if (depp) {
  743                 if (dep->de_Attributes & ATTR_DIRECTORY) {
  744                         dirclust = dep->de_StartCluster;
  745                         if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
  746                                 dirclust = MSDOSFSROOT;
  747                         if (dirclust == MSDOSFSROOT)
  748                                 diroffset = MSDOSFSROOT_OFS;
  749                         else
  750                                 diroffset = 0;
  751                 }
  752                 return (deget(pmp, dirclust, diroffset, LK_EXCLUSIVE, depp));
  753         }
  754 
  755         return 0;
  756 }
  757 
  758 /*
  759  * Be sure a directory is empty except for "." and "..". Return 1 if empty,
  760  * return 0 if not empty or error.
  761  */
  762 int
  763 dosdirempty(struct denode *dep)
  764 {
  765         int blsize;
  766         int error;
  767         u_long cn;
  768         daddr_t bn;
  769         struct buf *bp;
  770         struct msdosfsmount *pmp = dep->de_pmp;
  771         struct direntry *dentp;
  772 
  773         /*
  774          * Since the filesize field in directory entries for a directory is
  775          * zero, we just have to feel our way through the directory until
  776          * we hit end of file.
  777          */
  778         for (cn = 0;; cn++) {
  779                 if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
  780                         if (error == E2BIG)
  781                                 return (1);     /* it's empty */
  782                         return (0);
  783                 }
  784                 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
  785                 if (error) {
  786                         return (0);
  787                 }
  788                 for (dentp = (struct direntry *)bp->b_data;
  789                      (char *)dentp < bp->b_data + blsize;
  790                      dentp++) {
  791                         if (dentp->deName[0] != SLOT_DELETED &&
  792                             (dentp->deAttributes & ATTR_VOLUME) == 0) {
  793                                 /*
  794                                  * In dos directories an entry whose name
  795                                  * starts with SLOT_EMPTY (0) starts the
  796                                  * beginning of the unused part of the
  797                                  * directory, so we can just return that it
  798                                  * is empty.
  799                                  */
  800                                 if (dentp->deName[0] == SLOT_EMPTY) {
  801                                         brelse(bp);
  802                                         return (1);
  803                                 }
  804                                 /*
  805                                  * Any names other than "." and ".." in a
  806                                  * directory mean it is not empty.
  807                                  */
  808                                 if (bcmp(dentp->deName, ".          ", 11) &&
  809                                     bcmp(dentp->deName, "..         ", 11)) {
  810                                         brelse(bp);
  811 #ifdef MSDOSFS_DEBUG
  812                                         printf("dosdirempty(): entry found %02x, %02x\n",
  813                                             dentp->deName[0], dentp->deName[1]);
  814 #endif
  815                                         return (0);     /* not empty */
  816                                 }
  817                         }
  818                 }
  819                 brelse(bp);
  820         }
  821         /* NOTREACHED */
  822 }
  823 
  824 /*
  825  * Check to see if the directory described by target is in some
  826  * subdirectory of source.  This prevents something like the following from
  827  * succeeding and leaving a bunch or files and directories orphaned. mv
  828  * /a/b/c /a/b/c/d/e/f Where c and f are directories.
  829  *
  830  * source - the inode for /a/b/c
  831  * target - the inode for /a/b/c/d/e/f
  832  *
  833  * Returns 0 if target is NOT a subdirectory of source.
  834  * Otherwise returns a non-zero error number.
  835  */
  836 int
  837 doscheckpath(struct denode *source, struct denode *target, daddr_t *wait_scn)
  838 {
  839         daddr_t scn;
  840         struct msdosfsmount *pmp;
  841         struct direntry *ep;
  842         struct denode *dep;
  843         struct buf *bp = NULL;
  844         int error = 0;
  845 
  846         *wait_scn = 0;
  847 
  848         pmp = target->de_pmp;
  849         lockmgr_assert(&pmp->pm_checkpath_lock, KA_XLOCKED);
  850         KASSERT(pmp == source->de_pmp,
  851             ("doscheckpath: source and target on different filesystems"));
  852 
  853         if ((target->de_Attributes & ATTR_DIRECTORY) == 0 ||
  854             (source->de_Attributes & ATTR_DIRECTORY) == 0)
  855                 return (ENOTDIR);
  856 
  857         if (target->de_StartCluster == source->de_StartCluster)
  858                 return (EEXIST);
  859 
  860         if (target->de_StartCluster == MSDOSFSROOT ||
  861             (FAT32(pmp) && target->de_StartCluster == pmp->pm_rootdirblk))
  862                 return (0);
  863 
  864         dep = target;
  865         vget(DETOV(dep), LK_EXCLUSIVE);
  866         for (;;) {
  867                 if ((dep->de_Attributes & ATTR_DIRECTORY) == 0) {
  868                         error = ENOTDIR;
  869                         break;
  870                 }
  871                 scn = dep->de_StartCluster;
  872                 error = bread(pmp->pm_devvp, cntobn(pmp, scn),
  873                     pmp->pm_bpcluster, NOCRED, &bp);
  874                 if (error != 0)
  875                         break;
  876 
  877                 ep = (struct direntry *)bp->b_data + 1;
  878                 if ((ep->deAttributes & ATTR_DIRECTORY) == 0 ||
  879                     bcmp(ep->deName, "..         ", 11) != 0) {
  880                         error = ENOTDIR;
  881                         brelse(bp);
  882                         break;
  883                 }
  884 
  885                 scn = getushort(ep->deStartCluster);
  886                 if (FAT32(pmp))
  887                         scn |= getushort(ep->deHighClust) << 16;
  888                 brelse(bp);
  889 
  890                 if (scn == source->de_StartCluster) {
  891                         error = EINVAL;
  892                         break;
  893                 }
  894                 if (scn == MSDOSFSROOT)
  895                         break;
  896                 if (FAT32(pmp) && scn == pmp->pm_rootdirblk) {
  897                         /*
  898                          * scn should be 0 in this case,
  899                          * but we silently ignore the error.
  900                          */
  901                         break;
  902                 }
  903 
  904                 vput(DETOV(dep));
  905                 dep = NULL;
  906                 /* NOTE: deget() clears dep on error */
  907                 error = deget(pmp, scn, 0, LK_EXCLUSIVE | LK_NOWAIT, &dep);
  908                 if (error != 0) {
  909                         *wait_scn = scn;
  910                         break;
  911                 }
  912         }
  913 #ifdef MSDOSFS_DEBUG
  914         if (error == ENOTDIR)
  915                 printf("doscheckpath(): .. not a directory?\n");
  916 #endif
  917         if (dep != NULL)
  918                 vput(DETOV(dep));
  919         return (error);
  920 }
  921 
  922 /*
  923  * Read in the disk block containing the directory entry (dirclu, dirofs)
  924  * and return the address of the buf header, and the address of the
  925  * directory entry within the block.
  926  */
  927 int
  928 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
  929     struct buf **bpp, struct direntry **epp)
  930 {
  931         int error;
  932         daddr_t bn;
  933         int blsize;
  934 
  935         blsize = pmp->pm_bpcluster;
  936         if (dirclust == MSDOSFSROOT
  937             && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
  938                 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
  939         bn = detobn(pmp, dirclust, diroffset);
  940         if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
  941                 brelse(*bpp);
  942                 *bpp = NULL;
  943                 return (error);
  944         }
  945         if (epp)
  946                 *epp = bptoep(pmp, *bpp, diroffset);
  947         return (0);
  948 }
  949 
  950 /*
  951  * Read in the disk block containing the directory entry dep came from and
  952  * return the address of the buf header, and the address of the directory
  953  * entry within the block.
  954  */
  955 int
  956 readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
  957 {
  958 
  959         return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
  960             bpp, epp));
  961 }
  962 
  963 /*
  964  * Remove a directory entry. At this point the file represented by the
  965  * directory entry to be removed is still full length until no one has it
  966  * open.  When the file no longer being used msdosfs_inactive() is called
  967  * and will truncate the file to 0 length.  When the vnode containing the
  968  * denode is needed for some other purpose by VFS it will call
  969  * msdosfs_reclaim() which will remove the denode from the denode cache.
  970  *
  971  * pdep directory where the entry is removed
  972  * dep  file to be removed
  973  */
  974 int
  975 removede(struct denode *pdep, struct denode *dep)
  976 {
  977         int error;
  978         struct direntry *ep;
  979         struct buf *bp;
  980         daddr_t bn;
  981         int blsize;
  982         struct msdosfsmount *pmp = pdep->de_pmp;
  983         u_long offset = pdep->de_fndoffset;
  984 
  985 #ifdef MSDOSFS_DEBUG
  986         printf("removede(): filename %s, dep %p, offset %08lx\n",
  987             dep->de_Name, dep, offset);
  988 #endif
  989 
  990         dep->de_refcnt--;
  991         offset += sizeof(struct direntry);
  992         do {
  993                 offset -= sizeof(struct direntry);
  994                 error = pcbmap(pdep, de_cluster(pmp, offset), &bn, 0, &blsize);
  995                 if (error)
  996                         return error;
  997                 error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
  998                 if (error) {
  999                         return error;
 1000                 }
 1001                 ep = bptoep(pmp, bp, offset);
 1002                 /*
 1003                  * Check whether, if we came here the second time, i.e.
 1004                  * when underflowing into the previous block, the last
 1005                  * entry in this block is a longfilename entry, too.
 1006                  */
 1007                 if (ep->deAttributes != ATTR_WIN95
 1008                     && offset != pdep->de_fndoffset) {
 1009                         brelse(bp);
 1010                         break;
 1011                 }
 1012                 offset += sizeof(struct direntry);
 1013                 while (1) {
 1014                         /*
 1015                          * We are a bit aggressive here in that we delete any Win95
 1016                          * entries preceding this entry, not just the ones we "own".
 1017                          * Since these presumably aren't valid anyway,
 1018                          * there should be no harm.
 1019                          */
 1020                         offset -= sizeof(struct direntry);
 1021                         ep--->deName[0] = SLOT_DELETED;
 1022                         if ((pmp->pm_flags & MSDOSFSMNT_NOWIN95)
 1023                             || !(offset & pmp->pm_crbomask)
 1024                             || ep->deAttributes != ATTR_WIN95)
 1025                                 break;
 1026                 }
 1027                 if (DOINGASYNC(DETOV(pdep)))
 1028                         bdwrite(bp);
 1029                 else if ((error = bwrite(bp)) != 0)
 1030                         return error;
 1031         } while (!(pmp->pm_flags & MSDOSFSMNT_NOWIN95)
 1032             && !(offset & pmp->pm_crbomask)
 1033             && offset);
 1034         return 0;
 1035 }
 1036 
 1037 /*
 1038  * Create a unique DOS name in dvp
 1039  */
 1040 int
 1041 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
 1042 {
 1043         struct msdosfsmount *pmp = dep->de_pmp;
 1044         struct direntry *dentp;
 1045         int gen;
 1046         int blsize;
 1047         u_long cn;
 1048         daddr_t bn;
 1049         struct buf *bp;
 1050         int error;
 1051 
 1052         if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
 1053                 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
 1054                     cnp->cn_namelen, 0, pmp) ? 0 : EINVAL);
 1055 
 1056         for (gen = 1;; gen++) {
 1057                 /*
 1058                  * Generate DOS name with generation number
 1059                  */
 1060                 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
 1061                     cnp->cn_namelen, gen, pmp))
 1062                         return gen == 1 ? EINVAL : EEXIST;
 1063 
 1064                 /*
 1065                  * Now look for a dir entry with this exact name
 1066                  */
 1067                 for (cn = error = 0; !error; cn++) {
 1068                         if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
 1069                                 if (error == E2BIG)     /* EOF reached and not found */
 1070                                         return 0;
 1071                                 return error;
 1072                         }
 1073                         error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
 1074                         if (error) {
 1075                                 return error;
 1076                         }
 1077                         for (dentp = (struct direntry *)bp->b_data;
 1078                              (char *)dentp < bp->b_data + blsize;
 1079                              dentp++) {
 1080                                 if (dentp->deName[0] == SLOT_EMPTY) {
 1081                                         /*
 1082                                          * Last used entry and not found
 1083                                          */
 1084                                         brelse(bp);
 1085                                         return 0;
 1086                                 }
 1087                                 /*
 1088                                  * Ignore volume labels and Win95 entries
 1089                                  */
 1090                                 if (dentp->deAttributes & ATTR_VOLUME)
 1091                                         continue;
 1092                                 if (!bcmp(dentp->deName, cp, 11)) {
 1093                                         error = EEXIST;
 1094                                         break;
 1095                                 }
 1096                         }
 1097                         brelse(bp);
 1098                 }
 1099         }
 1100 }

Cache object: 232e414b86c99cf0e22d20bf98840fc6


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