Make sure you don't use buffers after you brelse/bwrite them. Also,
[unix-history] / usr / src / sys / ufs / lfs / lfs_segment.c
index fed7254..ad524b2 100644 (file)
@@ -4,7 +4,7 @@
  *
  * %sccs.include.redist.c%
  *
  *
  * %sccs.include.redist.c%
  *
- *     @(#)lfs_segment.c       7.19 (Berkeley) %G%
+ *     @(#)lfs_segment.c       7.32 (Berkeley) %G%
  */
 
 #include <sys/param.h>
  */
 
 #include <sys/param.h>
 #include <sys/proc.h>
 #include <sys/conf.h>
 #include <sys/vnode.h>
 #include <sys/proc.h>
 #include <sys/conf.h>
 #include <sys/vnode.h>
-#include <sys/specdev.h>
-#include <sys/fifo.h>
 #include <sys/malloc.h>
 #include <sys/mount.h>
 
 #include <sys/malloc.h>
 #include <sys/mount.h>
 
+#include <miscfs/specfs/specdev.h>
+#include <miscfs/fifofs/fifo.h>
+
 #include <ufs/ufs/quota.h>
 #include <ufs/ufs/inode.h>
 #include <ufs/ufs/dir.h>
 #include <ufs/ufs/quota.h>
 #include <ufs/ufs/inode.h>
 #include <ufs/ufs/dir.h>
 #include <ufs/lfs/lfs.h>
 #include <ufs/lfs/lfs_extern.h>
 
 #include <ufs/lfs/lfs.h>
 #include <ufs/lfs/lfs_extern.h>
 
-/* In-memory description of a segment about to be written. */
-struct segment {
-       struct buf      **bpp;          /* pointer to buffer array */
-       struct buf      **cbpp;         /* pointer to next available bp */
-       struct buf      *ibp;           /* buffer pointer to inode page */
-       struct finfo    *fip;           /* current fileinfo pointer */
-       void    *segsum;                /* segment summary info */
-       u_long  ninodes;                /* number of inodes in this segment */
-       u_long  seg_bytes_left;         /* bytes left in segment */
-       u_long  sum_bytes_left;         /* bytes left in summary block */
-       u_long  seg_number;             /* number of this segment */
-#define        SEGM_CKP        0x01            /* doing a checkpoint */
-       u_long  seg_flags;              /* run-time flags for this segment */
-};
-
+#define MAX_ACTIVE     10
 /*
  * Determine if it's OK to start a partial in this segment, or if we need
  * to go on to a new segment.
 /*
  * Determine if it's OK to start a partial in this segment, or if we need
  * to go on to a new segment.
@@ -57,22 +44,23 @@ struct segment {
 void    lfs_callback __P((struct buf *));
 void    lfs_gather __P((struct lfs *, struct segment *,
             struct vnode *, int (*) __P((struct lfs *, struct buf *))));
 void    lfs_callback __P((struct buf *));
 void    lfs_gather __P((struct lfs *, struct segment *,
             struct vnode *, int (*) __P((struct lfs *, struct buf *))));
+int     lfs_gatherblock __P((struct segment *, struct buf *, int *));
 void    lfs_initseg __P((struct lfs *, struct segment *));
 void    lfs_iset __P((struct inode *, daddr_t, time_t));
 int     lfs_match_data __P((struct lfs *, struct buf *));
 int     lfs_match_dindir __P((struct lfs *, struct buf *));
 int     lfs_match_indir __P((struct lfs *, struct buf *));
 int     lfs_match_tindir __P((struct lfs *, struct buf *));
 void    lfs_initseg __P((struct lfs *, struct segment *));
 void    lfs_iset __P((struct inode *, daddr_t, time_t));
 int     lfs_match_data __P((struct lfs *, struct buf *));
 int     lfs_match_dindir __P((struct lfs *, struct buf *));
 int     lfs_match_indir __P((struct lfs *, struct buf *));
 int     lfs_match_tindir __P((struct lfs *, struct buf *));
-struct buf *
-        lfs_newbuf __P((struct lfs *, daddr_t, size_t));
 void    lfs_newseg __P((struct lfs *));
 void    lfs_shellsort __P((struct buf **, daddr_t *, register int));
 void    lfs_newseg __P((struct lfs *));
 void    lfs_shellsort __P((struct buf **, daddr_t *, register int));
-void    lfs_updatemeta __P((struct lfs *,
-           struct segment *, struct vnode *, daddr_t *, struct buf **, int));
+void    lfs_supercallback __P((struct buf *));
+void    lfs_updatemeta __P((struct segment *));
 void    lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
 void    lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
-void    lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
-void    lfs_writeseg __P((struct lfs *, struct segment *));
+int     lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
+int     lfs_writeseg __P((struct lfs *, struct segment *));
 void    lfs_writesuper __P((struct lfs *, struct segment *));
 void    lfs_writesuper __P((struct lfs *, struct segment *));
+void    lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
+           struct segment *sp, int dirops));
 
 int    lfs_allclean_wakeup;            /* Cleaner wakeup address. */
 
 
 int    lfs_allclean_wakeup;            /* Cleaner wakeup address. */
 
@@ -89,23 +77,11 @@ lfs_vflush(vp)
 {
        struct inode *ip;
        struct lfs *fs;
 {
        struct inode *ip;
        struct lfs *fs;
-       struct mount *mp;
        struct segment *sp;
        int error, s;
 
        struct segment *sp;
        int error, s;
 
-#ifdef VERBOSE
-       printf("lfs_vflush\n");
-#endif
-       mp = vp->v_mount;
-       fs = VFSTOUFS(mp)->um_lfs;
-
-       /*
-        * XXX
-        * check flags?
-        * mp->mnt_flag & (MNT_MLOCK|MNT_RDONLY|MNT_MPBUSY) ||
-        */
-       if (vfs_busy(mp))
-               return (0);
+       fs = VFSTOUFS(vp->v_mount)->um_lfs;
+       lfs_seglock(fs);
 
        /*
         * Allocate a segment structure and enough space to hold pointers to
 
        /*
         * Allocate a segment structure and enough space to hold pointers to
@@ -116,7 +92,7 @@ lfs_vflush(vp)
        sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
            sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
        sp->seg_flags = SEGM_CKP;
        sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
            sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
        sp->seg_flags = SEGM_CKP;
-       lfs_initseg(fs, sp);
+       sp->vp = NULL;
 
        /*
         * Keep a cumulative count of the outstanding I/O operations.  If the
 
        /*
         * Keep a cumulative count of the outstanding I/O operations.  If the
@@ -128,13 +104,15 @@ lfs_vflush(vp)
        ++fs->lfs_iocount;
        splx(s);
 
        ++fs->lfs_iocount;
        splx(s);
 
-       if (vp->v_dirtyblkhd != NULL)
-               lfs_writefile(fs, sp, vp);
        ip = VTOI(vp);
        ip = VTOI(vp);
-       lfs_writeinode(fs, sp, ip);
-       ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
+       do {
+               lfs_initseg(fs, sp);
+               do {
+                       if (vp->v_dirtyblkhd != NULL)
+                               lfs_writefile(fs, sp, vp);
+               } while (lfs_writeinode(fs, sp, ip));
 
 
-       lfs_writeseg(fs, sp);
+       } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
 
        /*
         * If the I/O count is non-zero, sleep until it reaches zero.  At the
 
        /*
         * If the I/O count is non-zero, sleep until it reaches zero.  At the
@@ -148,7 +126,7 @@ lfs_vflush(vp)
                return (error);
        }
        splx(s);
                return (error);
        }
        splx(s);
-       vfs_unbusy(mp);
+       lfs_segunlock(fs);
 
        /*
         * XXX
 
        /*
         * XXX
@@ -160,32 +138,98 @@ lfs_vflush(vp)
        return (0);
 }
 
        return (0);
 }
 
+void
+lfs_writevnodes(fs, mp, sp, dirops)
+       struct lfs *fs;
+       struct mount *mp;
+       struct segment *sp;
+       int dirops;
+{
+       struct inode *ip;
+       struct vnode *vp;
+       int error, s;
+
+loop:  for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
+               /*
+                * If the vnode that we are about to sync is no longer
+                * associated with this mount point, start over.
+                */
+               if (vp->v_mount != mp)
+                       goto loop;
+
+               if (dirops && !(vp->v_flag & VDIROP) ||
+                   !dirops && (vp->v_flag & VDIROP))
+                       continue;
+               /*
+                * XXX
+                * Up the ref count so we don't get tossed out of
+                * memory.
+                */
+               VREF(vp);
+
+               /*
+                * Write the inode/file if dirty and it's not the
+                * the IFILE.
+                */
+               ip = VTOI(vp);
+               if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) ||
+                   vp->v_dirtyblkhd != NULL) &&
+                   ip->i_number != LFS_IFILE_INUM) {
+                       if (vp->v_dirtyblkhd != NULL)
+                               lfs_writefile(fs, sp, vp);
+                       (void) lfs_writeinode(fs, sp, ip);
+               }
+               vp->v_flag &= ~VDIROP;
+               vrele(vp);
+       }
+}
+
 int
 lfs_segwrite(mp, do_ckp)
        struct mount *mp;
        int do_ckp;                     /* Do a checkpoint. */
 {
 int
 lfs_segwrite(mp, do_ckp)
        struct mount *mp;
        int do_ckp;                     /* Do a checkpoint. */
 {
-       USES_VOP_ISLOCKED;
+       struct buf *bp;
        struct inode *ip;
        struct lfs *fs;
        struct segment *sp;
        struct vnode *vp;
        struct inode *ip;
        struct lfs *fs;
        struct segment *sp;
        struct vnode *vp;
-       int error, islocked, s;
+       SEGUSE *segusep;
+       daddr_t ibno;
+       CLEANERINFO *cip;
+       int clean, error, i, s;
 
 
-#ifdef VERBOSE
-       printf("lfs_segwrite\n");
-#endif
        fs = VFSTOUFS(mp)->um_lfs;
 
        fs = VFSTOUFS(mp)->um_lfs;
 
+       /*
+        * If we have fewer than 2 clean segments, wait until cleaner
+        * writes.
+        */
+       do {
+               LFS_CLEANERINFO(cip, fs, bp);
+               clean = cip->clean;
+               brelse(bp);
+               if (clean <= 2) {
+                       printf ("segs clean: %d\n", clean);
+                       wakeup(&lfs_allclean_wakeup);
+                       if (error = tsleep(&fs->lfs_avail, PRIBIO + 1,
+                           "lfs writer", 0))
+                               return (error);
+               }
+       } while (clean <= 2 );
+       lfs_seglock(fs);
+
        /*
         * Allocate a segment structure and enough space to hold pointers to
         * the maximum possible number of buffers which can be described in a
         * single summary block.
         */
        /*
         * Allocate a segment structure and enough space to hold pointers to
         * the maximum possible number of buffers which can be described in a
         * single summary block.
         */
+       do_ckp = do_ckp || fs->lfs_nactive > MAX_ACTIVE;
        sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
        sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
            sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
        sp->seg_flags = do_ckp ? SEGM_CKP : 0;
        sp = malloc(sizeof(struct segment), M_SEGMENT, M_WAITOK);
        sp->bpp = malloc(((LFS_SUMMARY_SIZE - sizeof(SEGSUM)) /
            sizeof(daddr_t) + 1) * sizeof(struct buf *), M_SEGMENT, M_WAITOK);
        sp->seg_flags = do_ckp ? SEGM_CKP : 0;
+       sp->vp = NULL;
        lfs_initseg(fs, sp);
 
        /*
        lfs_initseg(fs, sp);
 
        /*
@@ -199,63 +243,62 @@ lfs_segwrite(mp, do_ckp)
        ++fs->lfs_iocount;
        splx(s);
 
        ++fs->lfs_iocount;
        splx(s);
 
-loop:  for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
-               /*
-                * If the vnode that we are about to sync is no longer
-                * associated with this mount point, start over.
-                */
-               if (vp->v_mount != mp)
-                       goto loop;
+       lfs_writevnodes(fs, mp, sp, 0);
+       fs->lfs_writer = 1;
+       if (fs->lfs_dirops && (error =
+           tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) {
+               free(sp->bpp, M_SEGMENT);
+               free(sp, M_SEGMENT); 
+               fs->lfs_writer = 0;
+               return (error);
+       }
 
 
-               islocked = VOP_ISLOCKED(vp);
+       lfs_writevnodes(fs, mp, sp, 1);
 
 
-               /*
-                * XXX
-                * This is wrong, I think -- we should just wait until we
-                * get the vnode and go on.  Probably going to reschedule
-                * all of the writes we already scheduled...
-                */
-               if (islocked)
-                       VREF(vp);
-               else if (vget(vp))
-{
-printf("lfs_segment: failed to get vnode (tell Keith)!\n");
-                       goto loop;
-}
-               /*
-                * Write the inode/file if dirty and it's not the
-                * the IFILE.
-                */
-               ip = VTOI(vp);
-               if ((ip->i_flag & (IMOD | IACC | IUPD | ICHG) ||
-                   vp->v_dirtyblkhd != NULL) &&
-                   ip->i_number != LFS_IFILE_INUM) {
-                       if (vp->v_dirtyblkhd != NULL)
-                               lfs_writefile(fs, sp, vp);
-                       lfs_writeinode(fs, sp, ip);
-                       ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
+       /*
+        * If we are doing a checkpoint, mark everything since the
+        * last checkpoint as no longer ACTIVE.
+        */
+       if (do_ckp)
+               for (ibno = fs->lfs_cleansz + fs->lfs_segtabsz;
+                    --ibno >= fs->lfs_cleansz; ) {
+                       if (bread(fs->lfs_ivnode, ibno, fs->lfs_bsize,
+                           NOCRED, &bp))
+
+                               panic("lfs: ifile read");
+                       segusep = (SEGUSE *)bp->b_un.b_addr;
+                       for (i = fs->lfs_sepb; i--; segusep++)
+                               segusep->su_flags &= ~SEGUSE_ACTIVE;
+                               
+                       error = VOP_BWRITE(bp);
                }
                }
-               if (islocked)
-                       vrele(vp);
-               else
-                       vput(vp);
-       }
-       if (do_ckp) {
+
+       if (do_ckp || fs->lfs_doifile) {
                vp = fs->lfs_ivnode;
                while (vget(vp));
                ip = VTOI(vp);
                if (vp->v_dirtyblkhd != NULL)
                        lfs_writefile(fs, sp, vp);
                vp = fs->lfs_ivnode;
                while (vget(vp));
                ip = VTOI(vp);
                if (vp->v_dirtyblkhd != NULL)
                        lfs_writefile(fs, sp, vp);
-               lfs_writeinode(fs, sp, ip);
-               ip->i_flags &= ~(IMOD | IACC | IUPD | ICHG);
+               (void)lfs_writeinode(fs, sp, ip);
                vput(vp);
                vput(vp);
-       }
-       lfs_writeseg(fs, sp);
+               /*
+                * This should never happen because we just guaranteed
+                * that all the segment usage table blocks are dirty, so
+                * no new ones should get written.
+                */
+               if (lfs_writeseg(fs, sp) && do_ckp)
+                       panic("lfs_segwrite: created dirty blocks on ckp");
+       } else
+               (void) lfs_writeseg(fs, sp);
 
        /*
         * If the I/O count is non-zero, sleep until it reaches zero.  At the
         * moment, the user's process hangs around so we can sleep.
         */
 
        /*
         * If the I/O count is non-zero, sleep until it reaches zero.  At the
         * moment, the user's process hangs around so we can sleep.
         */
+       fs->lfs_writer = 0;
+       fs->lfs_doifile = 0;
+       wakeup(&fs->lfs_dirops);
+
        s = splbio();
        --fs->lfs_iocount;
        if (do_ckp) {
        s = splbio();
        --fs->lfs_iocount;
        if (do_ckp) {
@@ -266,10 +309,13 @@ printf("lfs_segment: failed to get vnode (tell Keith)!\n");
                        return (error);
                }
                splx(s);
                        return (error);
                }
                splx(s);
+               fs->lfs_nactive = 0;
                lfs_writesuper(fs, sp);
        } else 
                splx(s);
 
                lfs_writesuper(fs, sp);
        } else 
                splx(s);
 
