--- old/usr/src/cmd/ndmpd/ndmp/ndmpd_chkpnt.c	2013-05-01 01:43:39.884141142 +0000
+++ new/usr/src/cmd/ndmpd/ndmp/ndmpd_chkpnt.c	2013-05-01 01:43:39.890404952 +0000
@@ -203,7 +203,7 @@
 	}
 
 	p = strchr(snapname, '@') + 1;
-	if (zfs_hold(zhp, p, jname, recursive, B_FALSE, cleanup_fd) != 0) {
+	if (zfs_hold(zhp, p, jname, recursive, cleanup_fd) != 0) {
 		NDMP_LOG(LOG_ERR, "Cannot hold snapshot %s", p);
 		zfs_close(zhp);
 		return (-1);
--- old/usr/src/cmd/zfs/zfs_main.c	2013-05-01 01:43:40.073368258 +0000
+++ new/usr/src/cmd/zfs/zfs_main.c	2013-05-01 01:43:40.079581609 +0000
@@ -5164,8 +5164,7 @@
 			continue;
 		}
 		if (holding) {
-			if (zfs_hold(zhp, delim+1, tag, recursive,
-			    B_FALSE, -1) != 0)
+			if (zfs_hold(zhp, delim+1, tag, recursive, -1) != 0)
 				++errors;
 		} else {
 			if (zfs_release(zhp, delim+1, tag, recursive) != 0)
--- old/usr/src/lib/libzfs/common/libzfs.h	2013-05-01 01:43:40.473225394 +0000
+++ new/usr/src/lib/libzfs/common/libzfs.h	2013-05-01 01:43:40.479046172 +0000
@@ -594,7 +594,8 @@
 
 extern int zfs_promote(zfs_handle_t *);
 extern int zfs_hold(zfs_handle_t *, const char *, const char *,
-    boolean_t, boolean_t, int);
+    boolean_t, int);
+extern int zfs_hold_nvl(zfs_handle_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-05-01 01:43:40.684742319 +0000
+++ new/usr/src/lib/libzfs/common/libzfs_dataset.c	2013-05-01 01:43:40.690962674 +0000
@@ -4081,18 +4081,14 @@
 zfs_hold_one(zfs_handle_t *zhp, void *arg)
 {
 	struct holdarg *ha = arg;
-	zfs_handle_t *szhp;
 	char name[ZFS_MAXNAMELEN];
 	int rv = 0;
 
 	(void) snprintf(name, sizeof (name),
 	    "%s@%s", zhp->zfs_name, ha->snapname);
 
-	szhp = make_dataset_handle(zhp->zfs_hdl, name);
-	if (szhp) {
+	if (lzc_exists(name))
 		fnvlist_add_string(ha->nvl, name, ha->tag);
-		zfs_close(szhp);
-	}
 
 	if (ha->recursive)
 		rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha);
@@ -4102,23 +4098,33 @@
 
 int
 zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag,
-    boolean_t recursive, boolean_t enoent_ok, int cleanup_fd)
+    boolean_t recursive, 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_nvl(zhp, cleanup_fd, ha.nvl);
 	fnvlist_free(ha.nvl);
 
+	return (ret);
+}
+
+int
+zfs_hold_nvl(zfs_handle_t *zhp, 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);
 
@@ -4162,10 +4168,6 @@
 		case EEXIST:
 			(void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf);
 			break;
-		case ENOENT:
-			if (enoent_ok)
-				return (ENOENT);
-			/* FALLTHROUGH */
 		default:
 			(void) zfs_standard_error(hdl,
 			    fnvpair_value_int32(elem), errbuf);
