xfs: enforce synchronous writes in xfs_bwrite
[safe/jmp/linux-2.6] / fs / xfs / xfs_log.c
index 7ed15bd..77593c2 100644 (file)
 #include "xfs_dinode.h"
 #include "xfs_inode.h"
 #include "xfs_rw.h"
+#include "xfs_trace.h"
 
-
-#define xlog_write_adv_cnt(ptr, len, off, bytes) \
-       { (ptr) += (bytes); \
-         (len) -= (bytes); \
-         (off) += (bytes);}
+kmem_zone_t    *xfs_log_ticket_zone;
 
 /* Local miscellaneous function prototypes */
-STATIC int      xlog_bdstrat_cb(struct xfs_buf *);
-STATIC int      xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket,
+STATIC int      xlog_commit_record(struct log *log, struct xlog_ticket *ticket,
                                    xlog_in_core_t **, xfs_lsn_t *);
 STATIC xlog_t *  xlog_alloc_log(xfs_mount_t    *mp,
                                xfs_buftarg_t   *log_target,
@@ -58,11 +54,9 @@ STATIC xlog_t *  xlog_alloc_log(xfs_mount_t  *mp,
 STATIC int      xlog_space_left(xlog_t *log, int cycle, int bytes);
 STATIC int      xlog_sync(xlog_t *log, xlog_in_core_t *iclog);
 STATIC void     xlog_dealloc_log(xlog_t *log);
-STATIC int      xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[],
-                           int nentries, xfs_log_ticket_t tic,
-                           xfs_lsn_t *start_lsn,
-                           xlog_in_core_t **commit_iclog,
-                           uint flags);
+STATIC int      xlog_write(struct log *log, struct xfs_log_vec *log_vector,
+                           struct xlog_ticket *tic, xfs_lsn_t *start_lsn,
+                           xlog_in_core_t **commit_iclog, uint flags);
 
 /* local state machine functions */
 STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int);
@@ -73,18 +67,11 @@ STATIC int  xlog_state_get_iclog_space(xlog_t               *log,
                                       xlog_ticket_t    *ticket,
                                       int              *continued_write,
                                       int              *logoffsetp);
-STATIC void xlog_state_put_ticket(xlog_t       *log,
-                                 xlog_ticket_t *tic);
 STATIC int  xlog_state_release_iclog(xlog_t            *log,
                                     xlog_in_core_t     *iclog);
 STATIC void xlog_state_switch_iclogs(xlog_t            *log,
                                     xlog_in_core_t *iclog,
                                     int                eventual_size);
-STATIC int  xlog_state_sync(xlog_t                     *log,
-                           xfs_lsn_t                   lsn,
-                           uint                        flags,
-                           int                         *log_flushed);
-STATIC int  xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed);
 STATIC void xlog_state_want_sync(xlog_t        *log, xlog_in_core_t *iclog);
 
 /* local functions to manipulate grant head */
@@ -101,16 +88,14 @@ STATIC void xlog_ungrant_log_space(xlog_t   *log,
 
 
 /* local ticket functions */
-STATIC void            xlog_state_ticket_alloc(xlog_t *log);
-STATIC xlog_ticket_t   *xlog_ticket_get(xlog_t *log,
+STATIC xlog_ticket_t   *xlog_ticket_alloc(xlog_t *log,
                                         int    unit_bytes,
                                         int    count,
                                         char   clientid,
                                         uint   flags);
-STATIC void            xlog_ticket_put(xlog_t *log, xlog_ticket_t *ticket);
 
 #if defined(DEBUG)
-STATIC void    xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr);
+STATIC void    xlog_verify_dest_ptr(xlog_t *log, char *ptr);
 STATIC void    xlog_verify_grant_head(xlog_t *log, int equals);
 STATIC void    xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog,
                                  int count, boolean_t syncing);
@@ -125,57 +110,6 @@ STATIC void        xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog,
 
 STATIC int     xlog_iclogs_empty(xlog_t *log);
 
-#if defined(XFS_LOG_TRACE)
-void
-xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
-{
-       unsigned long cnts;
-
-       if (!log->l_grant_trace) {
-               log->l_grant_trace = ktrace_alloc(2048, KM_NOSLEEP);
-               if (!log->l_grant_trace)
-                       return;
-       }
-       /* ticket counts are 1 byte each */
-       cnts = ((unsigned long)tic->t_ocnt) | ((unsigned long)tic->t_cnt) << 8;
-
-       ktrace_enter(log->l_grant_trace,
-                    (void *)tic,
-                    (void *)log->l_reserve_headq,
-                    (void *)log->l_write_headq,
-                    (void *)((unsigned long)log->l_grant_reserve_cycle),
-                    (void *)((unsigned long)log->l_grant_reserve_bytes),
-                    (void *)((unsigned long)log->l_grant_write_cycle),
-                    (void *)((unsigned long)log->l_grant_write_bytes),
-                    (void *)((unsigned long)log->l_curr_cycle),
-                    (void *)((unsigned long)log->l_curr_block),
-                    (void *)((unsigned long)CYCLE_LSN(log->l_tail_lsn)),
-                    (void *)((unsigned long)BLOCK_LSN(log->l_tail_lsn)),
-                    (void *)string,
-                    (void *)((unsigned long)tic->t_trans_type),
-                    (void *)cnts,
-                    (void *)((unsigned long)tic->t_curr_res),
-                    (void *)((unsigned long)tic->t_unit_res));
-}
-
-void
-xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
-{
-       if (!iclog->ic_trace)
-               iclog->ic_trace = ktrace_alloc(256, KM_SLEEP);
-       ktrace_enter(iclog->ic_trace,
-                    (void *)((unsigned long)state),
-                    (void *)((unsigned long)current_pid()),
-                    (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
-                    (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
-                    (void *)NULL, (void *)NULL, (void *)NULL, (void *)NULL,
-                    (void *)NULL, (void *)NULL);
-}
-#else
-#define        xlog_trace_loggrant(log,tic,string)
-#define        xlog_trace_iclog(iclog,state)
-#endif /* XFS_LOG_TRACE */
-
 
 static void
 xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
@@ -228,20 +162,24 @@ xlog_grant_sub_space(struct log *log, int bytes)
 static void
 xlog_grant_add_space_write(struct log *log, int bytes)
 {
-       log->l_grant_write_bytes += bytes;
-       if (log->l_grant_write_bytes > log->l_logsize) {
-               log->l_grant_write_bytes -= log->l_logsize;
+       int tmp = log->l_logsize - log->l_grant_write_bytes;
+       if (tmp > bytes)
+               log->l_grant_write_bytes += bytes;
+       else {
                log->l_grant_write_cycle++;
+               log->l_grant_write_bytes = bytes - tmp;
        }
 }
 
 static void
 xlog_grant_add_space_reserve(struct log *log, int bytes)
 {
-       log->l_grant_reserve_bytes += bytes;
-       if (log->l_grant_reserve_bytes > log->l_logsize) {
-               log->l_grant_reserve_bytes -= log->l_logsize;
+       int tmp = log->l_logsize - log->l_grant_reserve_bytes;
+       if (tmp > bytes)
+               log->l_grant_reserve_bytes += bytes;
+       else {
                log->l_grant_reserve_cycle++;
+               log->l_grant_reserve_bytes = bytes - tmp;
        }
 }
 
@@ -298,14 +236,14 @@ xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type)
  * out when the next write occurs.
  */
 xfs_lsn_t
-xfs_log_done(xfs_mount_t       *mp,
-            xfs_log_ticket_t   xtic,
-            void               **iclog,
-            uint               flags)
+xfs_log_done(
+       struct xfs_mount        *mp,
+       struct xlog_ticket      *ticket,
+       struct xlog_in_core     **iclog,
+       uint                    flags)
 {
-       xlog_t          *log    = mp->m_log;
-       xlog_ticket_t   *ticket = (xfs_log_ticket_t) xtic;
-       xfs_lsn_t       lsn     = 0;
+       struct log              *log = mp->m_log;
+       xfs_lsn_t               lsn = 0;
 
        if (XLOG_FORCED_SHUTDOWN(log) ||
            /*
@@ -313,8 +251,7 @@ xfs_log_done(xfs_mount_t    *mp,
             * If we get an error, just continue and give back the log ticket.
             */
            (((ticket->t_flags & XLOG_TIC_INITED) == 0) &&
-            (xlog_commit_record(mp, ticket,
-                                (xlog_in_core_t **)iclog, &lsn)))) {
+            (xlog_commit_record(log, ticket, iclog, &lsn)))) {
                lsn = (xfs_lsn_t) -1;
                if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
                        flags |= XFS_LOG_REL_PERM_RESERV;
@@ -324,67 +261,27 @@ xfs_log_done(xfs_mount_t  *mp,
 
        if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 ||
            (flags & XFS_LOG_REL_PERM_RESERV)) {
+               trace_xfs_log_done_nonperm(log, ticket);
+
                /*
                 * Release ticket if not permanent reservation or a specific
                 * request has been made to release a permanent reservation.
                 */
-               xlog_trace_loggrant(log, ticket, "xfs_log_done: (non-permanent)");
                xlog_ungrant_log_space(log, ticket);
-               xlog_state_put_ticket(log, ticket);
+               xfs_log_ticket_put(ticket);
        } else {
-               xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
-               xlog_regrant_reserve_log_space(log, ticket);
-       }
+               trace_xfs_log_done_perm(log, ticket);
 
-       /* If this ticket was a permanent reservation and we aren't
-        * trying to release it, reset the inited flags; so next time
-        * we write, a start record will be written out.
-        */
-       if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) &&
-           (flags & XFS_LOG_REL_PERM_RESERV) == 0)
+               xlog_regrant_reserve_log_space(log, ticket);
+               /* If this ticket was a permanent reservation and we aren't
+                * trying to release it, reset the inited flags; so next time
+                * we write, a start record will be written out.
+                */
                ticket->t_flags |= XLOG_TIC_INITED;
+       }
 
        return lsn;
-}      /* xfs_log_done */
-
-
-/*
- * Force the in-core log to disk.  If flags == XFS_LOG_SYNC,
- *     the force is done synchronously.
- *
- * Asynchronous forces are implemented by setting the WANT_SYNC
- * bit in the appropriate in-core log and then returning.
- *
- * Synchronous forces are implemented with a semaphore.  All callers
- * to force a given lsn to disk will wait on a semaphore attached to the
- * specific in-core log.  When given in-core log finally completes its
- * write to disk, that thread will wake up all threads waiting on the
- * semaphore.
- */
-int
-_xfs_log_force(
-       xfs_mount_t     *mp,
-       xfs_lsn_t       lsn,
-       uint            flags,
-       int             *log_flushed)
-{
-       xlog_t          *log = mp->m_log;
-       int             dummy;
-
-       if (!log_flushed)
-               log_flushed = &dummy;
-
-       ASSERT(flags & XFS_LOG_FORCE);
-
-       XFS_STATS_INC(xs_log_force);
-
-       if (log->l_flags & XLOG_IO_ERROR)
-               return XFS_ERROR(EIO);
-       if (lsn == 0)
-               return xlog_state_sync_all(log, flags, log_flushed);
-       else
-               return xlog_state_sync(log, lsn, flags, log_flushed);
-}      /* xfs_log_force */
+}
 
 /*
  * Attaches a new iclog I/O completion callback routine during
@@ -393,16 +290,14 @@ _xfs_log_force(
  * executing the callback at an appropriate time.
  */
 int
-xfs_log_notify(xfs_mount_t       *mp,          /* mount of partition */
-              void               *iclog_hndl,  /* iclog to hang callback off */
-              xfs_log_callback_t *cb)
+xfs_log_notify(
+       struct xfs_mount        *mp,
+       struct xlog_in_core     *iclog,
+       xfs_log_callback_t      *cb)
 {
-       xlog_t *log = mp->m_log;
-       xlog_in_core_t    *iclog = (xlog_in_core_t *)iclog_hndl;
        int     abortflg;
 
-       cb->cb_next = NULL;
-       spin_lock(&log->l_icloglock);
+       spin_lock(&iclog->ic_callback_lock);
        abortflg = (iclog->ic_state & XLOG_STATE_IOERROR);
        if (!abortflg) {
                ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) ||
@@ -411,18 +306,16 @@ xfs_log_notify(xfs_mount_t          *mp,          /* mount of partition */
                *(iclog->ic_callback_tail) = cb;
                iclog->ic_callback_tail = &(cb->cb_next);
        }
-       spin_unlock(&log->l_icloglock);
+       spin_unlock(&iclog->ic_callback_lock);
        return abortflg;
-}      /* xfs_log_notify */
+}
 
 int