+       lfs_segunlock(fs);
+
        free(sp->bpp, M_SEGMENT);
        free(sp, M_SEGMENT);
 
        free(sp->bpp, M_SEGMENT);
        free(sp, M_SEGMENT);
 
@@ -289,12 +335,9 @@ lfs_writefile(fs, sp, vp)
        struct finfo *fip;
        IFILE *ifp;
 
        struct finfo *fip;
        IFILE *ifp;
 
-#ifdef VERBOSE
-       printf("lfs_writefile\n");
-#endif
        if (sp->seg_bytes_left < fs->lfs_bsize ||
            sp->sum_bytes_left < sizeof(struct finfo)) {
        if (sp->seg_bytes_left < fs->lfs_bsize ||
            sp->sum_bytes_left < sizeof(struct finfo)) {
-               lfs_writeseg(fs, sp);
+               (void) lfs_writeseg(fs, sp);
                lfs_initseg(fs, sp);
        }
        sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
                lfs_initseg(fs, sp);
        }
        sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
@@ -327,11 +370,12 @@ lfs_writefile(fs, sp, vp)
                sp->fip =
                    (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
                    sizeof(daddr_t) * (fip->fi_nblocks - 1));
                sp->fip =
                    (struct finfo *)((caddr_t)fip + sizeof(struct finfo) +
                    sizeof(daddr_t) * (fip->fi_nblocks - 1));
