only delete space used by inode, on inode deletion; required
[unix-history] / usr / src / sys / ufs / lfs / lfs_inode.c
index 83b7b7a..e3e28c9 100644 (file)
-/*     lfs_inode.c     4.28    82/10/17        */
-
-#include "../h/param.h"
-#include "../h/systm.h"
-#include "../h/mount.h"
-#include "../h/dir.h"
-#include "../h/user.h"
-#include "../h/inode.h"
-#include "../h/fs.h"
-#include "../h/conf.h"
-#include "../h/buf.h"
-#ifdef QUOTA
-#include "../h/quota.h"
-#endif
-#include "../h/kernel.h"
+/*
+ * Copyright (c) 1986, 1989, 1991 Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ *
+ *     @(#)lfs_inode.c 7.56 (Berkeley) %G%
+ */
 
 
-#define        INOHSZ  63
-#if    ((INOHSZ&(INOHSZ-1)) == 0)
-#define        INOHASH(dev,ino)        (((dev)+(ino))&(INOHSZ-1))
-#else
-#define        INOHASH(dev,ino)        (((dev)+(ino))%INOHSZ)
-#endif
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/mount.h>
+#include <sys/proc.h>
+#include <sys/file.h>
+#include <sys/buf.h>
+#include <sys/vnode.h>
+#include <sys/kernel.h>
+#include <sys/malloc.h>
 
 
-union ihead {                          /* inode LRU cache, Chris Maltby */
-       union  ihead *ih_head[2];
-       struct inode *ih_chain[2];
-} ihead[INOHSZ];
+#include <ufs/ufs/quota.h>
+#include <ufs/ufs/inode.h>
+#include <ufs/ufs/ufsmount.h>
+#include <ufs/ufs/ufs_extern.h>
 
 
-struct inode *ifreeh, **ifreet;
+#include <ufs/lfs/lfs.h>
+#include <ufs/lfs/lfs_extern.h>
 
 
-/*
- * Initialize hash links for inodes
- * and build inode free list.
- */
-ihinit()
+int
+lfs_init()
 {
 {
-       register int i;
-       register struct inode *ip = inode;
-       register union  ihead *ih = ihead;
-
-       for (i = INOHSZ; --i >= 0; ih++) {
-               ih->ih_head[0] = ih;
-               ih->ih_head[1] = ih;
-       }
-       ifreeh = ip;
-       ifreet = &ip->i_freef;
-       ip->i_freeb = &ifreeh;
-       ip->i_forw = ip;
-       ip->i_back = ip;
-       for (i = ninode; --i > 0; ) {
-               ++ip;
-               ip->i_forw = ip;
-               ip->i_back = ip;
-               *ifreet = ip;
-               ip->i_freeb = ifreet;
-               ifreet = &ip->i_freef;
-       }
-       ip->i_freef = NULL;
+#ifdef VERBOSE
+       printf("lfs_init\n");
+#endif
+       return (ufs_init());
 }
 
 }
 
-#ifdef notdef
 /*
 /*
- * Find an inode if it is incore.
- * This is the equivalent, for inodes,
- * of ``incore'' in bio.c or ``pfind'' in subr.c.
+ * Look up an LFS dinode number to find its incore vnode.  If not already
+ * in core, read it in from the specified device.  Return the inode locked.
+ * Detection and handling of mount points must be done by the calling routine.
  */
  */
-struct inode *
-ifind(dev, ino)
-       dev_t dev;
+int
+lfs_vget(mntp, ino, vpp)
+       struct mount *mntp;
        ino_t ino;
        ino_t ino;
+       struct vnode **vpp;
 {
 {
+       register struct lfs *fs;
        register struct inode *ip;
        register struct inode *ip;
-       register union  ihead *ih;
-
-       ih = &ihead[INOHASH(dev, ino)];
-       for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw)
-               if (ino==ip->i_number && dev==ip->i_dev)
-                       return (ip);
-       return ((struct inode *)0);
-}
-#endif notdef
-
-/*
- * Look up an inode by device,inumber.
- * If it is in core (in the inode structure),
- * honor the locking protocol.
- * If it is not in core, read it in from the
- * specified device.
- * If the inode is mounted on, perform
- * the indicated indirection.
- * In all cases, a pointer to a locked
- * inode structure is returned.
- *
- * panic: no imt -- if the mounted file
- *     system is not in the mount table.
- *     "cannot happen"
- */
-struct inode *
-iget(dev, fs, ino)
+       struct buf *bp;
+       struct ifile *ifp;
+       struct vnode *vp;
+       struct ufsmount *ump;
+       daddr_t daddr;
        dev_t dev;
        dev_t dev;