-xfs_log_release_iclog(xfs_mount_t *mp,
-                     void        *iclog_hndl)
+xfs_log_release_iclog(
+       struct xfs_mount        *mp,
+       struct xlog_in_core     *iclog)
 {
-       xlog_t *log = mp->m_log;
-       xlog_in_core_t    *iclog = (xlog_in_core_t *)iclog_hndl;
-
-       if (xlog_state_release_iclog(log, iclog)) {
+       if (xlog_state_release_iclog(mp->m_log, iclog)) {
                xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
                return EIO;
        }
@@ -441,17 +334,18 @@ xfs_log_release_iclog(xfs_mount_t *mp,
  * reservation, we prevent over allocation problems.
  */
 int
-xfs_log_reserve(xfs_mount_t     *mp,
-               int              unit_bytes,
-               int              cnt,
-               xfs_log_ticket_t *ticket,
-               __uint8_t        client,
-               uint             flags,
-               uint             t_type)
+xfs_log_reserve(
+       struct xfs_mount        *mp,
+       int                     unit_bytes,
+       int                     cnt,
+       struct xlog_ticket      **ticket,
+       __uint8_t               client,
+       uint                    flags,
+       uint                    t_type)
 {
-       xlog_t          *log = mp->m_log;
-       xlog_ticket_t   *internal_ticket;
-       int             retval = 0;
+       struct log              *log = mp->m_log;
+       struct xlog_ticket      *internal_ticket;
+       int                     retval = 0;
 
        ASSERT(client == XFS_TRANSACTION || client == XFS_LOG);
        ASSERT((flags & XFS_LOG_NOSLEEP) == 0);
@@ -461,22 +355,26 @@ xfs_log_reserve(xfs_mount_t        *mp,
 
        XFS_STATS_INC(xs_try_logspace);
 
+
        if (*ticket != NULL) {
                ASSERT(flags & XFS_LOG_PERM_RESERV);
-               internal_ticket = (xlog_ticket_t *)*ticket;
-               xlog_trace_loggrant(log, internal_ticket, "xfs_log_reserve: existing ticket (permanent trans)");
+               internal_ticket = *ticket;
+
+               trace_xfs_log_reserve(log, internal_ticket);
+
                xlog_grant_push_ail(mp, internal_ticket->t_unit_res);
                retval = xlog_regrant_write_log_space(log, internal_ticket);
        } else {
                /* may sleep if need to allocate more tickets */
-               internal_ticket = xlog_ticket_get(log, unit_bytes, cnt,
+               internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt,
                                                  client, flags);
+               if (!internal_ticket)
+                       return XFS_ERROR(ENOMEM);
                internal_ticket->t_trans_type = t_type;
                *ticket = internal_ticket;
-               xlog_trace_loggrant(log, internal_ticket, 
-                       (internal_ticket->t_flags & XLOG_TIC_PERM_RESERV) ?
-                       "xfs_log_reserve: create new ticket (permanent trans)" :
-                       "xfs_log_reserve: create new ticket");
+
+               trace_xfs_log_reserve(log, internal_ticket);
+
                xlog_grant_push_ail(mp,
                                    (internal_ticket->t_unit_res *
                                     internal_ticket->t_cnt));
@@ -498,11 +396,14 @@ xfs_log_reserve(xfs_mount_t        *mp,
  * Return error or zero.
  */
 int
-xfs_log_mount(xfs_mount_t      *mp,
-             xfs_buftarg_t     *log_target,
-             xfs_daddr_t       blk_offset,
-             int               num_bblks)
+xfs_log_mount(
+       xfs_mount_t     *mp,
+       xfs_buftarg_t   *log_target,
+       xfs_daddr_t     blk_offset,
+       int             num_bblks)
 {
+       int             error;
+
        if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
                cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname);
        else {
@@ -513,13 +414,27 @@ xfs_log_mount(xfs_mount_t *mp,
        }
 
        mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks);
+       if (IS_ERR(mp->m_log)) {
+               error = -PTR_ERR(mp->m_log);
+               goto out;
+       }
+
+       /*
+        * Initialize the AIL now we have a log.
+        */
+       error = xfs_trans_ail_init(mp);
+       if (error) {
+               cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
+               goto out_free_log;
+       }
+       mp->m_log->l_ailp = mp->m_ail;
 
        /*
         * skip log recovery on a norecovery mount.  pretend it all
         * just worked.
         */
        if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) {
-               int             error, readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
+               int     readonly = (mp->m_flags & XFS_MOUNT_RDONLY);
 
                if (readonly)
                        mp->m_flags &= ~XFS_MOUNT_RDONLY;
@@ -530,17 +445,22 @@ xfs_log_mount(xfs_mount_t *mp,
                        mp->m_flags |= XFS_MOUNT_RDONLY;
                if (error) {
                        cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
-                       xlog_dealloc_log(mp->m_log);
-                       return error;
+                       goto out_destroy_ail;
                }
        }
 
        /* Normal transactions can now occur */
        mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
 
-       /* End mounting message in xfs_log_mount_finish */
        return 0;
-}      /* xfs_log_mount */
+
+out_destroy_ail:
+       xfs_trans_ail_destroy(mp);
+out_free_log:
+       xlog_dealloc_log(mp->m_log);
+out:
+       return error;
+}
 
 /*
  * Finish the recovery of the file system.  This is separate from
@@ -551,12 +471,12 @@ xfs_log_mount(xfs_mount_t *mp,
  * mp          - ubiquitous xfs mount point structure
  */
 int
-xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
+xfs_log_mount_finish(xfs_mount_t *mp)
 {
        int     error;
 
        if (!(mp->m_flags & XFS_MOUNT_NORECOVERY))
-               error = xlog_recover_finish(mp->m_log, mfsi_flags);
+               error = xlog_recover_finish(mp->m_log);
        else {
                error = 0;
                ASSERT(mp->m_flags & XFS_MOUNT_RDONLY);
@@ -566,19 +486,6 @@ xfs_log_mount_finish(xfs_mount_t *mp, int mfsi_flags)
 }
 
 /*
- * Unmount processing for the log.
- */
-int
-xfs_log_unmount(xfs_mount_t *mp)
-{
-       int             error;
-
-       error = xfs_log_unmount_write(mp);
-       xfs_log_unmount_dealloc(mp);
-       return error;
-}
-
-/*
  * Final log writes as part of unmount.
  *
  * Mark the filesystem clean as unmount happens.  Note that during relocation
@@ -602,18 +509,10 @@ xfs_log_unmount_write(xfs_mount_t *mp)
 #ifdef DEBUG
        xlog_in_core_t   *first_iclog;
 #endif
-       xfs_log_iovec_t  reg[1];
-       xfs_log_ticket_t tic = NULL;
+       xlog_ticket_t   *tic = NULL;
        xfs_lsn_t        lsn;
        int              error;
 
-       /* the data section must be 32 bit size aligned */
-       struct {
-           __uint16_t magic;
-           __uint16_t pad1;
-           __uint32_t pad2; /* may as well make it 64 bits */
-       } magic = { XLOG_UNMOUNT_TYPE, 0, 0 };
-
        /*
         * Don't write out unmount record on read-only mounts.
         * Or, if we are doing a forced umount (typically because of IO errors).
@@ -621,7 +520,8 @@ xfs_log_unmount_write(xfs_mount_t *mp)
        if (mp->m_flags & XFS_MOUNT_RDONLY)
                return 0;
 
-       xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC);
+       error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
+       ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
 
 #ifdef DEBUG
        first_iclog = iclog = log->l_iclog;
@@ -634,16 +534,30 @@ xfs_log_unmount_write(xfs_mount_t *mp)
        } while (iclog != first_iclog);
 #endif
        if (! (XLOG_FORCED_SHUTDOWN(log))) {
-               reg[0].i_addr = (void*)&magic;
-               reg[0].i_len  = sizeof(magic);
-               XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_UNMOUNT);
-
                error = xfs_log_reserve(mp, 600, 1, &tic,
                                        XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
                if (!error) {
+                       /* the data section must be 32 bit size aligned */
+                       struct {
+                           __uint16_t magic;
+                           __uint16_t pad1;
+                           __uint32_t pad2; /* may as well make it 64 bits */
+                       } magic = {
+                               .magic = XLOG_UNMOUNT_TYPE,
+                       };
+                       struct xfs_log_iovec reg = {
+                               .i_addr = (void *)&magic,
+                               .i_len = sizeof(magic),
+                               .i_type = XLOG_REG_TYPE_UNMOUNT,
+                       };
+                       struct xfs_log_vec vec = {
+                               .lv_niovecs = 1,
+                               .lv_iovecp = &reg,
+                       };
+
                        /* remove inited flag */
-                       ((xlog_ticket_t *)tic)->t_flags = 0;
-                       error = xlog_write(mp, reg, 1, tic, &lsn,
+                       tic->t_flags = 0;
+                       error = xlog_write(log, &vec, tic, &lsn,
                                           NULL, XLOG_UNMOUNT_TRANS);
                        /*
                         * At this point, we're umounting anyway,
@@ -660,16 +574,16 @@ xfs_log_unmount_write(xfs_mount_t *mp)
 
                spin_lock(&log->l_icloglock);
                iclog = log->l_iclog;
-               iclog->ic_refcnt++;
-               spin_unlock(&log->l_icloglock);
+               atomic_inc(&iclog->ic_refcnt);
                xlog_state_want_sync(log, iclog);
-               (void) xlog_state_release_iclog(log, iclog);
+               spin_unlock(&log->l_icloglock);
+               error = xlog_state_release_iclog(log, iclog);
 
                spin_lock(&log->l_icloglock);
                if (!(iclog->ic_state == XLOG_STATE_ACTIVE ||
                      iclog->ic_state == XLOG_STATE_DIRTY)) {
                        if (!XLOG_FORCED_SHUTDOWN(log)) {
-                               sv_wait(&iclog->ic_forcesema, PMEM,
+                               sv_wait(&iclog->ic_force_wait, PMEM,
                                        &log->l_icloglock, s);
                        } else {
                                spin_unlock(&log->l_icloglock);
@@ -678,9 +592,9 @@ xfs_log_unmount_write(xfs_mount_t *mp)
                        spin_unlock(&log->l_icloglock);
                }
                if (tic) {
-                       xlog_trace_loggrant(log, tic, "unmount rec");
+                       trace_xfs_log_umount_write(log, tic);
                        xlog_ungrant_log_space(log, tic);
-                       xlog_state_put_ticket(log, tic);
+                       xfs_log_ticket_put(tic);
                }
        } else {
                /*
@@ -698,11 +612,11 @@ xfs_log_unmount_write(xfs_mount_t *mp)
                 */
                spin_lock(&log->l_icloglock);
                iclog = log->l_iclog;
-               iclog->ic_refcnt++;
-               spin_unlock(&log->l_icloglock);
+               atomic_inc(&iclog->ic_refcnt);
 
                xlog_state_want_sync(log, iclog);
-               (void) xlog_state_release_iclog(log, iclog);
+               spin_unlock(&log->l_icloglock);
+               error =  xlog_state_release_iclog(log, iclog);
 
                spin_lock(&log->l_icloglock);
 
@@ -710,49 +624,73 @@ xfs_log_unmount_write(xfs_mount_t *mp)
                        || iclog->ic_state == XLOG_STATE_DIRTY
                        || iclog->ic_state == XLOG_STATE_IOERROR) ) {
 
-                               sv_wait(&iclog->ic_forcesema, PMEM,
+                               sv_wait(&iclog->ic_force_wait, PMEM,
                                        &log->l_icloglock, s);
                } else {
                        spin_unlock(&log->l_icloglock);
                }
        }
 
-       return 0;
+       return error;
 }      /* xfs_log_unmount_write */
 
 /*
  * Deallocate log structures for unmount/relocation.
+ *
+ * We need to stop the aild from running before we destroy
+ * and deallocate the log as the aild references the log.
  */
 void
-xfs_log_unmount_dealloc(xfs_mount_t *mp)
+xfs_log_unmount(xfs_mount_t *mp)
 {
+       xfs_trans_ail_destroy(mp);
        xlog_dealloc_log(mp->m_log);
 }
 
+void
+xfs_log_item_init(
+       struct xfs_mount        *mp,
+       struct xfs_log_item     *item,
+       int                     type,
+       struct xfs_item_ops     *ops)
+{
+       item->li_mountp = mp;
+       item->li_ailp = mp->m_ail;
+       item->li_type = type;
+       item->li_ops = ops;
+}
+
 /*
  * Write region vectors to log.  The write happens using the space reservation
  * of the ticket (tic).  It is not a requirement that all writes for a given
- * transaction occur with one call to xfs_log_write().
+ * transaction occur with one call to xfs_log_write(). However, it is important
+ * to note that the transaction reservation code makes an assumption about the
+ * number of log headers a transaction requires that may be violated if you
+ * don't pass all the transaction vectors in one call....
  */
 int
-xfs_log_write(xfs_mount_t *    mp,
-             xfs_log_iovec_t   reg[],
-             int               nentries,
-             xfs_log_ticket_t  tic,
-             xfs_lsn_t         *start_lsn)
+xfs_log_write(
+       struct xfs_mount        *mp,
+       struct xfs_log_iovec    reg[],
+       int                     nentries,
+       struct xlog_ticket      *tic,
+       xfs_lsn_t               *start_lsn)
 {
-       int     error;
-       xlog_t *log = mp->m_log;
+       struct log              *log = mp->m_log;
+       int                     error;
+       struct xfs_log_vec      vec = {
+               .lv_niovecs = nentries,
+               .lv_iovecp = reg,
+       };
 
        if (XLOG_FORCED_SHUTDOWN(log))
                return XFS_ERROR(EIO);
 
-       if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
+       error = xlog_write(log, &vec, tic, start_lsn, NULL, 0);
+       if (error)
                xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
-       }
        return error;
-}      /* xfs_log_write */
-
+}
 
 void
 xfs_log_move_tail(xfs_mount_t  *mp,
@@ -796,7 +734,7 @@ xfs_log_move_tail(xfs_mount_t       *mp,
                                break;
                        tail_lsn = 0;
                        free_bytes -= tic->t_unit_res;
-                       sv_signal(&tic->t_sema);
+                       sv_signal(&tic->t_wait);
                        tic = tic->t_next;
                } while (tic != log->l_write_headq);
        }
@@ -817,7 +755,7 @@ xfs_log_move_tail(xfs_mount_t       *mp,
                                break;
                        tail_lsn = 0;
                        free_bytes -= need_bytes;
-                       sv_signal(&tic->t_sema);
+                       sv_signal(&tic->t_wait);
                        tic = tic->t_next;
                } while (tic != log->l_reserve_headq);
        }
@@ -826,31 +764,45 @@ xfs_log_move_tail(xfs_mount_t     *mp,
 
 /*
  * Determine if we have a transaction that has gone to disk
- * that needs to be covered. Log activity needs to be idle (no AIL and
- * nothing in the iclogs). And, we need to be in the right state indicating
- * something has gone out.
+ * that needs to be covered. To begin the transition to the idle state
+ * firstly the log needs to be idle (no AIL and nothing in the iclogs).
+ * If we are then in a state where covering is needed, the caller is informed
+ * that dummy transactions are required to move the log into the idle state.
+ *
+ * Because this is called as part of the sync process, we should also indicate
+ * that dummy transactions should be issued in anything but the covered or
+ * idle states. This ensures that the log tail is accurately reflected in
+ * the log at the end of the sync, hence if a crash occurrs avoids replay
+ * of transactions where the metadata is already on disk.
  */
 int
 xfs_log_need_covered(xfs_mount_t *mp)
 {
-       int             needed = 0, gen;
+       int             needed = 0;
        xlog_t          *log = mp->m_log;
 
        if (!xfs_fs_writable(mp))
                return 0;
 
        spin_lock(&log->l_icloglock);
-       if (((log->l_covered_state == XLOG_STATE_COVER_NEED) ||
-               (log->l_covered_state == XLOG_STATE_COVER_NEED2))
-                       && !xfs_trans_first_ail(mp, &gen)
-                       && xlog_iclogs_empty(log)) {
-               if (log->l_covered_state == XLOG_STATE_COVER_NEED)
-                       log->l_covered_state = XLOG_STATE_COVER_DONE;
-               else {
-                       ASSERT(log->l_covered_state == XLOG_STATE_COVER_NEED2);
-                       log->l_covered_state = XLOG_STATE_COVER_DONE2;
+       switch (log->l_covered_state) {
+       case XLOG_STATE_COVER_DONE:
+       case XLOG_STATE_COVER_DONE2:
+       case XLOG_STATE_COVER_IDLE:
+               break;
+       case XLOG_STATE_COVER_NEED:
+       case XLOG_STATE_COVER_NEED2:
+               if (!xfs_trans_ail_tail(log->l_ailp) &&
+                   xlog_iclogs_empty(log)) {
+                       if (log->l_covered_state == XLOG_STATE_COVER_NEED)
+                               log->l_covered_state = XLOG_STATE_COVER_DONE;
+                       else
+                               log->l_covered_state = XLOG_STATE_COVER_DONE2;
                }
+               /* FALLTHRU */
+       default:
                needed = 1;
+               break;
        }
        spin_unlock(&log->l_icloglock);
        return needed;
@@ -879,7 +831,7 @@ xlog_assign_tail_lsn(xfs_mount_t *mp)
        xfs_lsn_t tail_lsn;
        xlog_t    *log = mp->m_log;
 
-       tail_lsn = xfs_trans_tail_ail(mp);
+       tail_lsn = xfs_trans_ail_tail(mp->m_ail);
        spin_lock(&log->l_grant_lock);
        if (tail_lsn != 0) {
                log->l_tail_lsn = tail_lsn;
@@ -957,25 +909,19 @@ xlog_iodone(xfs_buf_t *bp)
        ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2);
        XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
        aborted = 0;
-
-       /*
-        * Some versions of cpp barf on the recursive definition of
-        * ic_log -> hic_fields.ic_log and expand ic_log twice when
-        * it is passed through two macros.  Workaround broken cpp.
-        */
        l = iclog->ic_log;
 
        /*
-        * If the ordered flag has been removed by a lower
-        * layer, it means the underlyin device no longer supports
+        * If the _XFS_BARRIER_FAILED flag was set by a lower
+        * layer, it means the underlying device no longer supports
         * barrier I/O. Warn loudly and turn off barriers.
         */
-       if ((l->l_mp->m_flags & XFS_MOUNT_BARRIER) && !XFS_BUF_ORDERED(bp)) {
+       if (bp->b_flags & _XFS_BARRIER_FAILED) {
+               bp->b_flags &= ~_XFS_BARRIER_FAILED;
                l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER;
                xfs_fs_cmn_err(CE_WARN, l->l_mp,
                                "xlog_iodone: Barriers are no longer supported"
                                " by device. Disabling barriers\n");
-               xfs_buftrace("XLOG_IODONE BARRIERS OFF", bp);
        }
 
        /*
@@ -1008,41 +954,9 @@ xlog_iodone(xfs_buf_t *bp)
 }      /* xlog_iodone */
 
 /*
- * The bdstrat callback function for log bufs. This gives us a central
- * place to trap bufs in case we get hit by a log I/O error and need to
- * shutdown. Actually, in practice, even when we didn't get a log error,
- * we transition the iclogs to IOERROR state *after* flushing all existing
- * iclogs to disk. This is because we don't want anymore new transactions to be
- * started or completed afterwards.
- */
-STATIC int
-xlog_bdstrat_cb(struct xfs_buf *bp)
-{
-       xlog_in_core_t *iclog;
-
-       iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
-
-       if ((iclog->ic_state & XLOG_STATE_IOERROR) == 0) {
-         /* note for irix bstrat will need  struct bdevsw passed
-          * Fix the following macro if the code ever is merged
-          */
-           XFS_bdstrat(bp);
-               return 0;
-       }
-
-       xfs_buftrace("XLOG__BDSTRAT IOERROR", bp);
-       XFS_BUF_ERROR(bp, EIO);
-       XFS_BUF_STALE(bp);
-       xfs_biodone(bp);
-       return XFS_ERROR(EIO);
-
-
-}
-
-/*
  * Return size of each in-core log record buffer.
  *
- * All machines get 8 x 32KB buffers by default, unless tuned otherwise.
+ * All machines get 8 x 32kB buffers by default, unless tuned otherwise.
  *
  * If the filesystem blocksize is too large, we may need to choose a
  * larger size since the directory code currently logs entire blocks.
@@ -1071,9 +985,9 @@ xlog_get_iclog_buffer_size(xfs_mount_t     *mp,
                        size >>= 1;
                }
 
-               if (XFS_SB_VERSION_HASLOGV2(&mp->m_sb)) {
-                       /* # headers = size / 32K
-                        * one header holds cycles from 32K of data
+               if (xfs_sb_version_haslogv2(&mp->m_sb)) {
+                       /* # headers = size / 32k
+                        * one header holds cycles from 32k of data
                         */
 
                        xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
@@ -1089,7 +1003,7 @@ xlog_get_iclog_buffer_size(xfs_mount_t    *mp,
                goto done;
        }
 
-       /* All machines use 32KB buffers by default. */
+       /* All machines use 32kB buffers by default. */
        log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
        log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
 
@@ -1097,32 +1011,8 @@ xlog_get_iclog_buffer_size(xfs_mount_t   *mp,
        log->l_iclog_hsize = BBSIZE;
        log->l_iclog_heads = 1;
 
-       /*
-        * For 16KB, we use 3 32KB buffers.  For 32KB block sizes, we use
-        * 4 32KB buffers.  For 64KB block sizes, we use 8 32KB buffers.
-        */
-       if (mp->m_sb.sb_blocksize >= 16*1024) {
-               log->l_iclog_size = XLOG_BIG_RECORD_BSIZE;
-               log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT;
-               if (mp->m_logbufs <= 0) {
-                       switch (mp->m_sb.sb_blocksize) {
-                           case 16*1024:                       /* 16 KB */
-                               log->l_iclog_bufs = 3;
-                               break;
-                           case 32*1024:                       /* 32 KB */
-                               log->l_iclog_bufs = 4;
-                               break;
-                           case 64*1024:                       /* 64 KB */
-                               log->l_iclog_bufs = 8;
-                               break;
-                           default:
-                               xlog_panic("XFS: Invalid blocksize");
-                               break;
-                       }
-               }
-       }
-
-done:  /* are we being asked to make the sizes selected above visible? */
+done:
+       /* are we being asked to make the sizes selected above visible? */
        if (mp->m_logbufs == 0)
                mp->m_logbufs = log->l_iclog_bufs;
        if (mp->m_logbsize == 0)
@@ -1148,8 +1038,13 @@ xlog_alloc_log(xfs_mount_t       *mp,
        xfs_buf_t               *bp;
        int                     i;
        int                     iclogsize;
+       int                     error = ENOMEM;
 
-       log = (xlog_t *)kmem_zalloc(sizeof(xlog_t), KM_SLEEP);
+       log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL);
+       if (!log) {
+               xlog_warn("XFS: Log allocation failed: No memory!");
+               goto out;
+       }
 
        log->l_mp          = mp;
        log->l_targ        = log_target;
@@ -1167,22 +1062,39 @@ xlog_alloc_log(xfs_mount_t      *mp,
        log->l_grant_reserve_cycle = 1;
        log->l_grant_write_cycle = 1;
 
-       if (XFS_SB_VERSION_HASSECTOR(&mp->m_sb)) {
+       error = EFSCORRUPTED;
+       if (xfs_sb_version_hassector(&mp->m_sb)) {
                log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT;
-               ASSERT(log->l_sectbb_log <= mp->m_sectbb_log);
+               if (log->l_sectbb_log < 0 ||
+                   log->l_sectbb_log > mp->m_sectbb_log) {
+                       xlog_warn("XFS: Log sector size (0x%x) out of range.",
+                                               log->l_sectbb_log);
+                       goto out_free_log;
+               }
+
                /* for larger sector sizes, must have v2 or external log */
-               ASSERT(log->l_sectbb_log == 0 ||
-                       log->l_logBBstart == 0 ||
-                       XFS_SB_VERSION_HASLOGV2(&mp->m_sb));
-               ASSERT(mp->m_sb.sb_logsectlog >= BBSHIFT);
+               if (log->l_sectbb_log != 0 &&
+                   (log->l_logBBstart != 0 &&
+                    !xfs_sb_version_haslogv2(&mp->m_sb))) {
+                       xlog_warn("XFS: log sector size (0x%x) invalid "
+                                 "for configuration.", log->l_sectbb_log);
+                       goto out_free_log;
+               }
+               if (mp->m_sb.sb_logsectlog < BBSHIFT) {
+                       xlog_warn("XFS: Log sector log (0x%x) too small.",
+                                               mp->m_sb.sb_logsectlog);
+                       goto out_free_log;
+               }
        }
        log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1;
 
        xlog_get_iclog_buffer_size(mp, log);
 
+       error = ENOMEM;
        bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp);
+       if (!bp)
+               goto out_free_log;
        XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
-       XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
        XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
        ASSERT(XFS_BUF_ISBUSY(bp));
        ASSERT(XFS_BUF_VALUSEMA(bp) <= 0);
@@ -1190,8 +1102,7 @@ xlog_alloc_log(xfs_mount_t        *mp,
 
        spin_lock_init(&log->l_icloglock);
        spin_lock_init(&log->l_grant_lock);
-       initnsema(&log->l_flushsema, 0, "ic-flush");
-       xlog_state_ticket_alloc(log);  /* wait until after icloglock inited */
+       sv_init(&log->l_flush_wait, 0, "flush_wait");
 
        /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
        ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
@@ -1207,44 +1118,48 @@ xlog_alloc_log(xfs_mount_t      *mp,
        iclogsize = log->l_iclog_size;
        ASSERT(log->l_iclog_size >= 4096);
        for (i=0; i < log->l_iclog_bufs; i++) {
-               *iclogp = (xlog_in_core_t *)
-                         kmem_zalloc(sizeof(xlog_in_core_t), KM_SLEEP);
+               *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL);
+               if (!*iclogp)
+                       goto out_free_iclog;
+
                iclog = *iclogp;
                iclog->ic_prev = prev_iclog;
                prev_iclog = iclog;
 
                bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp);
+               if (!bp)
+                       goto out_free_iclog;
                if (!XFS_BUF_CPSEMA(bp))
                        ASSERT(0);
                XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone);
-               XFS_BUF_SET_BDSTRAT_FUNC(bp, xlog_bdstrat_cb);
                XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1);
                iclog->ic_bp = bp;
-               iclog->hic_data = bp->b_addr;
-
+               iclog->ic_data = bp->b_addr;
+#ifdef DEBUG
                log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header);
-
+#endif
                head = &iclog->ic_header;
                memset(head, 0, sizeof(xlog_rec_header_t));
                head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
                head->h_version = cpu_to_be32(
-                       XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) ? 2 : 1);
+                       xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
                head->h_size = cpu_to_be32(log->l_iclog_size);
                /* new fields */
                head->h_fmt = cpu_to_be32(XLOG_FMT);
                memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t));
 
-
                iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize;
                iclog->ic_state = XLOG_STATE_ACTIVE;
                iclog->ic_log = log;
+               atomic_set(&iclog->ic_refcnt, 0);
+               spin_lock_init(&iclog->ic_callback_lock);
                iclog->ic_callback_tail = &(iclog->ic_callback);
-               iclog->ic_datap = (char *)iclog->hic_data + log->l_iclog_hsize;
+               iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize;
 
                ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp));
                ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0);
