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/ufs/ffs/ffs_inode.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 /*
    2  * Copyright (c) 1982, 1986, 1989, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      @(#)ffs_inode.c 8.13 (Berkeley) 4/21/95
   34  */
   35 
   36 #include <sys/cdefs.h>
   37 __FBSDID("$FreeBSD: releng/5.2/sys/ufs/ffs/ffs_inode.c 121205 2003-10-18 14:10:28Z phk $");
   38 
   39 #include "opt_quota.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/mount.h>
   44 #include <sys/proc.h>
   45 #include <sys/bio.h>
   46 #include <sys/buf.h>
   47 #include <sys/vnode.h>
   48 #include <sys/malloc.h>
   49 #include <sys/resourcevar.h>
   50 #include <sys/vmmeter.h>
   51 #include <sys/stat.h>
   52 
   53 #include <vm/vm.h>
   54 #include <vm/vm_extern.h>
   55 
   56 #include <ufs/ufs/extattr.h>
   57 #include <ufs/ufs/quota.h>
   58 #include <ufs/ufs/ufsmount.h>
   59 #include <ufs/ufs/inode.h>
   60 #include <ufs/ufs/ufs_extern.h>
   61 
   62 #include <ufs/ffs/fs.h>
   63 #include <ufs/ffs/ffs_extern.h>
   64 
   65 static int ffs_indirtrunc(struct inode *, ufs2_daddr_t, ufs2_daddr_t,
   66             ufs2_daddr_t, int, ufs2_daddr_t *);
   67 
   68 /*
   69  * Update the access, modified, and inode change times as specified by the
   70  * IN_ACCESS, IN_UPDATE, and IN_CHANGE flags respectively.  Write the inode
   71  * to disk if the IN_MODIFIED flag is set (it may be set initially, or by
   72  * the timestamp update).  The IN_LAZYMOD flag is set to force a write
   73  * later if not now.  If we write now, then clear both IN_MODIFIED and
   74  * IN_LAZYMOD to reflect the presumably successful write, and if waitfor is
   75  * set, then wait for the write to complete.
   76  */
   77 int
   78 ffs_update(vp, waitfor)
   79         struct vnode *vp;
   80         int waitfor;
   81 {
   82         struct fs *fs;
   83         struct buf *bp;
   84         struct inode *ip;
   85         int error;
   86 
   87 #ifdef DEBUG_VFS_LOCKS
   88         if ((vp->v_iflag & VI_XLOCK) == 0)
   89                 ASSERT_VOP_LOCKED(vp, "ffs_update");
   90 #endif
   91         ufs_itimes(vp);
   92         ip = VTOI(vp);
   93         if ((ip->i_flag & IN_MODIFIED) == 0 && waitfor == 0)
   94                 return (0);
   95         ip->i_flag &= ~(IN_LAZYMOD | IN_MODIFIED);
   96         fs = ip->i_fs;
   97         if (fs->fs_ronly)
   98                 return (0);
   99         /*
  100          * Ensure that uid and gid are correct. This is a temporary
  101          * fix until fsck has been changed to do the update.
  102          */
  103         if (fs->fs_magic == FS_UFS1_MAGIC &&            /* XXX */
  104             fs->fs_old_inodefmt < FS_44INODEFMT) {      /* XXX */
  105                 ip->i_din1->di_ouid = ip->i_uid;        /* XXX */
  106                 ip->i_din1->di_ogid = ip->i_gid;        /* XXX */
  107         }                                               /* XXX */
  108         error = bread(ip->i_devvp, fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
  109                 (int)fs->fs_bsize, NOCRED, &bp);
  110         if (error) {
  111                 brelse(bp);
  112                 return (error);
  113         }
  114         if (DOINGSOFTDEP(vp))
  115                 softdep_update_inodeblock(ip, bp, waitfor);
  116         else if (ip->i_effnlink != ip->i_nlink)
  117                 panic("ffs_update: bad link cnt");
  118         if (ip->i_ump->um_fstype == UFS1)
  119                 *((struct ufs1_dinode *)bp->b_data +
  120                     ino_to_fsbo(fs, ip->i_number)) = *ip->i_din1;
  121         else
  122                 *((struct ufs2_dinode *)bp->b_data +
  123                     ino_to_fsbo(fs, ip->i_number)) = *ip->i_din2;
  124         if (waitfor && !DOINGASYNC(vp)) {
  125                 return (bwrite(bp));
  126         } else if (vm_page_count_severe() || buf_dirty_count_severe()) {
  127                 return (bwrite(bp));
  128         } else {
  129                 if (bp->b_bufsize == fs->fs_bsize)
  130                         bp->b_flags |= B_CLUSTEROK;
  131                 bdwrite(bp);
  132                 return (0);
  133         }
  134 }
  135 
  136 #define SINGLE  0       /* index of single indirect block */
  137 #define DOUBLE  1       /* index of double indirect block */
  138 #define TRIPLE  2       /* index of triple indirect block */
  139 /*
  140  * Truncate the inode oip to at most length size, freeing the
  141  * disk blocks.
  142  */
  143 int
  144 ffs_truncate(vp, length, flags, cred, td)
  145         struct vnode *vp;
  146         off_t length;
  147         int flags;
  148         struct ucred *cred;
  149         struct thread *td;
  150 {
  151         struct vnode *ovp = vp;
  152         struct inode *oip;
  153         ufs2_daddr_t bn, lbn, lastblock, lastiblock[NIADDR], indir_lbn[NIADDR];
  154         ufs2_daddr_t oldblks[NDADDR + NIADDR], newblks[NDADDR + NIADDR];
  155         ufs2_daddr_t count, blocksreleased = 0, datablocks;
  156         struct fs *fs;
  157         struct buf *bp;
  158         int needextclean, softdepslowdown, extblocks;
  159         int offset, size, level, nblocks;
  160         int i, error, allerror;
  161         off_t osize;
  162 
  163         oip = VTOI(ovp);
  164         fs = oip->i_fs;
  165         if (length < 0)
  166                 return (EINVAL);
  167         /*
  168          * Historically clients did not have to specify which data
  169          * they were truncating. So, if not specified, we assume
  170          * traditional behavior, e.g., just the normal data.
  171          */
  172         if ((flags & (IO_EXT | IO_NORMAL)) == 0)
  173                 flags |= IO_NORMAL;
  174         /*
  175          * If we are truncating the extended-attributes, and cannot
  176          * do it with soft updates, then do it slowly here. If we are
  177          * truncating both the extended attributes and the file contents
  178          * (e.g., the file is being unlinked), then pick it off with
  179          * soft updates below.
  180          */
  181         needextclean = 0;
  182         softdepslowdown = DOINGSOFTDEP(ovp) && softdep_slowdown(ovp);
  183         extblocks = 0;
  184         datablocks = DIP(oip, i_blocks);
  185         if (fs->fs_magic == FS_UFS2_MAGIC && oip->i_din2->di_extsize > 0) {
  186                 extblocks = btodb(fragroundup(fs, oip->i_din2->di_extsize));
  187                 datablocks -= extblocks;
  188         }
  189         if ((flags & IO_EXT) && extblocks > 0) {
  190                 if (DOINGSOFTDEP(ovp) && softdepslowdown == 0 && length == 0) {
  191                         if ((flags & IO_NORMAL) == 0) {
  192                                 softdep_setup_freeblocks(oip, length, IO_EXT);
  193                                 return (0);
  194                         }
  195                         needextclean = 1;
  196                 } else {
  197                         if (length != 0)
  198                                 panic("ffs_truncate: partial trunc of extdata");
  199                         if ((error = VOP_FSYNC(ovp, cred, MNT_WAIT, td)) != 0)
  200                                 return (error);
  201                         osize = oip->i_din2->di_extsize;
  202                         oip->i_din2->di_blocks -= extblocks;
  203 #ifdef QUOTA
  204                         (void) chkdq(oip, -extblocks, NOCRED, 0);
  205 #endif
  206                         vinvalbuf(ovp, V_ALT, cred, td, 0, 0);
  207                         oip->i_din2->di_extsize = 0;
  208                         for (i = 0; i < NXADDR; i++) {
  209                                 oldblks[i] = oip->i_din2->di_extb[i];
  210                                 oip->i_din2->di_extb[i] = 0;
  211                         }
  212                         oip->i_flag |= IN_CHANGE | IN_UPDATE;
  213                         if ((error = ffs_update(ovp, 1)))
  214                                 return (error);
  215                         for (i = 0; i < NXADDR; i++) {
  216                                 if (oldblks[i] == 0)
  217                                         continue;
  218                                 ffs_blkfree(fs, oip->i_devvp, oldblks[i],
  219                                     sblksize(fs, osize, i), oip->i_number);
  220                         }
  221                 }
  222         }
  223         if ((flags & IO_NORMAL) == 0)
  224                 return (0);
  225         if (length > fs->fs_maxfilesize)
  226                 return (EFBIG);
  227         if (ovp->v_type == VLNK &&
  228             (oip->i_size < ovp->v_mount->mnt_maxsymlinklen ||
  229              datablocks == 0)) {
  230 #ifdef DIAGNOSTIC
  231                 if (length != 0)
  232                         panic("ffs_truncate: partial truncate of symlink");
  233 #endif
  234                 bzero(SHORTLINK(oip), (u_int)oip->i_size);
  235                 oip->i_size = 0;
  236                 DIP(oip, i_size) = 0;
  237                 oip->i_flag |= IN_CHANGE | IN_UPDATE;
  238                 if (needextclean)
  239                         softdep_setup_freeblocks(oip, length, IO_EXT);
  240                 return (UFS_UPDATE(ovp, 1));
  241         }
  242         if (oip->i_size == length) {
  243                 oip->i_flag |= IN_CHANGE | IN_UPDATE;
  244                 if (needextclean)
  245                         softdep_setup_freeblocks(oip, length, IO_EXT);
  246                 return (UFS_UPDATE(ovp, 0));
  247         }
  248         if (fs->fs_ronly)
  249                 panic("ffs_truncate: read-only filesystem");
  250 #ifdef QUOTA
  251         error = getinoquota(oip);
  252         if (error)
  253                 return (error);
  254 #endif
  255         if ((oip->i_flags & SF_SNAPSHOT) != 0)
  256                 ffs_snapremove(ovp);
  257         ovp->v_lasta = ovp->v_clen = ovp->v_cstart = ovp->v_lastw = 0;
  258         if (DOINGSOFTDEP(ovp)) {
  259                 if (length > 0 || softdepslowdown) {
  260                         /*
  261                          * If a file is only partially truncated, then
  262                          * we have to clean up the data structures
  263                          * describing the allocation past the truncation
  264                          * point. Finding and deallocating those structures
  265                          * is a lot of work. Since partial truncation occurs
  266                          * rarely, we solve the problem by syncing the file
  267                          * so that it will have no data structures left.
  268                          */
  269                         if ((error = VOP_FSYNC(ovp, cred, MNT_WAIT, td)) != 0)
  270                                 return (error);
  271                         if (oip->i_flag & IN_SPACECOUNTED)
  272                                 fs->fs_pendingblocks -= datablocks;
  273                 } else {
  274 #ifdef QUOTA
  275                         (void) chkdq(oip, -datablocks, NOCRED, 0);
  276 #endif
  277                         softdep_setup_freeblocks(oip, length, needextclean ?
  278                             IO_EXT | IO_NORMAL : IO_NORMAL);
  279                         vinvalbuf(ovp, needextclean ? 0 : V_NORMAL,
  280                             cred, td, 0, 0);
  281                         oip->i_flag |= IN_CHANGE | IN_UPDATE;
  282                         return (ffs_update(ovp, 0));
  283                 }
  284         }
  285         osize = oip->i_size;
  286         /*
  287          * Lengthen the size of the file. We must ensure that the
  288          * last byte of the file is allocated. Since the smallest
  289          * value of osize is 0, length will be at least 1.
  290          */
  291         if (osize < length) {
  292                 vnode_pager_setsize(ovp, length);
  293                 flags |= BA_CLRBUF;
  294                 error = UFS_BALLOC(ovp, length - 1, 1, cred, flags, &bp);
  295                 if (error)
  296                         return (error);
  297                 oip->i_size = length;
  298                 DIP(oip, i_size) = length;
  299                 if (bp->b_bufsize == fs->fs_bsize)
  300                         bp->b_flags |= B_CLUSTEROK;
  301                 if (flags & IO_SYNC)
  302                         bwrite(bp);
  303                 else
  304                         bawrite(bp);
  305                 oip->i_flag |= IN_CHANGE | IN_UPDATE;
  306                 return (UFS_UPDATE(ovp, 1));
  307         }
  308         /*
  309          * Shorten the size of the file. If the file is not being
  310          * truncated to a block boundary, the contents of the
  311          * partial block following the end of the file must be
  312          * zero'ed in case it ever becomes accessible again because
  313          * of subsequent file growth. Directories however are not
  314          * zero'ed as they should grow back initialized to empty.
  315          */
  316         offset = blkoff(fs, length);
  317         if (offset == 0) {
  318                 oip->i_size = length;
  319                 DIP(oip, i_size) = length;
  320         } else {
  321                 lbn = lblkno(fs, length);
  322                 flags |= BA_CLRBUF;
  323                 error = UFS_BALLOC(ovp, length - 1, 1, cred, flags, &bp);
  324                 if (error) {
  325                         return (error);
  326                 }
  327                 /*
  328                  * When we are doing soft updates and the UFS_BALLOC
  329                  * above fills in a direct block hole with a full sized
  330                  * block that will be truncated down to a fragment below,
  331                  * we must flush out the block dependency with an FSYNC
  332                  * so that we do not get a soft updates inconsistency
  333                  * when we create the fragment below.
  334                  */
  335                 if (DOINGSOFTDEP(ovp) && lbn < NDADDR &&
  336                     fragroundup(fs, blkoff(fs, length)) < fs->fs_bsize &&
  337                     (error = VOP_FSYNC(ovp, cred, MNT_WAIT, td)) != 0)
  338                         return (error);
  339                 oip->i_size = length;
  340                 DIP(oip, i_size) = length;
  341                 size = blksize(fs, oip, lbn);
  342                 if (ovp->v_type != VDIR)
  343                         bzero((char *)bp->b_data + offset,
  344                             (u_int)(size - offset));
  345                 /* Kirk's code has reallocbuf(bp, size, 1) here */
  346                 allocbuf(bp, size);
  347                 if (bp->b_bufsize == fs->fs_bsize)
  348                         bp->b_flags |= B_CLUSTEROK;
  349                 if (flags & IO_SYNC)
  350                         bwrite(bp);
  351                 else
  352                         bawrite(bp);
  353         }
  354         /*
  355          * Calculate index into inode's block list of
  356          * last direct and indirect blocks (if any)
  357          * which we want to keep.  Lastblock is -1 when
  358          * the file is truncated to 0.
  359          */
  360         lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
  361         lastiblock[SINGLE] = lastblock - NDADDR;
  362         lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
  363         lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
  364         nblocks = btodb(fs->fs_bsize);
  365         /*
  366          * Update file and block pointers on disk before we start freeing
  367          * blocks.  If we crash before free'ing blocks below, the blocks
  368          * will be returned to the free list.  lastiblock values are also
  369          * normalized to -1 for calls to ffs_indirtrunc below.
  370          */
  371         for (level = TRIPLE; level >= SINGLE; level--) {
  372                 oldblks[NDADDR + level] = DIP(oip, i_ib[level]);
  373                 if (lastiblock[level] < 0) {
  374                         DIP(oip, i_ib[level]) = 0;
  375                         lastiblock[level] = -1;
  376                 }
  377         }
  378         for (i = 0; i < NDADDR; i++) {
  379                 oldblks[i] = DIP(oip, i_db[i]);
  380                 if (i > lastblock)
  381                         DIP(oip, i_db[i]) = 0;
  382         }
  383         oip->i_flag |= IN_CHANGE | IN_UPDATE;
  384         allerror = UFS_UPDATE(ovp, 1);
  385         
  386         /*
  387          * Having written the new inode to disk, save its new configuration
  388          * and put back the old block pointers long enough to process them.
  389          * Note that we save the new block configuration so we can check it
  390          * when we are done.
  391          */
  392         for (i = 0; i < NDADDR; i++) {
  393                 newblks[i] = DIP(oip, i_db[i]);
  394                 DIP(oip, i_db[i]) = oldblks[i];
  395         }
  396         for (i = 0; i < NIADDR; i++) {
  397                 newblks[NDADDR + i] = DIP(oip, i_ib[i]);
  398                 DIP(oip, i_ib[i]) = oldblks[NDADDR + i];
  399         }
  400         oip->i_size = osize;
  401         DIP(oip, i_size) = osize;
  402 
  403         error = vtruncbuf(ovp, cred, td, length, fs->fs_bsize);
  404         if (error && (allerror == 0))
  405                 allerror = error;
  406 
  407         /*
  408          * Indirect blocks first.
  409          */
  410         indir_lbn[SINGLE] = -NDADDR;
  411         indir_lbn[DOUBLE] = indir_lbn[SINGLE] - NINDIR(fs) - 1;
  412         indir_lbn[TRIPLE] = indir_lbn[DOUBLE] - NINDIR(fs) * NINDIR(fs) - 1;
  413         for (level = TRIPLE; level >= SINGLE; level--) {
  414                 bn = DIP(oip, i_ib[level]);
  415                 if (bn != 0) {
  416                         error = ffs_indirtrunc(oip, indir_lbn[level],
  417                             fsbtodb(fs, bn), lastiblock[level], level, &count);
  418                         if (error)
  419                                 allerror = error;
  420                         blocksreleased += count;
  421                         if (lastiblock[level] < 0) {
  422                                 DIP(oip, i_ib[level]) = 0;
  423                                 ffs_blkfree(fs, oip->i_devvp, bn, fs->fs_bsize,
  424                                     oip->i_number);
  425                                 blocksreleased += nblocks;
  426                         }
  427                 }
  428                 if (lastiblock[level] >= 0)
  429                         goto done;
  430         }
  431 
  432         /*
  433          * All whole direct blocks or frags.
  434          */
  435         for (i = NDADDR - 1; i > lastblock; i--) {
  436                 long bsize;
  437 
  438                 bn = DIP(oip, i_db[i]);
  439                 if (bn == 0)
  440                         continue;
  441                 DIP(oip, i_db[i]) = 0;
  442                 bsize = blksize(fs, oip, i);
  443                 ffs_blkfree(fs, oip->i_devvp, bn, bsize, oip->i_number);
  444                 blocksreleased += btodb(bsize);
  445         }
  446         if (lastblock < 0)
  447                 goto done;
  448 
  449         /*
  450          * Finally, look for a change in size of the
  451          * last direct block; release any frags.
  452          */
  453         bn = DIP(oip, i_db[lastblock]);
  454         if (bn != 0) {
  455                 long oldspace, newspace;
  456 
  457                 /*
  458                  * Calculate amount of space we're giving
  459                  * back as old block size minus new block size.
  460                  */
  461                 oldspace = blksize(fs, oip, lastblock);
  462                 oip->i_size = length;
  463                 DIP(oip, i_size) = length;
  464                 newspace = blksize(fs, oip, lastblock);
  465                 if (newspace == 0)
  466                         panic("ffs_truncate: newspace");
  467                 if (oldspace - newspace > 0) {
  468                         /*
  469                          * Block number of space to be free'd is
  470                          * the old block # plus the number of frags
  471                          * required for the storage we're keeping.
  472                          */
  473                         bn += numfrags(fs, newspace);
  474                         ffs_blkfree(fs, oip->i_devvp, bn, oldspace - newspace,
  475                             oip->i_number);
  476                         blocksreleased += btodb(oldspace - newspace);
  477                 }
  478         }
  479 done:
  480 #ifdef DIAGNOSTIC
  481         for (level = SINGLE; level <= TRIPLE; level++)
  482                 if (newblks[NDADDR + level] != DIP(oip, i_ib[level]))
  483                         panic("ffs_truncate1");
  484         for (i = 0; i < NDADDR; i++)
  485                 if (newblks[i] != DIP(oip, i_db[i]))
  486                         panic("ffs_truncate2");
  487         VI_LOCK(ovp);
  488         if (length == 0 &&
  489             (fs->fs_magic != FS_UFS2_MAGIC || oip->i_din2->di_extsize == 0) &&
  490             (!TAILQ_EMPTY(&ovp->v_dirtyblkhd) ||
  491              !TAILQ_EMPTY(&ovp->v_cleanblkhd)))
  492                 panic("ffs_truncate3");
  493         VI_UNLOCK(ovp);
  494 #endif /* DIAGNOSTIC */
  495         /*
  496          * Put back the real size.
  497          */
  498         oip->i_size = length;
  499         DIP(oip, i_size) = length;
  500         DIP(oip, i_blocks) -= blocksreleased;
  501 
  502         if (DIP(oip, i_blocks) < 0)                     /* sanity */
  503                 DIP(oip, i_blocks) = 0;
  504         oip->i_flag |= IN_CHANGE;
  505 #ifdef QUOTA
  506         (void) chkdq(oip, -blocksreleased, NOCRED, 0);
  507 #endif
  508         return (allerror);
  509 }
  510 
  511 /*
  512  * Release blocks associated with the inode ip and stored in the indirect
  513  * block bn.  Blocks are free'd in LIFO order up to (but not including)
  514  * lastbn.  If level is greater than SINGLE, the block is an indirect block
  515  * and recursive calls to indirtrunc must be used to cleanse other indirect
  516  * blocks.
  517  */
  518 static int
  519 ffs_indirtrunc(ip, lbn, dbn, lastbn, level, countp)
  520         struct inode *ip;
  521         ufs2_daddr_t lbn, lastbn;
  522         ufs2_daddr_t dbn;
  523         int level;
  524         ufs2_daddr_t *countp;
  525 {
  526         struct buf *bp;
  527         struct fs *fs = ip->i_fs;
  528         struct vnode *vp;
  529         caddr_t copy = NULL;
  530         int i, nblocks, error = 0, allerror = 0;
  531         ufs2_daddr_t nb, nlbn, last;
  532         ufs2_daddr_t blkcount, factor, blocksreleased = 0;
  533         ufs1_daddr_t *bap1 = NULL;
  534         ufs2_daddr_t *bap2 = NULL;
  535 #       define BAP(ip, i) (((ip)->i_ump->um_fstype == UFS1) ? bap1[i] : bap2[i])
  536 
  537         /*
  538          * Calculate index in current block of last
  539          * block to be kept.  -1 indicates the entire
  540          * block so we need not calculate the index.
  541          */
  542         factor = 1;
  543         for (i = SINGLE; i < level; i++)
  544                 factor *= NINDIR(fs);
  545         last = lastbn;
  546         if (lastbn > 0)
  547                 last /= factor;
  548         nblocks = btodb(fs->fs_bsize);
  549         /*
  550          * Get buffer of block pointers, zero those entries corresponding
  551          * to blocks to be free'd, and update on disk copy first.  Since
  552          * double(triple) indirect before single(double) indirect, calls
  553          * to bmap on these blocks will fail.  However, we already have
  554          * the on disk address, so we have to set the b_blkno field
  555          * explicitly instead of letting bread do everything for us.
  556          */
  557         vp = ITOV(ip);
  558         bp = getblk(vp, lbn, (int)fs->fs_bsize, 0, 0, 0);
  559         if ((bp->b_flags & B_CACHE) == 0) {
  560                 curproc->p_stats->p_ru.ru_inblock++;    /* pay for read */
  561                 bp->b_iocmd = BIO_READ;
  562                 bp->b_flags &= ~B_INVAL;
  563                 bp->b_ioflags &= ~BIO_ERROR;
  564                 if (bp->b_bcount > bp->b_bufsize)
  565                         panic("ffs_indirtrunc: bad buffer size");
  566                 bp->b_blkno = dbn;
  567                 vfs_busy_pages(bp, 0);
  568                 bp->b_iooffset = dbtob(bp->b_blkno);
  569                 VOP_STRATEGY(bp->b_vp, bp);
  570                 error = bufwait(bp);
  571         }
  572         if (error) {
  573                 brelse(bp);
  574                 *countp = 0;
  575                 return (error);
  576         }
  577 
  578         if (ip->i_ump->um_fstype == UFS1)
  579                 bap1 = (ufs1_daddr_t *)bp->b_data;
  580         else
  581                 bap2 = (ufs2_daddr_t *)bp->b_data;
  582         if (lastbn != -1) {
  583                 MALLOC(copy, caddr_t, fs->fs_bsize, M_TEMP, M_WAITOK);
  584                 bcopy((caddr_t)bp->b_data, copy, (u_int)fs->fs_bsize);
  585                 for (i = last + 1; i < NINDIR(fs); i++)
  586                         BAP(ip, i) = 0;
  587                 if (DOINGASYNC(vp)) {
  588                         bawrite(bp);
  589                 } else {
  590                         error = bwrite(bp);
  591                         if (error)
  592                                 allerror = error;
  593                 }
  594                 if (ip->i_ump->um_fstype == UFS1)
  595                         bap1 = (ufs1_daddr_t *)copy;
  596                 else
  597                         bap2 = (ufs2_daddr_t *)copy;
  598         }
  599 
  600         /*
  601          * Recursively free totally unused blocks.
  602          */
  603         for (i = NINDIR(fs) - 1, nlbn = lbn + 1 - i * factor; i > last;
  604             i--, nlbn += factor) {
  605                 nb = BAP(ip, i);
  606                 if (nb == 0)
  607                         continue;
  608                 if (level > SINGLE) {
  609                         if ((error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
  610                             (ufs2_daddr_t)-1, level - 1, &blkcount)) != 0)
  611                                 allerror = error;
  612                         blocksreleased += blkcount;
  613                 }
  614                 ffs_blkfree(fs, ip->i_devvp, nb, fs->fs_bsize, ip->i_number);
  615                 blocksreleased += nblocks;
  616         }
  617 
  618         /*
  619          * Recursively free last partial block.
  620          */
  621         if (level > SINGLE && lastbn >= 0) {
  622                 last = lastbn % factor;
  623                 nb = BAP(ip, i);
  624                 if (nb != 0) {
  625                         error = ffs_indirtrunc(ip, nlbn, fsbtodb(fs, nb),
  626                             last, level - 1, &blkcount);
  627                         if (error)
  628                                 allerror = error;
  629                         blocksreleased += blkcount;
  630                 }
  631         }
  632         if (copy != NULL) {
  633                 FREE(copy, M_TEMP);
  634         } else {
  635                 bp->b_flags |= B_INVAL | B_NOCACHE;
  636                 brelse(bp);
  637         }
  638 
  639         *countp = blocksreleased;
  640         return (allerror);
  641 }

Cache object: 6318fc54d9d325c5b2e189fb56a0287a


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