BSD 4_4_Lite1 release
[unix-history] / usr / src / sys / ufs / lfs / lfs_segment.c
index 6d768fb..249d59d 100644 (file)
@@ -1,10 +1,36 @@
 /*
 /*
- * Copyright (c) 1991 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1991, 1993
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
  *
  *
- *     @(#)lfs_segment.c       7.34 (Berkeley) %G%
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *     @(#)lfs_segment.c       8.5 (Berkeley) 1/4/94
  */
 
 #include <sys/param.h>
  */
 
 #include <sys/param.h>
 #include <ufs/ufs/inode.h>
 #include <ufs/ufs/dir.h>
 #include <ufs/ufs/ufsmount.h>
 #include <ufs/ufs/inode.h>
 #include <ufs/ufs/dir.h>
 #include <ufs/ufs/ufsmount.h>
+#include <ufs/ufs/ufs_extern.h>
 
 #include <ufs/lfs/lfs.h>
 #include <ufs/lfs/lfs_extern.h>
 
 
 #include <ufs/lfs/lfs.h>
 #include <ufs/lfs/lfs_extern.h>
 
+extern int count_lock_queue __P((void));
+
 #define MAX_ACTIVE     10
 /*
  * Determine if it's OK to start a partial in this segment, or if we need
 #define MAX_ACTIVE     10
 /*
  * Determine if it's OK to start a partial in this segment, or if we need
@@ -45,7 +74,6 @@ 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_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 *));
 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 *));
@@ -55,15 +83,26 @@ void         lfs_newseg __P((struct lfs *));
 void    lfs_shellsort __P((struct buf **, daddr_t *, register int));
 void    lfs_supercallback __P((struct buf *));
 void    lfs_updatemeta __P((struct segment *));
 void    lfs_shellsort __P((struct buf **, daddr_t *, register int));
 void    lfs_supercallback __P((struct buf *));
 void    lfs_updatemeta __P((struct segment *));
+int     lfs_vref __P((struct vnode *));
+void    lfs_vunref __P((struct vnode *));
 void    lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
 int     lfs_writeinode __P((struct lfs *, struct segment *, struct inode *));
 int     lfs_writeseg __P((struct lfs *, struct segment *));
 void    lfs_writefile __P((struct lfs *, struct segment *, struct vnode *));
 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 *));
 void    lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
            struct segment *sp, int dirops));
 
 int    lfs_allclean_wakeup;            /* Cleaner wakeup address. */
 
 void    lfs_writevnodes __P((struct lfs *fs, struct mount *mp,
            struct segment *sp, int dirops));
 
 int    lfs_allclean_wakeup;            /* Cleaner wakeup address. */
 
+/* Statistics Counters */
+#define DOSTATS
+struct lfs_stats lfs_stats;
+
+/* op values to lfs_writevnodes */
+#define        VN_REG  0
+#define        VN_DIROP        1
+#define        VN_EMPTY        2
+
 /*
  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
 /*
  * Ifile and meta data blocks are not marked busy, so segment writes MUST be
  * single threaded.  Currently, there are two paths into lfs_segwrite, sync()
@@ -78,81 +117,51 @@ lfs_vflush(vp)
        struct inode *ip;
        struct lfs *fs;
        struct segment *sp;
        struct inode *ip;
        struct lfs *fs;
        struct segment *sp;
-       int error, s;
 
        fs = VFSTOUFS(vp->v_mount)->um_lfs;
        if (fs->lfs_nactive > MAX_ACTIVE)
 
        fs = VFSTOUFS(vp->v_mount)->um_lfs;
        if (fs->lfs_nactive > MAX_ACTIVE)
-               return(lfs_segwrite(vp->v_mount, 1));
+               return(lfs_segwrite(vp->v_mount, SEGM_SYNC|SEGM_CKP));
+       lfs_seglock(fs, SEGM_SYNC);
+       sp = fs->lfs_sp;
 
 
-       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.
-        */
-       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 = SEGM_CKP;
-       sp->vp = NULL;
-
-       /*
-        * Keep a cumulative count of the outstanding I/O operations.  If the
-        * disk drive catches up with us it could go to zero before we finish,
-        * so we artificially increment it by one until we've scheduled all of
-        * the writes we intend to do.
-        */
-       s = splbio();
-       ++fs->lfs_iocount;
-       splx(s);
 
        ip = VTOI(vp);
 
        ip = VTOI(vp);