@@ -4176,30 +4178,21 @@
 	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)
 {
 	struct holdarg *ha = arg;
-	zfs_handle_t *szhp;
 	char name[ZFS_MAXNAMELEN];
 	int rv = 0;
 
 	(void) snprintf(name, sizeof (name),
 	    "%s@%s", zhp->zfs_name, ha->snapname);
 
-	szhp = make_dataset_handle(zhp->zfs_hdl, name);
-	if (szhp) {
+	if (lzc_exists(name)) {
 		nvlist_t *holds = fnvlist_alloc();
 		fnvlist_add_boolean(holds, ha->tag);
 		fnvlist_add_nvlist(ha->nvl, name, holds);
-		zfs_close(szhp);
+		fnvlist_free(holds);
 	}
 
 	if (ha->recursive)
--- old/usr/src/lib/libzfs/common/libzfs_sendrecv.c	2013-05-01 01:43:41.017198294 +0000
+++ new/usr/src/lib/libzfs/common/libzfs_sendrecv.c	2013-05-01 01:43:41.023352674 +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;
@@ -942,41 +943,19 @@
 	return (0);
 }
 
-static int
-hold_for_send(zfs_handle_t *zhp, send_dump_data_t *sdd)
+static void
+gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
 {
-	zfs_handle_t *pzhp;
-	int error = 0;
-	char *thissnap;
-
 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
 
-	if (sdd->dryrun)
-		return (0);
-
 	/*
-	 * zfs_send() only opens a cleanup_fd for sends that need it,
+	 * zfs_send() only sets snapholds for sends that need them,
 	 * e.g. replication and doall.
 	 */
-	if (sdd->cleanup_fd == -1)
-		return (0);
-
-	thissnap = strchr(zhp->zfs_name, '@') + 1;
-	*(thissnap - 1) = '\0';
-	pzhp = zfs_open(zhp->zfs_hdl, zhp->zfs_name, ZFS_TYPE_DATASET);
-	*(thissnap - 1) = '@';
-
-	/*
-	 * It's OK if the parent no longer exists.  The send code will
-	 * handle that error.
-	 */
-	if (pzhp) {
-		error = zfs_hold(pzhp, thissnap, sdd->holdtag,
-		    B_FALSE, B_TRUE, sdd->cleanup_fd);
-		zfs_close(pzhp);
-	}
+	if (sdd->snapholds == NULL)
+		return;
 
-	return (error);
+	fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
 }
 
 static void *
@@ -1043,17 +1022,12 @@
 	    strcmp(sdd->fromsnap, thissnap) == 0);
 
 	if (!sdd->seenfrom && isfromsnap) {
-		err = hold_for_send(zhp, sdd);
-		if (err == 0) {
-			sdd->seenfrom = B_TRUE;
-			(void) strcpy(sdd->prevsnap, thissnap);
-			sdd->prevsnap_obj = zfs_prop_get_int(zhp,
-			    ZFS_PROP_OBJSETID);
-		} else if (err == ENOENT) {
-			err = 0;
-		}
+		gather_holds(zhp, sdd);
+		sdd->seenfrom = B_TRUE;
+		(void) strcpy(sdd->prevsnap, thissnap);
+		sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
 		zfs_close(zhp);
-		return (err);
+		return (0);
 	}
 
 	if (sdd->seento || !sdd->seenfrom) {
@@ -1104,14 +1078,7 @@
 		return (0);
 	}
 
-	err = hold_for_send(zhp, sdd);
-	if (err) {
-		if (err == ENOENT)
-			err = 0;
-		zfs_close(zhp);
-		return (err);
-	}
-
+	gather_holds(zhp, sdd);
 	fromorigin = sdd->prevsnap[0] == '\0' &&
 	    (sdd->fromorigin || sdd->replicate);
 
@@ -1542,8 +1509,10 @@
 			err = errno;
 			goto stderr_out;
 		}
