Merge branch 'topic/core-cleanup' into for-linus
[safe/jmp/linux-2.6] / fs / xfs / xfs_log.c
index a2f7422..2be0191 100644 (file)
@@ -40,6 +40,7 @@
 #include "xfs_dinode.h"
 #include "xfs_inode.h"
 #include "xfs_rw.h"
+#include "xfs_trace.h"
 
 kmem_zone_t    *xfs_log_ticket_zone;
 
@@ -49,7 +50,6 @@ kmem_zone_t   *xfs_log_ticket_zone;
          (off) += (bytes);}
 
 /* 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,
                                    xlog_in_core_t **, xfs_lsn_t *);
 STATIC xlog_t *  xlog_alloc_log(xfs_mount_t    *mp,
@@ -60,7 +60,7 @@ 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,
+                           int nentries, struct xlog_ticket *tic,
                            xfs_lsn_t *start_lsn,
                            xlog_in_core_t **commit_iclog,
                            uint flags);
@@ -79,11 +79,6 @@ STATIC int  xlog_state_release_iclog(xlog_t          *log,
 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 */
@@ -100,12 +95,11 @@ STATIC void xlog_ungrant_log_space(xlog_t   *log,
 
 
 /* local ticket functions */
-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);
@@ -123,85 +117,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)
-
-#define XLOG_TRACE_LOGGRANT_SIZE       2048
-#define XLOG_TRACE_ICLOG_SIZE          256
-
-void
-xlog_trace_loggrant_alloc(xlog_t *log)
-{
-       log->l_grant_trace = ktrace_alloc(XLOG_TRACE_LOGGRANT_SIZE, KM_NOFS);
-}
-
-void
-xlog_trace_loggrant_dealloc(xlog_t *log)
-{
-       ktrace_free(log->l_grant_trace);
-}
-
-void
-xlog_trace_loggrant(xlog_t *log, xlog_ticket_t *tic, xfs_caddr_t string)
-{
-       unsigned long cnts;
-
-       /* 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_alloc(xlog_in_core_t *iclog)
-{
-       iclog->ic_trace = ktrace_alloc(XLOG_TRACE_ICLOG_SIZE, KM_NOFS);
-}
-
-void
-xlog_trace_iclog_dealloc(xlog_in_core_t *iclog)
-{
-       ktrace_free(iclog->ic_trace);
-}
-
-void
-xlog_trace_iclog(xlog_in_core_t *iclog, uint state)
-{
-       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_alloc(log)
-#define        xlog_trace_loggrant_dealloc(log)
-#define        xlog_trace_loggrant(log,tic,string)
-
-#define        xlog_trace_iclog_alloc(iclog)
-#define        xlog_trace_iclog_dealloc(iclog)
-#define        xlog_trace_iclog(iclog,state)
-
-#endif /* XFS_LOG_TRACE */
-
 
 static void
 xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic)
@@ -328,14 +243,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) ||
            /*
@@ -343,8 +258,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(mp, ticket, iclog, &lsn)))) {
                lsn = (xfs_lsn_t) -1;
                if (ticket->t_flags & XLOG_TIC_PERM_RESERV) {
                        flags |= XFS_LOG_REL_PERM_RESERV;
@@ -354,15 +268,17 @@ 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_ticket_put(log, ticket);
+               xfs_log_ticket_put(ticket);
        } else {
-               xlog_trace_loggrant(log, ticket, "xfs_log_done: (permanent)");
+               trace_xfs_log_done_perm(log, ticket);
+
                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
@@ -372,67 +288,8 @@ xfs_log_done(xfs_mount_t   *mp,
        }
 
        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 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.
- */
-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 */
-
-/*
- * 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,
-       xfs_lsn_t       lsn,
-       uint            flags)
-{
-       int     error;
-       error = _xfs_log_force(mp, lsn, flags, NULL);
-       if (error) {
-               xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: "
-                       "error %d returned.", error);
-       }
 }
 
-
 /*
  * Attaches a new iclog I/O completion callback routine during
  * transaction commit.  If the log is in error state, a non-zero
@@ -440,11 +297,11 @@ 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_in_core_t    *iclog = (xlog_in_core_t *)iclog_hndl;
        int     abortflg;
 
        spin_lock(&iclog->ic_callback_lock);
@@ -458,16 +315,14 @@ xfs_log_notify(xfs_mount_t          *mp,          /* mount of partition */
        }
        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;
        }