-               sv_init(&iclog->ic_forcesema, SV_DEFAULT, "iclog-force");
-               sv_init(&iclog->ic_writesema, SV_DEFAULT, "iclog-write");
+               sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force");
+               sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
 
                iclogp = &iclog->ic_next;
        }
@@ -1252,6 +1167,24 @@ xlog_alloc_log(xfs_mount_t       *mp,
        log->l_iclog->ic_prev = prev_iclog;     /* re-write 1st prev ptr */
 
        return log;
+
+out_free_iclog:
+       for (iclog = log->l_iclog; iclog; iclog = prev_iclog) {
+               prev_iclog = iclog->ic_next;
+               if (iclog->ic_bp) {
+                       sv_destroy(&iclog->ic_force_wait);
+                       sv_destroy(&iclog->ic_write_wait);
+                       xfs_buf_free(iclog->ic_bp);
+               }
+               kmem_free(iclog);
+       }
+       spinlock_destroy(&log->l_icloglock);
+       spinlock_destroy(&log->l_grant_lock);
+       xfs_buf_free(log->l_xbuf);
+out_free_log:
+       kmem_free(log);
+out:
+       return ERR_PTR(-error);
 }      /* xlog_alloc_log */
 
 
@@ -1260,26 +1193,31 @@ xlog_alloc_log(xfs_mount_t      *mp,
  * ticket.  Return the lsn of the commit record.
  */
 STATIC int
-xlog_commit_record(xfs_mount_t  *mp,
-                  xlog_ticket_t *ticket,
-                  xlog_in_core_t **iclog,
-                  xfs_lsn_t    *commitlsnp)
+xlog_commit_record(
+       struct log              *log,
+       struct xlog_ticket      *ticket,
+       struct xlog_in_core     **iclog,
+       xfs_lsn_t               *commitlsnp)
 {
-       int             error;
-       xfs_log_iovec_t reg[1];
-
-       reg[0].i_addr = NULL;
-       reg[0].i_len = 0;
-       XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
+       struct xfs_mount *mp = log->l_mp;
+       int     error;
+       struct xfs_log_iovec reg = {
+               .i_addr = NULL,
+               .i_len = 0,
+               .i_type = XLOG_REG_TYPE_COMMIT,
+       };
+       struct xfs_log_vec vec = {
+               .lv_niovecs = 1,
+               .lv_iovecp = &reg,
+       };
 
        ASSERT_ALWAYS(iclog);
-       if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
-                              iclog, XLOG_COMMIT_TRANS))) {
+       error = xlog_write(log, &vec, ticket, commitlsnp, iclog,
+                                       XLOG_COMMIT_TRANS);
+       if (error)
                xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR);
-       }
        return error;
-}      /* xlog_commit_record */
-
+}
 
 /*
  * Push on the buffer cache code if we ever use more than 75% of the on-disk
@@ -1342,9 +1280,40 @@ xlog_grant_push_ail(xfs_mount_t  *mp,
      */
     if (threshold_lsn &&
        !XLOG_FORCED_SHUTDOWN(log))
-           xfs_trans_push_ail(mp, threshold_lsn);
+           xfs_trans_ail_push(log->l_ailp, threshold_lsn);
 }      /* xlog_grant_push_ail */
 