+		sdd.snapholds = fnvlist_alloc();
 	} else {
 		sdd.cleanup_fd = -1;
+		sdd.snapholds = NULL;
 	}
 	if (flags->verbose) {
 		/*
@@ -1565,6 +1534,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 gather 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_nvl(zhp, 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/lib/libzfs_core/common/libzfs_core.c	2013-05-01 01:43:41.320836373 +0000
+++ new/usr/src/lib/libzfs_core/common/libzfs_core.c	2013-05-01 01:43:41.326555554 +0000
@@ -346,11 +346,22 @@
  * uncleanly, the holds will be released when the pool is next opened
  * or imported.
  *
- * The return value will be 0 if all holds were created. Otherwise the return
- * value will be the errno of a (unspecified) hold that failed, no holds will
- * be created, and the errlist will have an entry for each hold that
- * failed (name = snapshot).  The value in the errlist will be the error
- * code (int32).
+ * Holds for snapshots which don't exist will be skipped and have an entry
+ * added to errlist, but will not cause an overall failure, except in the
+ * case that all holds where skipped.
+ *
+ * The return value will be 0 if the nvl holds was empty or all holds, for
+ * snapshots that existed, were succesfully created and at least one hold
+ * was created.
+ *
+ * If none of the snapshots for the requested holds existed ENOENT will be
+ * returned.
+ *
+ * Otherwise the return value will be the errno of a (unspecified) hold that
+ * failed, no holds will be created.
+ *
+ * In all cases the errlist will have an entry for each hold that failed
+ * (name = snapshot), with its value being the error code (int32).
  */
 int
 lzc_hold(nvlist_t *holds, int cleanup_fd, nvlist_t **errlist)
@@ -387,11 +398,19 @@
  * The snapshots must all be in the same pool.
  * The value is a nvlist whose keys are the holds to remove.
  *
- * The return value will be 0 if all holds were removed.
- * Otherwise the return value will be the errno of a (unspecified) release
- * that failed, no holds will be released, and the errlist will have an
- * entry for each snapshot that has failed releases (name = snapshot).
- * The value in the errlist will be the error code (int32) of a failed release.
+ * Holds which failed to release because they didn't exist will have an entry
+ * added to errlist, but will not cause an overall failure.
+ *
+ * The return value will be 0 if the nvl holds was empty or all holds, that
+ * existed, were succesfully removed and at least one hold was removed.
+ *
+ * If none of the holds specified existed ENOENT will be returned.
+ *
+ * Otherwise the return value will be the errno of a (unspecified) hold that
+ * failed to release and no holds will be released.
+ *
+ * In all cases the errlist will have an entry for each hold that failed to
+ * to release.
  */
 int
 lzc_release(nvlist_t *holds, nvlist_t **errlist)
--- old/usr/src/uts/common/fs/zfs/dsl_pool.c	2013-05-01 01:43:41.521376467 +0000
+++ new/usr/src/uts/common/fs/zfs/dsl_pool.c	2013-05-01 01:43:41.529657308 +0000
@@ -836,23 +836,35 @@
 	zap_cursor_t zc;
 	objset_t *mos = dp->dp_meta_objset;
 	uint64_t zapobj = dp->dp_tmp_userrefs_obj;
+	nvlist_t *holds;
 
 	if (zapobj == 0)
 		return;
 	ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
 
+	holds = fnvlist_alloc();
+
 	for (zap_cursor_init(&zc, mos, zapobj);
 	    zap_cursor_retrieve(&zc, &za) == 0;
 	    zap_cursor_advance(&zc)) {
 		char *htag;
 		uint64_t dsobj;
+		nvlist_t *tags;
 
 		htag = strchr(za.za_name, '-');
 		*htag = '\0';
 		++htag;
-		dsobj = strtonum(za.za_name, NULL);
-		dsl_dataset_user_release_tmp(dp, dsobj, htag);
+		if (nvlist_lookup_nvlist(holds, za.za_name, &tags) != 0) {
+			tags = fnvlist_alloc();
+			fnvlist_add_boolean(tags, htag);
+			fnvlist_add_nvlist(holds, za.za_name, tags);
+			fnvlist_free(tags);
+		} else {
+			fnvlist_add_boolean(tags, htag);
+		}
 	}
+	dsl_dataset_user_release_tmp(dp, holds);
+	fnvlist_free(holds);
 	zap_cursor_fini(&zc);
 }
 
--- old/usr/src/uts/common/fs/zfs/dsl_userhold.c	2013-05-01 01:43:41.735693505 +0000
+++ new/usr/src/uts/common/fs/zfs/dsl_userhold.c	2013-05-01 01:43:41.741440999 +0000
@@ -36,9 +36,12 @@
 #include <sys/zap.h>
 
 typedef struct dsl_dataset_user_hold_arg {
+	spa_t *dduha_spa;
 	nvlist_t *dduha_holds;
+	nvlist_t *dduha_tmpholds;
 	nvlist_t *dduha_errlist;
 	minor_t dduha_minor;
+	boolean_t dduha_holds_created;
 } dsl_dataset_user_hold_arg_t;
 
 /*
@@ -84,10 +87,13 @@
 	dsl_pool_t *dp = dmu_tx_pool(tx);
 	nvpair_t *pair;
 	int rv = 0;
+	boolean_t holds_possible;
 
 	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS)
 		return (SET_ERROR(ENOTSUP));
 
+	holds_possible = B_FALSE;
+
 	for (pair = nvlist_next_nvpair(dduha->dduha_holds, NULL); pair != NULL;
 	    pair = nvlist_next_nvpair(dduha->dduha_holds, pair)) {
 		int error = 0;
@@ -103,6 +109,19 @@
 		if (error == 0) {
 			error = dsl_dataset_hold(dp,
 			    nvpair_name(pair), FTAG, &ds);
+
+			if (error == ENOENT) {
+				/*
+				 * We register ENOENT errors so they can be
+				 * correctly reported if needed, such as when
+				 * all holds fail.
+				 */
+				if (dduha->dduha_errlist != NULL) {
+					fnvlist_add_int32(dduha->dduha_errlist,
+					    nvpair_name(pair), error);
+				}
+				continue;
+			}
 		}
 		if (error == 0) {
 			error = dsl_dataset_user_hold_check_one(ds, htag,
@@ -111,17 +130,31 @@
 		}
 
 		if (error != 0) {
+			if (dduha->dduha_errlist != NULL) {
+				fnvlist_add_int32(dduha->dduha_errlist,
+				    nvpair_name(pair), error);
+			}
 			rv = error;
-			fnvlist_add_int32(dduha->dduha_errlist,
-			    nvpair_name(pair), error);
+		} else {
+			holds_possible = B_TRUE;
 		}
 	}