@@ -486,17 +341,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);
@@ -506,24 +362,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));
@@ -563,6 +421,10 @@ xfs_log_mount(
        }
 
        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.
@@ -570,8 +432,9 @@ xfs_log_mount(
        error = xfs_trans_ail_init(mp);
        if (error) {
                cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error);
-               goto error;
+               goto out_free_log;
        }
+       mp->m_log->l_ailp = mp->m_ail;
 
        /*
         * skip log recovery on a norecovery mount.  pretend it all
@@ -589,19 +452,22 @@ xfs_log_mount(
                        mp->m_flags |= XFS_MOUNT_RDONLY;
                if (error) {
                        cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error);
-                       goto 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;
-error:
-       xfs_log_unmount_dealloc(mp);
+
+out_destroy_ail:
+       xfs_trans_ail_destroy(mp);
+out_free_log:
+       xlog_dealloc_log(mp->m_log);
+out:
        return error;
-}      /* xfs_log_mount */
+}
 
 /*
  * Finish the recovery of the file system.  This is separate from
@@ -627,19 +493,6 @@ xfs_log_mount_finish(xfs_mount_t *mp)
 }
 
 /*
- * 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
@@ -664,7 +517,7 @@ xfs_log_unmount_write(xfs_mount_t *mp)
        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;
 
@@ -682,7 +535,7 @@ xfs_log_unmount_write(xfs_mount_t *mp)
        if (mp->m_flags & XFS_MOUNT_RDONLY)
                return 0;
 
-       error = _xfs_log_force(mp, 0, XFS_LOG_FORCE|XFS_LOG_SYNC, NULL);
+       error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL);
        ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log)));
 
 #ifdef DEBUG
@@ -698,7 +551,7 @@ xfs_log_unmount_write(xfs_mount_t *mp)
        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);
+               reg[0].i_type = XLOG_REG_TYPE_UNMOUNT;
 
                error = xfs_log_reserve(mp, 600, 1, &tic,
                                        XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE);
@@ -723,8 +576,8 @@ xfs_log_unmount_write(xfs_mount_t *mp)
                spin_lock(&log->l_icloglock);
                iclog = log->l_iclog;
                atomic_inc(&iclog->ic_refcnt);
-               spin_unlock(&log->l_icloglock);
                xlog_state_want_sync(log, iclog);
+               spin_unlock(&log->l_icloglock);
                error = xlog_state_release_iclog(log, iclog);
 
                spin_lock(&log->l_icloglock);
@@ -740,9 +593,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_ticket_put(log, tic);
+                       xfs_log_ticket_put(tic);
                }
        } else {
                /*
@@ -761,9 +614,9 @@ xfs_log_unmount_write(xfs_mount_t *mp)
                spin_lock(&log->l_icloglock);
                iclog = log->l_iclog;
                atomic_inc(&iclog->ic_refcnt);
-               spin_unlock(&log->l_icloglock);
 
                xlog_state_want_sync(log, iclog);
+               spin_unlock(&log->l_icloglock);
                error =  xlog_state_release_iclog(log, iclog);
 
                spin_lock(&log->l_icloglock);
@@ -789,7 +642,7 @@ xfs_log_unmount_write(xfs_mount_t *mp)
  * 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);
@@ -801,24 +654,24 @@ xfs_log_unmount_dealloc(xfs_mount_t *mp)
  * transaction occur with one call to xfs_log_write().
  */
 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;
 
        if (XLOG_FORCED_SHUTDOWN(log))
                return XFS_ERROR(EIO);
 