-       register struct fs *fs;
-       ino_t ino;
-{
-       register struct inode *ip;
-       register union  ihead *ih;
-       register struct mount *mp;
-       register struct buf *bp;
-       register struct dinode *dp;
-       register struct inode *iq;
+       int error;
 
 
-loop:
-       if (getfs(dev) != fs)
-               panic("iget: bad fs");
-       ih = &ihead[INOHASH(dev, ino)];
-       for (ip = ih->ih_chain[0]; ip != (struct inode *)ih; ip = ip->i_forw)
-               if (ino == ip->i_number && dev == ip->i_dev) {
-                       if ((ip->i_flag&ILOCKED) != 0) {
-                               ip->i_flag |= IWANT;
-                               sleep((caddr_t)ip, PINOD);
-                               goto loop;
-                       }
-                       if ((ip->i_flag&IMOUNT) != 0) {
-                               for (mp = &mount[0]; mp < &mount[NMOUNT]; mp++)
-                                       if(mp->m_inodp == ip) {
-                                               dev = mp->m_dev;
-                                               fs = mp->m_bufp->b_un.b_fs;
-                                               ino = ROOTINO;
-                                               goto loop;
-                                       }
-                               panic("no imt");
-                       }
-                       if (ip->i_count == 0) {         /* ino on free list */
-                               if (iq = ip->i_freef)
-                                       iq->i_freeb = ip->i_freeb;
-                               else
-                                       ifreet = ip->i_freeb;
-                               *ip->i_freeb = iq;
-                               ip->i_freef = NULL;
-                               ip->i_freeb = NULL;
-                       }
-                       ip->i_count++;
-                       ip->i_flag |= ILOCKED;
-                       return(ip);
-               }
+#ifdef VERBOSE
+       printf("lfs_vget\n");
+#endif
+       ump = VFSTOUFS(mntp);
+       dev = ump->um_dev;
+       if ((*vpp = ufs_ihashget(dev, ino)) != NULL)
+               return (0);
+
+       /* Translate the inode number to a disk address. */
+       fs = ump->um_lfs;
+       if (ino == LFS_IFILE_INUM)
+               daddr = fs->lfs_idaddr;
+       else {
+               LFS_IENTRY(ifp, fs, ino, bp);
+               daddr = ifp->if_daddr;
+               brelse(bp);
+               if (daddr == LFS_UNUSED_DADDR)
+                       return (ENOENT);
+       }
 
 
-       if ((ip = ifreeh) == NULL) {
-               tablefull("inode");
-               u.u_error = ENFILE;
-               return(NULL);
+       /* Allocate new vnode/inode. */
+       if (error = lfs_vcreate(mntp, ino, &vp)) {
+               *vpp = NULL;
+               return (error);
        }
        }
-       if (iq = ip->i_freef)
-               iq->i_freeb = &ifreeh;
-       ifreeh = iq;
-       ip->i_freef = NULL;
-       ip->i_freeb = NULL;
+
        /*
        /*
-        * Now to take inode off the hash chain it was on
-        * (initially, or after an iflush, it is on a "hash chain"
-        * consisting entirely of itself, and pointed to by no-one,
-        * but that doesn't matter), and put it on the chain for
-        * its new (ino, dev) pair
+        * Put it onto its hash chain and lock it so that other requests for
+        * this inode will block if they arrive while we are sleeping waiting
+        * for old data structures to be purged or for the contents of the
+        * disk portion of this inode to be read.
         */
         */
-       remque(ip);
-       insque(ip, ih);
-#ifdef QUOTA
-       dqrele(ip->i_dquot);
-#endif
-       ip->i_dev = dev;
-       ip->i_fs = fs;
-       ip->i_number = ino;
-       ip->i_flag = ILOCKED;
-       ip->i_count++;
-       ip->i_lastr = 0;
-       bp = bread(dev, fsbtodb(fs, itod(fs, ino)), (int)fs->fs_bsize);
+       ip = VTOI(vp);
+       ufs_ihashins(ip);
+
        /*
        /*
-        * Check I/O errors
+        * XXX
+        * This may not need to be here, logically it should go down with
+        * the i_devvp initialization.
+        * Ask Kirk.
         */
         */
-       if ((bp->b_flags&B_ERROR) != 0) {
-               brelse(bp);
+       ip->i_lfs = ump->um_lfs;
+
+       /* Read in the disk contents for the inode, copy into the inode. */
+       if (error =
+           bread(ump->um_devvp, daddr, (int)fs->lfs_bsize, NOCRED, &bp)) {
                /*
                /*
-                * the inode doesn't contain anything useful, so it would
-                * be misleading to leave it on its hash chain.
-                * 'iput' will take care of putting it back on the free list.
+                * The inode does not contain anything useful, so it
+                * would be misleading to leave it on its hash chain.
+                * Iput() will return it to the free list.
                 */
                remque(ip);
                ip->i_forw = ip;
                ip->i_back = ip;
                 */
                remque(ip);
                ip->i_forw = ip;
                ip->i_back = ip;
-               /*
-                * we also loose its inumber, just in case (as iput
-                * doesn't do that any more) - but as it isn't on its
-                * hash chain, I doubt if this is really necessary .. kre
-                * (probably the two methods are interchangable)
-                */
-               ip->i_number = 0;
-#ifdef QUOTA
-               ip->i_dquot = NODQUOT;
-#endif
-               iput(ip);
-               return(NULL);
+
+               /* Unlock and discard unneeded inode. */
+               ufs_iput(ip);
+               brelse(bp);
+               *vpp = NULL;
+               return (error);
        }
        }
-       dp = bp->b_un.b_dino;
-       dp += itoo(fs, ino);
-       ip->i_ic = dp->di_ic;
+       ip->i_din = *lfs_ifind(fs, ino, bp->b_un.b_dino);
        brelse(bp);
        brelse(bp);
-#ifdef QUOTA
-       if (ip->i_mode == 0)
-               ip->i_dquot = NODQUOT;
-       else
-               ip->i_dquot = inoquota(ip);
-#endif
-       return (ip);
-}
 
 
-/*
- * Decrement reference count of
- * an inode structure.
- * On the last reference,
- * write the inode out and if necessary,
- * truncate and deallocate the file.
- */
-iput(ip)
-       register struct inode *ip;
-{
-
-       if ((ip->i_flag & ILOCKED) == 0)
-               panic("iput");
-       iunlock(ip);
-       irele(ip);
+       /*
+        * Initialize the vnode from the inode, check for aliases.  In all
+        * cases re-init ip, the underlying vnode/inode may have changed.
+        */
+       if (error = ufs_vinit(mntp, &lfs_specops, LFS_FIFOOPS, &vp)) {
+               ufs_iput(ip);
+               *vpp = NULL;
+               return (error);
+       }
+       /*
+        * Finish inode initialization now that aliasing has been resolved.
+        */
+       ip->i_devvp = ump->um_devvp;
+       VREF(ip->i_devvp);
+       *vpp = vp;
+       return (0);
 }
 
 }
 