+               sp->start_lbp = &sp->fip->fi_blocks[0];
        } else
                sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
 }
 
        } else
                sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
 }
 
-void
+int
 lfs_writeinode(fs, sp, ip)
        struct lfs *fs;
        struct segment *sp;
 lfs_writeinode(fs, sp, ip)
        struct lfs *fs;
        struct segment *sp;
@@ -342,17 +386,18 @@ lfs_writeinode(fs, sp, ip)
        SEGUSE *sup;
        daddr_t daddr;
        ino_t ino;
        SEGUSE *sup;
        daddr_t daddr;
        ino_t ino;
-       int ndx;
+       int error, ndx;
+       int redo_ifile = 0;
+
+       if (!(ip->i_flag & (IMOD | IACC | IUPD | ICHG)))
+               return;
 
 
-#ifdef VERBOSE
-       printf("lfs_writeinode\n");
-#endif
        /* Allocate a new inode block if necessary. */
        if (sp->ibp == NULL) {
                /* Allocate a new segment if necessary. */
                if (sp->seg_bytes_left < fs->lfs_bsize ||
                    sp->sum_bytes_left < sizeof(daddr_t)) {
        /* Allocate a new inode block if necessary. */
        if (sp->ibp == NULL) {
                /* Allocate a new segment if necessary. */
                if (sp->seg_bytes_left < fs->lfs_bsize ||
                    sp->sum_bytes_left < sizeof(daddr_t)) {
-                       lfs_writeseg(fs, sp);
+                       (void) lfs_writeseg(fs, sp);
                        lfs_initseg(fs, sp);
                }
 
                        lfs_initseg(fs, sp);
                }
 
@@ -360,8 +405,10 @@ lfs_writeinode(fs, sp, ip)
                daddr = fs->lfs_offset;
                fs->lfs_offset += fsbtodb(fs, 1);
                sp->ibp = *sp->cbpp++ =
                daddr = fs->lfs_offset;
                fs->lfs_offset += fsbtodb(fs, 1);
                sp->ibp = *sp->cbpp++ =
-                   lfs_newbuf(fs, daddr, fs->lfs_bsize);
-
+                   lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr,
+                   fs->lfs_bsize);
+               ++sp->start_bpp;
+               fs->lfs_avail -= fsbtodb(fs, 1);
                /* Set remaining space counters. */
                sp->seg_bytes_left -= fs->lfs_bsize;
                sp->sum_bytes_left -= sizeof(daddr_t);
                /* Set remaining space counters. */
                sp->seg_bytes_left -= fs->lfs_bsize;
                sp->sum_bytes_left -= sizeof(daddr_t);
@@ -371,10 +418,12 @@ lfs_writeinode(fs, sp, ip)
        }
 
        /* Update the inode times and copy the inode onto the inode page. */
        }
 
        /* Update the inode times and copy the inode onto the inode page. */