+       if (vp->v_dirtyblkhd.lh_first == NULL)
+               lfs_writevnodes(fs, vp->v_mount, sp, VN_EMPTY);
+
        do {
        do {
-               lfs_initseg(fs, sp);
                do {
                do {
-                       if (vp->v_dirtyblkhd != NULL)
+                       if (vp->v_dirtyblkhd.lh_first != NULL)
                                lfs_writefile(fs, sp, vp);
                } while (lfs_writeinode(fs, sp, ip));
 
        } while (lfs_writeseg(fs, sp) && ip->i_number == LFS_IFILE_INUM);
 
                                lfs_writefile(fs, sp, vp);
                } while (lfs_writeinode(fs, sp, ip));
 
        } 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
-        * moment, the user's process hangs around so we can sleep.
-        */
-       s = splbio();
-       if (--fs->lfs_iocount && (error =
-           tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs vflush", 0))) {
-               free(sp->bpp, M_SEGMENT);
-               free(sp, M_SEGMENT);
-               return (error);
-       }
-       splx(s);
+#ifdef DOSTATS
+       ++lfs_stats.nwrites;
+       if (sp->seg_flags & SEGM_SYNC)
+               ++lfs_stats.nsync_writes;
+       if (sp->seg_flags & SEGM_CKP)
+               ++lfs_stats.ncheckpoints;
+#endif
        lfs_segunlock(fs);
        lfs_segunlock(fs);
-
-       /*
-        * XXX
-        * Should be writing a checkpoint?
-        */
-       free(sp->bpp, M_SEGMENT);
-       free(sp, M_SEGMENT);
-
        return (0);
 }
 
 void
        return (0);
 }
 
 void
-lfs_writevnodes(fs, mp, sp, dirops)
+lfs_writevnodes(fs, mp, sp, op)
        struct lfs *fs;
        struct mount *mp;
        struct segment *sp;
        struct lfs *fs;
        struct mount *mp;
        struct segment *sp;
