* wrap the tail, we should blow up.  Rather than catch this case here,
  * we depend on other ASSERTions in other parts of the code.   XXXmiken
  *
- * This code also handles the case where the reservation head is behind
- * the tail.  The details of this case are described below, but the end
- * result is that we return the size of the log as the amount of space left.
+ * If reservation head is behind the tail, we have a problem. Warn about it,
+ * but then treat it as if the log is empty.
+ *
+ * If the log is shut down, the head and tail may be invalid or out of whack, so
+ * shortcut invalidity asserts in this case so that we don't trigger them
+ * falsely.
  */
 STATIC int
 xlog_space_left(
        struct xlog     *log,
        atomic64_t      *head)
 {
-       int             free_bytes;
        int             tail_bytes;
        int             tail_cycle;
        int             head_cycle;
        xlog_crack_atomic_lsn(&log->l_tail_lsn, &tail_cycle, &tail_bytes);
        tail_bytes = BBTOB(tail_bytes);
        if (tail_cycle == head_cycle && head_bytes >= tail_bytes)
-               free_bytes = log->l_logsize - (head_bytes - tail_bytes);
-       else if (tail_cycle + 1 < head_cycle)
+               return log->l_logsize - (head_bytes - tail_bytes);
+       if (tail_cycle + 1 < head_cycle)
                return 0;
-       else if (tail_cycle < head_cycle) {
+
+       /* Ignore potential inconsistency when shutdown. */
+       if (xlog_is_shutdown(log))
+               return log->l_logsize;
+
+       if (tail_cycle < head_cycle) {
                ASSERT(tail_cycle == (head_cycle - 1));
-               free_bytes = tail_bytes - head_bytes;
-       } else {
-               /*
-                * The reservation head is behind the tail.
-                * In this case we just want to return the size of the
-                * log as the amount of space left.
-                */
-               xfs_alert(log->l_mp, "xlog_space_left: head behind tail");
-               xfs_alert(log->l_mp,
-                         "  tail_cycle = %d, tail_bytes = %d",
-                         tail_cycle, tail_bytes);
-               xfs_alert(log->l_mp,
-                         "  GH   cycle = %d, GH   bytes = %d",
-                         head_cycle, head_bytes);
-               ASSERT(0);
-               free_bytes = log->l_logsize;
+               return tail_bytes - head_bytes;
        }
-       return free_bytes;
+
+       /*
+        * The reservation head is behind the tail. In this case we just want to
+        * return the size of the log as the amount of space left.
+        */
+       xfs_alert(log->l_mp, "xlog_space_left: head behind tail");
+       xfs_alert(log->l_mp, "  tail_cycle = %d, tail_bytes = %d",
+                 tail_cycle, tail_bytes);
+       xfs_alert(log->l_mp, "  GH   cycle = %d, GH   bytes = %d",
+                 head_cycle, head_bytes);
+       ASSERT(0);
+       return log->l_logsize;
 }