+/*
+ * The bdstrat callback function for log bufs. This gives us a central
+ * place to trap bufs in case we get hit by a log I/O error and need to
+ * shutdown. Actually, in practice, even when we didn't get a log error,
+ * we transition the iclogs to IOERROR state *after* flushing all existing
+ * iclogs to disk. This is because we don't want anymore new transactions to be
+ * started or completed afterwards.
+ */
+STATIC int
+xlog_bdstrat(
+       struct xfs_buf          *bp)
+{
+       struct xlog_in_core     *iclog;
+
+       iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *);
+       if (iclog->ic_state & XLOG_STATE_IOERROR) {
+               XFS_BUF_ERROR(bp, EIO);
+               XFS_BUF_STALE(bp);
+               xfs_biodone(bp);
+               /*
+                * It would seem logical to return EIO here, but we rely on
+                * the log state machine to propagate I/O errors instead of
+                * doing it here.
+                */
+               return 0;
+       }
+
+       bp->b_flags |= _XBF_RUN_QUEUES;
+       xfs_buf_iorequest(bp);
+       return 0;
+}
 
 /*
  * Flush out the in-core log (iclog) to the on-disk log in an asynchronous 
@@ -1383,10 +1352,10 @@ xlog_sync(xlog_t                *log,
        int             roundoff;       /* roundoff to BB or stripe */
        int             split = 0;      /* split write into two regions */
        int             error;
-       int             v2 = XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb);
+       int             v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb);
 
        XFS_STATS_INC(xs_log_writes);
-       ASSERT(iclog->ic_refcnt == 0);
+       ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
 
        /* Add for LR header */
        count_init = log->l_iclog_hsize + iclog->ic_offset;
@@ -1443,6 +1412,7 @@ xlog_sync(xlog_t          *log,
        XFS_BUF_ZEROFLAGS(bp);
        XFS_BUF_BUSY(bp);
        XFS_BUF_ASYNC(bp);
+       bp->b_flags |= XBF_LOG_BUFFER;
        /*
         * Do an ordered write for the log block.
         * Its unnecessary to flush the first split block in the log wrap case.
@@ -1463,7 +1433,7 @@ xlog_sync(xlog_t          *log,
         */
        XFS_BUF_WRITE(bp);
 
-       if ((error = XFS_bwrite(bp))) {
+       if ((error = xlog_bdstrat(bp))) {
                xfs_ioerror_alert("xlog_sync", log->l_mp, bp,
                                  XFS_BUF_ADDR(bp));
                return error;
@@ -1480,6 +1450,7 @@ xlog_sync(xlog_t          *log,
                XFS_BUF_ZEROFLAGS(bp);
                XFS_BUF_BUSY(bp);
                XFS_BUF_ASYNC(bp);
+               bp->b_flags |= XBF_LOG_BUFFER;
                if (log->l_mp->m_flags & XFS_MOUNT_BARRIER)
                        XFS_BUF_ORDERED(bp);
                dptr = XFS_BUF_PTR(bp);
@@ -1490,9 +1461,9 @@ xlog_sync(xlog_t          *log,
                 * case, though.
                 */
                for (i = 0; i < split; i += BBSIZE) {
-                       be32_add((__be32 *)dptr, 1);
+                       be32_add_cpu((__be32 *)dptr, 1);
                        if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM)
-                               be32_add((__be32 *)dptr, 1);
+                               be32_add_cpu((__be32 *)dptr, 1);
                        dptr += BBSIZE;
                }
 
@@ -1502,7 +1473,7 @@ xlog_sync(xlog_t          *log,
                /* account for internal log which doesn't start at block #0 */
                XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart);
                XFS_BUF_WRITE(bp);
-               if ((error = XFS_bwrite(bp))) {
+               if ((error = xlog_bdstrat(bp))) {
                        xfs_ioerror_alert("xlog_sync (split)", log->l_mp,
                                          bp, XFS_BUF_ADDR(bp));
                        return error;
@@ -1519,54 +1490,23 @@ STATIC void
 xlog_dealloc_log(xlog_t *log)
 {
        xlog_in_core_t  *iclog, *next_iclog;
-       xlog_ticket_t   *tic, *next_tic;
        int             i;
 
        iclog = log->l_iclog;
        for (i=0; i<log->l_iclog_bufs; i++) {
-               sv_destroy(&iclog->ic_forcesema);
-               sv_destroy(&iclog->ic_writesema);
+               sv_destroy(&iclog->ic_force_wait);
+               sv_destroy(&iclog->ic_write_wait);
                xfs_buf_free(iclog->ic_bp);
-#ifdef XFS_LOG_TRACE
-               if (iclog->ic_trace != NULL) {
-                       ktrace_free(iclog->ic_trace);
-               }
-#endif
                next_iclog = iclog->ic_next;
-               kmem_free(iclog, sizeof(xlog_in_core_t));
+               kmem_free(iclog);
                iclog = next_iclog;
        }
-       freesema(&log->l_flushsema);
        spinlock_destroy(&log->l_icloglock);
        spinlock_destroy(&log->l_grant_lock);
 
-       /* XXXsup take a look at this again. */
-       if ((log->l_ticket_cnt != log->l_ticket_tcnt)  &&
-           !XLOG_FORCED_SHUTDOWN(log)) {
-               xfs_fs_cmn_err(CE_WARN, log->l_mp,
-                       "xlog_dealloc_log: (cnt: %d, total: %d)",
-                       log->l_ticket_cnt, log->l_ticket_tcnt);
-               /* ASSERT(log->l_ticket_cnt == log->l_ticket_tcnt); */
-
-       } else {
-               tic = log->l_unmount_free;
-               while (tic) {
-                       next_tic = tic->t_next;
-                       kmem_free(tic, NBPP);
-                       tic = next_tic;
-               }
-       }
        xfs_buf_free(log->l_xbuf);
-#ifdef XFS_LOG_TRACE
-       if (log->l_trace != NULL) {
-               ktrace_free(log->l_trace);
-       }
-       if (log->l_grant_trace != NULL) {
-               ktrace_free(log->l_grant_trace);
-       }
-#endif
        log->l_mp->m_log = NULL;
-       kmem_free(log, sizeof(xlog_t));
+       kmem_free(log);
 }      /* xlog_dealloc_log */
 
 /*
@@ -1581,7 +1521,7 @@ xlog_state_finish_copy(xlog_t             *log,
 {
        spin_lock(&log->l_icloglock);
 
-       be32_add(&iclog->ic_header.h_num_logops, record_cnt);
+       be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt);
        iclog->ic_offset += copy_bytes;
 
        spin_unlock(&log->l_icloglock);
@@ -1698,6 +1638,192 @@ xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
 }
 
 /*
+ * Calculate the potential space needed by the log vector.  Each region gets
+ * its own xlog_op_header_t and may need to be double word aligned.
+ */
+static int
+xlog_write_calc_vec_length(
+       struct xlog_ticket      *ticket,
+       struct xfs_log_vec      *log_vector)
+{
+       struct xfs_log_vec      *lv;
+       int                     headers = 0;
+       int                     len = 0;
+       int                     i;
+
+       /* acct for start rec of xact */
+       if (ticket->t_flags & XLOG_TIC_INITED)
+               headers++;
+
+       for (lv = log_vector; lv; lv = lv->lv_next) {
+               headers += lv->lv_niovecs;
+
+               for (i = 0; i < lv->lv_niovecs; i++) {
+                       struct xfs_log_iovec    *vecp = &lv->lv_iovecp[i];
+
+                       len += vecp->i_len;
+                       xlog_tic_add_region(ticket, vecp->i_len, vecp->i_type);
+               }
+       }
+
+       ticket->t_res_num_ophdrs += headers;
+       len += headers * sizeof(struct xlog_op_header);
+
+       return len;
+}
+
+/*
+ * If first write for transaction, insert start record  We can't be trying to
+ * commit if we are inited.  We can't have any "partial_copy" if we are inited.
+ */
+static int
+xlog_write_start_rec(
+       struct xlog_op_header   *ophdr,
+       struct xlog_ticket      *ticket)
+{
+       if (!(ticket->t_flags & XLOG_TIC_INITED))
+               return 0;
+
+       ophdr->oh_tid   = cpu_to_be32(ticket->t_tid);
+       ophdr->oh_clientid = ticket->t_clientid;
+       ophdr->oh_len = 0;
+       ophdr->oh_flags = XLOG_START_TRANS;
+       ophdr->oh_res2 = 0;
+
+       ticket->t_flags &= ~XLOG_TIC_INITED;
+
+       return sizeof(struct xlog_op_header);
+}
+
+static xlog_op_header_t *
+xlog_write_setup_ophdr(
+       struct log              *log,
+       struct xlog_op_header   *ophdr,
+       struct xlog_ticket      *ticket,
+       uint                    flags)
+{
+       ophdr->oh_tid = cpu_to_be32(ticket->t_tid);
+       ophdr->oh_clientid = ticket->t_clientid;
+       ophdr->oh_res2 = 0;
+
+       /* are we copying a commit or unmount record? */
+       ophdr->oh_flags = flags;
+
+       /*
+        * We've seen logs corrupted with bad transaction client ids.  This
+        * makes sure that XFS doesn't generate them on.  Turn this into an EIO
+        * and shut down the filesystem.
+        */
+       switch (ophdr->oh_clientid)  {
+       case XFS_TRANSACTION:
+       case XFS_VOLUME:
+       case XFS_LOG:
+               break;
+       default:
+               xfs_fs_cmn_err(CE_WARN, log->l_mp,
+                       "Bad XFS transaction clientid 0x%x in ticket 0x%p",
+                       ophdr->oh_clientid, ticket);
+               return NULL;
+       }
+
+       return ophdr;
+}
+
+/*
+ * Set up the parameters of the region copy into the log. This has
+ * to handle region write split across multiple log buffers - this
+ * state is kept external to this function so that this code can
+ * can be written in an obvious, self documenting manner.
+ */
+static int
+xlog_write_setup_copy(
+       struct xlog_ticket      *ticket,
+       struct xlog_op_header   *ophdr,
+       int                     space_available,
+       int                     space_required,
+       int                     *copy_off,
+       int                     *copy_len,
+       int                     *last_was_partial_copy,
+       int                     *bytes_consumed)
+{
+       int                     still_to_copy;
+
+       still_to_copy = space_required - *bytes_consumed;
+       *copy_off = *bytes_consumed;
+
+       if (still_to_copy <= space_available) {
+               /* write of region completes here */
+               *copy_len = still_to_copy;
+               ophdr->oh_len = cpu_to_be32(*copy_len);
+               if (*last_was_partial_copy)
+                       ophdr->oh_flags |= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
+               *last_was_partial_copy = 0;
+               *bytes_consumed = 0;
+               return 0;
+       }
+
+       /* partial write of region, needs extra log op header reservation */
+       *copy_len = space_available;
+       ophdr->oh_len = cpu_to_be32(*copy_len);
+       ophdr->oh_flags |= XLOG_CONTINUE_TRANS;
+       if (*last_was_partial_copy)
+               ophdr->oh_flags |= XLOG_WAS_CONT_TRANS;
+       *bytes_consumed += *copy_len;
+       (*last_was_partial_copy)++;
+
+       /* account for new log op header */
+       ticket->t_curr_res -= sizeof(struct xlog_op_header);
+       ticket->t_res_num_ophdrs++;
+
+       return sizeof(struct xlog_op_header);
+}
+
+static int
+xlog_write_copy_finish(
+       struct log              *log,
+       struct xlog_in_core     *iclog,
+       uint                    flags,
+       int                     *record_cnt,
+       int                     *data_cnt,
+       int                     *partial_copy,
+       int                     *partial_copy_len,
+       int                     log_offset,
+       struct xlog_in_core     **commit_iclog)
+{
+       if (*partial_copy) {
+               /*
+                * This iclog has already been marked WANT_SYNC by
+                * xlog_state_get_iclog_space.
+                */
+               xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
+               *record_cnt = 0;
+               *data_cnt = 0;
+               return xlog_state_release_iclog(log, iclog);
+       }
+
+       *partial_copy = 0;
+       *partial_copy_len = 0;
+
+       if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
+               /* no more space in this iclog - push it. */
+               xlog_state_finish_copy(log, iclog, *record_cnt, *data_cnt);
+               *record_cnt = 0;
+               *data_cnt = 0;
+
+               spin_lock(&log->l_icloglock);
+               xlog_state_want_sync(log, iclog);
+               spin_unlock(&log->l_icloglock);
+
+               if (!commit_iclog)
+                       return xlog_state_release_iclog(log, iclog);
+               ASSERT(flags & XLOG_COMMIT_TRANS);
+               *commit_iclog = iclog;
+       }
+
+       return 0;
+}
+
+/*
  * Write some region out to in-core log
  *
  * This will be called when writing externally provided regions or when
@@ -1738,208 +1864,158 @@ xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket)
  *     bytes have been written out.
  */
 STATIC int
-xlog_write(xfs_mount_t *       mp,
-          xfs_log_iovec_t      reg[],
-          int                  nentries,
-          xfs_log_ticket_t     tic,
-          xfs_lsn_t            *start_lsn,
-          xlog_in_core_t       **commit_iclog,
-          uint                 flags)
-{
-    xlog_t          *log = mp->m_log;
-    xlog_ticket_t    *ticket = (xlog_ticket_t *)tic;
-    xlog_in_core_t   *iclog = NULL;  /* ptr to current in-core log */
-    xlog_op_header_t *logop_head;    /* ptr to log operation header */
-    __psint_t       ptr;            /* copy address into data region */
-    int                     len;            /* # xlog_write() bytes 2 still copy */
-    int                     index;          /* region index currently copying */
-    int                     log_offset;     /* offset (from 0) into data region */
-    int                     start_rec_copy; /* # bytes to copy for start record */
-    int                     partial_copy;   /* did we split a region? */
-    int                     partial_copy_len;/* # bytes copied if split region */
-    int                     need_copy;      /* # bytes need to memcpy this region */
-    int                     copy_len;       /* # bytes actually memcpy'ing */
-    int                     copy_off;       /* # bytes from entry start */
-    int                     contwr;         /* continued write of in-core log? */
-    int                     error;
-    int                     record_cnt = 0, data_cnt = 0;
-
-    partial_copy_len = partial_copy = 0;
-
-    /* Calculate potential maximum space.  Each region gets its own
-     * xlog_op_header_t and may need to be double word aligned.
-     */
-    len = 0;
-    if (ticket->t_flags & XLOG_TIC_INITED) {    /* acct for start rec of xact */
-       len += sizeof(xlog_op_header_t);
-       ticket->t_res_num_ophdrs++;
-    }
-
-    for (index = 0; index < nentries; index++) {
-       len += sizeof(xlog_op_header_t);            /* each region gets >= 1 */
-       ticket->t_res_num_ophdrs++;
-       len += reg[index].i_len;
-       xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type);
-    }
-    contwr = *start_lsn = 0;
-
-    if (ticket->t_curr_res < len) {
-       xlog_print_tic_res(mp, ticket);
+xlog_write(
+       struct log              *log,
+       struct xfs_log_vec      *log_vector,
+       struct xlog_ticket      *ticket,
+       xfs_lsn_t               *start_lsn,
+       struct xlog_in_core     **commit_iclog,
+       uint                    flags)
+{
+       struct xlog_in_core     *iclog = NULL;
+       struct xfs_log_iovec    *vecp;
+       struct xfs_log_vec      *lv;
+       int                     len;
+       int                     index;
+       int                     partial_copy = 0;
+       int                     partial_copy_len = 0;
+       int                     contwr = 0;
+       int                     record_cnt = 0;
+       int                     data_cnt = 0;
+       int                     error;
+
+       *start_lsn = 0;
+
+       len = xlog_write_calc_vec_length(ticket, log_vector);
+       if (ticket->t_curr_res < len) {
+               xlog_print_tic_res(log->l_mp, ticket);
 #ifdef DEBUG
-       xlog_panic(
-               "xfs_log_write: reservation ran out. Need to up reservation");
+               xlog_panic(
+       "xfs_log_write: reservation ran out. Need to up reservation");
 #else
-       /* Customer configurable panic */
-       xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp,
-               "xfs_log_write: reservation ran out. Need to up reservation");
-       /* If we did not panic, shutdown the filesystem */
-       xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
+               /* Customer configurable panic */
+               xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, log->l_mp,
+       "xfs_log_write: reservation ran out. Need to up reservation");
+
+               /* If we did not panic, shutdown the filesystem */
+               xfs_force_shutdown(log->l_mp, SHUTDOWN_CORRUPT_INCORE);
 #endif
-    } else
-       ticket->t_curr_res -= len;
+       }
 
-    for (index = 0; index < nentries; ) {
-       if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
-                                              &contwr, &log_offset)))
-               return error;
+       ticket->t_curr_res -= len;
 
-       ASSERT(log_offset <= iclog->ic_size - 1);
-       ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset);
+       index = 0;
+       lv = log_vector;
+       vecp = lv->lv_iovecp;
+       while (lv && index < lv->lv_niovecs) {
+               void            *ptr;
+               int             log_offset;
 
-       /* start_lsn is the first lsn written to. That's all we need. */
-       if (! *start_lsn)
-           *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
+               error = xlog_state_get_iclog_space(log, len, &iclog, ticket,
+                                                  &contwr, &log_offset);
+               if (error)
+                       return error;
 
-       /* This loop writes out as many regions as can fit in the amount
-        * of space which was allocated by xlog_state_get_iclog_space().
-        */
-       while (index < nentries) {
-           ASSERT(reg[index].i_len % sizeof(__int32_t) == 0);
-           ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0);
-           start_rec_copy = 0;
-
-           /* If first write for transaction, insert start record.
-            * We can't be trying to commit if we are inited.  We can't
-            * have any "partial_copy" if we are inited.
-            */
-           if (ticket->t_flags & XLOG_TIC_INITED) {
-               logop_head              = (xlog_op_header_t *)ptr;
-               logop_head->oh_tid      = cpu_to_be32(ticket->t_tid);
-               logop_head->oh_clientid = ticket->t_clientid;
-               logop_head->oh_len      = 0;
-               logop_head->oh_flags    = XLOG_START_TRANS;
-               logop_head->oh_res2     = 0;
-               ticket->t_flags         &= ~XLOG_TIC_INITED;    /* clear bit */
-               record_cnt++;
-
-               start_rec_copy = sizeof(xlog_op_header_t);
-               xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy);
-           }
+               ASSERT(log_offset <= iclog->ic_size - 1);
+               ptr = iclog->ic_datap + log_offset;
 
-           /* Copy log operation header directly into data section */
-           logop_head                  = (xlog_op_header_t *)ptr;
-           logop_head->oh_tid          = cpu_to_be32(ticket->t_tid);
-           logop_head->oh_clientid     = ticket->t_clientid;
-           logop_head->oh_res2         = 0;
+               /* start_lsn is the first lsn written to. That's all we need. */
+               if (!*start_lsn)
+                       *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn);
 