-       int dirops;
+       int op;
 {
        struct inode *ip;
        struct vnode *vp;
 {
        struct inode *ip;
        struct vnode *vp;
-       int error, s;
 
 
-loop:  for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
+loop:
+       for (vp = mp->mnt_vnodelist.lh_first;
+            vp != NULL;
+            vp = vp->v_mntvnodes.le_next) {
                /*
                 * If the vnode that we are about to sync is no longer
                 * associated with this mount point, start over.
                /*
                 * If the vnode that we are about to sync is no longer
                 * associated with this mount point, start over.
@@ -160,37 +169,43 @@ loop:     for (vp = mp->mnt_mounth; vp; vp = vp->v_mountf) {
                if (vp->v_mount != mp)
                        goto loop;
 
                if (vp->v_mount != mp)
                        goto loop;
 
-               if (dirops && !(vp->v_flag & VDIROP) ||
-                   !dirops && (vp->v_flag & VDIROP))
+               /* XXX ignore dirops for now
+               if (op == VN_DIROP && !(vp->v_flag & VDIROP) ||
+                   op != VN_DIROP && (vp->v_flag & VDIROP))
+                       continue;
+               */
+
+               if (op == VN_EMPTY && vp->v_dirtyblkhd.lh_first)
+                       continue;
+
+               if (vp->v_type == VNON)
+                       continue;
+
+               if (lfs_vref(vp))
                        continue;
                        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);
 
                /*
                 * 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) &&
+               if ((ip->i_flag &
+                   (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE) ||
+                   vp->v_dirtyblkhd.lh_first != NULL) &&
                    ip->i_number != LFS_IFILE_INUM) {
                    ip->i_number != LFS_IFILE_INUM) {
-                       if (vp->v_dirtyblkhd != NULL)
+                       if (vp->v_dirtyblkhd.lh_first != NULL)
                                lfs_writefile(fs, sp, vp);
                        (void) lfs_writeinode(fs, sp, ip);
                }
                vp->v_flag &= ~VDIROP;
                                lfs_writefile(fs, sp, vp);
                        (void) lfs_writeinode(fs, sp, ip);
                }
                vp->v_flag &= ~VDIROP;
-               vrele(vp);
+               lfs_vunref(vp);
        }
 }
 
 int
        }
 }
 
 int
-lfs_segwrite(mp, do_ckp)
+lfs_segwrite(mp, flags)
        struct mount *mp;
        struct mount *mp;
-       int do_ckp;                     /* Do a checkpoint. */
+       int flags;                      /* Do a checkpoint. */
 {
        struct buf *bp;
        struct inode *ip;
 {
        struct buf *bp;
        struct inode *ip;
@@ -200,7 +215,7 @@ lfs_segwrite(mp, do_ckp)
        SEGUSE *segusep;
        daddr_t ibno;
        CLEANERINFO *cip;
        SEGUSE *segusep;
        daddr_t ibno;
        CLEANERINFO *cip;
-       int clean, error, i, s;
+       int clean, do_ckp, error, i;
 
        fs = VFSTOUFS(mp)->um_lfs;
 
 
        fs = VFSTOUFS(mp)->um_lfs;
 
@@ -220,33 +235,20 @@ lfs_segwrite(mp, do_ckp)
                                return (error);
                }
        } while (clean <= 2 );
                                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->vp = NULL;
-       lfs_initseg(fs, sp);
+       do_ckp = flags & SEGM_CKP || fs->lfs_nactive > MAX_ACTIVE;
+       lfs_seglock(fs, flags | (do_ckp ? SEGM_CKP : 0));
+       sp = fs->lfs_sp;
 
 
-       /*
-        * Keep a cumulative count of the outstanding I/O operations.  If the
-        * disk drive catches up with us it could go to zero before we finish,
-        * so we artificially increment it by one until we've scheduled all of
-        * the writes we intend to do.  If not a checkpoint, we never do the
-        * final decrement, avoiding the wakeup in the callback routine.
-        */
-       s = splbio();
-       ++fs->lfs_iocount;
-       splx(s);
+       lfs_writevnodes(fs, mp, sp, VN_REG);
 
 
-       lfs_writevnodes(fs, mp, sp, 0);
+       /* XXX ignore ordering of dirops for now */
+       /* XXX
        fs->lfs_writer = 1;
        if (fs->lfs_dirops && (error =
            tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) {
        fs->lfs_writer = 1;
        if (fs->lfs_dirops && (error =
            tsleep(&fs->lfs_writer, PRIBIO + 1, "lfs writer", 0))) {
@@ -256,7 +258,8 @@ lfs_segwrite(mp, do_ckp)
                return (error);
        }
 
                return (error);
        }
 
-       lfs_writevnodes(fs, mp, sp, 1);
+       lfs_writevnodes(fs, mp, sp, VN_DIROP);
+       */
 
        /*
         * If we are doing a checkpoint, mark everything since the
 
        /*
         * If we are doing a checkpoint, mark everything since the
@@ -269,7 +272,7 @@ lfs_segwrite(mp, do_ckp)
                            NOCRED, &bp))
 
                                panic("lfs: ifile read");
                            NOCRED, &bp))
 
                                panic("lfs: ifile read");
-                       segusep = (SEGUSE *)bp->b_un.b_addr;
+                       segusep = (SEGUSE *)bp->b_data;
                        for (i = fs->lfs_sepb; i--; segusep++)
                                segusep->su_flags &= ~SEGUSE_ACTIVE;
                                
                        for (i = fs->lfs_sepb; i--; segusep++)
                                segusep->su_flags &= ~SEGUSE_ACTIVE;
                                
@@ -279,16 +282,14 @@ lfs_segwrite(mp, do_ckp)
        if (do_ckp || fs->lfs_doifile) {
 redo:
                vp = fs->lfs_ivnode;
        if (do_ckp || fs->lfs_doifile) {
 redo:
                vp = fs->lfs_ivnode;
-               while (vget(vp));
+               while (vget(vp, 1));
                ip = VTOI(vp);
                ip = VTOI(vp);
-               if (vp->v_dirtyblkhd != NULL)
+               if (vp->v_dirtyblkhd.lh_first != NULL)
                        lfs_writefile(fs, sp, vp);
                (void)lfs_writeinode(fs, sp, ip);
                vput(vp);
                        lfs_writefile(fs, sp, vp);
                (void)lfs_writeinode(fs, sp, ip);
                vput(vp);
-               if (lfs_writeseg(fs, sp) && do_ckp) {
-                       lfs_initseg(fs, sp);
+               if (lfs_writeseg(fs, sp) && do_ckp)
                        goto redo;
                        goto redo;
-               }
        } else
                (void) lfs_writeseg(fs, sp);
 
        } else
                (void) lfs_writeseg(fs, sp);
 
@@ -296,30 +297,20 @@ redo:
         * 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.
         */
+       /* XXX ignore dirops for now
        fs->lfs_writer = 0;
        fs->lfs_doifile = 0;
        wakeup(&fs->lfs_dirops);
        fs->lfs_writer = 0;
        fs->lfs_doifile = 0;
        wakeup(&fs->lfs_dirops);
-
-       s = splbio();
-       --fs->lfs_iocount;
-       if (do_ckp) {
-               if (fs->lfs_iocount && (error =
-                   tsleep(&fs->lfs_iocount, PRIBIO + 1, "lfs sync", 0))) {
-                       free(sp->bpp, M_SEGMENT);
-                       free(sp, M_SEGMENT);
-                       return (error);
-               }
-               splx(s);
-               fs->lfs_nactive = 0;
-               lfs_writesuper(fs, sp);
-       } else 
-               splx(s);
-
+       */
+
+#ifdef DOSTATS
+       ++lfs_stats.nwrites;
+       if (sp->seg_flags & SEGM_SYNC)
+               ++lfs_stats.nsync_writes;
+       if (sp->seg_flags & SEGM_CKP)
+               ++lfs_stats.ncheckpoints;
+#endif
        lfs_segunlock(fs);
        lfs_segunlock(fs);
-
-       free(sp->bpp, M_SEGMENT);
-       free(sp, M_SEGMENT);
-
        return (0);
 }
 
        return (0);
 }
 
