Change to includes. no more ../h
[unix-history] / usr / src / sys / ufs / ffs / ffs_inode.c
index c7adeef..2620c48 100644 (file)
@@ -1,24 +1,24 @@
-/*     ffs_inode.c     4.29    82/10/19        */
-
-#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"
+/*     ffs_inode.c     6.11    84/08/29        */
+
+#include "param.h"
+#include "systm.h"
+#include "mount.h"
+#include "dir.h"
+#include "user.h"
+#include "inode.h"
+#include "fs.h"
+#include "conf.h"
+#include "buf.h"
 #ifdef QUOTA
 #ifdef QUOTA
-#include "../h/quota.h"
+#include "quota.h"
 #endif
 #endif
-#include "../h/kernel.h"
+#include "kernel.h"
 
 
-#define        INOHSZ  63
+#define        INOHSZ  512
 #if    ((INOHSZ&(INOHSZ-1)) == 0)
 #define        INOHASH(dev,ino)        (((dev)+(ino))&(INOHSZ-1))
 #else
 #if    ((INOHSZ&(INOHSZ-1)) == 0)
 #define        INOHASH(dev,ino)        (((dev)+(ino))&(INOHSZ-1))
 #else
-#define        INOHASH(dev,ino)        (((dev)+(ino))%INOHSZ)
+#define        INOHASH(dev,ino)        (((unsigned)((dev)+(ino)))%INOHSZ)
 #endif
 
 union ihead {                          /* inode LRU cache, Chris Maltby */
 #endif
 
 union ihead {                          /* inode LRU cache, Chris Maltby */
@@ -108,12 +108,18 @@ iget(dev, fs, ino)
        register struct dinode *dp;
        register struct inode *iq;
 
        register struct dinode *dp;
        register struct inode *iq;
 
+
 loop:
 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) {
        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) {
+                       /*
+                        * Following is essentially an inline expanded
+                        * copy of igrab(), expanded inline for speed,
+                        * and so that the test for a mounted on inode
+                        * can be deferred until after we are sure that
+                        * the inode isn't busy.
+                        */
                        if ((ip->i_flag&ILOCKED) != 0) {
                                ip->i_flag |= IWANT;
                                sleep((caddr_t)ip, PINOD);
                        if ((ip->i_flag&ILOCKED) != 0) {
                                ip->i_flag |= IWANT;
                                sleep((caddr_t)ip, PINOD);
@@ -148,6 +154,8 @@ loop:
                u.u_error = ENFILE;
                return(NULL);
        }
                u.u_error = ENFILE;
                return(NULL);
        }
+       if (ip->i_count)
+               panic("free inode isn't");
        if (iq = ip->i_freef)
                iq->i_freeb = &ifreeh;
        ifreeh = iq;
        if (iq = ip->i_freef)
                iq->i_freeb = &ifreeh;
        ifreeh = iq;
@@ -162,15 +170,16 @@ loop:
         */
        remque(ip);
        insque(ip, ih);
         */
        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_dev = dev;
        ip->i_fs = fs;
        ip->i_number = ino;
+       cacheinval(ip);
        ip->i_flag = ILOCKED;
        ip->i_count++;
        ip->i_lastr = 0;
        ip->i_flag = ILOCKED;
        ip->i_count++;
        ip->i_lastr = 0;
+#ifdef QUOTA
+       dqrele(ip->i_dquot);
+#endif
        bp = bread(dev, fsbtodb(fs, itod(fs, ino)), (int)fs->fs_bsize);
        /*
         * Check I/O errors
        bp = bread(dev, fsbtodb(fs, itod(fs, ino)), (int)fs->fs_bsize);
        /*
         * Check I/O errors
@@ -211,6 +220,36 @@ loop:
        return (ip);
 }
 
        return (ip);
 }
 
+/*
+ * Convert a pointer to an inode into a reference to an inode.
+ *
+ * This is basically the internal piece of iget (after the
+ * inode pointer is located) but without the test for mounted
+ * filesystems.  It is caller's responsibility to check that
+ * the inode pointer is valid.
+ */
+igrab(ip)
+       register struct inode *ip;
+{
+       while ((ip->i_flag&ILOCKED) != 0) {
+               ip->i_flag |= IWANT;
+               sleep((caddr_t)ip, PINOD);
+       }
+       if (ip->i_count == 0) {         /* ino on free list */
+               register struct inode *iq;
+
+               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;
+}
+
 /*
  * Decrement reference count of
  * an inode structure.
 /*
  * Decrement reference count of
  * an inode structure.
@@ -224,7 +263,7 @@ iput(ip)
 
        if ((ip->i_flag & ILOCKED) == 0)
                panic("iput");
 
        if ((ip->i_flag & ILOCKED) == 0)
                panic("iput");
-       iunlock(ip);
+       IUNLOCK(ip);
        irele(ip);
 }
 
        irele(ip);
 }
 
@@ -236,20 +275,20 @@ irele(ip)
        if (ip->i_count == 1) {
                ip->i_flag |= ILOCKED;
                if (ip->i_nlink <= 0) {
        if (ip->i_count == 1) {
                ip->i_flag |= ILOCKED;
                if (ip->i_nlink <= 0) {
-                       itrunc(ip, 0);
+                       itrunc(ip, (u_long)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
                        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);
+                       (void) chkiq(ip->i_dev, ip, ip->i_uid, 0);
                        dqrele(ip->i_dquot);
                        ip->i_dquot = NODQUOT;
 #endif
                }
                IUPDAT(ip, &time, &time, 0);
                        dqrele(ip->i_dquot);
                        ip->i_dquot = NODQUOT;
 #endif
                }
                IUPDAT(ip, &time, &time, 0);
-               iunlock(ip);
+               IUNLOCK(ip);
                ip->i_flag = 0;
                /*
                 * Put the inode on the end of the free list.
                ip->i_flag = 0;
                /*
                 * Put the inode on the end of the free list.
@@ -270,7 +309,8 @@ irele(ip)
                }
                ip->i_freef = NULL;
                ifreet = &ip->i_freef;
                }
                ip->i_freef = NULL;
                ifreet = &ip->i_freef;
-       }
+       } else if (!(ip->i_flag & ILOCKED))
+               ITIMES(ip, &time, &time);
        ip->i_count--;
 }
 
        ip->i_count--;
 }
 
@@ -292,7 +332,7 @@ iupdat(ip, ta, tm, waitfor)
        register struct fs *fp;
 
        fp = ip->i_fs;
        register struct fs *fp;
 
        fp = ip->i_fs;
-       if ((ip->i_flag & (IUPD|IACC|ICHG)) != 0) {
+       if ((ip->i_flag & (IUPD|IACC|ICHG|IMOD)) != 0) {
                if (fp->fs_ronly)
                        return;
                bp = bread(ip->i_dev, fsbtodb(fp, itod(fp, ip->i_number)),
                if (fp->fs_ronly)
                        return;
                bp = bread(ip->i_dev, fsbtodb(fp, itod(fp, ip->i_number)),
@@ -307,7 +347,7 @@ iupdat(ip, ta, tm, waitfor)
                        ip->i_mtime = tm->tv_sec;
                if (ip->i_flag&ICHG)
                        ip->i_ctime = time.tv_sec;
                        ip->i_mtime = tm->tv_sec;
                if (ip->i_flag&ICHG)
                        ip->i_ctime = time.tv_sec;
-               ip->i_flag &= ~(IUPD|IACC|ICHG);
+               ip->i_flag &= ~(IUPD|IACC|ICHG|IMOD);
                dp = bp->b_un.b_dino + itoo(fp, ip->i_number);
                dp->di_ic = ip->i_ic;
                if (waitfor)
                dp = bp->b_un.b_dino + itoo(fp, ip->i_number);
                dp->di_ic = ip->i_ic;
                if (waitfor)
@@ -317,153 +357,231 @@ iupdat(ip, ta, tm, waitfor)
        }
 }
 
        }
 }
 
+#define        SINGLE  0       /* index of single indirect block */
+#define        DOUBLE  1       /* index of double indirect block */
+#define        TRIPLE  2       /* index of triple indirect block */
 /*
  * 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.  Free affected disk
  * blocks -- the blocks of the file
  * are removed in reverse order.
+ *
+ * NB: triple indirect blocks are untested.
  */
  */
-itrunc(ip, length)
-       register struct inode *ip;
-       register int length;
+itrunc(oip, length)
+       struct inode *oip;
+       u_long length;
 {
        register i;
 {
        register i;
-       daddr_t bn;
-       struct inode itmp;
+       register daddr_t lastblock;
+       daddr_t bn, lastiblock[NIADDR];
        register struct fs *fs;
        register struct fs *fs;
-#ifdef QUOTA
-       register long cnt = 0;
-       long tloop();
-#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;
-
-       /*
-        * 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);
+       register struct inode *ip;
+       struct inode tip;
+       long blocksreleased = 0, nblocks;
+       long indirtrunc();
+       int level;
 
 
+       if (oip->i_size <= length) {
+               oip->i_flag |= ICHG|IUPD;
+               iupdat(oip, &time, &time, 1);
+               return;
+       }
        /*
        /*
-        * Now return blocks to free list... if machine
-        * crashes, they will be harmless MISSING blocks.
+        * Calculate index into inode's block list of
+        * last direct and indirect blocks (if any)
+        * which we want to keep.  Lastblock is -1 when
+        * the file is truncated to 0.
         */
         */
-       fs = ip->i_fs;
+       fs = oip->i_fs;
+       lastblock = lblkno(fs, length + fs->fs_bsize - 1) - 1;
+       lastiblock[SINGLE] = lastblock - NDADDR;
+       lastiblock[DOUBLE] = lastiblock[SINGLE] - NINDIR(fs);
+       lastiblock[TRIPLE] = lastiblock[DOUBLE] - NINDIR(fs) * NINDIR(fs);
+       nblocks = btodb(fs->fs_bsize);
        /*
        /*
-        * release double indirect block first
+        * Update size of file and block pointers
+        * on disk before we start freeing blocks.
+        * If we crash before free'ing blocks below,
+        * the blocks will be returned to the free list.
+        * lastiblock values are also normalized to -1
+        * for calls to indirtrunc below.
+        * (? fsck doesn't check validity of pointers in indirect blocks)
         */
         */
-       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);
-       }
+       tip = *oip;
+       for (level = TRIPLE; level >= SINGLE; level--)
+               if (lastiblock[level] < 0) {
+                       oip->i_ib[level] = 0;
+                       lastiblock[level] = -1;
+               }
+       for (i = NDADDR - 1; i > lastblock; i--)
+               oip->i_db[i] = 0;
+       oip->i_size = length;
+       oip->i_flag |= ICHG|IUPD;
+       iupdat(oip, &time, &time, 1);
+       ip = &tip;
+
        /*
        /*
-        * release single indirect blocks second
+        * Indirect blocks first.
         */
         */
-       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);
+       for (level = TRIPLE; level >= SINGLE; level--) {
+               bn = ip->i_ib[level];
+               if (bn != 0) {
+                       blocksreleased +=
+                           indirtrunc(ip, bn, lastiblock[level], level);
+                       if (lastiblock[level] < 0) {
+                               ip->i_ib[level] = 0;
+                               free(ip, bn, (off_t)fs->fs_bsize);
+                               blocksreleased += nblocks;
+                       }
                }
                }
+               if (lastiblock[level] >= 0)
+                       goto done;
        }
        }