-irele(ip)
-       register struct inode *ip;
+int
+lfs_update(vp, ta, tm, waitfor)
+       register struct vnode *vp;
+       struct timeval *ta, *tm;
+        int waitfor;
 {
 {
-       int mode;
+       struct inode *ip;
 
 
-       if (ip->i_count == 1) {
-               ip->i_flag |= ILOCKED;
-               if (ip->i_nlink <= 0) {
-                       itrunc(ip, 0);
-                       mode = ip->i_mode;
-                       ip->i_mode = 0;
-                       ip->i_rdev = 0;
-                       ip->i_flag |= IUPD|ICHG;
-                       ifree(ip, ip->i_number, mode);
-#ifdef QUOTA
-                       chkiq(ip->i_dev, ip, ip->i_uid, 0);
-                       dqrele(ip->i_dquot);
-                       ip->i_dquot = NODQUOT;
+#ifdef VERBOSE
+       printf("lfs_update\n");
 #endif
 #endif
-               }
-               IUPDAT(ip, time, time, 0);
-               iunlock(ip);
-               ip->i_flag = 0;
-               /*
-                * Put the inode on the end of the free list.
-                * Possibly in some cases it would be better to
-                * put the inode at the head of the free list,
-                * (eg: where i_mode == 0 || i_number == 0)
-                * but I will think about that later .. kre
-                * (i_number is rarely 0 - only after an i/o error in iget,
-                * where i_mode == 0, the inode will probably be wanted
-                * again soon for an ialloc, so possibly we should keep it)
-                */
-               if (ifreeh) {
-                       *ifreet = ip;
-                       ip->i_freeb = ifreet;
-               } else {
-                       ifreeh = ip;
-                       ip->i_freeb = &ifreeh;
-               }
-               ip->i_freef = NULL;
-               ifreet = &ip->i_freef;
+       if (vp->v_mount->mnt_flag & MNT_RDONLY)
+               return (0);
+       ip = VTOI(vp);
+       if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) == 0)
+               return (0);
+       if (ip->i_flag&IACC)
+               ip->i_atime = ta->tv_sec;
+       if (ip->i_flag&IUPD) {
+               ip->i_mtime = tm->tv_sec;
+               INCRQUAD((ip)->i_modrev);
        }
        }
