--- old/usr/src/lib/libzfs/common/libzfs.h	2013-04-25 12:27:55.570838753 +0000
+++ new/usr/src/lib/libzfs/common/libzfs.h	2013-04-25 12:27:55.577593287 +0000
@@ -595,6 +595,9 @@
 extern int zfs_promote(zfs_handle_t *);
 extern int zfs_hold(zfs_handle_t *, const char *, const char *,
     boolean_t, boolean_t, int);
+extern int zfs_hold_add(zfs_handle_t *, const char *, const char *,
+    boolean_t, nvlist_t *);
+extern int zfs_hold_apply(zfs_handle_t *, boolean_t, int, nvlist_t *);
 extern int zfs_release(zfs_handle_t *, const char *, const char *, boolean_t);
 extern int zfs_get_holds(zfs_handle_t *, nvlist_t **);
 extern uint64_t zvol_volsize_to_reservation(uint64_t, nvlist_t *);
--- old/usr/src/lib/libzfs/common/libzfs_dataset.c	2013-04-25 12:27:55.780376180 +0000
+++ new/usr/src/lib/libzfs/common/libzfs_dataset.c	2013-04-25 12:27:55.786508480 +0000
@@ -4101,24 +4101,65 @@
 }
 
 int
+zfs_hold_add(zfs_handle_t *zhp, const char *snapname, const char *tag,
+    boolean_t enoent_ok, nvlist_t *holds)
+{
+	zfs_handle_t *szhp;
+	char name[ZFS_MAXNAMELEN];
+	char errbuf[1024];
+	int ret;
+
+	(void) snprintf(name, sizeof (name),
+	    "%s@%s", zhp->zfs_name, snapname);
+
+	szhp = make_dataset_handle(zhp->zfs_hdl, name);
+	if (szhp) {
+		fnvlist_add_string(holds, name, tag);
+		zfs_close(szhp);
+		return (0);
+	}
+
+	ret = ENOENT;
+	if (enoent_ok)
+		return (ret);
+
+	(void) snprintf(errbuf, sizeof (errbuf),
+	    dgettext(TEXT_DOMAIN, "cannot hold snapshot '%s@%s'"),
+	    zhp->zfs_name, snapname);
+	(void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf);
+
+	return (ret);
+}
+
+int
 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
     boolean_t recursive, boolean_t enoent_ok, int cleanup_fd)
 {
 	int ret;
 	struct holdarg ha;
-	nvlist_t *errors;
-	libzfs_handle_t *hdl = zhp->zfs_hdl;
-	char errbuf[1024];
-	nvpair_t *elem;
 
 	ha.nvl = fnvlist_alloc();
 	ha.snapname = snapname;
 	ha.tag = tag;
 	ha.recursive = recursive;
 	(void) zfs_hold_one(zfs_handle_dup(zhp), &ha);
-	ret = lzc_hold(ha.nvl, cleanup_fd, &errors);
+	ret = zfs_hold_apply(zhp, enoent_ok, cleanup_fd, ha.nvl);
 	fnvlist_free(ha.nvl);
 
+	return (ret);
+}
+
+int
+zfs_hold_apply(zfs_handle_t *zhp, boolean_t enoent_ok, int cleanup_fd, nvlist_t *holds)
+{
+	int ret;
+	nvlist_t *errors;
+	libzfs_handle_t *hdl = zhp->zfs_hdl;
+	char errbuf[1024];
+	nvpair_t *elem;
+
+	ret = lzc_hold(holds, cleanup_fd, &errors);
+
 	if (ret == 0)
 		return (0);
 
@@ -4176,13 +4217,6 @@
 	return (ret);
 }
 
-struct releasearg {
-	nvlist_t *nvl;
-	const char *snapname;
-	const char *tag;
-	boolean_t recursive;
-};
-
 static int
 zfs_release_one(zfs_handle_t *zhp, void *arg)
 {
@@ -4199,6 +4233,7 @@
 		nvlist_t *holds = fnvlist_alloc();
 		fnvlist_add_boolean(holds, ha->tag);
 		fnvlist_add_nvlist(ha->nvl, name, holds);
+		fnvlist_free(holds);
 		zfs_close(szhp);
 	}
 