-           /* header copied directly */
-           xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t));
+               /*
+                * This loop writes out as many regions as can fit in the amount
+                * of space which was allocated by xlog_state_get_iclog_space().
+                */
+               while (lv && index < lv->lv_niovecs) {
+                       struct xfs_log_iovec    *reg = &vecp[index];
+                       struct xlog_op_header   *ophdr;
+                       int                     start_rec_copy;
+                       int                     copy_len;
+                       int                     copy_off;
+
+                       ASSERT(reg->i_len % sizeof(__int32_t) == 0);
+                       ASSERT((unsigned long)ptr % sizeof(__int32_t) == 0);
+
+                       start_rec_copy = xlog_write_start_rec(ptr, ticket);
+                       if (start_rec_copy) {
+                               record_cnt++;
+                               xlog_write_adv_cnt(&ptr, &len, &log_offset,
+                                                  start_rec_copy);
+                       }
 
-           /* are we copying a commit or unmount record? */
-           logop_head->oh_flags = flags;
+                       ophdr = xlog_write_setup_ophdr(log, ptr, ticket, flags);
+                       if (!ophdr)
+                               return XFS_ERROR(EIO);
 
-           /*
-            * We've seen logs corrupted with bad transaction client
-            * ids.  This makes sure that XFS doesn't generate them on.
-            * Turn this into an EIO and shut down the filesystem.
-            */
-           switch (logop_head->oh_clientid)  {
-           case XFS_TRANSACTION:
-           case XFS_VOLUME:
-           case XFS_LOG:
-               break;
-           default:
-               xfs_fs_cmn_err(CE_WARN, mp,
-                   "Bad XFS transaction clientid 0x%x in ticket 0x%p",
-                   logop_head->oh_clientid, tic);
-               return XFS_ERROR(EIO);
-           }
+                       xlog_write_adv_cnt(&ptr, &len, &log_offset,
+                                          sizeof(struct xlog_op_header));
+
+                       len += xlog_write_setup_copy(ticket, ophdr,
+                                                    iclog->ic_size-log_offset,
+                                                    reg->i_len,
+                                                    &copy_off, &copy_len,
+                                                    &partial_copy,
+                                                    &partial_copy_len);
+                       xlog_verify_dest_ptr(log, ptr);
+
+                       /* copy region */
+                       ASSERT(copy_len >= 0);
+                       memcpy(ptr, reg->i_addr + copy_off, copy_len);
+                       xlog_write_adv_cnt(&ptr, &len, &log_offset, copy_len);
+
+                       copy_len += start_rec_copy + sizeof(xlog_op_header_t);
+                       record_cnt++;
+                       data_cnt += contwr ? copy_len : 0;
+
+                       error = xlog_write_copy_finish(log, iclog, flags,
+                                                      &record_cnt, &data_cnt,
+                                                      &partial_copy,
+                                                      &partial_copy_len,
+                                                      log_offset,
+                                                      commit_iclog);
+                       if (error)
+                               return error;
 
-           /* Partial write last time? => (partial_copy != 0)
-            * need_copy is the amount we'd like to copy if everything could
-            * fit in the current memcpy.
-            */
-           need_copy = reg[index].i_len - partial_copy_len;
-
-           copy_off = partial_copy_len;
-           if (need_copy <= iclog->ic_size - log_offset) { /*complete write */
-               copy_len = need_copy;
-               logop_head->oh_len = cpu_to_be32(copy_len);
-               if (partial_copy)
-                   logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS);
-               partial_copy_len = partial_copy = 0;
-           } else {                                        /* partial write */
-               copy_len = iclog->ic_size - log_offset;
-               logop_head->oh_len = cpu_to_be32(copy_len);
-               logop_head->oh_flags |= XLOG_CONTINUE_TRANS;
-               if (partial_copy)
-                       logop_head->oh_flags |= XLOG_WAS_CONT_TRANS;
-               partial_copy_len += copy_len;
-               partial_copy++;
-               len += sizeof(xlog_op_header_t); /* from splitting of region */
-               /* account for new log op header */
-               ticket->t_curr_res -= sizeof(xlog_op_header_t);
-               ticket->t_res_num_ophdrs++;
-           }
-           xlog_verify_dest_ptr(log, ptr);
-
-           /* copy region */
-           ASSERT(copy_len >= 0);
-           memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len);
-           xlog_write_adv_cnt(ptr, len, log_offset, copy_len);
-
-           /* make copy_len total bytes copied, including headers */
-           copy_len += start_rec_copy + sizeof(xlog_op_header_t);
-           record_cnt++;
-           data_cnt += contwr ? copy_len : 0;
-           if (partial_copy) {                 /* copied partial region */
-                   /* already marked WANT_SYNC by xlog_state_get_iclog_space */
-                   xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
-                   record_cnt = data_cnt = 0;
-                   if ((error = xlog_state_release_iclog(log, iclog)))
-                           return error;
-                   break;                      /* don't increment index */
-           } else {                            /* copied entire region */
-               index++;
-               partial_copy_len = partial_copy = 0;
-
-               if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) {
-                   xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
-                   record_cnt = data_cnt = 0;
-                   xlog_state_want_sync(log, iclog);
-                   if (commit_iclog) {
-                       ASSERT(flags & XLOG_COMMIT_TRANS);
-                       *commit_iclog = iclog;
-                   } else if ((error = xlog_state_release_iclog(log, iclog)))
-                          return error;
-                   if (index == nentries)
-                           return 0;           /* we are done */
-                   else
-                           break;
+                       /*
+                        * if we had a partial copy, we need to get more iclog
+                        * space but we don't want to increment the region
+                        * index because there is still more is this region to
+                        * write.
+                        *
+                        * If we completed writing this region, and we flushed
+                        * the iclog (indicated by resetting of the record
+                        * count), then we also need to get more log space. If
+                        * this was the last record, though, we are done and
+                        * can just return.
+                        */
+                       if (partial_copy)
+                               break;
+
+                       if (++index == lv->lv_niovecs) {
+                               lv = lv->lv_next;
+                               index = 0;
+                               if (lv)
+                                       vecp = lv->lv_iovecp;
+                       }
+                       if (record_cnt == 0) {
+                               if (!lv)
+                                       return 0;
+                               break;
+                       }
                }
-           } /* if (partial_copy) */
-       } /* while (index < nentries) */
-    } /* for (index = 0; index < nentries; ) */
-    ASSERT(len == 0);
+       }
+
+       ASSERT(len == 0);
+
+       xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
+       if (!commit_iclog)
+               return xlog_state_release_iclog(log, iclog);
 
-    xlog_state_finish_copy(log, iclog, record_cnt, data_cnt);
-    if (commit_iclog) {
        ASSERT(flags & XLOG_COMMIT_TRANS);
        *commit_iclog = iclog;
        return 0;
-    }
-    return xlog_state_release_iclog(log, iclog);
-}      /* xlog_write */
+}
 
 
 /*****************************************************************************
@@ -1952,7 +2028,7 @@ xlog_write(xfs_mount_t *  mp,
 /* Clean iclogs starting from the head.  This ordering must be
  * maintained, so an iclog doesn't become ACTIVE beyond one that
  * is SYNCING.  This is also required to maintain the notion that we use
- * a counting semaphore to hold off would be writers to the log when every
+ * a ordered wait queue to hold off would be writers to the log when every
  * iclog is trying to sync to disk.
  *
  * State Change: DIRTY -> ACTIVE
@@ -1968,7 +2044,7 @@ xlog_state_clean_log(xlog_t *log)
                if (iclog->ic_state == XLOG_STATE_DIRTY) {
                        iclog->ic_state = XLOG_STATE_ACTIVE;
                        iclog->ic_offset       = 0;
-                       iclog->ic_callback      = NULL;   /* don't need to free */
+                       ASSERT(iclog->ic_callback == NULL);
                        /*
                         * If the number of ops in this iclog indicate it just
                         * contains the dummy transaction, we can
@@ -2076,6 +2152,7 @@ xlog_state_do_callback(
        int                funcdidcallbacks; /* flag: function did callbacks */
        int                repeats;     /* for issuing console warnings if
                                         * looping too many times */
+       int                wake = 0;
 
        spin_lock(&log->l_icloglock);
        first_iclog = iclog = log->l_iclog;
@@ -2171,37 +2248,40 @@ xlog_state_do_callback(
                                        be64_to_cpu(iclog->ic_header.h_lsn);
                                spin_unlock(&log->l_grant_lock);
 
-                               /*
-                                * Keep processing entries in the callback list
-                                * until we come around and it is empty.  We
-                                * need to atomically see that the list is
-                                * empty and change the state to DIRTY so that
-                                * we don't miss any more callbacks being added.
-                                */
-                               spin_lock(&log->l_icloglock);
                        } else {
+                               spin_unlock(&log->l_icloglock);
                                ioerrors++;
                        }
-                       cb = iclog->ic_callback;
 
+                       /*
+                        * Keep processing entries in the callback list until
+                        * we come around and it is empty.  We need to
+                        * atomically see that the list is empty and change the
+                        * state to DIRTY so that we don't miss any more
+                        * callbacks being added.
+                        */
+                       spin_lock(&iclog->ic_callback_lock);
+                       cb = iclog->ic_callback;
                        while (cb) {
                                iclog->ic_callback_tail = &(iclog->ic_callback);
                                iclog->ic_callback = NULL;
-                               spin_unlock(&log->l_icloglock);
+                               spin_unlock(&iclog->ic_callback_lock);
 
                                /* perform callbacks in the order given */
                                for (; cb; cb = cb_next) {
                                        cb_next = cb->cb_next;
                                        cb->cb_func(cb->cb_arg, aborted);
                                }
-                               spin_lock(&log->l_icloglock);
+                               spin_lock(&iclog->ic_callback_lock);
                                cb = iclog->ic_callback;
                        }
 
                        loopdidcallbacks++;
                        funcdidcallbacks++;
 
+                       spin_lock(&log->l_icloglock);
                        ASSERT(iclog->ic_callback == NULL);
+                       spin_unlock(&iclog->ic_callback_lock);
                        if (!(iclog->ic_state & XLOG_STATE_IOERROR))
                                iclog->ic_state = XLOG_STATE_DIRTY;
 