+
+	/*
+	 * Check that at least one hold will possibly be created,
+	 * otherwise fail.
+	 */
+	if (rv == 0 && !holds_possible)
+		rv = ENOENT;
+
 	return (rv);
 }
 
-void
-dsl_dataset_user_hold_sync_one(dsl_dataset_t *ds, const char *htag,
-    minor_t minor, uint64_t now, dmu_tx_t *tx)
+
+static void
+dsl_dataset_user_hold_sync_one_impl(dsl_dataset_user_hold_arg_t *dduha,
+    dsl_dataset_t *ds, const char *htag, minor_t minor, uint64_t now,
+    dmu_tx_t *tx)
 {
 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
 	objset_t *mos = dp->dp_meta_objset;
@@ -145,9 +178,22 @@
 	VERIFY0(zap_add(mos, zapobj, htag, 8, 1, &now, tx));
 
 	if (minor != 0) {
+		char name[MAXNAMELEN];
+		nvlist_t *tags;
+
 		VERIFY0(dsl_pool_user_hold(dp, ds->ds_object,
 		    htag, now, tx));
-		dsl_register_onexit_hold_cleanup(ds, htag, minor);
+		(void) snprintf(name, sizeof(name), "%llx",
+		    (u_longlong_t)ds->ds_object);
+
+		if (nvlist_lookup_nvlist(dduha->dduha_tmpholds, name, &tags) != 0) {
+			tags = fnvlist_alloc();
+			fnvlist_add_boolean(tags, htag);
+			fnvlist_add_nvlist(dduha->dduha_tmpholds, name, tags);
+			fnvlist_free(tags);
+		} else {
+			fnvlist_add_boolean(tags, htag);
+		}
 	}
 
 	spa_history_log_internal_ds(ds, "hold", tx,
@@ -155,6 +201,79 @@
 	    htag, minor != 0, ds->ds_userrefs);
 }
 
+typedef struct zfs_hold_cleanup_arg {
+	char zhca_spaname[MAXNAMELEN];
+	uint64_t zhca_spa_load_guid;
+	nvlist_t *zhca_holds;
+} zfs_hold_cleanup_arg_t;
+
+static void
+dsl_dataset_user_release_onexit(void *arg)
+{
+	zfs_hold_cleanup_arg_t *ca = (zfs_hold_cleanup_arg_t *)arg;
+	spa_t *spa;
+	int error;
+
+	error = spa_open(ca->zhca_spaname, &spa, FTAG);
+	if (error != 0) {
+		zfs_dbgmsg("couldn't release holds on pool=%s "
+		    "because pool is no longer loaded",
+		    ca->zhca_spaname);
+		return;
+	}
+	if (spa_load_guid(spa) != ca->zhca_spa_load_guid) {
+		zfs_dbgmsg("couldn't release holds on pool=%s "
+		    "because pool is no longer loaded (guid doesn't match)",
+		    ca->zhca_spaname);
+		spa_close(spa, FTAG);
+		return;
+	}
+
+	(void) dsl_dataset_user_release_tmp(spa_get_dsl(spa), ca->zhca_holds);
+	fnvlist_free(ca->zhca_holds);
+	kmem_free(ca, sizeof(zfs_hold_cleanup_arg_t));
+	spa_close(spa, FTAG);
+}
+
+static void
+dsl_register_onexit_hold_cleanup(spa_t *spa, nvlist_t *holds, minor_t minor)
+{
+	zfs_hold_cleanup_arg_t *ca;
+
+	if (minor == 0 || nvlist_next_nvpair(holds, NULL) == NULL) {
+		fnvlist_free(holds);
+		return;
+	}
+
+	ASSERT(spa != NULL);
+	ca = kmem_alloc(sizeof (*ca), KM_SLEEP);
+
+	(void) strlcpy(ca->zhca_spaname, spa_name(spa),
+	    sizeof (ca->zhca_spaname));
+	ca->zhca_spa_load_guid = spa_load_guid(spa);
+	ca->zhca_holds = holds;
+	VERIFY0(zfs_onexit_add_cb(minor,
+	    dsl_dataset_user_release_onexit, ca, NULL));
+}
+
+void
+dsl_dataset_user_hold_sync_one(dsl_dataset_t *ds, const char *htag,
+    minor_t minor, uint64_t now, dmu_tx_t *tx)
+{
+	dsl_dataset_user_hold_arg_t dduha;
+
+	dduha.dduha_spa = NULL;
+	dduha.dduha_holds = NULL;
+	dduha.dduha_tmpholds = fnvlist_alloc();
+	dduha.dduha_errlist = NULL;
+	dduha.dduha_minor = minor;
+	dduha.dduha_holds_created = B_FALSE;
+
+	dsl_dataset_user_hold_sync_one_impl(&dduha, ds, htag, minor, now, tx);
+	dsl_register_onexit_hold_cleanup(dsl_dataset_get_spa(ds),
+	    dduha.dduha_tmpholds, minor);
+}
+
 static void
 dsl_dataset_user_hold_sync(void *arg, dmu_tx_t *tx)
 {
@@ -166,56 +285,123 @@
 	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);
