. ./common/preamble
 _begin_fstest auto freeze log metadata quick
 
-# Import common functions.
+_require_scratch
+_require_freeze
+_require_xfs_sysfs $(_short_dev $TEST_DEV)/log
+_require_command "$KILLALL_PROG" killall
+
+. ./common/filter
+
+devname=`_short_dev $SCRATCH_DEV`
+attrprefix="/sys/fs/xfs/$devname/log"
 
 # Override the default cleanup function.
 _cleanup()
        rm -f $tmp.*
 }
 
-# Use the information exported by XFS to sysfs to determine whether the log has
-# active reservations after a filesystem freeze.
-_check_scratch_log_state()
+#
+# The grant heads record reservations in bytes.
+#
+# The value is not exactly zero for complex reason.  In short: we must always
+# have space for at least one minimum sized log write between ailp->ail_head_lsn
+# and log->l_tail_lsn, and that is what is showing up in the grant head
+# reservation values.  We don't need to explicitly reserve it for the first
+# iclog write after mount, but we always end up with it being present after the
+# first checkpoint commits and the AIL returns the checkpoint's unused space
+# back to the grant head.
+#
+# Hence just check the value is between 0 and the maximum iclog size (256kB).
+#
+_check_scratch_log_state_new()
 {
-       devname=`_short_dev $SCRATCH_DEV`
-       attrpath="/sys/fs/xfs/$devname/log"
-
-       # freeze the fs to ensure data is synced and the log is flushed. this
-       # means no outstanding transactions, and thus no outstanding log
-       # reservations, should exist
-       xfs_freeze -f $SCRATCH_MNT
+       for attr in "reserve_grant_head_bytes" "write_grant_head_bytes"; do
+               space=`cat $attrprefix/$attr`
+               _within_tolerance $space 1024 0 $((256 * 1024))
+       done
+}
 
-       # the log head is exported in basic blocks and the log grant heads in
-       # bytes. convert the log head to bytes for precise comparison
-       log_head_cycle=`awk -F : '{ print $1 }' $attrpath/log_head_lsn`
-       log_head_bytes=`awk -F : '{ print $2 }' $attrpath/log_head_lsn`
+#
+# The log head is exported in basic blocks and the log grant heads in bytes.
+# Convert the log head to bytes for precise comparison.
+#
+_check_scratch_log_state_old()
+{
+       log_head_cycle=`awk -F : '{ print $1 }' $attrprefix/log_head_lsn`
+       log_head_bytes=`awk -F : '{ print $2 }' $attrprefix/log_head_lsn`
        log_head_bytes=$((log_head_bytes * 512))
 
        for attr in "reserve_grant_head" "write_grant_head"; do
-               cycle=`cat $attrpath/$attr | awk -F : '{ print $1 }'`
-               bytes=`cat $attrpath/$attr | awk -F : '{ print $2 }'`
+               cycle=`cat $attrprefix/$attr | awk -F : '{ print $1 }'`
+               bytes=`cat $attrprefix/$attr | awk -F : '{ print $2 }'`
 
                if [ $cycle != $log_head_cycle ] ||
                   [ $bytes != $log_head_bytes ]
                                "possible leak detected."
                fi
        done
-
-       xfs_freeze -u $SCRATCH_MNT
 }
 
+# Use the information exported by XFS to sysfs to determine whether the log has
+# active reservations after a filesystem freeze.
+_check_scratch_log_state()
+{
+       # freeze the fs to ensure data is synced and the log is flushed. this
+       # means no outstanding transactions, and thus no outstanding log
+       # reservations, should exist
+       xfs_freeze -f $SCRATCH_MNT
+
+       if [ -f "${attrprefix}/reserve_grant_head_bytes" ]; then
+           _check_scratch_log_state_new
+       else
+           _check_scratch_log_state_old
+       fi
 
-_require_scratch
-_require_freeze
-_require_xfs_sysfs $(_short_dev $TEST_DEV)/log
-_require_command "$KILLALL_PROG" killall
+       xfs_freeze -u $SCRATCH_MNT
+}
 
 echo "Silence is golden."