@@ -2212,7 +2292,7 @@ xlog_state_do_callback(
                        xlog_state_clean_log(log);
 
                        /* wake up threads waiting in xfs_log_force() */
-                       sv_broadcast(&iclog->ic_forcesema);
+                       sv_broadcast(&iclog->ic_force_wait);
 
                        iclog = iclog->ic_next;
                } while (first_iclog != iclog);
@@ -2222,7 +2302,7 @@ xlog_state_do_callback(
                        repeats = 0;
                        xfs_fs_cmn_err(CE_WARN, log->l_mp,
                                "%s: possible infinite loop (%d iterations)",
-                               __FUNCTION__, flushcnt);
+                               __func__, flushcnt);
                }
        } while (!ioerrors && loopdidcallbacks);
 
@@ -2254,15 +2334,13 @@ xlog_state_do_callback(
        }
 #endif
 
-       flushcnt = 0;
-       if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) {
-               flushcnt = log->l_flushcnt;
-               log->l_flushcnt = 0;
-       }
+       if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR))
+               wake = 1;
        spin_unlock(&log->l_icloglock);
-       while (flushcnt--)
-               vsema(&log->l_flushsema);
-}      /* xlog_state_do_callback */
+
+       if (wake)
+               sv_broadcast(&log->l_flush_wait);
+}
 
 
 /*
@@ -2276,8 +2354,7 @@ xlog_state_do_callback(
  * the second completion goes through.
  *
  * Callbacks could take time, so they are done outside the scope of the
- * global state machine log lock.  Assume that the calls to cvsema won't
- * take a long time.  At least we know it won't sleep.
+ * global state machine log lock.
  */
 STATIC void
 xlog_state_done_syncing(
@@ -2290,7 +2367,7 @@ xlog_state_done_syncing(
 
        ASSERT(iclog->ic_state == XLOG_STATE_SYNCING ||
               iclog->ic_state == XLOG_STATE_IOERROR);
-       ASSERT(iclog->ic_refcnt == 0);
+       ASSERT(atomic_read(&iclog->ic_refcnt) == 0);
        ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2);
 
 
@@ -2313,7 +2390,7 @@ xlog_state_done_syncing(
         * iclog buffer, we wake them all, one will get to do the
         * I/O, the others get to wait for the result.
         */
-       sv_broadcast(&iclog->ic_writesema);
+       sv_broadcast(&iclog->ic_write_wait);
        spin_unlock(&log->l_icloglock);
        xlog_state_do_callback(log, aborted, iclog);    /* also cleans log */
 }      /* xlog_state_done_syncing */
@@ -2321,11 +2398,9 @@ xlog_state_done_syncing(
 
 /*
  * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must
- * sleep.  The flush semaphore is set to the number of in-core buffers and
- * decremented around disk syncing.  Therefore, if all buffers are syncing,
- * this semaphore will cause new writes to sleep until a sync completes.
- * Otherwise, this code just does p() followed by v().  This approximates
- * a sleep/wakeup except we can't race.
+ * sleep.  We wait on the flush queue on the head iclog as that should be
+ * the first iclog to complete flushing. Hence if all iclogs are syncing,
+ * we will wait here and all new writes will sleep until a sync completes.
  *
  * The in-core logs are used in a circular fashion. They are not used
  * out-of-order even when an iclog past the head is free.
@@ -2360,19 +2435,17 @@ restart:
        }
 
        iclog = log->l_iclog;
-       if (! (iclog->ic_state == XLOG_STATE_ACTIVE)) {
-               log->l_flushcnt++;
-               spin_unlock(&log->l_icloglock);
-               xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
+       if (iclog->ic_state != XLOG_STATE_ACTIVE) {
                XFS_STATS_INC(xs_log_noiclogs);
-               /* Ensure that log writes happen */
-               psema(&log->l_flushsema, PINOD);
+
+               /* Wait for log writes to have flushed */
+               sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0);
                goto restart;
        }
-       ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE);
+
        head = &iclog->ic_header;
 
-       iclog->ic_refcnt++;                     /* prevents sync */
+       atomic_inc(&iclog->ic_refcnt);  /* prevents sync */
        log_offset = iclog->ic_offset;
 
        /* On the 1st write to an iclog, figure out lsn.  This works
@@ -2403,13 +2476,20 @@ restart:
        if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) {
                xlog_state_switch_iclogs(log, iclog, iclog->ic_size);
 
-               /* If I'm the only one writing to this iclog, sync it to disk */
-               if (iclog->ic_refcnt == 1) {
+               /*
+                * If I'm the only one writing to this iclog, sync it to disk.
+                * We need to do an atomic compare and decrement here to avoid
+                * racing with concurrent atomic_dec_and_lock() calls in
+                * xlog_state_release_iclog() when there is more than one
+                * reference to the iclog.
+                */
+               if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) {
+                       /* we are the only one */
                        spin_unlock(&log->l_icloglock);
-                       if ((error = xlog_state_release_iclog(log, iclog)))
+                       error = xlog_state_release_iclog(log, iclog);
+                       if (error)
                                return error;
                } else {
-                       iclog->ic_refcnt--;
                        spin_unlock(&log->l_icloglock);
                }
                goto restart;
@@ -2461,13 +2541,15 @@ xlog_grant_log_space(xlog_t        *log,
 
        /* Is there space or do we need to sleep? */
        spin_lock(&log->l_grant_lock);
-       xlog_trace_loggrant(log, tic, "xlog_grant_log_space: enter");
+
+       trace_xfs_log_grant_enter(log, tic);
 
        /* something is already sleeping; insert new transaction at end */
        if (log->l_reserve_headq) {
                xlog_ins_ticketq(&log->l_reserve_headq, tic);
-               xlog_trace_loggrant(log, tic,
-                                   "xlog_grant_log_space: sleep 1");
+
+               trace_xfs_log_grant_sleep1(log, tic);
+
                /*
                 * Gotta check this before going to sleep, while we're
                 * holding the grant lock.
@@ -2476,13 +2558,12 @@ xlog_grant_log_space(xlog_t        *log,
                        goto error_return;
 
                XFS_STATS_INC(xs_sleep_logspace);
-               sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
+               sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
                /*
                 * If we got an error, and the filesystem is shutting down,
                 * we'll catch it down below. So just continue...
                 */
-               xlog_trace_loggrant(log, tic,
-                                   "xlog_grant_log_space: wake 1");
+               trace_xfs_log_grant_wake1(log, tic);
                spin_lock(&log->l_grant_lock);
        }
        if (tic->t_flags & XFS_LOG_PERM_RESERV)
@@ -2499,20 +2580,22 @@ redo:
        if (free_bytes < need_bytes) {
                if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
                        xlog_ins_ticketq(&log->l_reserve_headq, tic);
-               xlog_trace_loggrant(log, tic,
-                                   "xlog_grant_log_space: sleep 2");
-               XFS_STATS_INC(xs_sleep_logspace);
-               sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
 
-               if (XLOG_FORCED_SHUTDOWN(log)) {
-                       spin_lock(&log->l_grant_lock);
-                       goto error_return;
-               }
+               trace_xfs_log_grant_sleep2(log, tic);
 
-               xlog_trace_loggrant(log, tic,
-                                   "xlog_grant_log_space: wake 2");
+               spin_unlock(&log->l_grant_lock);
                xlog_grant_push_ail(log->l_mp, need_bytes);
                spin_lock(&log->l_grant_lock);
+
+               XFS_STATS_INC(xs_sleep_logspace);
+               sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
+
+               spin_lock(&log->l_grant_lock);
+               if (XLOG_FORCED_SHUTDOWN(log))
+                       goto error_return;
+
+               trace_xfs_log_grant_wake2(log, tic);
+
                goto redo;
        } else if (tic->t_flags & XLOG_TIC_IN_Q)
                xlog_del_ticketq(&log->l_reserve_headq, tic);
@@ -2532,7 +2615,7 @@ redo:
                ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn)));
        }
 #endif
-       xlog_trace_loggrant(log, tic, "xlog_grant_log_space: exit");
+       trace_xfs_log_grant_exit(log, tic);
        xlog_verify_grant_head(log, 1);
        spin_unlock(&log->l_grant_lock);
        return 0;
@@ -2540,7 +2623,9 @@ redo:
  error_return:
        if (tic->t_flags & XLOG_TIC_IN_Q)
                xlog_del_ticketq(&log->l_reserve_headq, tic);
-       xlog_trace_loggrant(log, tic, "xlog_grant_log_space: err_ret");
+
+       trace_xfs_log_grant_error(log, tic);
+
        /*
         * If we are failing, make sure the ticket doesn't have any
         * current reservations. We don't want to add this back when
@@ -2580,7 +2665,8 @@ xlog_regrant_write_log_space(xlog_t          *log,
 #endif
 
        spin_lock(&log->l_grant_lock);
-       xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: enter");
+
+       trace_xfs_log_regrant_write_enter(log, tic);
 
        if (XLOG_FORCED_SHUTDOWN(log))
                goto error_return;
@@ -2591,7 +2677,7 @@ xlog_regrant_write_log_space(xlog_t          *log,
         * for more free space, otherwise try to get some space for
         * this transaction.
         */
-
+       need_bytes = tic->t_unit_res;
        if ((ntic = log->l_write_headq)) {
                free_bytes = xlog_space_left(log, log->l_grant_write_cycle,
                                             log->l_grant_write_bytes);
@@ -2601,7 +2687,7 @@ xlog_regrant_write_log_space(xlog_t          *log,
                        if (free_bytes < ntic->t_unit_res)
                                break;
                        free_bytes -= ntic->t_unit_res;
-                       sv_signal(&ntic->t_sema);
+                       sv_signal(&ntic->t_wait);
                        ntic = ntic->t_next;
                } while (ntic != log->l_write_headq);
 
@@ -2609,28 +2695,26 @@ xlog_regrant_write_log_space(xlog_t        *log,
                        if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
                                xlog_ins_ticketq(&log->l_write_headq, tic);
 
-                       xlog_trace_loggrant(log, tic,
-                                   "xlog_regrant_write_log_space: sleep 1");
+                       trace_xfs_log_regrant_write_sleep1(log, tic);
+
+                       spin_unlock(&log->l_grant_lock);
+                       xlog_grant_push_ail(log->l_mp, need_bytes);
+                       spin_lock(&log->l_grant_lock);
+
                        XFS_STATS_INC(xs_sleep_logspace);
-                       sv_wait(&tic->t_sema, PINOD|PLTWAIT,
+                       sv_wait(&tic->t_wait, PINOD|PLTWAIT,
                                &log->l_grant_lock, s);
 
                        /* If we're shutting down, this tic is already
                         * off the queue */
-                       if (XLOG_FORCED_SHUTDOWN(log)) {
-                               spin_lock(&log->l_grant_lock);
+                       spin_lock(&log->l_grant_lock);
+                       if (XLOG_FORCED_SHUTDOWN(log))
                                goto error_return;
-                       }
 
-                       xlog_trace_loggrant(log, tic,
-                                   "xlog_regrant_write_log_space: wake 1");
-                       xlog_grant_push_ail(log->l_mp, tic->t_unit_res);
-                       spin_lock(&log->l_grant_lock);
+                       trace_xfs_log_regrant_write_wake1(log, tic);
                }
        }
 
-       need_bytes = tic->t_unit_res;
-
 redo:
        if (XLOG_FORCED_SHUTDOWN(log))
                goto error_return;
@@ -2640,19 +2724,21 @@ redo:
        if (free_bytes < need_bytes) {
                if ((tic->t_flags & XLOG_TIC_IN_Q) == 0)
                        xlog_ins_ticketq(&log->l_write_headq, tic);
+               spin_unlock(&log->l_grant_lock);
+               xlog_grant_push_ail(log->l_mp, need_bytes);
+               spin_lock(&log->l_grant_lock);
+
                XFS_STATS_INC(xs_sleep_logspace);
-               sv_wait(&tic->t_sema, PINOD|PLTWAIT, &log->l_grant_lock, s);
+               trace_xfs_log_regrant_write_sleep2(log, tic);
+
+               sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
 
                /* If we're shutting down, this tic is already off the queue */
-               if (XLOG_FORCED_SHUTDOWN(log)) {
-                       spin_lock(&log->l_grant_lock);
+               spin_lock(&log->l_grant_lock);
+               if (XLOG_FORCED_SHUTDOWN(log))
                        goto error_return;
-               }
 
-               xlog_trace_loggrant(log, tic,
-                                   "xlog_regrant_write_log_space: wake 2");
-               xlog_grant_push_ail(log->l_mp, need_bytes);
-               spin_lock(&log->l_grant_lock);
+               trace_xfs_log_regrant_write_wake2(log, tic);
                goto redo;
        } else if (tic->t_flags & XLOG_TIC_IN_Q)
                xlog_del_ticketq(&log->l_write_headq, tic);
@@ -2667,7 +2753,8 @@ redo:
        }
 #endif
 
-       xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: exit");
+       trace_xfs_log_regrant_write_exit(log, tic);
+
        xlog_verify_grant_head(log, 1);
        spin_unlock(&log->l_grant_lock);
        return 0;
@@ -2676,7 +2763,9 @@ redo:
  error_return:
        if (tic->t_flags & XLOG_TIC_IN_Q)
                xlog_del_ticketq(&log->l_reserve_headq, tic);
-       xlog_trace_loggrant(log, tic, "xlog_regrant_write_log_space: err_ret");
+
+       trace_xfs_log_regrant_write_error(log, tic);
+
        /*
         * If we are failing, make sure the ticket doesn't have any
         * current reservations. We don't want to add this back when
@@ -2700,8 +2789,8 @@ STATIC void
 xlog_regrant_reserve_log_space(xlog_t       *log,
                               xlog_ticket_t *ticket)
 {
-       xlog_trace_loggrant(log, ticket,
-                           "xlog_regrant_reserve_log_space: enter");
+       trace_xfs_log_regrant_reserve_enter(log, ticket);
+
        if (ticket->t_cnt > 0)
                ticket->t_cnt--;
 
@@ -2709,8 +2798,9 @@ xlog_regrant_reserve_log_space(xlog_t          *log,
        xlog_grant_sub_space(log, ticket->t_curr_res);
        ticket->t_curr_res = ticket->t_unit_res;
        xlog_tic_reset_res(ticket);
-       xlog_trace_loggrant(log, ticket,
-                           "xlog_regrant_reserve_log_space: sub current res");
+
+       trace_xfs_log_regrant_reserve_sub(log, ticket);
+
        xlog_verify_grant_head(log, 1);
 
        /* just return if we still have some of the pre-reserved space */