--- old/usr/src/lib/libzfs/common/libzfs_sendrecv.c	2013-04-25 12:27:56.101884635 +0000
+++ new/usr/src/lib/libzfs/common/libzfs_sendrecv.c	2013-04-25 12:27:56.108107276 +0000
@@ -793,6 +793,7 @@
 	int outfd;
 	boolean_t err;
 	nvlist_t *fss;
+	nvlist_t *snapholds;
 	avl_tree_t *fsavl;
 	snapfilter_cb_t *filter_cb;
 	void *filter_cb_arg;
@@ -951,14 +952,12 @@
 
 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
 
-	if (sdd->dryrun)
-		return (0);
-
 	/*
-	 * zfs_send() only opens a cleanup_fd for sends that need it,
-	 * e.g. replication and doall.
+	 * We process if snapholds is not NULL even if on a dry run as
+	 * this is used to pre-calculate the required holds so they can
+	 * be processed in one kernel request
 	 */
-	if (sdd->cleanup_fd == -1)
+	if (sdd->snapholds == NULL)
 		return (0);
 
 	thissnap = strchr(zhp->zfs_name, '@') + 1;
@@ -971,8 +970,8 @@
 	 * handle that error.
 	 */
 	if (pzhp) {
-		error = zfs_hold(pzhp, thissnap, sdd->holdtag,
-		    B_FALSE, B_TRUE, sdd->cleanup_fd);
+		error = zfs_hold_add(pzhp, thissnap, sdd->holdtag, B_TRUE,
+		    sdd->snapholds);
 		zfs_close(pzhp);
 	}
 
@@ -1542,8 +1541,10 @@
 			err = errno;
 			goto stderr_out;
 		}
+		sdd.snapholds = fnvlist_alloc();
 	} else {
 		sdd.cleanup_fd = -1;
+		sdd.snapholds = NULL;
 	}
 	if (flags->verbose) {
 		/*
@@ -1565,6 +1566,30 @@
 			    "total estimated size is %s\n"), buf);
 		}
 	}
+
+	if (sdd.snapholds != NULL) {
+		/* Holds are required */
+		if (!flags->verbose) {
+			/*
+			 * A verbose dry run wasn't done so do a non-verbose
+			 * dry run to collate snapshot hold's.
+			 */
+			sdd.dryrun = B_TRUE;
+			err = dump_filesystems(zhp, &sdd);
+			sdd.dryrun = flags->dryrun;
+		}
+
+		if (err != 0) {
+			fnvlist_free(sdd.snapholds);
+			goto stderr_out;
+		}
+
+		err = zfs_hold_apply(zhp, B_TRUE, sdd.cleanup_fd, sdd.snapholds);
+		fnvlist_free(sdd.snapholds);
+		if (err != 0)
+			goto stderr_out;
+	}
+	
 	err = dump_filesystems(zhp, &sdd);
 	fsavl_destroy(fsavl);
 	nvlist_free(fss);
--- old/usr/src/uts/common/fs/zfs/dsl_pool.c	2013-04-25 12:27:56.396708549 +0000
+++ new/usr/src/uts/common/fs/zfs/dsl_pool.c	2013-04-25 12:27:56.404169096 +0000
@@ -832,27 +832,46 @@
 void
 dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
 {
+	char *htag;
 	zap_attribute_t za;
 	zap_cursor_t zc;
 	objset_t *mos = dp->dp_meta_objset;
 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
+	uint64_t dsobj;
+	nvlist_t *holds, *tags;
+	dsl_dataset_t *ds;
+	char name[MAXNAMELEN];
 
 	if (zapobj == 0)
 		return;
 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
 
+	holds = fnvlist_alloc();
+
+	dsl_pool_config_enter(dp, FTAG);
 	for (zap_cursor_init(&zc, mos, zapobj);
 	    zap_cursor_retrieve(&zc, &za) == 0;
 	    zap_cursor_advance(&zc)) {
-		char *htag;
-		uint64_t dsobj;
-
 		htag = strchr(za.za_name, '-');
 		*htag = '\0';
 		++htag;
 		dsobj = strtonum(za.za_name, NULL);
-		dsl_dataset_user_release_tmp(dp, dsobj, htag);
+		if (dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds) == 0) {
+			dsl_dataset_name(ds, name);
+			if (nvlist_lookup_nvlist(holds, name, &tags) != 0) {
+				tags = fnvlist_alloc();
+				fnvlist_add_boolean(tags, htag);
+				fnvlist_add_nvlist(holds, name, tags);
+				fnvlist_free(tags);
+			} else {
+				fnvlist_add_boolean(tags, htag);
+			}
+			dsl_dataset_rele(ds, FTAG);
+		}
 	}