-		dsl_dataset_rele(ds, FTAG);
+		char *name;
+		int error;
+
+		name = nvpair_name(pair);
+		error = dsl_dataset_hold(dp, name, FTAG, &ds);
+		if (error == 0) {
+			dsl_dataset_user_hold_sync_one_impl(dduha, ds,
+			    fnvpair_value_string(pair), dduha->dduha_minor,
+			    now, tx);
+			dsl_dataset_rele(ds, FTAG);
+			dduha->dduha_holds_created = B_TRUE;
+		} else if (dduha->dduha_errlist != NULL) {
+			/*
+			 * We register ENOENT errors so they can be correctly
+			 * reported if needed, such as when all holds fail.
+			 */
+			fnvlist_add_int32(dduha->dduha_errlist, name, error);
+		}
 	}
+	dduha->dduha_spa = dp->dp_spa;
 }
 
 /*
+ * The full semantics of this function are described in the comment above
+ * lzc_hold().
+ *
+ * To summarize:
  * holds is nvl of snapname -> holdname
  * errlist will be filled in with snapname -> error
- * if cleanup_minor is not 0, the holds will be temporary, cleaned up
- * when the process exits.
  *
- * if any fails, all will fail.
+ * The snaphosts must all be in the same pool.
+ *
+ * Holds for snapshots that don't exist will be skipped.
+ *
+ * If none of the snapshots for requested holds exist then ENOENT will be
+ * returned.
+ *
+ * If cleanup_minor is not 0, the holds will be temporary, which will be cleaned
+ * up when the process exits.
+ *
+ * On success all the holds, for snapshots that existed, will be created and 0
+ * will be returned.
+ *
+ * On failure no holds will be created, the errlist will be filled in,
+ * and an errno will returned.
+ *
+ * In all cases the errlist will contain entries for holds where the snapshot
+ * didn't exist.
  */
 int
 dsl_dataset_user_hold(nvlist_t *holds, minor_t cleanup_minor, nvlist_t *errlist)
 {
 	dsl_dataset_user_hold_arg_t dduha;
 	nvpair_t *pair;
+	int ret;
 
 	pair = nvlist_next_nvpair(holds, NULL);
 	if (pair == NULL)
 		return (0);
 
+	dduha.dduha_spa = NULL;
 	dduha.dduha_holds = holds;
+	dduha.dduha_tmpholds = fnvlist_alloc();
 	dduha.dduha_errlist = errlist;
 	dduha.dduha_minor = cleanup_minor;
+	dduha.dduha_holds_created = B_FALSE;
+
+	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) {
+		/* Check we created at least one hold. */
+		if (dduha.dduha_holds_created) {
+			dsl_register_onexit_hold_cleanup(dduha.dduha_spa,
+			    dduha.dduha_tmpholds, cleanup_minor);
+		} else {
+			fnvlist_free(dduha.dduha_tmpholds);
+			ret = ENOENT;
+		}
+	} else {
+		fnvlist_free(dduha.dduha_tmpholds);
+	}
 
-	return (dsl_sync_task(nvpair_name(pair), dsl_dataset_user_hold_check,
-	    dsl_dataset_user_hold_sync, &dduha, fnvlist_num_pairs(holds)));
+	return (ret);
 }
 