@@ -2720,8 +2810,9 @@ xlog_regrant_reserve_log_space(xlog_t          *log,
        }
 
        xlog_grant_add_space_reserve(log, ticket->t_unit_res);
-       xlog_trace_loggrant(log, ticket,
-                           "xlog_regrant_reserve_log_space: exit");
+
+       trace_xfs_log_regrant_reserve_exit(log, ticket);
+
        xlog_verify_grant_head(log, 0);
        spin_unlock(&log->l_grant_lock);
        ticket->t_curr_res = ticket->t_unit_res;
@@ -2751,11 +2842,11 @@ xlog_ungrant_log_space(xlog_t        *log,
                ticket->t_cnt--;
 
        spin_lock(&log->l_grant_lock);
-       xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: enter");
+       trace_xfs_log_ungrant_enter(log, ticket);
 
        xlog_grant_sub_space(log, ticket->t_curr_res);
 
-       xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: sub current");
+       trace_xfs_log_ungrant_sub(log, ticket);
 
        /* If this is a permanent reservation ticket, we may be able to free
         * up more space based on the remaining count.
@@ -2765,7 +2856,8 @@ xlog_ungrant_log_space(xlog_t          *log,
                xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt);
        }
 
-       xlog_trace_loggrant(log, ticket, "xlog_ungrant_log_space: exit");
+       trace_xfs_log_ungrant_exit(log, ticket);
+
        xlog_verify_grant_head(log, 1);
        spin_unlock(&log->l_grant_lock);
        xfs_log_move_tail(log->l_mp, 1);
@@ -2773,18 +2865,6 @@ xlog_ungrant_log_space(xlog_t         *log,
 
 
 /*
- * Atomically put back used ticket.
- */
-STATIC void
-xlog_state_put_ticket(xlog_t       *log,
-                     xlog_ticket_t *tic)
-{
-       spin_lock(&log->l_icloglock);
-       xlog_ticket_put(log, tic);
-       spin_unlock(&log->l_icloglock);
-}      /* xlog_state_put_ticket */
-
-/*
  * Flush iclog to disk if this is the last reference to the given iclog and
  * the WANT_SYNC bit is set.
  *
@@ -2794,33 +2874,35 @@ xlog_state_put_ticket(xlog_t        *log,
  *
  */
 STATIC int
-xlog_state_release_iclog(xlog_t                *log,
-                        xlog_in_core_t *iclog)
+xlog_state_release_iclog(
+       xlog_t          *log,
+       xlog_in_core_t  *iclog)
 {
        int             sync = 0;       /* do we sync? */
 
-       xlog_assign_tail_lsn(log->l_mp);
+       if (iclog->ic_state & XLOG_STATE_IOERROR)
+               return XFS_ERROR(EIO);
 
-       spin_lock(&log->l_icloglock);
+       ASSERT(atomic_read(&iclog->ic_refcnt) > 0);
+       if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock))
+               return 0;
 
        if (iclog->ic_state & XLOG_STATE_IOERROR) {
                spin_unlock(&log->l_icloglock);
                return XFS_ERROR(EIO);
        }
-
-       ASSERT(iclog->ic_refcnt > 0);
        ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE ||
               iclog->ic_state == XLOG_STATE_WANT_SYNC);
 
-       if (--iclog->ic_refcnt == 0 &&
-           iclog->ic_state == XLOG_STATE_WANT_SYNC) {
+       if (iclog->ic_state == XLOG_STATE_WANT_SYNC) {
+               /* update tail before writing to iclog */
+               xlog_assign_tail_lsn(log->l_mp);
                sync++;
                iclog->ic_state = XLOG_STATE_SYNCING;
                iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn);
                xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn);
                /* cycle incremented when incrementing curr_block */
        }
-
        spin_unlock(&log->l_icloglock);
 
        /*
@@ -2830,11 +2912,9 @@ xlog_state_release_iclog(xlog_t          *log,
         * this iclog has consistent data, so we ignore IOERROR
         * flags after this point.
         */
-       if (sync) {
+       if (sync)
                return xlog_sync(log, iclog);
-       }
        return 0;
-
 }      /* xlog_state_release_iclog */
 
 
@@ -2862,7 +2942,7 @@ xlog_state_switch_iclogs(xlog_t           *log,
        log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize);
 
        /* Round up to next log-sunit */
-       if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
+       if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
            log->l_mp->m_sb.sb_logsunit > 1) {
                __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit);
                log->l_curr_block = roundup(log->l_curr_block, sunit_bb);
@@ -2879,7 +2959,6 @@ xlog_state_switch_iclogs(xlog_t           *log,
        log->l_iclog = iclog->ic_next;
 }      /* xlog_state_switch_iclogs */
 
-
 /*
  * Write out all data in the in-core log as of this exact moment in time.
  *
@@ -2896,7 +2975,7 @@ xlog_state_switch_iclogs(xlog_t           *log,
  *     2. the current iclog is drity, and the previous iclog is in the
  *             active or dirty state.
  *
- * We may sleep (call psema) if:
+ * We may sleep if:
  *
  *     1. the current iclog is not in the active nor dirty state.
  *     2. the current iclog dirty, and the previous iclog is not in the
@@ -2907,11 +2986,17 @@ xlog_state_switch_iclogs(xlog_t         *log,
  *        b) when we return from flushing out this iclog, it is still
  *             not in the active nor dirty state.
  */
-STATIC int
-xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
+int
+_xfs_log_force(
+       struct xfs_mount        *mp,
+       uint                    flags,
+       int                     *log_flushed)
 {
-       xlog_in_core_t  *iclog;
-       xfs_lsn_t       lsn;
+       struct log              *log = mp->m_log;
+       struct xlog_in_core     *iclog;
+       xfs_lsn_t               lsn;
+
+       XFS_STATS_INC(xs_log_force);
 
        spin_lock(&log->l_icloglock);
 
@@ -2934,7 +3019,8 @@ xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
                 * previous iclog and go to sleep.
                 */
                if (iclog->ic_state == XLOG_STATE_DIRTY ||
-                   (iclog->ic_refcnt == 0 && iclog->ic_offset == 0)) {
+                   (atomic_read(&iclog->ic_refcnt) == 0
+                    && iclog->ic_offset == 0)) {
                        iclog = iclog->ic_prev;
                        if (iclog->ic_state == XLOG_STATE_ACTIVE ||
                            iclog->ic_state == XLOG_STATE_DIRTY)
@@ -2942,21 +3028,23 @@ xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
                        else
                                goto maybe_sleep;
                } else {
-                       if (iclog->ic_refcnt == 0) {
+                       if (atomic_read(&iclog->ic_refcnt) == 0) {
                                /* We are the only one with access to this
                                 * iclog.  Flush it out now.  There should
                                 * be a roundoff of zero to show that someone
                                 * has already taken care of the roundoff from
                                 * the previous sync.
                                 */
-                               iclog->ic_refcnt++;
+                               atomic_inc(&iclog->ic_refcnt);
                                lsn = be64_to_cpu(iclog->ic_header.h_lsn);
                                xlog_state_switch_iclogs(log, iclog, 0);
                                spin_unlock(&log->l_icloglock);
 
                                if (xlog_state_release_iclog(log, iclog))
                                        return XFS_ERROR(EIO);
-                               *log_flushed = 1;
+
+                               if (log_flushed)
+                                       *log_flushed = 1;
                                spin_lock(&log->l_icloglock);
                                if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn &&
                                    iclog->ic_state != XLOG_STATE_DIRTY)
@@ -2992,7 +3080,7 @@ maybe_sleep:
                        return XFS_ERROR(EIO);
                }
                XFS_STATS_INC(xs_log_force_sleep);
-               sv_wait(&iclog->ic_forcesema, PINOD, &log->l_icloglock, s);
+               sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s);
                /*
                 * No need to grab the log lock here since we're
                 * only deciding whether or not to return EIO
@@ -3000,19 +3088,37 @@ maybe_sleep:
                 */
                if (iclog->ic_state & XLOG_STATE_IOERROR)
                        return XFS_ERROR(EIO);
-               *log_flushed = 1;
-
+               if (log_flushed)
+                       *log_flushed = 1;
        } else {
 
 no_sleep:
                spin_unlock(&log->l_icloglock);
        }
        return 0;
-}      /* xlog_state_sync_all */
+}
+
+/*
+ * Wrapper for _xfs_log_force(), to be used when caller doesn't care
+ * about errors or whether the log was flushed or not. This is the normal
+ * interface to use when trying to unpin items or move the log forward.
+ */
+void
+xfs_log_force(
+       xfs_mount_t     *mp,
+       uint            flags)
+{
+       int     error;
 
+       error = _xfs_log_force(mp, flags, NULL);
+       if (error) {
+               xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
+                       "error %d returned.", error);
+       }
+}
 
 /*
- * Used by code which implements synchronous log forces.
+ * Force the in-core log to disk for a specific LSN.
  *
  * Find in-core log with lsn.
  *     If it is in the DIRTY state, just return.
@@ -3020,109 +3126,142 @@ no_sleep:
  *             state and go to sleep or return.
  *     If it is in any other state, go to sleep or return.
  *
- * If filesystem activity goes to zero, the iclog will get flushed only by
- * bdflush().
+ * Synchronous forces are implemented with a signal variable. All callers
+ * to force a given lsn to disk will wait on a the sv attached to the
+ * specific in-core log.  When given in-core log finally completes its
+ * write to disk, that thread will wake up all threads waiting on the
+ * sv.
  */