+       if (ip->i_flag & IMOD)
+               --fs->lfs_uinodes;
        ITIMES(ip, &time, &time);
        ITIMES(ip, &time, &time);
+       ip->i_flag &= ~(IMOD | IACC | IUPD | ICHG);
        bp = sp->ibp;
        bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din;
        bp = sp->ibp;
        bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din;
-
        /* Increment inode count in segment summary block. */
        ++((SEGSUM *)(sp->segsum))->ss_ninos;
 
        /* Increment inode count in segment summary block. */
        ++((SEGSUM *)(sp->segsum))->ss_ninos;
 
@@ -387,25 +436,93 @@ lfs_writeinode(fs, sp, ip)
         * address and access times for this inode in the ifile.
         */
        ino = ip->i_number;
         * address and access times for this inode in the ifile.
         */
        ino = ip->i_number;
-       if (ino == LFS_IFILE_INUM)
+       if (ino == LFS_IFILE_INUM) {
+               daddr = fs->lfs_idaddr;
                fs->lfs_idaddr = bp->b_blkno;
                fs->lfs_idaddr = bp->b_blkno;
+       } else {
+               LFS_IENTRY(ifp, fs, ino, ibp);
+               daddr = ifp->if_daddr;
+               ifp->if_daddr = bp->b_blkno;
+               error = VOP_BWRITE(ibp);
+       }
 
 
-       LFS_IENTRY(ifp, fs, ino, ibp);
-       daddr = ifp->if_daddr;
-       ifp->if_daddr = bp->b_blkno;
-       LFS_UBWRITE(ibp);
-
-       if (daddr != LFS_UNUSED_DADDR) {
+       /*
+        * No need to update segment usage if there was no former inode address
+        * or if the last inode address is in the current partial segment.
+        */
+       if (daddr != LFS_UNUSED_DADDR && 
+           !(daddr >= fs->lfs_lastpseg && daddr <= bp->b_blkno)) {
                LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
 #ifdef DIAGNOSTIC
                LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
 #ifdef DIAGNOSTIC
-               if (sup->su_nbytes < sizeof(struct dinode))
+               if (sup->su_nbytes < sizeof(struct dinode)) {
                        /* XXX -- Change to a panic. */
                        printf("lfs: negative bytes (segment %d)\n",
                            datosn(fs, daddr));
                        /* XXX -- Change to a panic. */
                        printf("lfs: negative bytes (segment %d)\n",
                            datosn(fs, daddr));
+                       panic("negative bytes");
+               }
 #endif
                sup->su_nbytes -= sizeof(struct dinode);
 #endif
                sup->su_nbytes -= sizeof(struct dinode);
-               LFS_UBWRITE(bp);
+               redo_ifile =
+                   (ino == LFS_IFILE_INUM && !(bp->b_flags & B_GATHERED));
+               error = VOP_BWRITE(bp);
+       }
+       return (redo_ifile);
+}
+
+int
+lfs_gatherblock(sp, bp, sptr)
+       struct segment *sp;
+       struct buf *bp;
+       int *sptr;
+{
+       struct lfs *fs;
+       int version;
+
+       /*
+        * If full, finish this segment.  We may be doing I/O, so
+        * release and reacquire the splbio().
+        */
+#ifdef DIAGNOSTIC
+       if (sp->vp == NULL)
+               panic ("lfs_gatherblock: Null vp in segment");
+#endif
+       fs = sp->fs;
+       if (sp->sum_bytes_left < sizeof(daddr_t) ||
+           sp->seg_bytes_left < fs->lfs_bsize) {
+               if (sptr)
+                       splx(*sptr);
+               lfs_updatemeta(sp);
+
+               /* Add the current file to the segment summary. */
+               ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
+
+               version = sp->fip->fi_version;
+               (void) lfs_writeseg(fs, sp);
+               lfs_initseg(fs, sp);
+
+               sp->fip->fi_version = version;
+               sp->fip->fi_ino = VTOI(sp->vp)->i_number;
+
+               sp->sum_bytes_left -= 
+                   sizeof(struct finfo) - sizeof(daddr_t);
+
+               if (sptr)
+                       *sptr = splbio();
+               return(1);
        }
        }
+
+       /* Insert into the buffer list, update the FINFO block. */
+if (bp->b_vp == sp->fs->lfs_ivnode &&
+((bp->b_lblkno == 0 && (bp->b_un.b_daddr[0] > 26 || bp->b_un.b_daddr[1] > 26)) ||
+(bp->b_lblkno > 2)))
+       printf ("Bad ifile block\n");
+       bp->b_flags |= B_GATHERED;
+       *sp->cbpp++ = bp;
+       sp->fip->fi_blocks[sp->fip->fi_nblocks++] = bp->b_lblkno;
+
+       sp->sum_bytes_left -= sizeof(daddr_t);
+       sp->seg_bytes_left -= bp->b_bufsize;
+       return(0);
 }
 
 void
 }
 
 void