+typedef int (dsl_holdfunc_t)(dsl_pool_t *dp, const char *name, void *tag,
+    dsl_dataset_t **dsp);
+
 typedef struct dsl_dataset_user_release_arg {
+	dsl_holdfunc_t *ddura_holdfunc;
 	nvlist_t *ddura_holds;
 	nvlist_t *ddura_todelete;
 	nvlist_t *ddura_errlist;
+	boolean_t ddura_holds_found;
 } dsl_dataset_user_release_arg_t;
 
+/* Place a dataset hold on the snapshot identified by passed dsobj string */
+static
+int dsl_dataset_hold_byobj(dsl_pool_t *dp, const char *dsobj, void *tag,
+    dsl_dataset_t **dsp)
+{
+	return dsl_dataset_hold_obj(dp, strtonum(dsobj, NULL), tag, dsp);
+}
+
 static int
-dsl_dataset_user_release_check_one(dsl_dataset_t *ds,
-    nvlist_t *holds, boolean_t *todelete)
+dsl_dataset_user_release_check_one(dsl_dataset_user_release_arg_t *ddura,
+    dsl_dataset_t *ds, nvlist_t *holds, boolean_t *todelete)
 {
 	uint64_t zapobj;
 	nvpair_t *pair;
 	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
 	int error;
 	int numholds = 0;
+	int ret;
 
 	*todelete = B_FALSE;
+	ret = 0;
 
 	if (!dsl_dataset_is_snapshot(ds))
 		return (SET_ERROR(EINVAL));
@@ -226,11 +412,14 @@
 
 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
 	    pair = nvlist_next_nvpair(holds, pair)) {
-		/* Make sure the hold exists */
 		uint64_t tmp;
+
 		error = zap_lookup(mos, zapobj, nvpair_name(pair), 8, 1, &tmp);
-		if (error == ENOENT)
-			error = SET_ERROR(ESRCH);
+		/* Non-existent holds aren't always fatal. */
+		if (error == ENOENT) {
+			ret = error;
+			continue;
+		}
 		if (error != 0)
 			return (error);
 		numholds++;
@@ -244,13 +433,18 @@
 			return (SET_ERROR(EBUSY));
 		*todelete = B_TRUE;
 	}
-	return (0);
+
+	if (numholds != 0)
+		ddura->ddura_holds_found = B_TRUE;
+
+	return (ret);
 }
 
 static int
 dsl_dataset_user_release_check(void *arg, dmu_tx_t *tx)
 {
 	dsl_dataset_user_release_arg_t *ddura = arg;
+	dsl_holdfunc_t *holdfunc = ddura->ddura_holdfunc;
 	dsl_pool_t *dp = dmu_tx_pool(tx);
 	nvpair_t *pair;
 	int rv = 0;
@@ -269,12 +463,17 @@
 		if (error != 0)
 			return (SET_ERROR(EINVAL));
 
-		error = dsl_dataset_hold(dp, name, FTAG, &ds);
+		error = holdfunc(dp, name, FTAG, &ds);
 		if (error == 0) {
 			boolean_t deleteme;
-			error = dsl_dataset_user_release_check_one(ds,
+			error = dsl_dataset_user_release_check_one(ddura, ds,
 			    holds, &deleteme);
-			if (error == 0 && deleteme) {
+			/*
+			 * Don't check for error == 0 as deleteme is only set
+			 * to B_TRUE if it's correct to do so dispite the error
+			 * e.g. ENOENT.
+			 */
+			if (deleteme) {
 				fnvlist_add_boolean(ddura->ddura_todelete,
 				    name);
 			}
@@ -285,30 +484,55 @@
 				fnvlist_add_int32(ddura->ddura_errlist,
 				    name, error);
 			}
-			rv = error;
+			/* Non-existent holds aren't always fatal. */
+			if (error != ENOENT)
+				rv = error;
 		}
 	}
+
+	/*
+	 * None of the specified holds existed so avoid the overhead of a sync
+	 * and return ENOENT.
+	 */
+	if (rv == 0 && !ddura->ddura_holds_found)
+		rv = ENOENT;
+
 	return (rv);
 }
 
 static void
