static int replmd_op_callback(struct ldb_request *req, struct ldb_reply *ares)
{
	int ret;
	struct replmd_replicated_request *ac =
		talloc_get_type_abort(req->context, struct replmd_replicated_request);
	struct replmd_private *replmd_private =
		talloc_get_type_abort(ldb_module_get_private(ac->module), struct replmd_private);
	struct nc_entry *modified_partition;
	struct ldb_control *partition_ctrl;
	const struct dsdb_control_current_partition *partition;

	struct ldb_control **controls;

	partition_ctrl = ldb_reply_get_control(ares, DSDB_CONTROL_CURRENT_PARTITION_OID);

	controls = ares->controls;
	if (ldb_request_get_control(ac->req,
				    DSDB_CONTROL_CURRENT_PARTITION_OID) == NULL) {
		/*
		 * Remove the current partition control from what we pass up
		 * the chain if it hasn't been requested manually.
		 */
		controls = ldb_controls_except_specified(ares->controls, ares,
							 partition_ctrl);
	}

	if (ares->error != LDB_SUCCESS) {
		struct GUID_txt_buf guid_txt;
		struct ldb_message *msg = NULL;
		char *s = NULL;

		if (ac->apply_mode == false) {
			DBG_NOTICE("Originating update failure. Error is: %s\n",
				   ldb_strerror(ares->error));
			return ldb_module_done(ac->req, controls,
					       ares->response, ares->error);
		}

		msg = ac->objs->objects[ac->index_current].msg;
		/*
		 * Set at DBG_NOTICE as once these start to happe, they
		 * will happen a lot until resolved, due to repeated
		 * replication.  The caller will probably print the
		 * ldb error string anyway.
		 */
		DBG_NOTICE("DRS replication apply failure for %s. Error is: %s\n",
			   ldb_dn_get_linearized(msg->dn),
			   ldb_strerror(ares->error));

		s = ldb_ldif_message_redacted_string(ldb_module_get_ctx(ac->module),
						     ac,
						     LDB_CHANGETYPE_ADD,
						     msg);

		DBG_INFO("Failing DRS %s replication message was %s:\n%s\n",
			 ac->search_msg == NULL ? "ADD" : "MODIFY",
			 GUID_buf_string(&ac->objs->objects[ac->index_current].object_guid,
					 &guid_txt),
			 s);
		talloc_free(s);
		return ldb_module_done(ac->req, controls,
				       ares->response, ares->error);
	}

	if (ares->type != LDB_REPLY_DONE) {
		ldb_set_errstring(ldb_module_get_ctx(ac->module), "Invalid reply type for notify\n!");
		return ldb_module_done(ac->req, NULL,
				       NULL, LDB_ERR_OPERATIONS_ERROR);
	}

	if (ac->apply_mode == false) {
		struct la_backlink *bl;
		/*
		 * process our backlink list after an replmd_add(),
		 * creating and deleting backlinks as necessary (this
		 * code is sync).  The other cases are handled inline
		 * with the modify.
		 */
		for (bl=ac->la_backlinks; bl; bl=bl->next) {
			ret = replmd_process_backlink(ac->module, bl, ac->req);
			if (ret != LDB_SUCCESS) {
				return ldb_module_done(ac->req, NULL,
						       NULL, ret);
			}
		}
	}
	
	if (!partition_ctrl) {
		ldb_set_errstring(ldb_module_get_ctx(ac->module),"No partition control on reply");
		return ldb_module_done(ac->req, NULL,
				       NULL, LDB_ERR_OPERATIONS_ERROR);
	}

	partition = talloc_get_type_abort(partition_ctrl->data,
				    struct dsdb_control_current_partition);

	if (ac->seq_num > 0) {
		for (modified_partition = replmd_private->ncs; modified_partition;
		     modified_partition = modified_partition->next) {
			if (ldb_dn_compare(modified_partition->dn, partition->dn) == 0) {
				break;
			}
		}

		if (modified_partition == NULL) {
			modified_partition = talloc_zero(replmd_private, struct nc_entry);
			if (!modified_partition) {
				ldb_oom(ldb_module_get_ctx(ac->module));
				return ldb_module_done(ac->req, NULL,
						       NULL, LDB_ERR_OPERATIONS_ERROR);
			}
			modified_partition->dn = ldb_dn_copy(modified_partition, partition->dn);
			if (!modified_partition->dn) {
				ldb_oom(ldb_module_get_ctx(ac->module));
				return ldb_module_done(ac->req, NULL,
						       NULL, LDB_ERR_OPERATIONS_ERROR);
			}
			DLIST_ADD(replmd_private->ncs, modified_partition);
		}

		if (ac->seq_num > modified_partition->mod_usn) {
			modified_partition->mod_usn = ac->seq_num;
			if (ac->is_urgent) {
				modified_partition->mod_usn_urgent = ac->seq_num;
			}
		}
		if (!ac->apply_mode) {
			replmd_private->originating_updates = true;
		}
	}

	if (ac->apply_mode) {
		ret = replmd_replicated_apply_isDeleted(ac);
		if (ret != LDB_SUCCESS) {
			return ldb_module_done(ac->req, NULL, NULL, ret);
		}
		return ret;
	} else {
		/* free the partition control container here, for the
		 * common path.  Other cases will have it cleaned up
		 * eventually with the ares */
		talloc_free(partition_ctrl);
		return ldb_module_done(ac->req, controls,
				       ares->response, LDB_SUCCESS);
	}
}
static int replmd_op_callback(struct ldb_request *req, struct ldb_reply *ares)
{
	int ret;
	struct replmd_replicated_request *ac =
		talloc_get_type_abort(req->context, struct replmd_replicated_request);
	struct replmd_private *replmd_private =
		talloc_get_type_abort(ldb_module_get_private(ac->module), struct replmd_private);
	struct nc_entry *modified_partition;
	struct ldb_control *partition_ctrl;
	const struct dsdb_control_current_partition *partition;

	struct ldb_control **controls;

	partition_ctrl = ldb_reply_get_control(ares, DSDB_CONTROL_CURRENT_PARTITION_OID);

	controls = ares->controls;
	if (ldb_request_get_control(ac->req,
				    DSDB_CONTROL_CURRENT_PARTITION_OID) == NULL) {
		 
		controls = ldb_controls_except_specified(ares->controls, ares,
							 partition_ctrl);
	}

	if (ares->error != LDB_SUCCESS) {
		struct GUID_txt_buf guid_txt;
		struct ldb_message *msg = NULL;
		char *s = NULL;

		if (ac->apply_mode == false) {
			DBG_NOTICE("Originating update failure. Error is: %s\n",
				   ldb_strerror(ares->error));
			return ldb_module_done(ac->req, controls,
					       ares->response, ares->error);
		}

		msg = ac->objs->objects[ac->index_current].msg;
		 
		DBG_NOTICE("DRS replication apply failure for %s. Error is: %s\n",
			   ldb_dn_get_linearized(msg->dn),
			   ldb_strerror(ares->error));

		s = ldb_ldif_message_redacted_string(ldb_module_get_ctx(ac->module),
						     ac,
						     LDB_CHANGETYPE_ADD,
						     msg);

		DBG_INFO("Failing DRS %s replication message was %s:\n%s\n",
			 ac->search_msg == NULL ? "ADD" : "MODIFY",
			 GUID_buf_string(&ac->objs->objects[ac->index_current].object_guid,
					 &guid_txt),
			 s);
		talloc_free(s);
		return ldb_module_done(ac->req, controls,
				       ares->response, ares->error);
	}

	if (ares->type != LDB_REPLY_DONE) {
		ldb_set_errstring(ldb_module_get_ctx(ac->module), "Invalid reply type for notify\n!");
		return ldb_module_done(ac->req, NULL,
				       NULL, LDB_ERR_OPERATIONS_ERROR);
	}

	if (ac->apply_mode == false) {
		struct la_backlink *bl;
		 
		for (bl=ac->la_backlinks; bl; bl=bl->next) {
			ret = replmd_process_backlink(ac->module, bl, ac->req);
			if (ret != LDB_SUCCESS) {
				return ldb_module_done(ac->req, NULL,
						       NULL, ret);
			}
		}
	}
	
	if (!partition_ctrl) {
		ldb_set_errstring(ldb_module_get_ctx(ac->module),"No partition control on reply");
		return ldb_module_done(ac->req, NULL,
				       NULL, LDB_ERR_OPERATIONS_ERROR);
	}

	partition = talloc_get_type_abort(partition_ctrl->data,
				    struct dsdb_control_current_partition);

	if (ac->seq_num > 0) {
		for (modified_partition = replmd_private->ncs; modified_partition;
		     modified_partition = modified_partition->next) {
			if (ldb_dn_compare(modified_partition->dn, partition->dn) == 0) {
				break;
			}
		}

		if (modified_partition == NULL) {
			modified_partition = talloc_zero(replmd_private, struct nc_entry);
			if (!modified_partition) {
				ldb_oom(ldb_module_get_ctx(ac->module));
				return ldb_module_done(ac->req, NULL,
						       NULL, LDB_ERR_OPERATIONS_ERROR);
			}
			modified_partition->dn = ldb_dn_copy(modified_partition, partition->dn);
			if (!modified_partition->dn) {
				ldb_oom(ldb_module_get_ctx(ac->module));
				return ldb_module_done(ac->req, NULL,
						       NULL, LDB_ERR_OPERATIONS_ERROR);
			}
			DLIST_ADD(replmd_private->ncs, modified_partition);
		}

		if (ac->seq_num > modified_partition->mod_usn) {
			modified_partition->mod_usn = ac->seq_num;
			if (ac->is_urgent) {
				modified_partition->mod_usn_urgent = ac->seq_num;
			}
		}
		if (!ac->apply_mode) {
			replmd_private->originating_updates = true;
		}
	}

	if (ac->apply_mode) {
		ret = replmd_replicated_apply_isDeleted(ac);
		if (ret != LDB_SUCCESS) {
			return ldb_module_done(ac->req, NULL, NULL, ret);
		}
		return ret;
	} else {
		 
		talloc_free(partition_ctrl);
		return ldb_module_done(ac->req, controls,
				       ares->response, LDB_SUCCESS);
	}
}