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/msdosfs/msdosfs_denode.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_denode.c,v 1.28 1998/02/10 14:10:00 mrg 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/kernel.h>
   54 #include <sys/mount.h>
   55 #include <sys/malloc.h>
   56 #include <sys/proc.h>
   57 #include <sys/buf.h>
   58 #include <sys/vnode.h>
   59 
   60 #include <vm/vm.h>
   61 #include <vm/vm_extern.h>
   62 
   63 #include <msdosfs/bpb.h>
   64 #include <msdosfs/msdosfsmount.h>
   65 #include <msdosfs/direntry.h>
   66 #include <msdosfs/denode.h>
   67 #include <msdosfs/fat.h>
   68 
   69 static MALLOC_DEFINE(M_MSDOSFSNODE, "MSDOSFS node", "MSDOSFS vnode private part");
   70 
   71 static struct denode **dehashtbl;
   72 static u_long dehash;                   /* size of hash table - 1 */
   73 #define DEHASH(dev, dcl, doff)  (dehashtbl[(minor(dev) + (dcl) + (doff) /       \
   74                                 sizeof(struct direntry)) & dehash])
   75 #ifndef NULL_SIMPLELOCKS
   76 static struct simplelock dehash_slock;
   77 #endif
   78 
   79 union _qcvt {
   80         quad_t qcvt;
   81         long val[2];
   82 };
   83 #define SETHIGH(q, h) { \
   84         union _qcvt tmp; \
   85         tmp.qcvt = (q); \
   86         tmp.val[_QUAD_HIGHWORD] = (h); \
   87         (q) = tmp.qcvt; \
   88 }
   89 #define SETLOW(q, l) { \
   90         union _qcvt tmp; \
   91         tmp.qcvt = (q); \
   92         tmp.val[_QUAD_LOWWORD] = (l); \
   93         (q) = tmp.qcvt; \
   94 }
   95 
   96 static struct denode *
   97                 msdosfs_hashget __P((dev_t dev, u_long dirclust,
   98                                      u_long diroff));
   99 static void     msdosfs_hashins __P((struct denode *dep));
  100 static void     msdosfs_hashrem __P((struct denode *dep));
  101 
  102 /*ARGSUSED*/
  103 int 
  104 msdosfs_init(vfsp)
  105         struct vfsconf *vfsp;
  106 {
  107         dehashtbl = hashinit(desiredvnodes/2, M_MSDOSFSMNT, &dehash);
  108         simple_lock_init(&dehash_slock);
  109         return (0);
  110 }
  111 
  112 int 
  113 msdosfs_uninit(vfsp)
  114         struct vfsconf *vfsp;
  115 {
  116 
  117         if (dehashtbl)
  118                 free(dehashtbl, M_MSDOSFSMNT);
  119         return (0);
  120 }
  121 
  122 static struct denode *
  123 msdosfs_hashget(dev, dirclust, diroff)
  124         dev_t dev;
  125         u_long dirclust;
  126         u_long diroff;
  127 {
  128         struct proc *p = curproc;       /* XXX */
  129         struct denode *dep;
  130         struct vnode *vp;
  131 
  132 loop:
  133         simple_lock(&dehash_slock);
  134         for (dep = DEHASH(dev, dirclust, diroff); dep; dep = dep->de_next) {
  135                 if (dirclust == dep->de_dirclust
  136                     && diroff == dep->de_diroffset
  137                     && dev == dep->de_dev
  138                     && dep->de_refcnt != 0) {
  139                         vp = DETOV(dep);
  140                         simple_lock(&vp->v_interlock);
  141                         simple_unlock(&dehash_slock);
  142                         if (vget(vp, LK_EXCLUSIVE | LK_INTERLOCK, p))
  143                                 goto loop;
  144                         return (dep);
  145                 }
  146         }
  147         simple_unlock(&dehash_slock);
  148         return (NULL);
  149 }
  150 
  151 static void
  152 msdosfs_hashins(dep)
  153         struct denode *dep;
  154 {
  155         struct denode **depp, *deq;
  156 
  157         simple_lock(&dehash_slock);
  158         depp = &DEHASH(dep->de_dev, dep->de_dirclust, dep->de_diroffset);
  159         deq = *depp;
  160         if (deq)
  161                 deq->de_prev = &dep->de_next;
  162         dep->de_next = deq;
  163         dep->de_prev = depp;
  164         *depp = dep;
  165         simple_unlock(&dehash_slock);
  166 }
  167 
  168 static void
  169 msdosfs_hashrem(dep)
  170         struct denode *dep;
  171 {
  172         struct denode *deq;
  173 
  174         simple_lock(&dehash_slock);
  175         deq = dep->de_next;
  176         if (deq)
  177                 deq->de_prev = dep->de_prev;
  178         *dep->de_prev = deq;
  179 #ifdef DIAGNOSTIC
  180         dep->de_next = NULL;
  181         dep->de_prev = NULL;
  182 #endif
  183         simple_unlock(&dehash_slock);
  184 }
  185 
  186 /*
  187  * If deget() succeeds it returns with the gotten denode locked().
  188  *
  189  * pmp       - address of msdosfsmount structure of the filesystem containing
  190  *             the denode of interest.  The pm_dev field and the address of
  191  *             the msdosfsmount structure are used.
  192  * dirclust  - which cluster bp contains, if dirclust is 0 (root directory)
  193  *             diroffset is relative to the beginning of the root directory,
  194  *             otherwise it is cluster relative.
  195  * diroffset - offset past begin of cluster of denode we want
  196  * depp      - returns the address of the gotten denode.
  197  */
  198 int
  199 deget(pmp, dirclust, diroffset, depp)
  200         struct msdosfsmount *pmp;       /* so we know the maj/min number */
  201         u_long dirclust;                /* cluster this dir entry came from */
  202         u_long diroffset;               /* index of entry within the cluster */
  203         struct denode **depp;           /* returns the addr of the gotten denode */
  204 {
  205         int error;
  206         dev_t dev = pmp->pm_dev;
  207         struct mount *mntp = pmp->pm_mountp;
  208         struct direntry *direntptr;
  209         struct denode *ldep;
  210         struct vnode *nvp;
  211         struct buf *bp;
  212         struct proc *p = curproc;       /* XXX */
  213         struct timeval tv;
  214 
  215 #ifdef MSDOSFS_DEBUG
  216         printf("deget(pmp %p, dirclust %lu, diroffset %lx, depp %p)\n",
  217             pmp, dirclust, diroffset, depp);
  218 #endif
  219 
  220         /*
  221          * On FAT32 filesystems, root is a (more or less) normal
  222          * directory
  223          */
  224         if (FAT32(pmp) && dirclust == MSDOSFSROOT)
  225                 dirclust = pmp->pm_rootdirblk;
  226 
  227         /*
  228          * See if the denode is in the denode cache. Use the location of
  229          * the directory entry to compute the hash value. For subdir use
  230          * address of "." entry. For root dir (if not FAT32) use cluster
  231          * MSDOSFSROOT, offset MSDOSFSROOT_OFS
  232          *
  233          * NOTE: The check for de_refcnt > 0 below insures the denode being
  234          * examined does not represent an unlinked but still open file.
  235          * These files are not to be accessible even when the directory
  236          * entry that represented the file happens to be reused while the
  237          * deleted file is still open.
  238          */
  239         ldep = msdosfs_hashget(dev, dirclust, diroffset);
  240         if (ldep) {
  241                 *depp = ldep;
  242                 return (0);
  243         }
  244 
  245         /*
  246          * Do the MALLOC before the getnewvnode since doing so afterward
  247          * might cause a bogus v_data pointer to get dereferenced
  248          * elsewhere if MALLOC should block.
  249          */
  250         MALLOC(ldep, struct denode *, sizeof(struct denode), M_MSDOSFSNODE, M_WAITOK);
  251 
  252         /*
  253          * Directory entry was not in cache, have to create a vnode and
  254          * copy it from the passed disk buffer.
  255          */
  256         /* getnewvnode() does a VREF() on the vnode */
  257         error = getnewvnode(VT_MSDOSFS, mntp, msdosfs_vnodeop_p, &nvp);
  258         if (error) {
  259                 *depp = NULL;
  260                 FREE(ldep, M_MSDOSFSNODE);
  261                 return error;
  262         }
  263         bzero((caddr_t)ldep, sizeof *ldep);
  264         lockinit(&ldep->de_lock, PINOD, "denode", VLKTIMEOUT, 0);
  265         nvp->v_data = ldep;
  266         ldep->de_vnode = nvp;
  267         ldep->de_flag = 0;
  268         ldep->de_devvp = 0;
  269         ldep->de_dev = dev;
  270         ldep->de_dirclust = dirclust;
  271         ldep->de_diroffset = diroffset;
  272         fc_purge(ldep, 0);      /* init the fat cache for this denode */
  273 
  274         /*
  275          * Lock the denode so that it can't be accessed until we've read
  276          * it in and have done what we need to it.  Do this here instead
  277          * of at the start of msdosfs_hashins() so that reinsert() can
  278          * call msdosfs_hashins() with a locked denode.
  279          */
  280         if (lockmgr(&ldep->de_lock, LK_EXCLUSIVE, (struct simplelock *)0, p))
  281                 panic("deget: unexpected lock failure");
  282 
  283         /*
  284          * Insert the denode into the hash queue.
  285          */
  286         msdosfs_hashins(ldep);
  287 
  288         ldep->de_pmp = pmp;
  289         ldep->de_refcnt = 1;
  290         /*
  291          * Copy the directory entry into the denode area of the vnode.
  292          */
  293         if ((dirclust == MSDOSFSROOT
  294              || (FAT32(pmp) && dirclust == pmp->pm_rootdirblk))
  295             && diroffset == MSDOSFSROOT_OFS) {
  296                 /*
  297                  * Directory entry for the root directory. There isn't one,
  298                  * so we manufacture one. We should probably rummage
  299                  * through the root directory and find a label entry (if it
  300                  * exists), and then use the time and date from that entry
  301                  * as the time and date for the root denode.
  302                  */
  303                 nvp->v_flag |= VROOT; /* should be further down         XXX */
  304 
  305                 ldep->de_Attributes = ATTR_DIRECTORY;
  306                 ldep->de_LowerCase = 0;
  307                 if (FAT32(pmp))
  308                         ldep->de_StartCluster = pmp->pm_rootdirblk;
  309                         /* de_FileSize will be filled in further down */
  310                 else {
  311                         ldep->de_StartCluster = MSDOSFSROOT;
  312                         ldep->de_FileSize = pmp->pm_rootdirsize * DEV_BSIZE;
  313                 }
  314                 /*
  315                  * fill in time and date so that dos2unixtime() doesn't
  316                  * spit up when called from msdosfs_getattr() with root
  317                  * denode
  318                  */
  319                 ldep->de_CHun = 0;
  320                 ldep->de_CTime = 0x0000;        /* 00:00:00      */
  321                 ldep->de_CDate = (0 << DD_YEAR_SHIFT) | (1 << DD_MONTH_SHIFT)
  322                     | (1 << DD_DAY_SHIFT);
  323                 /* Jan 1, 1980   */
  324                 ldep->de_ADate = ldep->de_CDate;
  325                 ldep->de_MTime = ldep->de_CTime;
  326                 ldep->de_MDate = ldep->de_CDate;
  327                 /* leave the other fields as garbage */
  328         } else {
  329                 error = readep(pmp, dirclust, diroffset, &bp, &direntptr);
  330                 if (error) {
  331                         /*
  332                          * The denode does not contain anything useful, so
  333                          * it would be wrong to leave it on its hash chain.
  334                          * Arrange for vput() to just forget about it.
  335                          */
  336                         ldep->de_Name[0] = SLOT_DELETED;
  337 
  338                         vput(nvp);
  339                         *depp = NULL;
  340                         return (error);
  341                 }
  342                 DE_INTERNALIZE(ldep, direntptr);
  343                 brelse(bp);
  344         }
  345 
  346         /*
  347          * Fill in a few fields of the vnode and finish filling in the
  348          * denode.  Then return the address of the found denode.
  349          */
  350         if (ldep->de_Attributes & ATTR_DIRECTORY) {
  351                 /*
  352                  * Since DOS directory entries that describe directories
  353                  * have 0 in the filesize field, we take this opportunity
  354                  * to find out the length of the directory and plug it into
  355                  * the denode structure.
  356                  */
  357                 u_long size;
  358 
  359                 /*
  360                  * XXX Sometimes, these arrives that . entry have cluster
  361                  * number 0, when it shouldn't.  Use real cluster number
  362                  * instead of what is written in directory entry.
  363                  */
  364                 if ((diroffset == 0) && (ldep->de_StartCluster != dirclust)) {
  365                         printf("deget(): . entry at clust %ld != %ld\n",
  366                                         dirclust, ldep->de_StartCluster);
  367                         ldep->de_StartCluster = dirclust;
  368                 }
  369 
  370                 nvp->v_type = VDIR;
  371                 if (ldep->de_StartCluster != MSDOSFSROOT) {
  372                         error = pcbmap(ldep, 0xffff, 0, &size, 0);
  373                         if (error == E2BIG) {
  374                                 ldep->de_FileSize = de_cn2off(pmp, size);
  375                                 error = 0;
  376                         } else
  377                                 printf("deget(): pcbmap returned %d\n", error);
  378                 }
  379         } else
  380                 nvp->v_type = VREG;
  381         getmicrouptime(&tv);
  382         SETHIGH(ldep->de_modrev, tv.tv_sec);
  383         SETLOW(ldep->de_modrev, tv.tv_usec * 4294);
  384         ldep->de_devvp = pmp->pm_devvp;
  385         VREF(ldep->de_devvp);
  386         *depp = ldep;
  387         return (0);
  388 }
  389 
  390 int
  391 deupdat(dep, waitfor)
  392         struct denode *dep;
  393         int waitfor;
  394 {
  395         int error;
  396         struct buf *bp;
  397         struct direntry *dirp;
  398         struct timespec ts;
  399 
  400         if (DETOV(dep)->v_mount->mnt_flag & MNT_RDONLY)
  401                 return (0);
  402         getnanotime(&ts);
  403         DETIMES(dep, &ts, &ts, &ts);
  404         if ((dep->de_flag & DE_MODIFIED) == 0)
  405                 return (0);
  406         dep->de_flag &= ~DE_MODIFIED;
  407         if (dep->de_Attributes & ATTR_DIRECTORY)
  408                 return (0);
  409         if (dep->de_refcnt <= 0)
  410                 return (0);
  411         error = readde(dep, &bp, &dirp);
  412         if (error)
  413                 return (error);
  414         DE_EXTERNALIZE(dirp, dep);
  415         if (waitfor)
  416                 return (bwrite(bp));
  417         else {
  418                 bdwrite(bp);
  419                 return (0);
  420         }
  421 }
  422 
  423 /*
  424  * Truncate the file described by dep to the length specified by length.
  425  */
  426 int
  427 detrunc(dep, length, flags, cred, p)
  428         struct denode *dep;
  429         u_long length;
  430         int flags;
  431         struct ucred *cred;
  432         struct proc *p;
  433 {
  434         int error;
  435         int allerror;
  436         u_long eofentry;
  437         u_long chaintofree;
  438         daddr_t bn;
  439         int boff;
  440         int isadir = dep->de_Attributes & ATTR_DIRECTORY;
  441         struct buf *bp;
  442         struct msdosfsmount *pmp = dep->de_pmp;
  443 
  444 #ifdef MSDOSFS_DEBUG
  445         printf("detrunc(): file %s, length %lu, flags %x\n", dep->de_Name, length, flags);
  446 #endif
  447 
  448         /*
  449          * Disallow attempts to truncate the root directory since it is of
  450          * fixed size.  That's just the way dos filesystems are.  We use
  451          * the VROOT bit in the vnode because checking for the directory
  452          * bit and a startcluster of 0 in the denode is not adequate to
  453          * recognize the root directory at this point in a file or
  454          * directory's life.
  455          */
  456         if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp)) {
  457                 printf("detrunc(): can't truncate root directory, clust %ld, offset %ld\n",
  458                     dep->de_dirclust, dep->de_diroffset);
  459                 return (EINVAL);
  460         }
  461 
  462 
  463         if (dep->de_FileSize < length) {
  464                 vnode_pager_setsize(DETOV(dep), length);
  465                 return deextend(dep, length, cred);
  466         }
  467 
  468         /*
  469          * If the desired length is 0 then remember the starting cluster of
  470          * the file and set the StartCluster field in the directory entry
  471          * to 0.  If the desired length is not zero, then get the number of
  472          * the last cluster in the shortened file.  Then get the number of
  473          * the first cluster in the part of the file that is to be freed.
  474          * Then set the next cluster pointer in the last cluster of the
  475          * file to CLUST_EOFE.
  476          */
  477         if (length == 0) {
  478                 chaintofree = dep->de_StartCluster;
  479                 dep->de_StartCluster = 0;
  480                 eofentry = ~0;
  481         } else {
  482                 error = pcbmap(dep, de_clcount(pmp, length) - 1, 0, 
  483                                &eofentry, 0);
  484                 if (error) {
  485 #ifdef MSDOSFS_DEBUG
  486                         printf("detrunc(): pcbmap fails %d\n", error);
  487 #endif
  488                         return (error);
  489                 }
  490         }
  491 
  492         fc_purge(dep, de_clcount(pmp, length));
  493 
  494         /*
  495          * If the new length is not a multiple of the cluster size then we
  496          * must zero the tail end of the new last cluster in case it
  497          * becomes part of the file again because of a seek.
  498          */
  499         if ((boff = length & pmp->pm_crbomask) != 0) {
  500                 if (isadir) {
  501                         bn = cntobn(pmp, eofentry);
  502                         error = bread(pmp->pm_devvp, bn, pmp->pm_bpcluster,
  503                             NOCRED, &bp);
  504                         if (error) {
  505                                 brelse(bp);
  506 #ifdef MSDOSFS_DEBUG
  507                                 printf("detrunc(): bread fails %d\n", error);
  508 #endif
  509                                 return (error);
  510                         }
  511                         bzero(bp->b_data + boff, pmp->pm_bpcluster - boff);
  512                         if (flags & IO_SYNC)
  513                                 bwrite(bp);
  514                         else
  515                                 bdwrite(bp);
  516                 }
  517         }
  518 
  519         /*
  520          * Write out the updated directory entry.  Even if the update fails
  521          * we free the trailing clusters.
  522          */
  523         dep->de_FileSize = length;
  524         if (!isadir)
  525                 dep->de_flag |= DE_UPDATE|DE_MODIFIED;
  526         allerror = vtruncbuf(DETOV(dep), cred, p, length, pmp->pm_bpcluster);
  527 #ifdef MSDOSFS_DEBUG
  528         if (allerror)
  529                 printf("detrunc(): vtruncbuf error %d\n", allerror);
  530 #endif
  531         error = deupdat(dep, 1);
  532         if (error && (allerror == 0))
  533                 allerror = error;
  534 #ifdef MSDOSFS_DEBUG
  535         printf("detrunc(): allerror %d, eofentry %lu\n",
  536                allerror, eofentry);
  537 #endif
  538 
  539         /*
  540          * If we need to break the cluster chain for the file then do it
  541          * now.
  542          */
  543         if (eofentry != ~0) {
  544                 error = fatentry(FAT_GET_AND_SET, pmp, eofentry,
  545                                  &chaintofree, CLUST_EOFE);
  546                 if (error) {
  547 #ifdef MSDOSFS_DEBUG
  548                         printf("detrunc(): fatentry errors %d\n", error);
  549 #endif
  550                         return (error);
  551                 }
  552                 fc_setcache(dep, FC_LASTFC, de_cluster(pmp, length - 1),
  553                             eofentry);
  554         }
  555 
  556         /*
  557          * Now free the clusters removed from the file because of the
  558          * truncation.
  559          */
  560         if (chaintofree != 0 && !MSDOSFSEOF(pmp, chaintofree))
  561                 freeclusterchain(pmp, chaintofree);
  562 
  563         return (allerror);
  564 }
  565 
  566 /*
  567  * Extend the file described by dep to length specified by length.
  568  */
  569 int
  570 deextend(dep, length, cred)
  571         struct denode *dep;
  572         u_long length;
  573         struct ucred *cred;
  574 {
  575         struct msdosfsmount *pmp = dep->de_pmp;
  576         u_long count;
  577         int error;
  578 
  579         /*
  580          * The root of a DOS filesystem cannot be extended.
  581          */
  582         if ((DETOV(dep)->v_flag & VROOT) && !FAT32(pmp))
  583                 return (EINVAL);
  584 
  585         /*
  586          * Directories cannot be extended.
  587          */
  588         if (dep->de_Attributes & ATTR_DIRECTORY)
  589                 return (EISDIR);
  590 
  591         if (length <= dep->de_FileSize)
  592                 panic("deextend: file too large");
  593 
  594         /*
  595          * Compute the number of clusters to allocate.
  596          */
  597         count = de_clcount(pmp, length) - de_clcount(pmp, dep->de_FileSize);
  598         if (count > 0) {
  599                 if (count > pmp->pm_freeclustercount)
  600                         return (ENOSPC);
  601                 error = extendfile(dep, count, NULL, NULL, DE_CLEAR);
  602                 if (error) {
  603                         /* truncate the added clusters away again */
  604                         (void) detrunc(dep, dep->de_FileSize, 0, cred, NULL);
  605                         return (error);
  606                 }
  607         }
  608         dep->de_FileSize = length;
  609         dep->de_flag |= DE_UPDATE|DE_MODIFIED;
  610         return (deupdat(dep, 1));
  611 }
  612 
  613 /*
  614  * Move a denode to its correct hash queue after the file it represents has
  615  * been moved to a new directory.
  616  */
  617 void
  618 reinsert(dep)
  619         struct denode *dep;
  620 {
  621         /*
  622          * Fix up the denode cache.  If the denode is for a directory,
  623          * there is nothing to do since the hash is based on the starting
  624          * cluster of the directory file and that hasn't changed.  If for a
  625          * file the hash is based on the location of the directory entry,
  626          * so we must remove it from the cache and re-enter it with the
  627          * hash based on the new location of the directory entry.
  628          */
  629         if (dep->de_Attributes & ATTR_DIRECTORY)
  630                 return;
  631         msdosfs_hashrem(dep);
  632         msdosfs_hashins(dep);
  633 }
  634 
  635 int
  636 msdosfs_reclaim(ap)
  637         struct vop_reclaim_args /* {
  638                 struct vnode *a_vp;
  639         } */ *ap;
  640 {
  641         struct vnode *vp = ap->a_vp;
  642         struct denode *dep = VTODE(vp);
  643 
  644 #ifdef MSDOSFS_DEBUG
  645         printf("msdosfs_reclaim(): dep %p, file %s, refcnt %ld\n",
  646             dep, dep->de_Name, dep->de_refcnt);
  647 #endif
  648 
  649         if (prtactive && vp->v_usecount != 0)
  650                 vprint("msdosfs_reclaim(): pushing active", vp);
  651         /*
  652          * Remove the denode from its hash chain.
  653          */
  654         msdosfs_hashrem(dep);
  655         /*
  656          * Purge old data structures associated with the denode.
  657          */
  658         cache_purge(vp);
  659         if (dep->de_devvp) {
  660                 vrele(dep->de_devvp);
  661                 dep->de_devvp = 0;
  662         }
  663 #if 0 /* XXX */
  664         dep->de_flag = 0;
  665 #endif
  666         FREE(dep, M_MSDOSFSNODE);
  667         vp->v_data = NULL;
  668 
  669         return (0);
  670 }
  671 
  672 int
  673 msdosfs_inactive(ap)
  674         struct vop_inactive_args /* {
  675                 struct vnode *a_vp;
  676                 struct proc *a_p;
  677         } */ *ap;
  678 {
  679         struct vnode *vp = ap->a_vp;
  680         struct denode *dep = VTODE(vp);
  681         struct proc *p = ap->a_p;
  682         int error = 0;
  683 
  684 #ifdef MSDOSFS_DEBUG
  685         printf("msdosfs_inactive(): dep %p, de_Name[0] %x\n", dep, dep->de_Name[0]);
  686 #endif
  687 
  688         if (prtactive && vp->v_usecount != 0)
  689                 vprint("msdosfs_inactive(): pushing active", vp);
  690 
  691         /*
  692          * Ignore denodes related to stale file handles.
  693          */
  694         if (dep->de_Name[0] == SLOT_DELETED)
  695                 goto out;
  696 
  697         /*
  698          * If the file has been deleted and it is on a read/write
  699          * filesystem, then truncate the file, and mark the directory slot
  700          * as empty.  (This may not be necessary for the dos filesystem.)
  701          */
  702 #ifdef MSDOSFS_DEBUG
  703         printf("msdosfs_inactive(): dep %p, refcnt %ld, mntflag %x, MNT_RDONLY %x\n",
  704                dep, dep->de_refcnt, vp->v_mount->mnt_flag, MNT_RDONLY);
  705 #endif
  706         if (dep->de_refcnt <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) {
  707                 error = detrunc(dep, (u_long) 0, 0, NOCRED, p);
  708                 dep->de_flag |= DE_UPDATE;
  709                 dep->de_Name[0] = SLOT_DELETED;
  710         }
  711         deupdat(dep, 0);
  712 
  713 out:
  714         VOP_UNLOCK(vp, 0, p);
  715         /*
  716          * If we are done with the denode, reclaim it
  717          * so that it can be reused immediately.
  718          */
  719 #ifdef MSDOSFS_DEBUG
  720         printf("msdosfs_inactive(): v_usecount %d, de_Name[0] %x\n", vp->v_usecount,
  721                dep->de_Name[0]);
  722 #endif
  723         if (dep->de_Name[0] == SLOT_DELETED)
  724                 vrecycle(vp, (struct simplelock *)0, p);
  725         return (error);
  726 }

Cache object: 35a5632666e20ccd69f6c734da500585


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