-       ip->i_count--;
-}
+       if (ip->i_flag&ICHG)
+               ip->i_ctime = time.tv_sec;
+       ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
 
 
-/*
- * Check accessed and update flags on
- * an inode structure.
- * If any is on, update the inode
- * with the current time.
- * If waitfor is given, then must insure
- * i/o order so wait for write to complete.
- */
-iupdat(ip, ta, tm, waitfor)
-       register struct inode *ip;
-       struct timeval *ta, *tm;
-       int waitfor;
-{
-       register struct buf *bp;
-       struct dinode *dp;
-       register struct fs *fp;
+       /* Push back the vnode and any dirty blocks it may have. */
+       return (waitfor ? lfs_vflush(vp) : 0);
+}
 
 
-       fp = ip->i_fs;
-       if ((ip->i_flag & (IUPD|IACC|ICHG)) != 0) {
-               if (fp->fs_ronly)
-                       return;
-               bp = bread(ip->i_dev, fsbtodb(fp, itod(fp, ip->i_number)),
-                       (int)fp->fs_bsize);
-               if (bp->b_flags & B_ERROR) {
-                       brelse(bp);
-                       return;
-               }
-               if (ip->i_flag&IACC)
-                       ip->i_atime = ta->tv_sec;
-               if (ip->i_flag&IUPD)
-                       ip->i_mtime = tm->tv_sec;
-               if (ip->i_flag&ICHG)
-                       ip->i_ctime = time.tv_sec;
-               ip->i_flag &= ~(IUPD|IACC|ICHG);
-               dp = bp->b_un.b_dino + itoo(fp, ip->i_number);
-               dp->di_ic = ip->i_ic;
-               if (waitfor)
-                       bwrite(bp);
-               else
-                       bdwrite(bp);
+/* Update segment usage information when removing a block. */
+#define UPDATE_SEGUSE \
+       if (lastseg != -1) { \
+               LFS_SEGENTRY(sup, fs, lastseg, sup_bp); \
+               sup->su_nbytes -= fs->lfs_bsize * num; \
+               LFS_UBWRITE(sup_bp); \
+               blocksreleased += num; \
        }
        }
+
+#define SEGDEC { \
+       if (daddr != UNASSIGNED) { \
+               if (lastseg != (seg = datosn(fs, daddr))) { \
+                       UPDATE_SEGUSE; \
+                       num = 1; \
+                       lastseg = seg; \
+               } else \
+                       ++num; \
+       } \
 }
 
 /*
 }
 
 /*
- * Truncate the inode ip to at most
- * length size.  Free affected disk
- * blocks -- the blocks of the file
- * are removed in reverse order.
+ * Truncate the inode ip to at most length size.  Update segment usage
+ * table information.
  */
  */