@@ -337,11 +328,11 @@ lfs_writefile(fs, sp, vp)
        IFILE *ifp;
 
        if (sp->seg_bytes_left < fs->lfs_bsize ||
        IFILE *ifp;
 
        if (sp->seg_bytes_left < fs->lfs_bsize ||
-           sp->sum_bytes_left < sizeof(struct finfo)) {
+           sp->sum_bytes_left < sizeof(struct finfo))
                (void) lfs_writeseg(fs, sp);
                (void) lfs_writeseg(fs, sp);
-               lfs_initseg(fs, sp);
-       }
+
        sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
        sp->sum_bytes_left -= sizeof(struct finfo) - sizeof(daddr_t);
+       ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
 
        fip = sp->fip;
        fip->fi_nblocks = 0;
 
        fip = sp->fip;
        fip->fi_nblocks = 0;
@@ -363,17 +354,15 @@ lfs_writefile(fs, sp, vp)
 #endif
 
        fip = sp->fip;
 #endif
 
        fip = sp->fip;
-#ifdef META
-       printf("lfs_writefile: adding %d blocks\n", fip->fi_nblocks);
-#endif
        if (fip->fi_nblocks != 0) {
        if (fip->fi_nblocks != 0) {
-               ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
                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];
                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
+       } else {
                sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
                sp->sum_bytes_left += sizeof(struct finfo) - sizeof(daddr_t);
+               --((SEGSUM *)(sp->segsum))->ss_nfinfo;
+       }
 }
 
 int
 }
 
 int
@@ -387,20 +376,18 @@ lfs_writeinode(fs, sp, ip)
        SEGUSE *sup;
        daddr_t daddr;
        ino_t ino;
        SEGUSE *sup;
        daddr_t daddr;
        ino_t ino;
-       int error, ndx;
+       int error, i, ndx;
        int redo_ifile = 0;
 
        int redo_ifile = 0;
 
-       if (!(ip->i_flag & (IMOD | IACC | IUPD | ICHG)))
-               return;
+       if (!(ip->i_flag & (IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE)))
+               return(0);
 
        /* 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 ||
 
        /* 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)) {
+                   sp->sum_bytes_left < sizeof(daddr_t))
                        (void) lfs_writeseg(fs, sp);
                        (void) lfs_writeseg(fs, sp);
-                       lfs_initseg(fs, sp);
-               }
 
                /* Get next inode block. */
                daddr = fs->lfs_offset;
 
                /* Get next inode block. */
                daddr = fs->lfs_offset;
@@ -408,6 +395,9 @@ lfs_writeinode(fs, sp, ip)
                sp->ibp = *sp->cbpp++ =
                    lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr,
                    fs->lfs_bsize);
                sp->ibp = *sp->cbpp++ =
                    lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, daddr,
                    fs->lfs_bsize);
+               /* Zero out inode numbers */
+               for (i = 0; i < INOPB(fs); ++i)
+                       ((struct dinode *)sp->ibp->b_data)[i].di_inumber = 0;
                ++sp->start_bpp;
                fs->lfs_avail -= fsbtodb(fs, 1);
                /* Set remaining space counters. */
                ++sp->start_bpp;
                fs->lfs_avail -= fsbtodb(fs, 1);
                /* Set remaining space counters. */
@@ -419,12 +409,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)
+       if (ip->i_flag & IN_MODIFIED)
                --fs->lfs_uinodes;
        ITIMES(ip, &time, &time);
                --fs->lfs_uinodes;
        ITIMES(ip, &time, &time);
-       ip->i_flag &= ~(IMOD | IACC | IUPD | ICHG);
+       ip->i_flag &= ~(IN_ACCESS | IN_CHANGE | IN_MODIFIED | IN_UPDATE);
        bp = sp->ibp;
        bp = sp->ibp;
-       bp->b_un.b_dino[sp->ninodes % INOPB(fs)] = ip->i_din;
+       ((struct dinode *)bp->b_data)[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;
 
@@ -494,16 +484,13 @@ lfs_gatherblock(sp, bp, sptr)
                        splx(*sptr);
                lfs_updatemeta(sp);
 
                        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);
                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->fip->fi_version = version;
                sp->fip->fi_ino = VTOI(sp->vp)->i_number;