-       if ((error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0))) {
+       error = xlog_write(mp, reg, nentries, 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,
@@ -892,9 +745,16 @@ 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)
@@ -906,17 +766,24 @@ xfs_log_need_covered(xfs_mount_t *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_ail_tail(mp->m_ail)
-                       && 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;
@@ -1023,12 +890,6 @@ 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;
 
        /*
@@ -1042,7 +903,6 @@ xlog_iodone(xfs_buf_t *bp)
                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);
        }
 
        /*
@@ -1075,41 +935,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.
@@ -1139,8 +967,8 @@ xlog_get_iclog_buffer_size(xfs_mount_t     *mp,
                }
 
                if (xfs_sb_version_haslogv2(&mp->m_sb)) {
-                       /* # headers = size / 32K
-                        * one header holds cycles from 32K of data
+                       /* # headers = size / 32k
+                        * one header holds cycles from 32k of data
                         */
 
                        xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE;
@@ -1156,7 +984,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;
 
@@ -1164,32 +992,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)
@@ -1215,8 +1019,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;
@@ -1234,22 +1043,39 @@ xlog_alloc_log(xfs_mount_t      *mp,
        log->l_grant_reserve_cycle = 1;
        log->l_grant_write_cycle = 1;
 
+       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);
@@ -1259,7 +1085,6 @@ xlog_alloc_log(xfs_mount_t        *mp,
        spin_lock_init(&log->l_grant_lock);
        sv_init(&log->l_flush_wait, 0, "flush_wait");
 
-       xlog_trace_loggrant_alloc(log);
        /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */
        ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0);
 
@@ -1274,20 +1099,23 @@ 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
@@ -1307,21 +1135,37 @@ xlog_alloc_log(xfs_mount_t      *mp,
                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_force_wait, SV_DEFAULT, "iclog-force");
                sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write");
 
-               xlog_trace_iclog_alloc(iclog);
-
                iclogp = &iclog->ic_next;
        }
        *iclogp = log->l_iclog;                 /* complete ring */
        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 */
 
 
@@ -1340,7 +1184,7 @@ xlog_commit_record(xfs_mount_t  *mp,
 
        reg[0].i_addr = NULL;
        reg[0].i_len = 0;
-       XLOG_VEC_SET_TYPE(&reg[0], XLOG_REG_TYPE_COMMIT);
+       reg[0].i_type = XLOG_REG_TYPE_COMMIT;
 
        ASSERT_ALWAYS(iclog);
        if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp,
@@ -1412,9 +1256,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 
@@ -1513,6 +1388,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.
@@ -1533,7 +1409,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;
@@ -1550,6 +1426,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);
@@ -1572,7 +1449,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;
@@ -1596,7 +1473,6 @@ xlog_dealloc_log(xlog_t *log)
                sv_destroy(&iclog->ic_force_wait);
                sv_destroy(&iclog->ic_write_wait);
                xfs_buf_free(iclog->ic_bp);
-               xlog_trace_iclog_dealloc(iclog);
                next_iclog = iclog->ic_next;
                kmem_free(iclog);
                iclog = next_iclog;
@@ -1605,7 +1481,6 @@ xlog_dealloc_log(xlog_t *log)
        spinlock_destroy(&log->l_grant_lock);
 
        xfs_buf_free(log->l_xbuf);
-       xlog_trace_loggrant_dealloc(log);
        log->l_mp->m_log = NULL;
        kmem_free(log);
 }      /* xlog_dealloc_log */
@@ -1779,16 +1654,16 @@ 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_write(
+       struct xfs_mount        *mp,
+       struct xfs_log_iovec    reg[],
+       int                     nentries,
+       struct xlog_ticket      *ticket,
+       xfs_lsn_t               *start_lsn,
+       struct xlog_in_core     **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 */
@@ -1902,7 +1777,7 @@ xlog_write(xfs_mount_t *  mp,
            default:
                xfs_fs_cmn_err(CE_WARN, mp,
                    "Bad XFS transaction clientid 0x%x in ticket 0x%p",
-                   logop_head->oh_clientid, tic);
+                   logop_head->oh_clientid, ticket);
                return XFS_ERROR(EIO);
            }
 