@@ -415,29 +532,14 @@ lfs_gather(fs, sp, vp, match)
        struct vnode *vp;
        int (*match) __P((struct lfs *, struct buf *));
 {
        struct vnode *vp;
        int (*match) __P((struct lfs *, struct buf *));
 {
-       struct buf **bpp, *bp, *nbp;
-       struct finfo *fip;
-       struct inode *ip;
-       daddr_t *lbp, *start_lbp;
-       u_long version;
+       struct buf *bp;
        int s;
 
        int s;
 
-#ifdef VERBOSE
-       printf("lfs_gather\n");
-#endif
-       ip = VTOI(vp);
-       bpp = sp->cbpp;
-       fip = sp->fip;
-       start_lbp = lbp = &fip->fi_blocks[fip->fi_nblocks];
-
-loop:  s = splbio();
-       for (bp = vp->v_dirtyblkhd; bp; bp = nbp) {
-               nbp = bp->b_blockf;
-               /*
-                * XXX
-                * Should sleep on any BUSY buffer if doing an fsync?
-                */
-               if (bp->b_flags & B_BUSY || !match(fs, bp))
+       sp->vp = vp;
+       s = splbio();
+loop:  for (bp = vp->v_dirtyblkhd; bp; bp = bp->b_blockf) {
+               if (bp->b_flags & B_BUSY || !match(fs, bp) ||
+                   bp->b_flags & B_GATHERED)
                        continue;
 #ifdef DIAGNOSTIC
                if (!(bp->b_flags & B_DELWRI))
                        continue;
 #ifdef DIAGNOSTIC
                if (!(bp->b_flags & B_DELWRI))
@@ -445,85 +547,50 @@ loop:     s = splbio();
                if (!(bp->b_flags & B_LOCKED))
                        panic("lfs_gather: bp not B_LOCKED");
 #endif
                if (!(bp->b_flags & B_LOCKED))
                        panic("lfs_gather: bp not B_LOCKED");
 #endif
-               /*
-                * If full, finish this segment.  We may be doing I/O, so
-                * release and reacquire the splbio().
-                */
-               if (sp->sum_bytes_left < sizeof(daddr_t) ||
-                   sp->seg_bytes_left < fs->lfs_bsize) {
-                       splx(s);
-                       lfs_updatemeta(fs,
-                           sp, vp, start_lbp, bpp, lbp - start_lbp);
-
-                       /* Add the current file to the segment summary. */
-                       ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
-
-                       version = fip->fi_version;
-                       lfs_writeseg(fs, sp);
-                       lfs_initseg(fs, sp);
-
-                       fip = sp->fip;
-                       fip->fi_version = version;
-                       fip->fi_ino = ip->i_number;
-                       start_lbp = lbp = fip->fi_blocks;
-
-                       sp->sum_bytes_left -= 
-                           sizeof(struct finfo) - sizeof(daddr_t);
-
-                       bpp = sp->cbpp;
+               if (lfs_gatherblock(sp, bp, &s))
                        goto loop;
                        goto loop;
-               }
-
-               /* Insert into the buffer list, update the FINFO block. */
-               *sp->cbpp++ = bp;
-               ++fip->fi_nblocks;
-               *lbp++ = bp->b_lblkno;
-
-               sp->sum_bytes_left -= sizeof(daddr_t);
-               sp->seg_bytes_left -= bp->b_bufsize;
        }
        splx(s);
        }
        splx(s);
-       lfs_updatemeta(fs, sp, vp, start_lbp, bpp, lbp - start_lbp);
+       lfs_updatemeta(sp);
+       sp->vp = NULL;
 }
 
 }
 
+
 /*
  * Update the metadata that points to the blocks listed in the FINFO
  * array.
  */
 void
 /*
  * Update the metadata that points to the blocks listed in the FINFO
  * array.
  */
 void
-lfs_updatemeta(fs, sp, vp, lbp, bpp, nblocks)
-       struct lfs *fs;
+lfs_updatemeta(sp)
        struct segment *sp;
        struct segment *sp;
-       struct vnode *vp;
-       daddr_t *lbp;
-       struct buf **bpp;
-       int nblocks;
 {
 {
-       USES_VOP_BWRITE;
        SEGUSE *sup;
        struct buf *bp;
        SEGUSE *sup;
        struct buf *bp;
+       struct lfs *fs;
+       struct vnode *vp;
        INDIR a[NIADDR], *ap;
        struct inode *ip;
        daddr_t daddr, lbn, off;
        INDIR a[NIADDR], *ap;
        struct inode *ip;
        daddr_t daddr, lbn, off;
-       int db_per_fsb, error, i, num;
+       int db_per_fsb, error, i, nblocks, num;
 
 
-#ifdef VERBOSE
-       printf("lfs_updatemeta\n");
-#endif
-       if (nblocks == 0)
+       vp = sp->vp;
+       nblocks = &sp->fip->fi_blocks[sp->fip->fi_nblocks] - sp->start_lbp;
+       if (vp == NULL || nblocks == 0) 
                return;
 
        /* Sort the blocks. */
                return;
 
        /* Sort the blocks. */
-       lfs_shellsort(bpp, lbp, nblocks);
+       if (!(sp->seg_flags & SEGM_CLEAN))
+               lfs_shellsort(sp->start_bpp, sp->start_lbp, nblocks);
 
        /*
         * Assign disk addresses, and update references to the logical
         * block and the segment usage information.
         */
 
        /*
         * Assign disk addresses, and update references to the logical
         * block and the segment usage information.
         */
+       fs = sp->fs;
        db_per_fsb = fsbtodb(fs, 1);
        db_per_fsb = fsbtodb(fs, 1);
-       for (i = nblocks; i--; ++bpp) {
-               lbn = *lbp++;
-               (*bpp)->b_blkno = off = fs->lfs_offset;
+       for (i = nblocks; i--; ++sp->start_bpp) {
+               lbn = *sp->start_lbp++;
+               (*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
                fs->lfs_offset += db_per_fsb;
 
                if (error = lfs_bmaparray(vp, lbn, &daddr, a, &num))
                fs->lfs_offset += db_per_fsb;
 
                if (error = lfs_bmaparray(vp, lbn, &daddr, a, &num))
@@ -541,6 +608,15 @@ lfs_updatemeta(fs, sp, vp, lbp, bpp, nblocks)
                        if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
                                panic("lfs_updatemeta: bread bno %d",
                                    ap->in_lbn);
                        if (bread(vp, ap->in_lbn, fs->lfs_bsize, NOCRED, &bp))
                                panic("lfs_updatemeta: bread bno %d",
                                    ap->in_lbn);
+                       /*
+                        * Bread may create a new indirect block which needs
+                        * to get counted for the inode.
+                        */
+                       if (bp->b_blkno == -1 && !(bp->b_flags & B_CACHE)) {
+printf ("Updatemeta allocating indirect block: shouldn't happen\n");
+                               ip->i_blocks += btodb(fs->lfs_bsize);
+                               fs->lfs_bfree -= btodb(fs->lfs_bsize);
+                       }
                        bp->b_un.b_daddr[ap->in_off] = off;
                        VOP_BWRITE(bp);
                }
                        bp->b_un.b_daddr[ap->in_off] = off;
                        VOP_BWRITE(bp);
                }
@@ -549,13 +625,15 @@ lfs_updatemeta(fs, sp, vp, lbp, bpp, nblocks)
                if (daddr != UNASSIGNED) {
                        LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
 #ifdef DIAGNOSTIC
                if (daddr != UNASSIGNED) {
                        LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
 #ifdef DIAGNOSTIC
-                       if (sup->su_nbytes < fs->lfs_bsize)
+                       if (sup->su_nbytes < fs->lfs_bsize) {
                                /* XXX -- Change to a panic. */
                                printf("lfs: negative bytes (segment %d)\n",
                                    datosn(fs, daddr));
                                /* XXX -- Change to a panic. */
                                printf("lfs: negative bytes (segment %d)\n",
                                    datosn(fs, daddr));
+                               panic ("Negative Bytes");
+                       }
 #endif
                        sup->su_nbytes -= fs->lfs_bsize;
 #endif
                        sup->su_nbytes -= fs->lfs_bsize;
-                       LFS_UBWRITE(bp);
+                       error = VOP_BWRITE(bp);
                }
        }
 }
                }
        }
 }