+
        /*
        /*
-        * finally release direct blocks
+        * All whole direct blocks or frags.
         */
         */
-       for (i = NDADDR - 1; i>=0; i--) {
+       for (i = NDADDR - 1; i > lastblock; i--) {
+               register int size;
+
                bn = ip->i_db[i];
                bn = ip->i_db[i];
-               if (bn == (daddr_t)0)
+               if (bn == 0)
                        continue;
                        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_db[i] = 0;
+               size = (off_t)blksize(fs, ip, i);
+               free(ip, bn, size);
+               blocksreleased += btodb(size);
        }
        }
-       ip->i_size = 0;
+       if (lastblock < 0)
+               goto done;
+
        /*
        /*
-        * Inode was written and flags updated above.
-        * No need to modify flags here.
+        * Finally, look for a change in size of the
+        * last direct block; release any frags.
         */
         */
+       bn = ip->i_db[lastblock];
+       if (bn != 0) {
+               int oldspace, newspace;
+
+               /*
+                * Calculate amount of space we're giving
+                * back as old block size minus new block size.
+                */
+               oldspace = blksize(fs, ip, lastblock);
+               ip->i_size = length;
+               newspace = blksize(fs, ip, lastblock);
+               if (newspace == 0)
+                       panic("itrunc: newspace");
+               if (oldspace - newspace > 0) {
+                       /*
+                        * Block number of space to be free'd is
+                        * the old block # plus the number of frags
+                        * required for the storage we're keeping.
+                        */
+                       bn += numfrags(fs, newspace);
+                       free(ip, bn, oldspace - newspace);
+                       blocksreleased += btodb(oldspace - newspace);
+               }
+       }
+done:
+/* BEGIN PARANOIA */
+       for (level = SINGLE; level <= TRIPLE; level++)
+               if (ip->i_ib[level] != oip->i_ib[level])
+                       panic("itrunc1");
+       for (i = 0; i < NDADDR; i++)
+               if (ip->i_db[i] != oip->i_db[i])
+                       panic("itrunc2");
+/* END PARANOIA */
+       oip->i_blocks -= blocksreleased;
+       if (oip->i_blocks < 0)                  /* sanity */
+               oip->i_blocks = 0;
+       oip->i_flag |= ICHG;
 #ifdef QUOTA
 #ifdef QUOTA
