--- old/usr/src/uts/common/fs/zfs/dbuf.c	2013-05-01 11:13:42.694378788 +0200
+++ new/usr/src/uts/common/fs/zfs/dbuf.c	2013-05-01 11:13:42.186220566 +0200
@@ -22,6 +22,7 @@
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  * Copyright (c) 2013 by Delphix. All rights reserved.
+ * Copyright (c) 2013 Martin Matuska. All rights reserved.
  */
 
 #include <sys/zfs_context.h>
@@ -2682,7 +2683,7 @@
 		wp_flag = WP_SPILL;
 	wp_flag |= (db->db_state == DB_NOFILL) ? WP_NOFILL : 0;
 
-	dmu_write_policy(os, dn, db->db_level, wp_flag, &zp);
+	dmu_write_policy(os, dn, db->db_level, wp_flag, &zp, txg);
 	DB_DNODE_EXIT(db);
 
 	if (db->db_level == 0 && dr->dt.dl.dr_override_state == DR_OVERRIDDEN) {
--- old/usr/src/uts/common/fs/zfs/dmu.c	2013-05-01 11:13:43.600935029 +0200
+++ new/usr/src/uts/common/fs/zfs/dmu.c	2013-05-01 11:13:43.111401819 +0200
@@ -21,6 +21,7 @@
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright (c) 2013 by Delphix. All rights reserved.
+ * Copyright (c) 2013 Martin Matuska. All rights reserved.
  */
 
 #include <sys/dmu.h>
@@ -42,6 +43,7 @@
 #include <sys/zio_checksum.h>
 #include <sys/zio_compress.h>
 #include <sys/sa.h>
+#include <sys/zfeature.h>
 #ifdef _KERNEL
 #include <sys/vmsystm.h>
 #include <sys/zfs_znode.h>
@@ -1417,7 +1419,7 @@
 
 	DB_DNODE_ENTER(db);
 	dn = DB_DNODE(db);
-	dmu_write_policy(os, dn, db->db_level, WP_DMU_SYNC, &zp);
+	dmu_write_policy(os, dn, db->db_level, WP_DMU_SYNC, &zp, txg);
 	DB_DNODE_EXIT(db);
 
 	/*
@@ -1555,9 +1557,11 @@
 }
 
 int zfs_mdcomp_disable = 0;
+int zfs_mdcomp_lz4 = 0;
 
 void
-dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp)
+dmu_write_policy(objset_t *os, dnode_t *dn, int level, int wp, zio_prop_t *zp,
+    uint64_t txg)
 {
 	dmu_object_type_t type = dn ? dn->dn_type : DMU_OT_OBJSET;
 	boolean_t ismd = (level > 0 || DMU_OT_IS_METADATA(type) ||
@@ -1582,8 +1586,26 @@
 		 * XXX -- we should design a compression algorithm
 		 * that specializes in arrays of bps.
 		 */
-		compress = zfs_mdcomp_disable ? ZIO_COMPRESS_EMPTY :
-		    ZIO_COMPRESS_LZJB;
+		if (zfs_mdcomp_disable)
+			compress = ZIO_COMPRESS_EMPTY;
+		else if (zfs_mdcomp_lz4 && os->os_spa != NULL) {
+			zfeature_info_t *feat = &spa_feature_table
+			    [SPA_FEATURE_LZ4_COMPRESS];
+
+			if (spa_feature_is_active(os->os_spa, feat))
+				compress = ZIO_COMPRESS_LZ4;
+			else if (spa_feature_is_enabled(os->os_spa, feat)) {
+				dmu_tx_t *tx;
+
+				tx = dmu_tx_create_assigned(
+				    spa_get_dsl(os->os_spa), txg);
+				spa_feature_incr(os->os_spa, feat, tx);
+				dmu_tx_commit(tx);
+				compress = ZIO_COMPRESS_LZ4;
+			} else
+				compress = ZIO_COMPRESS_LZJB;
+		} else
+			compress = ZIO_COMPRESS_LZJB;
 
 		/*
 		 * Metadata always gets checksummed.  If the data
--- old/usr/src/uts/common/fs/zfs/dmu_objset.c	2013-05-01 11:13:44.422766459 +0200
+++ new/usr/src/uts/common/fs/zfs/dmu_objset.c	2013-05-01 11:13:43.900772205 +0200
@@ -21,6 +21,7 @@
 /*
  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  * Copyright (c) 2013 by Delphix. All rights reserved.
+ * Copyright (c) 2013 Martin Matuska. All rights reserved.
  */
 
 /* Portions Copyright 2010 Robert Milkowski */
@@ -988,7 +989,7 @@
 	    ZB_ROOT_OBJECT, ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
 	arc_release(os->os_phys_buf, &os->os_phys_buf);
 
-	dmu_write_policy(os, NULL, 0, 0, &zp);
+	dmu_write_policy(os, NULL, 0, 0, &zp, tx->tx_txg);
 
 	zio = arc_write(pio, os->os_spa, tx->tx_txg,
 	    os->os_rootbp, os->os_phys_buf, DMU_OS_IS_L2CACHEABLE(os), &zp,
--- old/usr/src/uts/common/fs/zfs/sys/dmu.h	2013-05-01 11:13:45.250305466 +0200
+++ new/usr/src/uts/common/fs/zfs/sys/dmu.h	2013-05-01 11:13:44.708013893 +0200
@@ -24,6 +24,7 @@
  * Copyright (c) 2012 by Delphix. All rights reserved.
  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
+ * Copyright (c) 2013 Martin Matuska. All rights reserved.
  */
 
 /* Portions Copyright 2010 Robert Milkowski */
@@ -399,7 +400,7 @@
 #define	WP_SPILL	0x4
 
 void dmu_write_policy(objset_t *os, struct dnode *dn, int level, int wp,
-    struct zio_prop *zp);
+    struct zio_prop *zp, uint64_t txg);
 /*
  * The bonus data is accessed more or less like a regular buffer.
  * You must dmu_bonus_hold() to get the buffer, which will give you a