-
+               /* Add the current file to the segment summary. */
+               ++((SEGSUM *)(sp->segsum))->ss_nfinfo;
                sp->sum_bytes_left -= 
                    sizeof(struct finfo) - sizeof(daddr_t);
 
                sp->sum_bytes_left -= 
                    sizeof(struct finfo) - sizeof(daddr_t);
 
@@ -513,16 +500,12 @@ lfs_gatherblock(sp, bp, sptr)
        }
 
        /* Insert into the buffer list, update the FINFO block. */
        }
 
        /* 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);
        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;
+       sp->seg_bytes_left -= fs->lfs_bsize;
        return(0);
 }
 
        return(0);
 }
 
@@ -538,7 +521,7 @@ lfs_gather(fs, sp, vp, match)
 
        sp->vp = vp;
        s = splbio();
 
        sp->vp = vp;
        s = splbio();
-loop:  for (bp = vp->v_dirtyblkhd; bp; bp = bp->b_blockf) {
+loop:  for (bp = vp->v_dirtyblkhd.lh_first; bp; bp = bp->b_vnbufs.le_next) {
                if (bp->b_flags & B_BUSY || !match(fs, bp) ||
                    bp->b_flags & B_GATHERED)
                        continue;
                if (bp->b_flags & B_BUSY || !match(fs, bp) ||
                    bp->b_flags & B_GATHERED)
                        continue;
@@ -569,7 +552,7 @@ lfs_updatemeta(sp)
        struct buf *bp;
        struct lfs *fs;
        struct vnode *vp;
        struct buf *bp;
        struct lfs *fs;
        struct vnode *vp;
-       INDIR a[NIADDR], *ap;
+       struct indir a[NIADDR + 2], *ap;
        struct inode *ip;
        daddr_t daddr, lbn, off;
        int db_per_fsb, error, i, nblocks, num;
        struct inode *ip;
        daddr_t daddr, lbn, off;
        int db_per_fsb, error, i, nblocks, num;
@@ -594,8 +577,8 @@ lfs_updatemeta(sp)
                (*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
                fs->lfs_offset += db_per_fsb;
 
                (*sp->start_bpp)->b_blkno = off = fs->lfs_offset;
                fs->lfs_offset += db_per_fsb;
 
-               if (error = lfs_bmaparray(vp, lbn, &daddr, a, &num))
-                       panic("lfs_updatemeta: lfs_bmaparray %d", error);
+               if (error = ufs_bmaparray(vp, lbn, &daddr, a, &num, NULL))
+                       panic("lfs_updatemeta: ufs_bmaparray %d", error);
                ip = VTOI(vp);
                switch (num) {
                case 0:
                ip = VTOI(vp);
                switch (num) {
                case 0:
@@ -618,12 +601,13 @@ printf ("Updatemeta allocating indirect block: shouldn't happen\n");
                                ip->i_blocks += btodb(fs->lfs_bsize);
                                fs->lfs_bfree -= btodb(fs->lfs_bsize);
                        }
                                ip->i_blocks += btodb(fs->lfs_bsize);
                                fs->lfs_bfree -= btodb(fs->lfs_bsize);
                        }
-                       bp->b_un.b_daddr[ap->in_off] = off;
+                       ((daddr_t *)bp->b_data)[ap->in_off] = off;
                        VOP_BWRITE(bp);
                }
 
                /* Update segment usage information. */
                        VOP_BWRITE(bp);
                }
 
                /* Update segment usage information. */