@@ -573,9 +651,6 @@ lfs_initseg(fs, sp)
        struct buf *bp;
        daddr_t lbn, *lbnp;
 
        struct buf *bp;
        daddr_t lbn, *lbnp;
 
-#ifdef VERBOSE
-       printf("lfs_initseg\n");
-#endif
        /* Advance to the next segment. */
        if (!LFS_PARTIAL_FITS(fs)) {
                /* Wake up any cleaning procs waiting on this file system. */
        /* Advance to the next segment. */
        if (!LFS_PARTIAL_FITS(fs)) {
                /* Wake up any cleaning procs waiting on this file system. */
@@ -602,15 +677,18 @@ lfs_initseg(fs, sp)
                sp->seg_bytes_left = (fs->lfs_dbpseg -
                    (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
        }
                sp->seg_bytes_left = (fs->lfs_dbpseg -
                    (fs->lfs_offset - fs->lfs_curseg)) * DEV_BSIZE;
        }
+       fs->lfs_lastpseg = fs->lfs_offset;
 
 
+       sp->fs = fs;
        sp->ibp = NULL;
        sp->ninodes = 0;
 
        /* Get a new buffer for SEGSUM and enter it into the buffer list. */
        sp->cbpp = sp->bpp;
        sp->ibp = NULL;
        sp->ninodes = 0;
 
        /* Get a new buffer for SEGSUM and enter it into the buffer list. */
        sp->cbpp = sp->bpp;
-       *sp->cbpp = lfs_newbuf(fs, fs->lfs_offset, LFS_SUMMARY_SIZE);
+       *sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_offset,
+            LFS_SUMMARY_SIZE);
        sp->segsum = (*sp->cbpp)->b_un.b_addr;
        sp->segsum = (*sp->cbpp)->b_un.b_addr;
-       ++sp->cbpp;
+       sp->start_bpp = ++sp->cbpp;
        fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
 
        /* Set point to SEGSUM, initialize it. */
        fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
 
        /* Set point to SEGSUM, initialize it. */
@@ -621,6 +699,7 @@ lfs_initseg(fs, sp)
        /* Set pointer to first FINFO, initialize it. */
        sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM));
        sp->fip->fi_nblocks = 0;
        /* Set pointer to first FINFO, initialize it. */
        sp->fip = (struct finfo *)(sp->segsum + sizeof(SEGSUM));
        sp->fip->fi_nblocks = 0;
+       sp->start_lbp = &sp->fip->fi_blocks[0];
 
        sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
        sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
 
        sp->seg_bytes_left -= LFS_SUMMARY_SIZE;
        sp->sum_bytes_left = LFS_SUMMARY_SIZE - sizeof(SEGSUM);
@@ -636,29 +715,19 @@ lfs_newseg(fs)
        CLEANERINFO *cip;
        SEGUSE *sup;
        struct buf *bp;
        CLEANERINFO *cip;
        SEGUSE *sup;
        struct buf *bp;
-       int curseg, isdirty, sn;
+       int curseg, error, isdirty, sn;
 
 
-#ifdef VERBOSE
-       printf("lfs_newseg\n");
-#endif
-       /*
-        * Turn off the active bit for the current segment, turn on the
-        * active and dirty bits for the next segment, update the cleaner
-        * info.  Set the current segment to the next segment, get a new
-        * next segment.
-        */
-       LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_curseg), bp);
-       sup->su_flags &= ~SEGUSE_ACTIVE;
-       LFS_UBWRITE(bp);
-
-       LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
-       sup->su_flags |= SEGUSE_ACTIVE | SEGUSE_DIRTY;
-       LFS_UBWRITE(bp);
+        LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
+        sup->su_flags |= SEGUSE_DIRTY;
+       sup->su_nbytes = 0;
+       sup->su_nsums = 0;
+       sup->su_ninos = 0;
+        (void) VOP_BWRITE(bp);
 
        LFS_CLEANERINFO(cip, fs, bp);
        --cip->clean;
        ++cip->dirty;
 
        LFS_CLEANERINFO(cip, fs, bp);
        --cip->clean;
        ++cip->dirty;
-       LFS_UBWRITE(bp);
+       (void) VOP_BWRITE(bp);
 
        fs->lfs_lastseg = fs->lfs_curseg;
        fs->lfs_curseg = fs->lfs_nextseg;
 
        fs->lfs_lastseg = fs->lfs_curseg;
        fs->lfs_curseg = fs->lfs_nextseg;
@@ -672,29 +741,36 @@ lfs_newseg(fs)
                if (!isdirty)
                        break;
        }
                if (!isdirty)
                        break;
        }
+
+       ++fs->lfs_nactive;
        fs->lfs_nextseg = sntoda(fs, sn);
 }
 
        fs->lfs_nextseg = sntoda(fs, sn);
 }
 
