have to update the correct inode (from mike@cs.utah.edu)
[unix-history] / usr / src / sys / ufs / ufs / ufs_inode.c
index 0f60b3c..ae130f0 100644 (file)
@@ -14,7 +14,7 @@
  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  *
- *     @(#)ufs_inode.c 7.17 (Berkeley) %G%
+ *     @(#)ufs_inode.c 7.24 (Berkeley) %G%
  */
 
 #include "param.h"
  */
 
 #include "param.h"
@@ -46,6 +46,8 @@ union ihead {
        struct inode *ih_chain[2];
 } ihead[INOHSZ];
 
        struct inode *ih_chain[2];
 } ihead[INOHSZ];
 
+int prtactive; /* 1 => print out reclaim of active vnodes */
+
 /*
  * Initialize hash links for inodes.
  */
 /*
  * Initialize hash links for inodes.
  */
@@ -119,7 +121,6 @@ loop:
        ip->i_devvp = 0;
        ip->i_lastr = 0;
        ip->i_mode = 0;
        ip->i_devvp = 0;
        ip->i_lastr = 0;
        ip->i_mode = 0;
-       ip->i_flags = 0;
 #ifdef QUOTA
        ip->i_dquot = NODQUOT;
 #endif
 #ifdef QUOTA
        ip->i_dquot = NODQUOT;
 #endif
@@ -156,9 +157,8 @@ loop:
        vp = ITOV(ip);
        vp->v_type = IFTOVT(ip->i_mode);
        if (vp->v_type == VCHR || vp->v_type == VBLK) {
        vp = ITOV(ip);
        vp->v_type = IFTOVT(ip->i_mode);
        if (vp->v_type == VCHR || vp->v_type == VBLK) {
-               vp->v_rdev = ip->i_rdev;
                vp->v_op = &spec_inodeops;
                vp->v_op = &spec_inodeops;
-               if (nvp = checkalias(vp, mntp)) {
+               if (nvp = checkalias(vp, ip->i_rdev, mntp)) {
                        /*
                         * Reinitialize aliased inode.
                         */
                        /*
                         * Reinitialize aliased inode.
                         */
@@ -166,7 +166,7 @@ loop:
                        iq = VTOI(vp);
                        iq->i_vnode = vp;
                        iq->i_lastr = 0;
                        iq = VTOI(vp);
                        iq->i_vnode = vp;
                        iq->i_lastr = 0;
-                       iq->i_flags = 0;
+                       iq->i_flag = 0;
                        ILOCK(iq);
                        iq->i_din = ip->i_din;
                        iq->i_dev = dev;
                        ILOCK(iq);
                        iq->i_din = ip->i_din;
                        iq->i_dev = dev;
@@ -230,19 +230,19 @@ ufs_inactive(vp)
        register struct inode *ip = VTOI(vp);
        int mode, error = 0;
 
        register struct inode *ip = VTOI(vp);
        int mode, error = 0;
 
-       if (vp->v_count != 0)
-               printf("ufs_inactive: pushing active ino %d dev 0x%x\n",
-                       ip->i_number, ip->i_dev);
+       if (prtactive && vp->v_count != 0)
+               vprint("ufs_inactive: pushing active", vp);
        /*
         * Get rid of inodes related to stale file handles.
         */
        if (ip->i_mode == 0) {
        /*
         * Get rid of inodes related to stale file handles.
         */
        if (ip->i_mode == 0) {
-               vgone(vp);
+               if ((vp->v_flag & VXLOCK) == 0)
+                       vgone(vp);
                return (0);
        }
        ILOCK(ip);
        if (ip->i_nlink <= 0 && (vp->v_mount->m_flag & M_RDONLY) == 0) {
                return (0);
        }
        ILOCK(ip);
        if (ip->i_nlink <= 0 && (vp->v_mount->m_flag & M_RDONLY) == 0) {
-               error = itrunc(ip, (u_long)0);
+               error = itrunc(ip, (u_long)0, 0);
                mode = ip->i_mode;
                ip->i_mode = 0;
                ip->i_rdev = 0;
                mode = ip->i_mode;
                ip->i_mode = 0;
                ip->i_rdev = 0;
@@ -255,14 +255,20 @@ ufs_inactive(vp)
 #endif
        }
        IUPDAT(ip, &time, &time, 0);
 #endif
        }
        IUPDAT(ip, &time, &time, 0);
-       IUNLOCK(ip);
-       ip->i_flag = 0;
        /*
         * If we are done with the inode, reclaim it
         * so that it can be reused immediately.
         */
        /*
         * If we are done with the inode, reclaim it
         * so that it can be reused immediately.
         */
-       if (vp->v_count == 0 && ip->i_mode == 0)
-               vgone(vp);
+       if (vp->v_count == 0 && ip->i_mode == 0) {
+               vinvalbuf(vp, 0);
+               IUNLOCK(ip);
+               ip->i_flag = 0;
+               if ((vp->v_flag & VXLOCK) == 0)
+                       vgone(vp);
+               return (error);
+       }
+       IUNLOCK(ip);
+       ip->i_flag = 0;
        return (error);
 }
 
        return (error);
 }
 
@@ -274,9 +280,8 @@ ufs_reclaim(vp)
 {
        register struct inode *ip = VTOI(vp);
 
 {
        register struct inode *ip = VTOI(vp);
 
-       if (vp->v_count != 0)
-               printf("ufs_reclaim: pushing active ino %d dev 0x%x\n",
-                       ip->i_number, ip->i_dev);
+       if (prtactive && vp->v_count != 0)
+               vprint("ufs_reclaim: pushing active", vp);
        /*
         * Remove the inode from its hash chain.
         */
        /*
         * Remove the inode from its hash chain.
         */
@@ -352,9 +357,10 @@ iupdat(ip, ta, tm, waitfor)
  *
  * NB: triple indirect blocks are untested.
  */
  *
  * NB: triple indirect blocks are untested.
  */
-itrunc(oip, length)
+itrunc(oip, length, flags)
        register struct inode *oip;
        u_long length;
        register struct inode *oip;
        u_long length;
+       int flags;
 {
        register daddr_t lastblock;
        daddr_t bn, lbn, lastiblock[NIADDR];
 {
        register daddr_t lastblock;
        daddr_t bn, lbn, lastiblock[NIADDR];
@@ -364,7 +370,7 @@ itrunc(oip, length)
        int offset, osize, size, level;
        long count, nblocks, blocksreleased = 0;
        register int i;
        int offset, osize, size, level;
        long count, nblocks, blocksreleased = 0;
        register int i;
-       int error, allerror = 0;
+       int aflags, error, allerror;
        struct inode tip;
 
        if (oip->i_size <= length) {
        struct inode tip;
 
        if (oip->i_size <= length) {
@@ -397,23 +403,22 @@ itrunc(oip, length)
                oip->i_size = length;
        } else {
                lbn = lblkno(fs, length);
                oip->i_size = length;
        } else {
                lbn = lblkno(fs, length);
-               error = balloc(oip, lbn, offset, &bn, B_CLRBUF);
-               if (error)
+               aflags = B_CLRBUF;
+               if (flags & IO_SYNC)
+                       aflags |= B_SYNC;
+               if (error = balloc(oip, lbn, offset, &bp, aflags))
                        return (error);
                        return (error);
-               if ((long)bn < 0)
-                       panic("itrunc: hole");
                oip->i_size = length;
                size = blksize(fs, oip, lbn);
                oip->i_size = length;
                size = blksize(fs, oip, lbn);
+               bn = bp->b_blkno;
                count = howmany(size, CLBYTES);
                        munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
                count = howmany(size, CLBYTES);
                        munhash(oip->i_devvp, bn + i * CLBYTES / DEV_BSIZE);
-               error = bread(oip->i_devvp, bn, size, NOCRED, &bp);
-               if (error) {
-                       oip->i_size = osize;
-                       brelse(bp);
-                       return (error);
-               }
                bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
                bzero(bp->b_un.b_addr + offset, (unsigned)(size - offset));
-               bdwrite(bp);
+               brealloc(bp, size);
+               if (flags & IO_SYNC)
+                       bwrite(bp);
+               else
+                       bdwrite(bp);
        }
        /*
         * Update file and block pointers
        }
        /*
         * Update file and block pointers
@@ -433,7 +438,8 @@ itrunc(oip, length)
        for (i = NDADDR - 1; i > lastblock; i--)
                oip->i_db[i] = 0;
        oip->i_flag |= ICHG|IUPD;
        for (i = NDADDR - 1; i > lastblock; i--)
                oip->i_db[i] = 0;
        oip->i_flag |= ICHG|IUPD;
-       allerror = syncip(oip);
+       vinvalbuf(ITOV(oip), (length > 0));
+       allerror = iupdat(oip, &time, &time, MNT_WAIT);
 
        /*
         * Indirect blocks first.
 
        /*
         * Indirect blocks first.
@@ -574,11 +580,15 @@ indirtrunc(ip, bn, lastbn, level, countp)
                *countp = 0;
                return (error);
        }
                *countp = 0;
                return (error);
        }
+       if ((bp->b_flags & B_CACHE) == 0)
+               reassignbuf(bp, ITOV(ip));
        bap = bp->b_un.b_daddr;
        MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
        bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
        bzero((caddr_t)&bap[last + 1],
          (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
        bap = bp->b_un.b_daddr;
        MALLOC(copy, daddr_t *, fs->fs_bsize, M_TEMP, M_WAITOK);
        bcopy((caddr_t)bap, (caddr_t)copy, (u_int)fs->fs_bsize);
        bzero((caddr_t)&bap[last + 1],
          (u_int)(NINDIR(fs) - (last + 1)) * sizeof (daddr_t));
+       if (last == -1)
+               bp->b_flags |= B_INVAL;
        error = bwrite(bp);
        if (error)
                allerror = error;
        error = bwrite(bp);
        if (error)
                allerror = error;
@@ -620,54 +630,6 @@ indirtrunc(ip, bn, lastbn, level, countp)
        return (allerror);
 }
 
        return (allerror);
 }
 
-/*
- * 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).
- */
-int busyprt = 0;       /* patch to print out busy inodes */
-
-#ifdef QUOTA
-iflush(mp, iq)
-       struct mount *mp;
-       struct inode *iq;
-#else
-iflush(mp)
-       struct mount *mp;
-#endif
-{
-       register struct vnode *vp, *nvp;
-       register struct inode *ip;
-       int busy = 0;
-
-       for (vp = mp->m_mounth; vp; vp = nvp) {
-               nvp = vp->v_mountf;
-               ip = VTOI(vp);
-#ifdef QUOTA
-               if (ip == iq)
-                       continue;
-#endif
-               if (vp->v_count) {
-                       busy++;
-                       if (!busyprt)
-                               continue;
-                       printf("%s %d on dev 0x%x count %d type %d\n",
-                           "iflush: busy inode ", ip->i_number, ip->i_dev,
-                           vp->v_count, vp->v_type);
-                       continue;
-               }
-               /*
-                * With v_count == 0, all we need to do is clear out the
-                * vnode data structures and we are done.
-                */
-               vgone(vp);
-       }
-       if (busy)
-               return (EBUSY);
-       return (0);
-}
-
 /*
  * Lock an inode. If its already locked, set the WANT bit and sleep.
  */
 /*
  * Lock an inode. If its already locked, set the WANT bit and sleep.
  */
@@ -690,8 +652,7 @@ iunlock(ip)
 {
 
        if ((ip->i_flag & ILOCKED) == 0)
 {
 
        if ((ip->i_flag & ILOCKED) == 0)
-               printf("unlocking unlocked inode %d on dev 0x%x\n",
-                       ip->i_number, ip->i_dev);
+               vprint("iunlock: unlocked inode", ITOV(ip));
        ip->i_flag &= ~ILOCKED;
        if (ip->i_flag&IWANT) {
                ip->i_flag &= ~IWANT;
        ip->i_flag &= ~ILOCKED;
        if (ip->i_flag&IWANT) {
                ip->i_flag &= ~IWANT;