-itrunc(ip, length)
-       register struct inode *ip;
-       register int length;
+/* ARGSUSED */
+int
+lfs_truncate(vp, length, flags)
+       struct vnode *vp;
+       u_long length;
+       int flags;
 {
 {
-       register i;
-       dev_t dev;
-       daddr_t bn;
-       struct inode itmp;
-       register struct fs *fs;
-#ifdef QUOTA
-       register long cnt = 0;
-       long tloop();
+       register INDIR *ap;
+       register int i;
+       register daddr_t *daddrp;
+       struct buf *bp, *sup_bp;
+       struct ifile *ifp;
+       struct inode *ip;
+       struct lfs *fs;
+       INDIR a[NIADDR + 2], a_end[NIADDR + 2];
+       SEGUSE *sup;
+       daddr_t daddr, lastblock, lbn, olastblock;
+       off_t off;
+       long blocksreleased;
+       int error, depth, lastseg, num, offset, seg, size;
+
+#ifdef VERBOSE
+       printf("lfs_truncate\n");
 #endif
 #endif
-       /*
-        * Only plain files, directories and symbolic
-        * links contain blocks.
-        */
-       i = ip->i_mode & IFMT;
-       if (i != IFREG && i != IFDIR && i != IFLNK)
-               return;
-       if (ip->i_size <= length)
-               return;
+       vnode_pager_setsize(vp, length);
 
 
-       /*
-        * Clean inode on disk before freeing blocks
-        * to insure no duplicates if system crashes.
-        */
-       itmp = *ip;
-       itmp.i_size = length;
-       for (i = 0; i < NDADDR; i++)
-               itmp.i_db[i] = 0;
-       for (i = 0; i < NIADDR; i++)
-               itmp.i_ib[i] = 0;
-       itmp.i_flag |= ICHG|IUPD;
-       iupdat(&itmp, time, time, 1);
-       ip->i_flag &= ~(IUPD|IACC|ICHG);
+       ip = VTOI(vp);
+       fs = ip->i_lfs;
 
 
-       /*
-        * Now return blocks to free list... if machine
-        * crashes, they will be harmless MISSING blocks.
-        */
-       fs = ip->i_fs;
-       /*
-        * release double indirect block first
-        */
-       bn = ip->i_ib[NIADDR-1];
-       if (bn != (daddr_t)0) {
-               ip->i_ib[NIADDR - 1] = (daddr_t)0;
-#ifdef QUOTA
-               cnt +=
-#endif
-                       tloop(ip, bn, 1);
+       /* If truncating the file to 0, update the version number. */
+       if (length == 0) {
+               LFS_IENTRY(ifp, fs, ip->i_number, bp);
+               ++ifp->if_version;
+               LFS_UBWRITE(bp);
        }
        }
-       /*
-        * release single indirect blocks second
-        */
-       for (i = NIADDR - 2; i >= 0; i--) {
-               bn = ip->i_ib[i];
-               if (bn != (daddr_t)0) {
-                       ip->i_ib[i] = (daddr_t)0;
-#ifdef QUOTA
-                       cnt +=
-#endif
-                               tloop(ip, bn, 0);
-               }
+
+       /* If length is larger than the file, just update the times. */
+       if (ip->i_size <= length) {
+               ip->i_flag |= ICHG|IUPD;
+               return (lfs_update(vp, &time, &time, 1));
        }
        }
+
        /*
        /*
-        * finally release direct blocks
+        * Calculate index into inode's block list of last direct and indirect
+        * blocks (if any) which we want to keep.  Lastblock is 0 when the
+        * file is truncated to 0.
         */
         */
-       for (i = NDADDR - 1; i>=0; i--) {
-               register size;
+       lastblock = lblkno(fs, length + fs->lfs_bsize - 1);
+       olastblock = lblkno(fs, ip->i_size + fs->lfs_bsize - 1) - 1;
 
 
-               bn = ip->i_db[i];
-               if (bn == (daddr_t)0)
-                       continue;
-               ip->i_db[i] = (daddr_t)0;
-#ifndef QUOTA
-               fre(ip, bn, (off_t)blksize(fs, ip, i));
-#else
-               fre(ip, bn, size = (off_t)blksize(fs, ip, i));
-               cnt += size / DEV_BSIZE;
-#endif
-       }
-       ip->i_size = 0;
        /*
        /*
-        * Inode was written and flags updated above.
-        * No need to modify flags here.
+        * Update the size of the file. If the file is not being truncated to
+        * a block boundry, the contents of the partial block following the end
+        * of the file must be zero'ed in case it ever become accessable again
+        * because of subsequent file growth.
         */
         */
+       offset = blkoff(fs, length);
+       if (offset == 0)
+               ip->i_size = length;
+       else {
+               lbn = lblkno(fs, length);
 #ifdef QUOTA
 #ifdef QUOTA
-       (void) chkdq(ip, -cnt, 0);
-#endif
-}
-
-#ifdef QUOTA
-long
-#endif
-tloop(ip, bn, indflg)
-       register struct inode *ip;
-       daddr_t bn;
-       int indflg;
-{
-       register i;
-       register struct buf *bp;
-       register daddr_t *bap;
-       register struct fs *fs;
-       daddr_t nb;
-#ifdef QUOTA
-       register long cnt = 0;
-#endif
-
-       bp = NULL;
-       fs = ip->i_fs;
-       for (i = NINDIR(fs) - 1; i >= 0; i--) {
-               if (bp == NULL) {
-                       bp = bread(ip->i_dev, fsbtodb(fs, bn),
-                               (int)fs->fs_bsize);
-                       if (bp->b_flags & B_ERROR) {
-                               brelse(bp);
-                               return;
-                       }
-                       bap = bp->b_un.b_daddr;
-               }
-               nb = bap[i];
-               if (nb == (daddr_t)0)
-                       continue;
-               if (indflg) {
-#ifdef QUOTA
-                       cnt +=
-#endif
-                               tloop(ip, nb, 0);
-               } else {
-                       fre(ip, nb, (int)fs->fs_bsize);
-#ifdef QUOTA
-                       cnt += fs->fs_bsize / DEV_BSIZE;
-#endif
-               }
+               if (error = getinoquota(ip))
+                       return (error);
+#endif 
+               if (error = bread(vp, lbn, fs->lfs_bsize, NOCRED, &bp))
+                       return (error);
+               ip->i_size = length;
+               size = blksize(fs);
+               (void)vnode_pager_uncache(vp);
+               bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
+               allocbuf(bp, size);
+               LFS_UBWRITE(bp);
        }
        }
-       if (bp != NULL)
-               brelse(bp);
-       fre(ip, bn, (int)fs->fs_bsize);
-#ifdef QUOTA
-       cnt += fs->fs_bsize / DEV_BSIZE;
-       return(cnt);
-#endif
-}
-
-/*
- * remove any inodes in the inode cache belonging to dev
- *
- * There should not be any active ones, return error if any are found
- * (nb: this is a user error, not a system err)
- *
- * Also, count the references to dev by block devices - this really
- * has nothing to do with the object of the procedure, but as we have
- * to scan the inode table here anyway, we might as well get the
- * extra benefit.
- *
- * this is called from sumount()/sys3.c when dev is being unmounted
- */
-#ifdef QUOTA
-iflush(dev, iq)
-       dev_t dev;
-       struct inode *iq;
-#else
-iflush(dev)
-       dev_t dev;
-#endif
-{
-       register struct inode *ip;
-       register open = 0;
+       /*
+        * Modify sup->su_nbyte counters for each deleted block; keep track
+        * of number of blocks removed for ip->i_blocks.
+        */
+       blocksreleased = 0;
+       num = 0;
+       lastseg = -1;
 
 
-       for (ip = inode; ip < inodeNINODE; ip++) {
-#ifdef QUOTA
-               if (ip != iq && ip->i_dev == dev)
-#else
-               if (ip->i_dev == dev)
+       for (lbn = olastblock; lbn >= lastblock;) {
+               lfs_bmaparray(vp, lbn, &daddr, a, &depth);
+               if (lbn == olastblock)
+                       for (i = NIADDR + 2; i--;)
+                               a_end[i] = a[i];
+               switch (depth) {
+               case 0:                         /* Direct block. */
+                       daddr = ip->i_db[lbn];
+                       SEGDEC;
+                       ip->i_db[lbn] = 0;
+                       --lbn;
+                       break;
+#ifdef DIAGNOSTIC
+               case 1:                         /* An indirect block. */
+                       panic("lfs_truncate: lfs_bmaparray returned depth 1");
+                       /* NOTREACHED */
 #endif
 #endif
-                       if (ip->i_count)
-                               return(-1);
-                       else {
-                               remque(ip);
-                               ip->i_forw = ip;
-                               ip->i_back = ip;
+               default:                        /* Chain of indirect blocks. */
+                       ap = a + --depth;
+                       if (ap->in_off > 0 && lbn != lastblock) {
+                               lbn -= ap->in_off < lbn - lastblock ?
+                                   ap->in_off : lbn - lastblock;
+                               break;
+                       }
+                       for (; depth && (ap->in_off == 0 || lbn == lastblock);
+                           --ap, --depth) {
                                /*
                                /*
-                                * as i_count == 0, the inode was on the free
-                                * list already, just leave it there, it will
-                                * fall off the bottom eventually. We could
-                                * perhaps move it to the head of the free
-                                * list, but as umounts are done so
-                                * infrequently, we would gain very little,
-                                * while making the code bigger.
+                                * XXX
+                                * The indirect block may not yet exist, so
+                                * bread will create one just so we can free
+                                * it.
                                 */
                                 */
-#ifdef QUOTA
-                               dqrele(ip->i_dquot);
-                               ip->i_dquot = NODQUOT;
-#endif
+                               if (bread(vp,
+                                   ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
+                                       panic("lfs_truncate: bread bno %d",
+                                           ap->in_lbn);
+                               daddrp = bp->b_un.b_daddr + ap->in_off;
+                               for (i = ap->in_off;
+                                   i++ <= a_end[depth].in_off;) {
+                                       daddr = *daddrp++;
+                                       SEGDEC;
+                               }
+                               a_end[depth].in_off=NINDIR(fs)-1;
+                               if (ap->in_off > 0 && lbn == lastblock) {
+                                       bzero(bp->b_un.b_daddr + ap->in_off,
+                                           fs->lfs_bsize - 
+                                           ap->in_off * sizeof(daddr_t));
+                                       LFS_UBWRITE(bp);
+                               } else 
+                                       brelse (bp);
                        }
                        }
-               else if (ip->i_count && (ip->i_mode&IFMT)==IFBLK &&
-                   ip->i_rdev == dev)
-                       open++;
+                       if (a[1].in_off == 0) {
+                               off = a[0].in_off;
+                               daddr = ip->i_ib[off];
+                               SEGDEC;
+                               ip->i_ib[off] = 0;
+                       }
+                       if (lbn == lastblock)
+                               --lbn;
+                       else {
+                               lbn -= NINDIR(fs);
+                               if (lbn < lastblock)
+                                       lbn = lastblock;
+                       }
+               }
        }
        }
-       return (open);
-}
-
-/*
- * Lock an inode. If its already locked, set the WANT bit and sleep.
- */
-ilock(ip)
-       register struct inode *ip;
-{
-
-       ILOCK(ip);
-}
-
-/*
- * Unlock an inode.  If WANT bit is on, wakeup.
- */
-iunlock(ip)
-       register struct inode *ip;
-{
-
-       IUNLOCK(ip);
+       UPDATE_SEGUSE;
+       ip->i_blocks -= blocksreleased;
+       /* 
+        * XXX
+        * Currently, we don't know when we allocate an indirect block, so
+        * ip->i_blocks isn't getting incremented appropriately.  As a result,
+        * when we delete any indirect blocks, we get a bad number here.
+        */
+       if (ip->i_blocks < 0)
+               ip->i_blocks = 0;
+       ip->i_flag |= ICHG|IUPD;
+       (void)vinvalbuf(vp, length > 0); 
+       error = lfs_update(vp, &time, &time, MNT_WAIT);
+       return (0);
 }
 }