@@ -1957,7 +1832,9 @@ xlog_write(xfs_mount_t *  mp,
                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;
+                   spin_lock(&log->l_icloglock);
                    xlog_state_want_sync(log, iclog);
+                   spin_unlock(&log->l_icloglock);
                    if (commit_iclog) {
                        ASSERT(flags & XLOG_COMMIT_TRANS);
                        *commit_iclog = iclog;
@@ -2401,7 +2278,6 @@ restart:
 
        iclog = log->l_iclog;
        if (iclog->ic_state != XLOG_STATE_ACTIVE) {
-               xlog_trace_iclog(iclog, XLOG_TRACE_SLEEP_FLUSH);
                XFS_STATS_INC(xs_log_noiclogs);
 
                /* Wait for log writes to have flushed */
@@ -2507,13 +2383,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.
@@ -2527,8 +2405,7 @@ xlog_grant_log_space(xlog_t          *log,
                 * 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)
@@ -2545,20 +2422,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");
+
+               trace_xfs_log_grant_sleep2(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_wait, PINOD|PLTWAIT, &log->l_grant_lock, s);
 
-               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_grant_log_space: wake 2");
-               xlog_grant_push_ail(log->l_mp, need_bytes);
-               spin_lock(&log->l_grant_lock);
+               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);
@@ -2578,7 +2457,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;
@@ -2586,7 +2465,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
@@ -2626,7 +2507,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;
@@ -2637,7 +2519,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);
@@ -2655,28 +2537,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_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;
@@ -2686,19 +2566,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);
+               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);
@@ -2713,7 +2595,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;
@@ -2722,7 +2605,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
@@ -2746,8 +2631,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--;
 
@@ -2755,8 +2640,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 */
@@ -2766,8 +2652,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;
@@ -2797,11 +2684,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.
@@ -2811,7 +2698,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);
@@ -2913,7 +2801,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.
  *
@@ -2941,11 +2828,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);
 
@@ -2991,7 +2884,9 @@ xlog_state_sync_all(xlog_t *log, uint flags, int *log_flushed)
 
                                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)
@@ -3035,19 +2930,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.
@@ -3055,109 +2968,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;
+       struct log              *log = mp->m_log;
+       struct xlog_in_core     *iclog;
+       int                     already_slept = 0;
 
-try_again:
-    spin_lock(&log->l_icloglock);
-    iclog = log->l_iclog;
-
-    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_write_wait, PSWP,
-                               &log->l_icloglock, s);
-                       *log_flushed = 1;
-                       already_slept = 1;
-                       goto try_again;
-               } else {
+       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 completion 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_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);
-               *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;
-}      /* xlog_state_sync */
+       spin_unlock(&log->l_icloglock);
+       return 0;
+}
+
+/*
+ * 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
@@ -3166,7 +3112,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);
@@ -3174,10 +3120,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 */
-
+}
 
 
 /*****************************************************************************
@@ -3188,22 +3131,33 @@ xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog)
  */
 
 /*
- * Free a used ticket.
+ * Free a used ticket when its refcount falls to zero.
  */
-STATIC void
-xlog_ticket_put(xlog_t         *log,
-               xlog_ticket_t   *ticket)
+void
+xfs_log_ticket_put(
+       xlog_ticket_t   *ticket)
 {
-       sv_destroy(&ticket->t_wait);
-       kmem_zone_free(xfs_log_ticket_zone, ticket);
-}      /* xlog_ticket_put */
+       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);
+       }
+}
 
+xlog_ticket_t *
+xfs_log_ticket_get(
+       xlog_ticket_t   *ticket)
+{
+       ASSERT(atomic_read(&ticket->t_ref) > 0);
+       atomic_inc(&ticket->t_ref);
+       return ticket;
+}
 
 /*
  * Allocate and initialise a new log ticket.
  */
 STATIC xlog_ticket_t *
-xlog_ticket_get(xlog_t         *log,
+xlog_ticket_alloc(xlog_t               *log,
                int             unit_bytes,
                int             cnt,
                char            client,
@@ -3274,6 +3228,7 @@ 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;
@@ -3289,7 +3244,7 @@ xlog_ticket_get(xlog_t            *log,
        xlog_tic_reset_res(tic);
 
        return tic;
-}      /* xlog_ticket_get */
+}
 
 
 /******************************************************************************
@@ -3418,7 +3373,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;
 
@@ -3513,7 +3468,6 @@ xfs_log_force_umount(
        xlog_ticket_t   *tic;
        xlog_t          *log;
        int             retval;
-       int             dummy;
 
        log = mp->m_log;
 
@@ -3524,7 +3478,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;
        }
 
@@ -3545,7 +3500,9 @@ xfs_log_force_umount(
        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
@@ -3584,13 +3541,14 @@ xfs_log_force_umount(
        }
        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);