-               if (daddr != UNASSIGNED) {
+               if (daddr != UNASSIGNED &&
+                   !(daddr >= fs->lfs_lastpseg && daddr <= off)) {
                        LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
 #ifdef DIAGNOSTIC
                        if (sup->su_nbytes < fs->lfs_bsize) {
                        LFS_SEGENTRY(sup, fs, datosn(fs, daddr), bp);
 #ifdef DIAGNOSTIC
                        if (sup->su_nbytes < fs->lfs_bsize) {
@@ -642,23 +626,26 @@ printf ("Updatemeta allocating indirect block: shouldn't happen\n");
 /*
  * Start a new segment.
  */
 /*
  * Start a new segment.
  */
-void
-lfs_initseg(fs, sp)
+int
+lfs_initseg(fs)
        struct lfs *fs;
        struct lfs *fs;
-       struct segment *sp;
 {
 {
+       struct segment *sp;
        SEGUSE *sup;
        SEGSUM *ssp;
        struct buf *bp;
        SEGUSE *sup;
        SEGSUM *ssp;
        struct buf *bp;
-       daddr_t lbn, *lbnp;
+       int repeat;
 
 
+       sp = fs->lfs_sp;
+
+       repeat = 0;
        /* 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. */
-               wakeup(&fs->lfs_nextseg);
                wakeup(&lfs_allclean_wakeup);
 
                lfs_newseg(fs);
                wakeup(&lfs_allclean_wakeup);
 
                lfs_newseg(fs);
+               repeat = 1;
                fs->lfs_offset = fs->lfs_curseg;
                sp->seg_number = datosn(fs, fs->lfs_curseg);
                sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
                fs->lfs_offset = fs->lfs_curseg;
                sp->seg_number = datosn(fs, fs->lfs_curseg);
                sp->seg_bytes_left = fs->lfs_dbpseg * DEV_BSIZE;
@@ -688,7 +675,8 @@ lfs_initseg(fs, sp)
        sp->cbpp = sp->bpp;
        *sp->cbpp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_offset,
             LFS_SUMMARY_SIZE);
        sp->cbpp = sp->bpp;
        *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_data;
+       bzero(sp->segsum, LFS_SUMMARY_SIZE);
        sp->start_bpp = ++sp->cbpp;
        fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
 
        sp->start_bpp = ++sp->cbpp;
        fs->lfs_offset += LFS_SUMMARY_SIZE / DEV_BSIZE;
 
@@ -704,6 +692,8 @@ lfs_initseg(fs, sp)
 
        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);
+
+       return(repeat);
 }
 
 /*
 }
 
 /*
@@ -716,7 +706,7 @@ lfs_newseg(fs)
        CLEANERINFO *cip;
        SEGUSE *sup;
        struct buf *bp;
        CLEANERINFO *cip;
        SEGUSE *sup;
        struct buf *bp;
-       int curseg, error, isdirty, sn;
+       int curseg, isdirty, sn;
 
         LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
         sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
 
         LFS_SEGENTRY(sup, fs, datosn(fs, fs->lfs_nextseg), bp);
         sup->su_flags |= SEGUSE_DIRTY | SEGUSE_ACTIVE;
@@ -745,6 +735,9 @@ lfs_newseg(fs)
 
        ++fs->lfs_nactive;
        fs->lfs_nextseg = sntoda(fs, sn);
 
        ++fs->lfs_nactive;
        fs->lfs_nextseg = sntoda(fs, sn);
+#ifdef DOSTATS
+       ++lfs_stats.segsused;
+#endif
 }
 
 int
 }
 
 int
@@ -759,7 +752,7 @@ lfs_writeseg(fs, sp)
        dev_t i_dev;
        size_t size;
        u_long *datap, *dp;
        dev_t i_dev;
        size_t size;
        u_long *datap, *dp;
-       int ch_per_blk, do_again, error, i, nblocks, num, s;
+       int ch_per_blk, do_again, i, nblocks, num, s;
        int (*strategy)__P((struct vop_strategy_args *));
        struct vop_strategy_args vop_strategy_a;
        u_short ninos;
        int (*strategy)__P((struct vop_strategy_args *));
        struct vop_strategy_args vop_strategy_a;
        u_short ninos;
@@ -770,7 +763,7 @@ lfs_writeseg(fs, sp)
         * 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.
         */
         * 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))
+       if ((nblocks = sp->cbpp - sp->bpp) == 1)
                return (0);
 
        ssp = (SEGSUM *)sp->segsum;
                return (0);
 
        ssp = (SEGSUM *)sp->segsum;
@@ -800,7 +793,7 @@ lfs_writeseg(fs, sp)
                        if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
                                panic("lfs_writeseg: copyin failed");
                } else
                        if (copyin((*bpp)->b_saveaddr, dp++, sizeof(u_long)))
                                panic("lfs_writeseg: copyin failed");
                } else
-                       *dp++ = (*bpp)->b_un.b_words[0];
+                       *dp++ = ((u_long *)(*bpp)->b_data)[0];
        }
        ssp->ss_create = time.tv_sec;
        ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
        }
        ssp->ss_create = time.tv_sec;
        ssp->ss_datasum = cksum(datap, (nblocks - 1) * sizeof(u_long));