-dsl_dataset_user_release_sync_one(dsl_dataset_t *ds, nvlist_t *holds,
-    dmu_tx_t *tx)
+dsl_dataset_user_release_sync_one(dsl_dataset_user_release_arg_t *ddura,
+    dsl_dataset_t *ds, nvlist_t *holds, dmu_tx_t *tx)
 {
 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
 	objset_t *mos = dp->dp_meta_objset;
-	uint64_t zapobj;
-	int error;
 	nvpair_t *pair;
 
 	for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
 	    pair = nvlist_next_nvpair(holds, pair)) {
-		ds->ds_userrefs--;
-		error = dsl_pool_user_release(dp, ds->ds_object,
-		    nvpair_name(pair), tx);
+		uint64_t zapobj;
+		int error;
+		char *name;
+
+		name = nvpair_name(pair);
+
+		/* Remove temporary hold if one exists. */
+		error = dsl_pool_user_release(dp, ds->ds_object, name, tx);
 		VERIFY(error == 0 || error == ENOENT);
+
+		/* Remove user hold if one exists. */
 		zapobj = ds->ds_phys->ds_userrefs_obj;
-		VERIFY0(zap_remove(mos, zapobj, nvpair_name(pair), tx));
+		error = zap_remove(mos, zapobj, name, tx);
+		if (error == ENOENT)
+			continue;
+		VERIFY0(error);
+
+		/* Only if we removed a hold do we decrement userrefs. */
+		mutex_enter(&ds->ds_lock);
+		ds->ds_userrefs--;
+		mutex_exit(&ds->ds_lock);
+
+		ddura->ddura_holds_found = B_TRUE;
 
 		spa_history_log_internal_ds(ds, "release", tx,
 		    "tag=%s refs=%lld", nvpair_name(pair),
@@ -320,18 +544,31 @@
 dsl_dataset_user_release_sync(void *arg, dmu_tx_t *tx)
 {
 	dsl_dataset_user_release_arg_t *ddura = arg;
+	dsl_holdfunc_t *holdfunc = ddura->ddura_holdfunc;
 	dsl_pool_t *dp = dmu_tx_pool(tx);
 	nvpair_t *pair;
 
+	/*
+	 * Even though check suggested that at least one of our holds where
+	 * found this may have changed. Recalculate ddura_holds_found so that
+	 * we can return ENOENT from the caller in the case that no holds
+	 * where actually released.
+	 */
+	ddura->ddura_holds_found = B_FALSE;
+
 	for (pair = nvlist_next_nvpair(ddura->ddura_holds, NULL); pair != NULL;
 	    pair = nvlist_next_nvpair(ddura->ddura_holds, pair)) {
 		dsl_dataset_t *ds;
+		int error;
 
-		VERIFY0(dsl_dataset_hold(dp, nvpair_name(pair), FTAG, &ds));
-		dsl_dataset_user_release_sync_one(ds,
+		error = holdfunc(dp, nvpair_name(pair), FTAG, &ds);
+		if (error == ENOENT)
+			continue;
+		VERIFY0(error);
+
+		dsl_dataset_user_release_sync_one(ddura, ds,
 		    fnvpair_value_nvlist(pair), tx);
-		if (nvlist_exists(ddura->ddura_todelete,
-		    nvpair_name(pair))) {
+		if (nvlist_exists(ddura->ddura_todelete, nvpair_name(pair))) {
 			ASSERT(ds->ds_userrefs == 0 &&
 			    ds->ds_phys->ds_num_children == 1 &&
 			    DS_IS_DEFER_DESTROY(ds));
@@ -342,161 +579,112 @@
 }
 
 /*
+ * The full semantics of this function are described in the comment above
+ * lzc_release().
+ *
+ * To summarize:
+ * Releases holds specified in the nvl holds.
+ *
  * holds is nvl of snapname -> { holdname, ... }
  * errlist will be filled in with snapname -> error
+ * 
+ * If tmpdp is not NULL the names for holds should be the dbobj's of snapshots,
+ * otherwise they should be the names of shapshots.
  *
- * if any fails, all will fail.
+ * As a release may cause snapshots to be destroyed this trys to ensure they
+ * aren't mounted.
+ *
+ * The release of non-existent holds are skipped.
+ *
+ * At least one hold must have been released for the this function to succeed
+ * and return 0.
  */
-int
-dsl_dataset_user_release(nvlist_t *holds, nvlist_t *errlist)
+static int
+dsl_dataset_user_release_impl(nvlist_t *holds, nvlist_t *errlist,
+    dsl_pool_t *tmpdp)
 {
 	dsl_dataset_user_release_arg_t ddura;
 	nvpair_t *pair;
+	char *pool;
 	int error;
 
 	pair = nvlist_next_nvpair(holds, NULL);
 	if (pair == NULL)
 		return (0);
 
+#ifdef _KERNEL
+	/*
+	 * The release may cause snapshots to be destroyed; make sure they
+	 * are not mounted.
+	 */
+	if (tmpdp != NULL) {
+		/* Temporary holds are specified by dbobj. */
+		ddura.ddura_holdfunc = dsl_dataset_hold_byobj;
+		pool = spa_name(tmpdp->dp_spa);
+
+		dsl_pool_config_enter(tmpdp, FTAG);
+		for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
+		    pair = nvlist_next_nvpair(holds, pair)) {
+			dsl_dataset_t *ds;
+
+			error = dsl_dataset_hold_byobj(tmpdp, nvpair_name(pair),
+			    FTAG, &ds);
+			if (error == 0) {
+				char name[MAXNAMELEN];
+				dsl_dataset_name(ds, name);
+				dsl_dataset_rele(ds, FTAG);
+				zfs_unmount_snap(name);
+			}
+		}
+		dsl_pool_config_exit(tmpdp, FTAG);
+	} else {
+		/* Non-temporary holds are specified by name. */
+		ddura.ddura_holdfunc = dsl_dataset_hold;
+		pool = nvpair_name(pair);
+
+		for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL;
+		    pair = nvlist_next_nvpair(holds, pair))
+			zfs_unmount_snap(nvpair_name(pair));
+	}
+#endif
+
 	ddura.ddura_holds = holds;
 	ddura.ddura_errlist = errlist;
 	ddura.ddura_todelete = fnvlist_alloc();
+	ddura.ddura_holds_found = B_FALSE;
 
-	error = dsl_sync_task(nvpair_name(pair), dsl_dataset_user_release_check,
-	    dsl_dataset_user_release_sync, &ddura, fnvlist_num_pairs(holds));
+	error = dsl_sync_task(pool, dsl_dataset_user_release_check,
+	    dsl_dataset_user_release_sync, &ddura,
+	    fnvlist_num_pairs(holds));
 	fnvlist_free(ddura.ddura_todelete);
-	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;
+	/* If at least one hold wasn't removed return ENOENT. */
+	if (error == 0 && !ddura.ddura_holds_found)
+		error = ENOENT;
 
-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_tmp_arg_t *ddurta = arg;
-	dsl_pool_t *dp = dmu_tx_pool(tx);
-	dsl_dataset_t *ds;
-
-	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);
-}
-
 /*
- * Called at spa_load time to release a stale temporary user hold.
- * Also called by the onexit code.
+ * holds is nvl of snapname -> { holdname, ... }
+ * errlist will be filled in with snapname -> error
+ *
+ * if any fails, all will fail.
  */
-void
-dsl_dataset_user_release_tmp(dsl_pool_t *dp, uint64_t dsobj, const char *htag)
-{
-	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)
+int
+dsl_dataset_user_release(nvlist_t *holds, nvlist_t *errlist)
 {
-	zfs_hold_cleanup_arg_t *ca = arg;
-	spa_t *spa;
-	int error;
-
-	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);
+	return dsl_dataset_user_release_impl(holds, errlist, NULL);
 }
 
+/*
+ * holds is nvl of snapdsobj -> { holdname, ... }
+ */
 void
-dsl_register_onexit_hold_cleanup(dsl_dataset_t *ds, const char *htag,
-    minor_t minor)
+dsl_dataset_user_release_tmp(struct dsl_pool *dp, nvlist_t *holds)
 {
-	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));
-	VERIFY0(zfs_onexit_add_cb(minor,
-	    dsl_dataset_user_release_onexit, ca, NULL));
+	ASSERT(dp != NULL);
+	(void) dsl_dataset_user_release_impl(holds, NULL, dp);
 }
 
 int
--- old/usr/src/uts/common/fs/zfs/sys/dsl_dataset.h	2013-05-01 01:43:41.947000334 +0000
+++ new/usr/src/uts/common/fs/zfs/sys/dsl_dataset.h	2013-05-01 01:43:41.952772971 +0000
@@ -187,8 +187,6 @@
 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);
 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-05-01 01:43:42.138651262 +0000
+++ new/usr/src/uts/common/fs/zfs/sys/dsl_userhold.h	2013-05-01 01:43:42.144334950 +0000
@@ -43,8 +43,7 @@
     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);
+void dsl_dataset_user_release_tmp(struct dsl_pool *dp, nvlist_t *holds);
 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-05-01 01:43:42.318581181 +0000
+++ new/usr/src/uts/common/fs/zfs/zfs_ioctl.c	2013-05-01 01:43:42.324798246 +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));
 }
 