-void
+int
 lfs_writeseg(fs, sp)
        struct lfs *fs;
        struct segment *sp;
 {
 lfs_writeseg(fs, sp)
        struct lfs *fs;
        struct segment *sp;
 {
-       USES_VOP_STRATEGY;
+       extern int locked_queue_count;
        struct buf **bpp, *bp, *cbp;
        SEGUSE *sup;
        SEGSUM *ssp;
        dev_t i_dev;
        struct buf **bpp, *bp, *cbp;
        SEGUSE *sup;
        SEGSUM *ssp;
        dev_t i_dev;
-       u_long *datap, *dp;
        size_t size;
        size_t size;
-       int ch_per_blk, i, nblocks, num, s, (*strategy)__P((struct vop_strategy_args *));
+       u_long *datap, *dp;
+       int ch_per_blk, do_again, error, i, nblocks, num, s;
+       int (*strategy)__P((struct vop_strategy_args *));
+       struct vop_strategy_args vop_strategy_a;
+       u_short ninos;
        char *p;
 
        char *p;
 
-#ifdef VERBOSE
-       printf("lfs_writeseg\n");
-#endif
-       if ((nblocks = sp->cbpp - sp->bpp) == 0)
-               return;
+       /*
+        * If there are no buffers other than the segment summary to write
+        * and it is not a checkpoint, don't do anything.  On a checkpoint,
+        * even if there aren't any buffers, you need to write the superblock.
+        */
+       if ((nblocks = sp->cbpp - sp->bpp) == 1 && !(sp->seg_flags & SEGM_CKP))
+               return (0);
 
        /*
         * Compute checksum across data and then across summary; the first
 
        /*
         * Compute checksum across data and then across summary; the first
@@ -709,11 +785,25 @@ lfs_writeseg(fs, sp)
                *dp++ = (*++bpp)->b_un.b_words[0];
        ssp = (SEGSUM *)sp->segsum;
        ssp->ss_create = time.tv_sec;
                *dp++ = (*++bpp)->b_un.b_words[0];
        ssp = (SEGSUM *)sp->segsum;
        ssp->ss_create = time.tv_sec;
-       ssp->ss_datasum = cksum(datap, nblocks * sizeof(u_long));
+       ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
        ssp->ss_sumsum =
            cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
        free(datap, M_SEGMENT);
 
        ssp->ss_sumsum =
            cksum(&ssp->ss_datasum, LFS_SUMMARY_SIZE - sizeof(ssp->ss_sumsum));
        free(datap, M_SEGMENT);
 
+       /* Update the segment usage information. */
+       LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
+       ninos = (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs);
+       sup->su_nbytes += nblocks - 1 - ninos << fs->lfs_bshift;
+       sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
+       sup->su_nbytes += LFS_SUMMARY_SIZE;
+       sup->su_lastmod = time.tv_sec;
+       sup->su_flags |= SEGUSE_ACTIVE;
+       sup->su_ninos += ninos;
+       ++sup->su_nsums;
+       do_again = !(bp->b_flags & B_GATHERED);
+       (void)VOP_BWRITE(bp);
+       fs->lfs_bfree -= (fsbtodb(fs, ninos) + LFS_SUMMARY_SIZE / DEV_BSIZE);
+
        i_dev = VTOI(fs->lfs_ivnode)->i_dev;
        strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
 
        i_dev = VTOI(fs->lfs_ivnode)->i_dev;
        strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
 
@@ -741,41 +831,59 @@ lfs_writeseg(fs, sp)
                i -= num;
                size = num * fs->lfs_bsize;
 
                i -= num;
                size = num * fs->lfs_bsize;
 
-               cbp = lfs_newbuf(fs, (*bpp)->b_blkno, 0);
+               cbp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp,
+                   (*bpp)->b_blkno, size);
                cbp->b_dev = i_dev;
                cbp->b_dev = i_dev;
-               cbp->b_flags = B_ASYNC | B_BUSY | B_CALL;
-               cbp->b_iodone = lfs_callback;
-               cbp->b_saveaddr = cbp->b_un.b_addr;
-               cbp->b_un.b_addr = malloc(size, M_SEGMENT, M_WAITOK);
+               cbp->b_flags |= B_ASYNC | B_BUSY;
 
                s = splbio();
                ++fs->lfs_iocount;
                for (p = cbp->b_un.b_addr; num--;) {
                        bp = *bpp++;
 
                s = splbio();
                ++fs->lfs_iocount;
                for (p = cbp->b_un.b_addr; num--;) {
                        bp = *bpp++;
-                       bcopy(bp->b_un.b_addr, p, bp->b_bcount);
+                       /*
+                        * Fake buffers from the cleaner are marked as B_INVAL.
+                        * We need to copy the data from user space rather than
+                        * from the buffer indicated.
+                        * XXX == what do I do on an error?
+                        */
+                       if (bp->b_flags & B_INVAL) {
+                               if (copyin(bp->b_saveaddr, p, bp->b_bcount))
+                                       panic("lfs_writeseg: copyin failed");
+                       } else
+                               bcopy(bp->b_un.b_addr, p, bp->b_bcount);
                        p += bp->b_bcount;
                        p += bp->b_bcount;
-                       bp->b_flags &=
-                           ~(B_DONE | B_ERROR | B_READ | B_DELWRI | B_LOCKED);
-                       if (!(bp->b_flags & B_NOCACHE)) {
+                       if (bp->b_flags & B_LOCKED)
+                               --locked_queue_count;
+                       bp->b_flags &= ~(B_ERROR | B_READ | B_DELWRI |
+                            B_LOCKED | B_GATHERED);
+                       if (bp->b_flags & B_CALL) {
+                               /* if B_CALL, it was created with newbuf */
+                               brelvp(bp);
+                               free(bp, M_SEGMENT);
+                       } else {
                                bremfree(bp);
                                reassignbuf(bp, bp->b_vp);
                                bremfree(bp);
                                reassignbuf(bp, bp->b_vp);
+                               brelse(bp);
                        }
                        }
-                       brelse(bp);
                }
                }
+               ++cbp->b_vp->v_numoutput;
                splx(s);
                cbp->b_bcount = p - cbp->b_un.b_addr;
                splx(s);
                cbp->b_bcount = p - cbp->b_un.b_addr;
+               /*
+                * XXXX This is a gross and disgusting hack.  Since these
+                * buffers are physically addressed, they hang off the
+                * device vnode (devvp).  As a result, they have no way
+                * of getting to the LFS superblock or lfs structure to
+                * keep track of the number of I/O's pending.  So, I am
+                * going to stuff the fs into the saveaddr field of
+                * the buffer (yuk).
+                */
+               cbp->b_saveaddr = (caddr_t)fs;
                vop_strategy_a.a_desc = VDESC(vop_strategy);
                vop_strategy_a.a_bp = cbp;
                (strategy)(&vop_strategy_a);
        }
                vop_strategy_a.a_desc = VDESC(vop_strategy);
                vop_strategy_a.a_bp = cbp;
                (strategy)(&vop_strategy_a);
        }