@@ -819,8 +812,8 @@ lfs_writeseg(fs, sp)
        /*
         * When we simply write the blocks we lose a rotation for every block
         * written.  To avoid this problem, we allocate memory in chunks, copy
        /*
         * When we simply write the blocks we lose a rotation for every block
         * written.  To avoid this problem, we allocate memory in chunks, copy
-        * the buffers into the chunk and write the chunk.  56K was chosen as
-        * some driver/controllers can't handle unsigned 16 bit transfers.
+        * the buffers into the chunk and write the chunk.  MAXPHYS is the
+        * largest size I/O devices can handle.
         * When the data is copied to the chunk, turn off the the B_LOCKED bit
         * and brelse the buffer (which will move them to the LRU list).  Add
         * the B_CALL flag to the buffer header so we can count I/O's for the
         * When the data is copied to the chunk, turn off the the B_LOCKED bit
         * and brelse the buffer (which will move them to the LRU list).  Add
         * the B_CALL flag to the buffer header so we can count I/O's for the
@@ -831,8 +824,7 @@ lfs_writeseg(fs, sp)
         * easily make the buffers contiguous in kernel memory and if that's
         * fast enough.
         */
         * easily make the buffers contiguous in kernel memory and if that's
         * fast enough.
         */
-#define        LFS_CHUNKSIZE   (56 * 1024)
-       ch_per_blk = LFS_CHUNKSIZE / fs->lfs_bsize;
+       ch_per_blk = MAXPHYS / fs->lfs_bsize;
        for (bpp = sp->bpp, i = nblocks; i;) {
                num = ch_per_blk;
                if (num > i)
        for (bpp = sp->bpp, i = nblocks; i;) {
                num = ch_per_blk;
                if (num > i)
@@ -847,7 +839,7 @@ lfs_writeseg(fs, sp)
 
                s = splbio();
                ++fs->lfs_iocount;
 
                s = splbio();
                ++fs->lfs_iocount;
-               for (p = cbp->b_un.b_addr; num--;) {
+               for (p = cbp->b_data; num--;) {
                        bp = *bpp++;
                        /*
                         * Fake buffers from the cleaner are marked as B_INVAL.
                        bp = *bpp++;
                        /*
                         * Fake buffers from the cleaner are marked as B_INVAL.
@@ -859,7 +851,7 @@ lfs_writeseg(fs, sp)
                                if (copyin(bp->b_saveaddr, p, bp->b_bcount))
                                        panic("lfs_writeseg: copyin failed");
                        } else
                                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);
+                               bcopy(bp->b_data, p, bp->b_bcount);
                        p += bp->b_bcount;
                        if (bp->b_flags & B_LOCKED)
                                --locked_queue_count;
                        p += bp->b_bcount;
                        if (bp->b_flags & B_LOCKED)
                                --locked_queue_count;
@@ -868,16 +860,19 @@ lfs_writeseg(fs, sp)
                        if (bp->b_flags & B_CALL) {
                                /* if B_CALL, it was created with newbuf */
                                brelvp(bp);
                        if (bp->b_flags & B_CALL) {
                                /* if B_CALL, it was created with newbuf */
                                brelvp(bp);
+                               if (!(bp->b_flags & B_INVAL))
+                                       free(bp->b_data, M_SEGMENT);
                                free(bp, M_SEGMENT);
                        } else {
                                bremfree(bp);
                                free(bp, M_SEGMENT);
                        } else {
                                bremfree(bp);
+                               bp->b_flags |= B_DONE;
                                reassignbuf(bp, bp->b_vp);
                                brelse(bp);
                        }
                }
                ++cbp->b_vp->v_numoutput;
                splx(s);
                                reassignbuf(bp, bp->b_vp);
                                brelse(bp);
                        }
                }
                ++cbp->b_vp->v_numoutput;
                splx(s);
-               cbp->b_bcount = p - cbp->b_un.b_addr;
+               cbp->b_bcount = p - (char *)cbp->b_data;
                /*
                 * XXXX This is a gross and disgusting hack.  Since these
                 * buffers are physically addressed, they hang off the
                /*
                 * XXXX This is a gross and disgusting hack.  Since these
                 * buffers are physically addressed, they hang off the
@@ -892,13 +887,31 @@ lfs_writeseg(fs, sp)
                vop_strategy_a.a_bp = cbp;
                (strategy)(&vop_strategy_a);
        }
                vop_strategy_a.a_bp = cbp;
                (strategy)(&vop_strategy_a);
        }
-       return (do_again);
+       /*
+        * XXX
+        * Vinvalbuf can move locked buffers off the locked queue
+        * and we have no way of knowing about this.  So, after
+        * doing a big write, we recalculate how many bufers are
+        * really still left on the locked queue.
+        */
+       locked_queue_count = count_lock_queue();
+       wakeup(&locked_queue_count);
+#ifdef DOSTATS
+       ++lfs_stats.psegwrites;
+       lfs_stats.blocktot += nblocks - 1;
+       if (fs->lfs_sp->seg_flags & SEGM_SYNC)
+               ++lfs_stats.psyncwrites;
+       if (fs->lfs_sp->seg_flags & SEGM_CLEAN) {
+               ++lfs_stats.pcleanwrites;
+               lfs_stats.cleanblocks += nblocks - 1;
+       }
+#endif
+       return (lfs_initseg(fs) || do_again);
 }
 
 void
 }
 
 void