-       (void) chkdq(ip, -cnt, 0);
+       (void) chkdq(oip, -blocksreleased, 0);
 #endif
 }
 
 #endif
 }
 
-#ifdef QUOTA
+/*
+ * Release blocks associated with the inode ip and
+ * stored in the indirect block bn.  Blocks are free'd
+ * in LIFO order up to (but not including) lastbn.  If
+ * level is greater than SINGLE, the block is an indirect
+ * block and recursive calls to indirtrunc must be used to
+ * cleanse other indirect blocks.
+ *
+ * NB: triple indirect blocks are untested.
+ */
 long
 long
-#endif
-tloop(ip, bn, indflg)
+indirtrunc(ip, bn, lastbn, level)
        register struct inode *ip;
        register struct inode *ip;
-       daddr_t bn;
-       int indflg;
+       daddr_t bn, lastbn;
+       int level;
 {
 {
-       register i;
-       register struct buf *bp;
+       register int i;
+       struct buf *bp, *copy;
        register daddr_t *bap;
        register daddr_t *bap;
-       register struct fs *fs;
-       daddr_t nb;
-#ifdef QUOTA
-       register long cnt = 0;
-#endif
+       register struct fs *fs = ip->i_fs;
+       daddr_t nb, last;
+       long factor;
+       int blocksreleased = 0, nblocks;
 
 
-       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;
-               }
+       /*
+        * Calculate index in current block of last
+        * block to be kept.  -1 indicates the entire
+        * block so we need not calculate the index.
+        */
+       factor = 1;
+       for (i = SINGLE; i < level; i++)
+               factor *= NINDIR(fs);
+       last = lastbn;
+       if (lastbn > 0)
+               last /= factor;
+       nblocks = btodb(fs->fs_bsize);
+       /*
+        * Get buffer of block pointers, zero those 
+        * entries corresponding to blocks to be free'd,
+        * and update on disk copy first.
+        */
+       copy = geteblk((int)fs->fs_bsize);
+       bp = bread(ip->i_dev, fsbtodb(fs, bn), (int)fs->fs_bsize);
+       if (bp->b_flags&B_ERROR) {
+               brelse(copy);
+               brelse(bp);
+               return (0);
+       }
+       bap = bp->b_un.b_daddr;
+       bcopy((caddr_t)bap, (caddr_t)copy->b_un.b_daddr, (u_int)fs->fs_bsize);
+       bzero((caddr_t)&bap[last + 1],
+         (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
+       bwrite(bp);
+       bp = copy, bap = bp->b_un.b_daddr;
+
+       /*
+        * Recursively free totally unused blocks.
+        */
+       for (i = NINDIR(fs) - 1; i > last; i--) {
                nb = bap[i];
                nb = bap[i];
-               if (nb == (daddr_t)0)
+               if (nb == 0)
                        continue;
                        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 (level > SINGLE)
+                       blocksreleased +=
+                           indirtrunc(ip, nb, (daddr_t)-1, level - 1);
+               free(ip, nb, (int)fs->fs_bsize);
+               blocksreleased += nblocks;
        }
        }
-       if (bp != NULL)
-               brelse(bp);
-       fre(ip, bn, (int)fs->fs_bsize);
-#ifdef QUOTA
-       cnt += fs->fs_bsize / DEV_BSIZE;
-       return(cnt);
-#endif
+
+       /*
+        * Recursively free last partial block.
+        */
+       if (level > SINGLE && lastbn >= 0) {
+               last = lastbn % factor;
+               nb = bap[i];
+               if (nb != 0)
+                       blocksreleased += indirtrunc(ip, nb, last, level - 1);
+       }
+       brelse(bp);
+       return (blocksreleased);
 }
 
 /*
 }
 
 /*