+	dsl_pool_config_exit(dp, FTAG);
+	dsl_dataset_user_release(holds, NULL);
+	fnvlist_free(holds);
 	zap_cursor_fini(&zc);
 }
 
--- old/usr/src/uts/common/fs/zfs/dsl_userhold.c	2013-04-25 12:27:56.605218513 +0000
+++ new/usr/src/uts/common/fs/zfs/dsl_userhold.c	2013-04-25 12:27:56.612591347 +0000
@@ -147,7 +147,6 @@
 	if (minor != 0) {
 		VERIFY0(dsl_pool_user_hold(dp, ds->ds_object,
 		    htag, now, tx));
-		dsl_register_onexit_hold_cleanup(ds, htag, minor);
 	}
 
 	spa_history_log_internal_ds(ds, "hold", tx,
@@ -166,6 +165,7 @@
 	for (pair = nvlist_next_nvpair(dduha->dduha_holds, NULL); pair != NULL;
 	    pair = nvlist_next_nvpair(dduha->dduha_holds, pair)) {
 		dsl_dataset_t *ds;
+
 		VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
 		dsl_dataset_user_hold_sync_one(ds, fnvpair_value_string(pair),
 		    dduha->dduha_minor, now, tx);
@@ -186,6 +186,7 @@
 {
 	dsl_dataset_user_hold_arg_t dduha;
 	nvpair_t *pair;
+	int ret;
 
 	pair = nvlist_next_nvpair(holds, NULL);
 	if (pair == NULL)
@@ -195,8 +196,12 @@
 	dduha.dduha_errlist = errlist;
 	dduha.dduha_minor = cleanup_minor;
 
-	return (dsl_sync_task(nvpair_name(pair), dsl_dataset_user_hold_check,
-	    dsl_dataset_user_hold_sync, &dduha, fnvlist_num_pairs(holds)));
+	ret = dsl_sync_task(nvpair_name(pair), dsl_dataset_user_hold_check,
+	    dsl_dataset_user_hold_sync, &dduha, fnvlist_num_pairs(holds));
+	if (ret == 0)
+		dsl_register_onexit_hold_cleanup(holds, cleanup_minor);
+
+	return (ret);
 }
 
 typedef struct dsl_dataset_user_release_arg {
@@ -351,13 +356,24 @@
 dsl_dataset_user_release(nvlist_t *holds, nvlist_t *errlist)
 {
 	dsl_dataset_user_release_arg_t ddura;
-	nvpair_t *pair;
+	nvpair_t *pair, *pair2;
 	int error;
 
 	pair = nvlist_next_nvpair(holds, NULL);
 	if (pair == NULL)
 		return (0);
 
+#ifdef _KERNEL
+	/*
+	 * The release may cause the snapshot to be destroyed; make sure it
+	 * is not mounted.
+	 */
+	for (pair2 = pair; pair2 != NULL;
+	    pair2 = nvlist_next_nvpair(holds, pair2)) {
+		zfs_unmount_snap(nvpair_name(pair2));
+	}
+#endif
+
 	ddura.ddura_holds = holds;
 	ddura.ddura_errlist = errlist;
 	ddura.ddura_todelete = fnvlist_alloc();
@@ -368,133 +384,38 @@
 	return (error);
 }
 
-typedef struct dsl_dataset_user_release_tmp_arg {
-	uint64_t ddurta_dsobj;
-	nvlist_t *ddurta_holds;
-	boolean_t ddurta_deleteme;
-} dsl_dataset_user_release_tmp_arg_t;
-
-static int
-dsl_dataset_user_release_tmp_check(void *arg, dmu_tx_t *tx)
-{
-	dsl_dataset_user_release_tmp_arg_t *ddurta = arg;
-	dsl_pool_t *dp = dmu_tx_pool(tx);
-	dsl_dataset_t *ds;
-	int error;
-
-	if (!dmu_tx_is_syncing(tx))
-		return (0);
-
-	error = dsl_dataset_hold_obj(dp, ddurta->ddurta_dsobj, FTAG, &ds);
-	if (error)
-		return (error);
-
-	error = dsl_dataset_user_release_check_one(ds,
-	    ddurta->ddurta_holds, &ddurta->ddurta_deleteme);
-	dsl_dataset_rele(ds, FTAG);
-	return (error);
-}
-
 static void
-dsl_dataset_user_release_tmp_sync(void *arg, dmu_tx_t *tx)
+dsl_dataset_user_release_onexit(void *arg)
 {
-	dsl_dataset_user_release_tmp_arg_t *ddurta = arg;
-	dsl_pool_t *dp = dmu_tx_pool(tx);
-	dsl_dataset_t *ds;
+	nvlist_t *holds = arg;
 
-	VERIFY0(dsl_dataset_hold_obj(dp, ddurta->ddurta_dsobj, FTAG, &ds));
-	dsl_dataset_user_release_sync_one(ds, ddurta->ddurta_holds, tx);
-	if (ddurta->ddurta_deleteme) {
-		ASSERT(ds->ds_userrefs == 0 &&
-		    ds->ds_phys->ds_num_children == 1 &&
-		    DS_IS_DEFER_DESTROY(ds));
-		dsl_destroy_snapshot_sync_impl(ds, B_FALSE, tx);
-	}
-	dsl_dataset_rele(ds, FTAG);
+	(void) dsl_dataset_user_release(holds, NULL);
+	fnvlist_free(holds);
 }
 
-/*
- * Called at spa_load time to release a stale temporary user hold.
- * Also called by the onexit code.
- */
 void
-dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, const char *htag)
+dsl_register_onexit_hold_cleanup(nvlist_t *holds, minor_t minor)
 {
-	dsl_dataset_user_release_tmp_arg_t ddurta;
-	dsl_dataset_t *ds;
-	int error;
-
-#ifdef _KERNEL
-	/* Make sure it is not mounted. */
-	dsl_pool_config_enter(dp, FTAG);
-	error = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
-	if (error == 0) {
-		char name[MAXNAMELEN];
-		dsl_dataset_name(ds, name);
-		dsl_dataset_rele(ds, FTAG);
-		dsl_pool_config_exit(dp, FTAG);
-		zfs_unmount_snap(name);
-	} else {
-		dsl_pool_config_exit(dp, FTAG);
-	}
-#endif
-
-	ddurta.ddurta_dsobj = dsobj;
-	ddurta.ddurta_holds = fnvlist_alloc();
-	fnvlist_add_boolean(ddurta.ddurta_holds, htag);
-
-	(void) dsl_sync_task(spa_name(dp->dp_spa),
-	    dsl_dataset_user_release_tmp_check,
-	    dsl_dataset_user_release_tmp_sync, &ddurta, 1);
-	fnvlist_free(ddurta.ddurta_holds);
-}
-
-typedef struct zfs_hold_cleanup_arg {
-	char zhca_spaname[MAXNAMELEN];
-	uint64_t zhca_spa_load_guid;
-	uint64_t zhca_dsobj;
-	char zhca_htag[MAXNAMELEN];
-} zfs_hold_cleanup_arg_t;
-
-static void
-dsl_dataset_user_release_onexit(void *arg)
-{
-	zfs_hold_cleanup_arg_t *ca = arg;
-	spa_t *spa;
-	int error;
+	nvlist_t *ca;
+	nvpair_t *pair;
+	char *htag;
 
-	error = spa_open(ca->zhca_spaname, &spa, FTAG);
-	if (error != 0) {
-		zfs_dbgmsg("couldn't release hold on pool=%s ds=%llu tag=%s "
-		    "because pool is no longer loaded",
-		    ca->zhca_spaname, ca->zhca_dsobj, ca->zhca_htag);
-		return;
-	}
-	if (spa_load_guid(spa) != ca->zhca_spa_load_guid) {
-		zfs_dbgmsg("couldn't release hold on pool=%s ds=%llu tag=%s "
-		    "because pool is no longer loaded (guid doesn't match)",
-		    ca->zhca_spaname, ca->zhca_dsobj, ca->zhca_htag);
-		spa_close(spa, FTAG);
-		return;
-	}
-
-	dsl_dataset_user_release_tmp(spa_get_dsl(spa),
-	    ca->zhca_dsobj, ca->zhca_htag);
-	kmem_free(ca, sizeof (zfs_hold_cleanup_arg_t));
-	spa_close(spa, FTAG);
-}
+	ca = fnvlist_alloc();
+	/*
+	 * Convert from hold format: nvl of snapname -> holdname
+	 * to release format: nvl of snapname -> { holdname, ... }
+	 */
+	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
+	    pair = nvlist_next_nvpair(holds, pair)) {
+		if (nvpair_value_string(pair, &htag) == 0) {
+			nvlist_t *tags;
 
-void
-dsl_register_onexit_hold_cleanup(dsl_dataset_t *ds, const char *htag,
-    minor_t minor)
-{
-	zfs_hold_cleanup_arg_t *ca = kmem_alloc(sizeof (*ca), KM_SLEEP);
-	spa_t *spa = dsl_dataset_get_spa(ds);
-	(void) strlcpy(ca->zhca_spaname, spa_name(spa),
-	    sizeof (ca->zhca_spaname));
-	ca->zhca_spa_load_guid = spa_load_guid(spa);
-	ca->zhca_dsobj = ds->ds_object;
-	(void) strlcpy(ca->zhca_htag, htag, sizeof (ca->zhca_htag));
+			tags = fnvlist_alloc();
+			fnvlist_add_boolean(tags, htag);
+			fnvlist_add_nvlist(ca, nvpair_name(pair), tags);
+			fnvlist_free(tags);
+		}
+	}
 	VERIFY0(zfs_onexit_add_cb(minor,
 	    dsl_dataset_user_release_onexit, ca, NULL));
 }
--- old/usr/src/uts/common/fs/zfs/sys/dsl_dataset.h	2013-04-25 12:27:56.798132415 +0000
+++ new/usr/src/uts/common/fs/zfs/sys/dsl_dataset.h	2013-04-25 12:27:56.803845795 +0000
@@ -187,8 +187,7 @@
 void dsl_dataset_disown(dsl_dataset_t *ds, void *tag);
 void dsl_dataset_name(dsl_dataset_t *ds, char *name);
 boolean_t dsl_dataset_tryown(dsl_dataset_t *ds, void *tag);
-void dsl_register_onexit_hold_cleanup(dsl_dataset_t *ds, const char *htag,
-    minor_t minor);
+void dsl_register_onexit_hold_cleanup(nvlist_t *holds, minor_t minor);
 uint64_t dsl_dataset_create_sync(dsl_dir_t *pds, const char *lastname,
     dsl_dataset_t *origin, uint64_t flags, cred_t *, dmu_tx_t *);
 uint64_t dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
--- old/usr/src/uts/common/fs/zfs/sys/dsl_userhold.h	2013-04-25 12:27:56.978505874 +0000
+++ new/usr/src/uts/common/fs/zfs/sys/dsl_userhold.h	2013-04-25 12:27:56.984531733 +0000
@@ -43,8 +43,6 @@
     nvlist_t *errlist);
 int dsl_dataset_user_release(nvlist_t *holds, nvlist_t *errlist);
 int dsl_dataset_get_holds(const char *dsname, nvlist_t *nvl);
-void dsl_dataset_user_release_tmp(struct dsl_pool *dp, uint64_t dsobj,
-    const char *htag);
 int dsl_dataset_user_hold_check_one(struct dsl_dataset *ds, const char *htag,
     boolean_t temphold, struct dmu_tx *tx);
 void dsl_dataset_user_hold_sync_one(struct dsl_dataset *ds, const char *htag,
--- old/usr/src/uts/common/fs/zfs/zfs_ioctl.c	2013-04-25 12:27:57.153603356 +0000
+++ new/usr/src/uts/common/fs/zfs/zfs_ioctl.c	2013-04-25 12:27:57.162939126 +0000
@@ -4980,16 +4980,6 @@
 static int
 zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist)
 {
-	nvpair_t *pair;
-
-	/*
-	 * The release may cause the snapshot to be destroyed; make sure it
-	 * is not mounted.
-	 */
-	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
-	    pair = nvlist_next_nvpair(holds, pair))
-		zfs_unmount_snap(nvpair_name(pair));
-
 	return (dsl_dataset_user_release(holds, errlist));
 }
 