-lfs_writesuper(fs, sp)
+lfs_writesuper(fs)
        struct lfs *fs;
        struct lfs *fs;
-       struct segment *sp;
 {
        struct buf *bp;
        dev_t i_dev;
 {
        struct buf *bp;
        dev_t i_dev;
@@ -913,26 +926,19 @@ lfs_writesuper(fs, sp)
        fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
        bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_sboffs[0],
            LFS_SBPAD);
        fs->lfs_cksum = cksum(fs, sizeof(struct lfs) - sizeof(fs->lfs_cksum));
        bp = lfs_newbuf(VTOI(fs->lfs_ivnode)->i_devvp, fs->lfs_sboffs[0],
            LFS_SBPAD);
-       *bp->b_un.b_lfs = *fs;
+       *(struct lfs *)bp->b_data = *fs;
 
 
-       /* Write the first superblock (wait). */
+       /* XXX Toggle between first two superblocks; for now just write first */
        bp->b_dev = i_dev;
        bp->b_dev = i_dev;
-       bp->b_flags |= B_BUSY;
-       bp->b_flags &= ~(B_DONE | B_CALL | B_ERROR | B_READ | B_DELWRI);
+       bp->b_flags |= B_BUSY | B_CALL | B_ASYNC;
+       bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
+       bp->b_iodone = lfs_supercallback;
        vop_strategy_a.a_desc = VDESC(vop_strategy);
        vop_strategy_a.a_bp = bp;
        s = splbio();
        vop_strategy_a.a_desc = VDESC(vop_strategy);
        vop_strategy_a.a_bp = bp;
        s = splbio();
-       bp->b_vp->v_numoutput += 2;
+       ++bp->b_vp->v_numoutput;
        splx(s);
        (strategy)(&vop_strategy_a);
        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];
-       bp->b_flags |= B_CALL | B_ASYNC | B_BUSY;
-       bp->b_flags &= ~(B_DONE | B_ERROR | B_READ | B_DELWRI);
-       bp->b_iodone = lfs_supercallback;
-       (strategy)(&vop_strategy_a);
 }
 
 /*
 }
 
 /*
@@ -993,10 +999,11 @@ lfs_newbuf(vp, daddr, size)
        size_t nbytes;
 
        nbytes = roundup(size, DEV_BSIZE);
        size_t nbytes;
 
        nbytes = roundup(size, DEV_BSIZE);
-       bp = malloc(sizeof(struct buf) + nbytes, M_SEGMENT, M_WAITOK);
-       bzero(bp, sizeof(struct buf) + nbytes);
+       bp = malloc(sizeof(struct buf), M_SEGMENT, M_WAITOK);
+       bzero(bp, sizeof(struct buf));
+       if (nbytes)
+               bp->b_data = malloc(nbytes, M_SEGMENT, M_WAITOK);
        bgetvp(vp, bp);
        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_bufsize = size;
        bp->b_bcount = size;
        bp->b_lblkno = daddr;
@@ -1023,6 +1030,7 @@ lfs_callback(bp)
                wakeup(&fs->lfs_iocount);
 
        brelvp(bp);
                wakeup(&fs->lfs_iocount);
 
        brelvp(bp);
+       free(bp->b_data, M_SEGMENT);
        free(bp, M_SEGMENT);
 }
 
        free(bp, M_SEGMENT);
 }
 
@@ -1031,6 +1039,7 @@ lfs_supercallback(bp)
        struct buf *bp;
 {
        brelvp(bp);
        struct buf *bp;
 {
        brelvp(bp);
+       free(bp->b_data, M_SEGMENT);
        free(bp, M_SEGMENT);
 }
 
        free(bp, M_SEGMENT);
 }
 
@@ -1073,3 +1082,30 @@ lfs_shellsort(bp_array, lb_array, nmemb)
                                        break;
 }
 
                                        break;
 }
 
+/*
+ * Check VXLOCK.  Return 1 if the vnode is locked.  Otherwise, vget it.
+ */
+lfs_vref(vp)
+       register struct vnode *vp;
+{
+
+       if (vp->v_flag & VXLOCK)
+               return(1);
+       return (vget(vp, 0));
+}
+
+void
+lfs_vunref(vp)
+       register struct vnode *vp;
+{
+       extern int lfs_no_inactive;
+
+       /*
+        * This is vrele except that we do not want to VOP_INACTIVE
+        * this vnode. Rather than inline vrele here, we use a global
+        * flag to tell lfs_inactive not to run. Yes, its gross.
+        */
+       lfs_no_inactive = 1;
+       vrele(vp);
+       lfs_no_inactive = 0;
+}