-STATIC int
-xlog_state_sync(xlog_t   *log,
-               xfs_lsn_t lsn,
-               uint      flags,
-               int       *log_flushed)
+int
+_xfs_log_force_lsn(
+       struct xfs_mount        *mp,
+       xfs_lsn_t               lsn,
+       uint                    flags,
+       int                     *log_flushed)
 {
-    xlog_in_core_t     *iclog;
-    int                        already_slept = 0;
-
-try_again:
-    spin_lock(&log->l_icloglock);
-    iclog = log->l_iclog;
+       struct log              *log = mp->m_log;
+       struct xlog_in_core     *iclog;
+       int                     already_slept = 0;
 
-    if (iclog->ic_state & XLOG_STATE_IOERROR) {
-           spin_unlock(&log->l_icloglock);
-           return XFS_ERROR(EIO);
-    }
+       ASSERT(lsn != 0);
 
-    do {
-       if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
-               iclog = iclog->ic_next;
-               continue;
-       }
+       XFS_STATS_INC(xs_log_force);
 
-       if (iclog->ic_state == XLOG_STATE_DIRTY) {
+try_again:
+       spin_lock(&log->l_icloglock);
+       iclog = log->l_iclog;
+       if (iclog->ic_state & XLOG_STATE_IOERROR) {
                spin_unlock(&log->l_icloglock);
-               return 0;
+               return XFS_ERROR(EIO);
        }
 
-       if (iclog->ic_state == XLOG_STATE_ACTIVE) {
-               /*
-                * We sleep here if we haven't already slept (e.g.
-                * this is the first time we've looked at the correct
-                * iclog buf) and the buffer before us is going to
-                * be sync'ed. The reason for this is that if we
-                * are doing sync transactions here, by waiting for
-                * the previous I/O to complete, we can allow a few
-                * more transactions into this iclog before we close
-                * it down.
-                *
-                * Otherwise, we mark the buffer WANT_SYNC, and bump
-                * up the refcnt so we can release the log (which drops
-                * the ref count).  The state switch keeps new transaction
-                * commits from using this buffer.  When the current commits
-                * finish writing into the buffer, the refcount will drop to
-                * zero and the buffer will go out then.
-                */
-               if (!already_slept &&
-                   (iclog->ic_prev->ic_state & (XLOG_STATE_WANT_SYNC |
-                                                XLOG_STATE_SYNCING))) {
-                       ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
-                       XFS_STATS_INC(xs_log_force_sleep);
-                       sv_wait(&iclog->ic_prev->ic_writesema, PSWP,
-                               &log->l_icloglock, s);
-                       *log_flushed = 1;
-                       already_slept = 1;
-                       goto try_again;
-               } else {
-                       iclog->ic_refcnt++;
+       do {
+               if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) {
+                       iclog = iclog->ic_next;
+                       continue;
+               }
+
+               if (iclog->ic_state == XLOG_STATE_DIRTY) {
+                       spin_unlock(&log->l_icloglock);
+                       return 0;
+               }
+
+               if (iclog->ic_state == XLOG_STATE_ACTIVE) {
+                       /*
+                        * We sleep here if we haven't already slept (e.g.
+                        * this is the first time we've looked at the correct
+                        * iclog buf) and the buffer before us is going to
+                        * be sync'ed. The reason for this is that if we
+                        * are doing sync transactions here, by waiting for
+                        * the previous I/O to complete, we can allow a few
+                        * more transactions into this iclog before we close
+                        * it down.
+                        *
+                        * Otherwise, we mark the buffer WANT_SYNC, and bump
+                        * up the refcnt so we can release the log (which
+                        * drops the ref count).  The state switch keeps new
+                        * transaction commits from using this buffer.  When
+                        * the current commits finish writing into the buffer,
+                        * the refcount will drop to zero and the buffer will
+                        * go out then.
+                        */
+                       if (!already_slept &&
+                           (iclog->ic_prev->ic_state &
+                            (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) {
+                               ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR));
+
+                               XFS_STATS_INC(xs_log_force_sleep);
+
+                               sv_wait(&iclog->ic_prev->ic_write_wait,
+                                       PSWP, &log->l_icloglock, s);
+                               if (log_flushed)
+                                       *log_flushed = 1;
+                               already_slept = 1;
+                               goto try_again;
+                       }
+                       atomic_inc(&iclog->ic_refcnt);
                        xlog_state_switch_iclogs(log, iclog, 0);
                        spin_unlock(&log->l_icloglock);
                        if (xlog_state_release_iclog(log, iclog))
                                return XFS_ERROR(EIO);
-                       *log_flushed = 1;
+                       if (log_flushed)
+                               *log_flushed = 1;
                        spin_lock(&log->l_icloglock);
                }
-       }
 
-       if ((flags & XFS_LOG_SYNC) && /* sleep */
-           !(iclog->ic_state & (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
+               if ((flags & XFS_LOG_SYNC) && /* sleep */
+                   !(iclog->ic_state &
+                     (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) {
+                       /*
+                        * Don't wait on completion if we know that we've
+                        * gotten a log write error.
+                        */
+                       if (iclog->ic_state & XLOG_STATE_IOERROR) {
+                               spin_unlock(&log->l_icloglock);
+                               return XFS_ERROR(EIO);
+                       }
+                       XFS_STATS_INC(xs_log_force_sleep);
+                       sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s);
+                       /*
+                        * No need to grab the log lock here since we're
+                        * only deciding whether or not to return EIO
+                        * and the memory read should be atomic.
+                        */
+                       if (iclog->ic_state & XLOG_STATE_IOERROR)
+                               return XFS_ERROR(EIO);
 
-               /*
-                * Don't wait on the forcesema if we know that we've
-                * gotten a log write error.
-                */
-               if (iclog->ic_state & XLOG_STATE_IOERROR) {
+                       if (log_flushed)
+                               *log_flushed = 1;
+               } else {                /* just return */
                        spin_unlock(&log->l_icloglock);
-                       return XFS_ERROR(EIO);
                }
-               XFS_STATS_INC(xs_log_force_sleep);
-               sv_wait(&iclog->ic_forcesema, PSWP, &log->l_icloglock, s);
-               /*
-                * No need to grab the log lock here since we're
-                * only deciding whether or not to return EIO
-                * and the memory read should be atomic.
-                */
-               if (iclog->ic_state & XLOG_STATE_IOERROR)
-                       return XFS_ERROR(EIO);
-               *log_flushed = 1;
-       } else {                /* just return */
-               spin_unlock(&log->l_icloglock);
-       }
-       return 0;
 
-    } while (iclog != log->l_iclog);
+               return 0;
+       } while (iclog != log->l_iclog);
+
+       spin_unlock(&log->l_icloglock);
+       return 0;
+}
 
-    spin_unlock(&log->l_icloglock);
-    return 0;
-}      /* xlog_state_sync */
+/*
+ * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care
+ * about errors or whether the log was flushed or not. This is the normal
+ * interface to use when trying to unpin items or move the log forward.
+ */
+void
+xfs_log_force_lsn(
+       xfs_mount_t     *mp,
+       xfs_lsn_t       lsn,
+       uint            flags)
+{
+       int     error;
 
+       error = _xfs_log_force_lsn(mp, lsn, flags, NULL);
+       if (error) {
+               xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
+                       "error %d returned.", error);
+       }
+}
 
 /*
  * Called when we want to mark the current iclog as being ready to sync to
@@ -3131,7 +3270,7 @@ try_again:
 STATIC void
 xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
 {
-       spin_lock(&log->l_icloglock);
+       assert_spin_locked(&log->l_icloglock);
 
        if (iclog->ic_state == XLOG_STATE_ACTIVE) {
                xlog_state_switch_iclogs(log, iclog, 0);
@@ -3139,10 +3278,7 @@ xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
                ASSERT(iclog->ic_state &
                        (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR));
        }
-
-       spin_unlock(&log->l_icloglock);
-}      /* xlog_state_want_sync */
-
+}
 
 
 /*****************************************************************************
@@ -3153,118 +3289,46 @@ xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
  */
 
 /*
- *     Algorithm doesn't take into account page size. ;-(
+ * Free a used ticket when its refcount falls to zero.
  */
-STATIC void
-xlog_state_ticket_alloc(xlog_t *log)
+void
+xfs_log_ticket_put(
+       xlog_ticket_t   *ticket)
 {
-       xlog_ticket_t   *t_list;
-       xlog_ticket_t   *next;
-       xfs_caddr_t     buf;
-       uint            i = (NBPP / sizeof(xlog_ticket_t)) - 2;
-
-       /*
-        * The kmem_zalloc may sleep, so we shouldn't be holding the
-        * global lock.  XXXmiken: may want to use zone allocator.
-        */
-       buf = (xfs_caddr_t) kmem_zalloc(NBPP, KM_SLEEP);
-
-       spin_lock(&log->l_icloglock);
-
-       /* Attach 1st ticket to Q, so we can keep track of allocated memory */
-       t_list = (xlog_ticket_t *)buf;
-       t_list->t_next = log->l_unmount_free;
-       log->l_unmount_free = t_list++;
-       log->l_ticket_cnt++;
-       log->l_ticket_tcnt++;
-
-       /* Next ticket becomes first ticket attached to ticket free list */
-       if (log->l_freelist != NULL) {
-               ASSERT(log->l_tail != NULL);
-               log->l_tail->t_next = t_list;
-       } else {
-               log->l_freelist = t_list;
-       }
-       log->l_ticket_cnt++;
-       log->l_ticket_tcnt++;
-
-       /* Cycle through rest of alloc'ed memory, building up free Q */
-       for ( ; i > 0; i--) {
-               next = t_list + 1;
-               t_list->t_next = next;
-               t_list = next;
-               log->l_ticket_cnt++;
-               log->l_ticket_tcnt++;
+       ASSERT(atomic_read(&ticket->t_ref) > 0);
+       if (atomic_dec_and_test(&ticket->t_ref)) {
+               sv_destroy(&ticket->t_wait);
+               kmem_zone_free(xfs_log_ticket_zone, ticket);
        }
-       t_list->t_next = NULL;
-       log->l_tail = t_list;
-       spin_unlock(&log->l_icloglock);
-}      /* xlog_state_ticket_alloc */
-
+}
 
-/*
- * Put ticket into free list
- *
- * Assumption: log lock is held around this call.
- */
-STATIC void
-xlog_ticket_put(xlog_t         *log,
-               xlog_ticket_t   *ticket)
+xlog_ticket_t *
+xfs_log_ticket_get(
+       xlog_ticket_t   *ticket)
 {
-       sv_destroy(&ticket->t_sema);
-
-       /*
-        * Don't think caching will make that much difference.  It's
-        * more important to make debug easier.
-        */
-#if 0
-       /* real code will want to use LIFO for caching */
-       ticket->t_next = log->l_freelist;
-       log->l_freelist = ticket;
-       /* no need to clear fields */
-#else
-       /* When we debug, it is easier if tickets are cycled */
-       ticket->t_next     = NULL;
-       if (log->l_tail) {
-               log->l_tail->t_next = ticket;
-       } else {
-               ASSERT(log->l_freelist == NULL);
-               log->l_freelist = ticket;
-       }
-       log->l_tail         = ticket;
-#endif /* DEBUG */
-       log->l_ticket_cnt++;
-}      /* xlog_ticket_put */
-
+       ASSERT(atomic_read(&ticket->t_ref) > 0);
+       atomic_inc(&ticket->t_ref);
+       return ticket;
+}
 
 /*
- * Grab ticket off freelist or allocation some more
+ * Allocate and initialise a new log ticket.
  */
 STATIC xlog_ticket_t *
-xlog_ticket_get(xlog_t         *log,
-               int             unit_bytes,
-               int             cnt,
-               char            client,
-               uint            xflags)
+xlog_ticket_alloc(
+       struct log      *log,
+       int             unit_bytes,
+       int             cnt,
+       char            client,
+       uint            xflags)
 {
-       xlog_ticket_t   *tic;
+       struct xlog_ticket *tic;
        uint            num_headers;
+       int             iclog_space;
 
- alloc:
-       if (log->l_freelist == NULL)
-               xlog_state_ticket_alloc(log);           /* potentially sleep */
-
-       spin_lock(&log->l_icloglock);
-       if (log->l_freelist == NULL) {
-               spin_unlock(&log->l_icloglock);
-               goto alloc;
-       }
-       tic             = log->l_freelist;
-       log->l_freelist = tic->t_next;
-       if (log->l_freelist == NULL)
-               log->l_tail = NULL;
-       log->l_ticket_cnt--;
-       spin_unlock(&log->l_icloglock);
+       tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL);
+       if (!tic)
+               return NULL;
 
        /*
         * Permanent reservations have up to 'cnt'-1 active log operations
@@ -3304,18 +3368,42 @@ xlog_ticket_get(xlog_t          *log,
        /* for start-rec */
        unit_bytes += sizeof(xlog_op_header_t);
 
-       /* for LR headers */
-       num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log);
+       /*
+        * for LR headers - the space for data in an iclog is the size minus
+        * the space used for the headers. If we use the iclog size, then we
+        * undercalculate the number of headers required.
+        *
+        * Furthermore - the addition of op headers for split-recs might
+        * increase the space required enough to require more log and op
+        * headers, so take that into account too.
+        *
+        * IMPORTANT: This reservation makes the assumption that if this
+        * transaction is the first in an iclog and hence has the LR headers
+        * accounted to it, then the remaining space in the iclog is
+        * exclusively for this transaction.  i.e. if the transaction is larger
+        * than the iclog, it will be the only thing in that iclog.
+        * Fundamentally, this means we must pass the entire log vector to
+        * xlog_write to guarantee this.
+        */
+       iclog_space = log->l_iclog_size - log->l_iclog_hsize;
+       num_headers = howmany(unit_bytes, iclog_space);
+
+       /* for split-recs - ophdrs added when data split over LRs */
+       unit_bytes += sizeof(xlog_op_header_t) * num_headers;
+
+       /* add extra header reservations if we overrun */
+       while (!num_headers ||
+              howmany(unit_bytes, iclog_space) > num_headers) {
+               unit_bytes += sizeof(xlog_op_header_t);
+               num_headers++;
+       }
        unit_bytes += log->l_iclog_hsize * num_headers;
 
        /* for commit-rec LR header - note: padding will subsume the ophdr */
        unit_bytes += log->l_iclog_hsize;
 
-       /* for split-recs - ophdrs added when data split over LRs */
-       unit_bytes += sizeof(xlog_op_header_t) * num_headers;
-
        /* for roundoff padding for transaction data and one for commit record */
-       if (XFS_SB_VERSION_HASLOGV2(&log->l_mp->m_sb) &&
+       if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) &&
            log->l_mp->m_sb.sb_logsunit > 1) {
                /* log su roundoff */
                unit_bytes += 2*log->l_mp->m_sb.sb_logsunit;
@@ -3324,22 +3412,23 @@ xlog_ticket_get(xlog_t          *log,
                unit_bytes += 2*BBSIZE;
         }
 
+       atomic_set(&tic->t_ref, 1);
        tic->t_unit_res         = unit_bytes;
        tic->t_curr_res         = unit_bytes;
        tic->t_cnt              = cnt;
        tic->t_ocnt             = cnt;
-       tic->t_tid              = (xlog_tid_t)((__psint_t)tic & 0xffffffff);
+       tic->t_tid              = random32();
        tic->t_clientid         = client;
        tic->t_flags            = XLOG_TIC_INITED;
        tic->t_trans_type       = 0;
        if (xflags & XFS_LOG_PERM_RESERV)
                tic->t_flags |= XLOG_TIC_PERM_RESERV;
-       sv_init(&(tic->t_sema), SV_DEFAULT, "logtick");
+       sv_init(&tic->t_wait, SV_DEFAULT, "logtick");
 
        xlog_tic_reset_res(tic);
 
        return tic;
-}      /* xlog_ticket_get */
+}
 
 
 /******************************************************************************
@@ -3355,20 +3444,22 @@ xlog_ticket_get(xlog_t          *log,
  * part of the log in case we trash the log structure.
  */
 void
-xlog_verify_dest_ptr(xlog_t     *log,
-                    __psint_t  ptr)
+xlog_verify_dest_ptr(
+       struct log      *log,
+       char            *ptr)
 {
        int i;
        int good_ptr = 0;
 
-       for (i=0; i < log->l_iclog_bufs; i++) {
-               if (ptr >= (__psint_t)log->l_iclog_bak[i] &&
-                   ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size)
+       for (i = 0; i < log->l_iclog_bufs; i++) {
+               if (ptr >= log->l_iclog_bak[i] &&
+                   ptr <= log->l_iclog_bak[i] + log->l_iclog_size)
                        good_ptr++;
        }
-       if (! good_ptr)
+
+       if (!good_ptr)
                xlog_panic("xlog_verify_dest_ptr: invalid ptr");
-}      /* xlog_verify_dest_ptr */
+}
 
 STATIC void
 xlog_verify_grant_head(xlog_t *log, int equals)
@@ -3468,7 +3559,7 @@ xlog_verify_iclog(xlog_t   *log,
        ptr = iclog->ic_datap;
        base_ptr = ptr;
        ophead = (xlog_op_header_t *)ptr;
-       xhdr = (xlog_in_core_2_t *)&iclog->ic_header;
+       xhdr = iclog->ic_data;
        for (i = 0; i < len; i++) {
                ophead = (xlog_op_header_t *)ptr;
 
@@ -3563,7 +3654,6 @@ xfs_log_force_umount(
        xlog_ticket_t   *tic;
        xlog_t          *log;
        int             retval;
-       int             dummy;
 
        log = mp->m_log;
 
@@ -3574,7 +3664,8 @@ xfs_log_force_umount(
        if (!log ||
            log->l_flags & XLOG_ACTIVE_RECOVERY) {
                mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
-               XFS_BUF_DONE(mp->m_sb_bp);
+               if (mp->m_sb_bp)
+                       XFS_BUF_DONE(mp->m_sb_bp);
                return 0;
        }
 
@@ -3592,10 +3683,12 @@ xfs_log_force_umount(
         * before we mark the filesystem SHUTDOWN and wake
         * everybody up to tell the bad news.
         */
-       spin_lock(&log->l_grant_lock);
        spin_lock(&log->l_icloglock);
+       spin_lock(&log->l_grant_lock);
        mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN;
-       XFS_BUF_DONE(mp->m_sb_bp);
+       if (mp->m_sb_bp)
+               XFS_BUF_DONE(mp->m_sb_bp);
+
        /*
         * This flag is sort of redundant because of the mount flag, but
         * it's good to maintain the separation between the log and the rest
@@ -3621,26 +3714,27 @@ xfs_log_force_umount(
         */
        if ((tic = log->l_reserve_headq)) {
                do {
-                       sv_signal(&tic->t_sema);
+                       sv_signal(&tic->t_wait);
                        tic = tic->t_next;
                } while (tic != log->l_reserve_headq);
        }
 
        if ((tic = log->l_write_headq)) {
                do {
-                       sv_signal(&tic->t_sema);
+                       sv_signal(&tic->t_wait);
                        tic = tic->t_next;
                } while (tic != log->l_write_headq);
        }
        spin_unlock(&log->l_grant_lock);
 
-       if (! (log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
+       if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) {
                ASSERT(!logerror);
                /*
                 * Force the incore logs to disk before shutting the
                 * log down completely.
                 */
-               xlog_state_sync_all(log, XFS_LOG_FORCE|XFS_LOG_SYNC, &dummy);
+               _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
+
                spin_lock(&log->l_icloglock);
                retval = xlog_state_ioerror(log);
                spin_unlock(&log->l_icloglock);