-
-       /* Update the segment usage information. */
-       LFS_SEGENTRY(sup, fs, sp->seg_number, bp);
-       sup->su_nbytes += nblocks - 1 - 
-           (ssp->ss_ninos + INOPB(fs) - 1) / INOPB(fs) << fs->lfs_bshift;
-       sup->su_nbytes += ssp->ss_ninos * sizeof(struct dinode);
-       sup->su_lastmod = time.tv_sec;
-       LFS_UBWRITE(bp);
+       return (do_again);
 }
 
 void
 }
 
 void
@@ -783,35 +891,38 @@ lfs_writesuper(fs, sp)
        struct lfs *fs;
        struct segment *sp;
 {
        struct lfs *fs;
        struct segment *sp;
 {
-       USES_VOP_STRATEGY;
        struct buf *bp;
        dev_t i_dev;
        int (*strategy) __P((struct vop_strategy_args *));
        struct buf *bp;
        dev_t i_dev;
        int (*strategy) __P((struct vop_strategy_args *));
+       int s;
+       struct vop_strategy_args vop_strategy_a;
 
 
-#ifdef VERBOSE
-       printf("lfs_writesuper\n");
-#endif
        i_dev = VTOI(fs->lfs_ivnode)->i_dev;
        strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
 
        /* Checksum the superblock and copy it into a buffer. */
        fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
        i_dev = VTOI(fs->lfs_ivnode)->i_dev;
        strategy = VTOI(fs->lfs_ivnode)->i_devvp->v_op[VOFFSET(vop_strategy)];
 
        /* Checksum the superblock and copy it into a buffer. */
        fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
-       bp = lfs_newbuf(fs, fs->lfs_sboffs[0], LFS_SBPAD);
+       bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_sboffs[0],
+           LFS_SBPAD);
        *bp->b_un.b_lfs = *fs;
 
        /* Write the first superblock (wait). */
        bp->b_dev = i_dev;
        bp->b_flags |= B_BUSY;
        *bp->b_un.b_lfs = *fs;
 
        /* Write the first superblock (wait). */
        bp->b_dev = i_dev;
        bp->b_flags |= B_BUSY;
-       bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
+       bp->b_flags &= ~(B_DONE | B_CALL | B_ERROR | B_READ | B_DELWRI);
        vop_strategy_a.a_desc = VDESC(vop_strategy);
        vop_strategy_a.a_bp = bp;
        vop_strategy_a.a_desc = VDESC(vop_strategy);
        vop_strategy_a.a_bp = bp;
+       s = splbio();
+       bp->b_vp->v_numoutput += 2;
+       splx(s);
        (strategy)(&vop_strategy_a);
        biowait(bp);
 
        /* Write the second superblock (don't wait). */
        bp->b_blkno = bp->b_lblkno = fs->lfs_sboffs[1];
        (strategy)(&vop_strategy_a);
        biowait(bp);
 
        /* Write the second superblock (don't wait). */
        bp->b_blkno = bp->b_lblkno = fs->lfs_sboffs[1];
-       bp->b_flags |= B_ASYNC | B_BUSY;
+       bp->b_flags |= B_CALL | B_ASYNC | B_BUSY;
        bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
        bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
+       bp->b_iodone = lfs_supercallback;
        (strategy)(&vop_strategy_a);
 }
 
        (strategy)(&vop_strategy_a);
 }
 
@@ -864,29 +975,27 @@ lfs_match_tindir(fs, bp)
  * Allocate a new buffer header.
  */
 struct buf *
  * Allocate a new buffer header.
  */
 struct buf *
-lfs_newbuf(fs, daddr, size)
-       struct lfs *fs;
+lfs_newbuf(vp, daddr, size)
+       struct vnode *vp;
        daddr_t daddr;
        size_t size;
 {
        struct buf *bp;
        daddr_t daddr;
        size_t size;
 {
        struct buf *bp;
-
-#ifdef VERBOSE
-       printf("lfs_newbuf\n");
-#endif
-       bp = getnewbuf();
-       bremhash(bp);
-       bgetvp(fs->lfs_ivnode, bp);
-       bp->b_bcount = 0;
+       size_t nbytes;
+
+       nbytes = roundup(size, DEV_BSIZE);
+       bp = malloc(sizeof(struct buf) + nbytes, M_SEGMENT, M_WAITOK);
+       bzero(bp, sizeof(struct buf) + nbytes);
+       bgetvp(vp, bp);
+       bp->b_un.b_addr = (caddr_t)(bp + 1);
+       bp->b_bufsize = size;
+       bp->b_bcount = size;
        bp->b_lblkno = daddr;
        bp->b_blkno = daddr;
        bp->b_error = 0;
        bp->b_resid = 0;
        bp->b_lblkno = daddr;
        bp->b_blkno = daddr;
        bp->b_error = 0;
        bp->b_resid = 0;
-       if (size)
-               allocbuf(bp, size);
-       bp->b_flags |= B_NOCACHE;
-       bp->b_saveaddr = NULL;
-       binshash(bp, &bfreelist[BQ_AGE]);
+       bp->b_iodone = lfs_callback;
+       bp->b_flags |= B_BUSY | B_CALL | B_NOCACHE;
        return (bp);
 }
 
        return (bp);
 }
 
@@ -896,7 +1005,7 @@ lfs_callback(bp)
 {
        struct lfs *fs;
 
 {
        struct lfs *fs;
 
-       fs = VFSTOUFS(bp->b_vp->v_mount)->um_lfs;
+       fs = (struct lfs *)bp->b_saveaddr;
 #ifdef DIAGNOSTIC
        if (fs->lfs_iocount == 0)
                panic("lfs_callback: zero iocount\n");
 #ifdef DIAGNOSTIC
        if (fs->lfs_iocount == 0)
                panic("lfs_callback: zero iocount\n");
@@ -904,12 +1013,16 @@ lfs_callback(bp)
        if (--fs->lfs_iocount == 0)
                wakeup(&fs->lfs_iocount);
 
        if (--fs->lfs_iocount == 0)
                wakeup(&fs->lfs_iocount);
 
-       if (bp->b_saveaddr) {
-               free(bp->b_un.b_addr, M_SEGMENT);
-               bp->b_un.b_addr = bp->b_saveaddr;
-               bp->b_saveaddr = NULL;
-       }
-       brelse(bp);
+       brelvp(bp);
+       free(bp, M_SEGMENT);
+}
+
+void
+lfs_supercallback(bp)
+       struct buf *bp;
+{
+       brelvp(bp);
+       free(bp, M_SEGMENT);
 }
 
 /*
 }
 
 /*
@@ -950,3 +1063,4 @@ lfs_shellsort(bp_array, lb_array, nmemb)
                                } else
                                        break;
 }
                                } else
                                        break;
 }
+