12654012Reza Sabdar/*
22654012Reza Sabdar * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
32654012Reza Sabdar * Use is subject to license terms.
42654012Reza Sabdar */
52654012Reza Sabdar
62654012Reza Sabdar/*
72654012Reza Sabdar * BSD 3 Clause License
82654012Reza Sabdar *
92654012Reza Sabdar * Copyright (c) 2007, The Storage Networking Industry Association.
102654012Reza Sabdar *
112654012Reza Sabdar * Redistribution and use in source and binary forms, with or without
122654012Reza Sabdar * modification, are permitted provided that the following conditions
132654012Reza Sabdar * are met:
142654012Reza Sabdar * 	- Redistributions of source code must retain the above copyright
152654012Reza Sabdar *	  notice, this list of conditions and the following disclaimer.
162654012Reza Sabdar *
172654012Reza Sabdar * 	- Redistributions in binary form must reproduce the above copyright
182654012Reza Sabdar *	  notice, this list of conditions and the following disclaimer in
192654012Reza Sabdar *	  the documentation and/or other materials provided with the
202654012Reza Sabdar *	  distribution.
212654012Reza Sabdar *
222654012Reza Sabdar *	- Neither the name of The Storage Networking Industry Association (SNIA)
232654012Reza Sabdar *	  nor the names of its contributors may be used to endorse or promote
242654012Reza Sabdar *	  products derived from this software without specific prior written
252654012Reza Sabdar *	  permission.
262654012Reza Sabdar *
272654012Reza Sabdar * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
282654012Reza Sabdar * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
292654012Reza Sabdar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
302654012Reza Sabdar * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
312654012Reza Sabdar * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
322654012Reza Sabdar * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
332654012Reza Sabdar * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
342654012Reza Sabdar * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
352654012Reza Sabdar * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
362654012Reza Sabdar * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
372654012Reza Sabdar * POSSIBILITY OF SUCH DAMAGE.
382654012Reza Sabdar */
392654012Reza Sabdar#include <locale.h>
402654012Reza Sabdar#include <stdlib.h>
412654012Reza Sabdar#include <strings.h>
422654012Reza Sabdar#include <unistd.h>
432654012Reza Sabdar#include <fcntl.h>
442654012Reza Sabdar#include <door.h>
452654012Reza Sabdar#include <thread.h>
462654012Reza Sabdar#include <ndmpd_door.h>
472654012Reza Sabdar#include <libndmp.h>
482654012Reza Sabdar
492654012Reza Sabdarstatic int ndmp_door_fildes = -1;
502654012Reza Sabdarstatic char *buf;
512654012Reza Sabdarstatic ndmp_door_ctx_t *dec_ctx;
522654012Reza Sabdarstatic ndmp_door_ctx_t *enc_ctx;
532654012Reza Sabdarstatic door_arg_t arg;
542654012Reza Sabdarstatic mutex_t ndmp_lock = DEFAULTMUTEX;
552654012Reza Sabdar
562654012Reza Sabdarstatic int ndmp_door_setup(int opcode);
572654012Reza Sabdarstatic int ndmp_door_call(void);
582654012Reza Sabdarstatic int ndmp_door_fini(void);
592654012Reza Sabdar
602654012Reza Sabdar/* ndmp library APIs */
612654012Reza Sabdarint
622654012Reza Sabdarndmp_get_devinfo(ndmp_devinfo_t **dip, size_t *size)
632654012Reza Sabdar{
642654012Reza Sabdar	ndmp_devinfo_t *dipptr;
652654012Reza Sabdar	int i;
662654012Reza Sabdar	int opcode = NDMP_DEVICES_GET_INFO;
672654012Reza Sabdar
682654012Reza Sabdar	(void) mutex_lock(&ndmp_lock);
692654012Reza Sabdar	if (ndmp_door_setup(opcode))
702654012Reza Sabdar		goto err;
712654012Reza Sabdar
722654012Reza Sabdar	if (ndmp_door_call())
732654012Reza Sabdar		goto err;
742654012Reza Sabdar
752654012Reza Sabdar	/* get the number of devices available */
762654012Reza Sabdar	*size = ndmp_door_get_uint32(dec_ctx);
772654012Reza Sabdar
782654012Reza Sabdar	*dip = malloc(sizeof (ndmp_devinfo_t) * *size);
792654012Reza Sabdar	if (!*dip) {
802654012Reza Sabdar		free(buf);
812654012Reza Sabdar		ndmp_errno = ENDMP_MEM_ALLOC;
822654012Reza Sabdar		goto err;
832654012Reza Sabdar	}
842654012Reza Sabdar	dipptr = *dip;
852654012Reza Sabdar	for (i = 0; i < *size; i++, dipptr++) {
862654012Reza Sabdar		dipptr->nd_dev_type = ndmp_door_get_int32(dec_ctx);
872654012Reza Sabdar		dipptr->nd_name = ndmp_door_get_string(dec_ctx);
882654012Reza Sabdar		dipptr->nd_lun = ndmp_door_get_int32(dec_ctx);
892654012Reza Sabdar		dipptr->nd_sid = ndmp_door_get_int32(dec_ctx);
902654012Reza Sabdar		dipptr->nd_vendor = ndmp_door_get_string(dec_ctx);
912654012Reza Sabdar		dipptr->nd_product = ndmp_door_get_string(dec_ctx);
922654012Reza Sabdar		dipptr->nd_revision = ndmp_door_get_string(dec_ctx);
937bc22e4Reza Sabdar		dipptr->nd_serial = ndmp_door_get_string(dec_ctx);
947bc22e4Reza Sabdar		dipptr->nd_wwn = ndmp_door_get_string(dec_ctx);
952654012Reza Sabdar	}
962654012Reza Sabdar	if (ndmp_door_fini()) {
972654012Reza Sabdar		free(*dip);
982654012Reza Sabdar		goto err;
992654012Reza Sabdar	}
1002654012Reza Sabdar	(void) mutex_unlock(&ndmp_lock);
1012654012Reza Sabdar	return (0);
1022654012Reza Sabdarerr:
1032654012Reza Sabdar	(void) mutex_unlock(&ndmp_lock);
1042654012Reza Sabdar	return (-1);
1052654012Reza Sabdar}
1062654012Reza Sabdar
1072654012Reza Sabdarvoid
1082654012Reza Sabdarndmp_get_devinfo_free(ndmp_devinfo_t *dip, size_t size)
1092654012Reza Sabdar{
1102654012Reza Sabdar	ndmp_devinfo_t *dipptr;
1112654012Reza Sabdar	int i;
1122654012Reza Sabdar
1132654012Reza Sabdar	dipptr = dip;
1142654012Reza Sabdar	for (i = 0; i < size; i++, dipptr++) {
1152654012Reza Sabdar		free(dipptr->nd_name);
1162654012Reza Sabdar		free(dipptr->nd_vendor);
1172654012Reza Sabdar		free(dipptr->nd_product);
1182654012Reza Sabdar		free(dipptr->nd_revision);
1192654012Reza Sabdar	}
1202654012Reza Sabdar	free(dip);
1212654012Reza Sabdar}
1222654012Reza Sabdar
1232654012Reza Sabdarint
1242654012Reza Sabdarndmp_terminate_session(int session)
1252654012Reza Sabdar{
1262654012Reza Sabdar	int ret;
1272654012Reza Sabdar	int opcode = NDMP_TERMINATE_SESSION_ID;
1282654012Reza Sabdar
1292654012Reza Sabdar	(void) mutex_lock(&ndmp_lock);
1302654012Reza Sabdar	if (ndmp_door_setup(opcode))
1312654012Reza Sabdar		goto err;
1322654012Reza Sabdar
1332654012Reza Sabdar	ndmp_door_put_uint32(enc_ctx, session);
1342654012Reza Sabdar	if (ndmp_door_call())
1352654012Reza Sabdar		goto err;
1362654012Reza Sabdar
1372654012Reza Sabdar	ret = ndmp_door_get_uint32(dec_ctx);
1382654012Reza Sabdar	if (ndmp_door_fini())
1392654012Reza Sabdar		goto err;
1402654012Reza Sabdar
1412654012Reza Sabdar	(void) mutex_unlock(&ndmp_lock);
1422654012Reza Sabdar	return (ret);
1432654012Reza Sabdarerr:
1442654012Reza Sabdar	(void) mutex_unlock(&ndmp_lock);
1452654012Reza Sabdar	return (-1);
1462654012Reza Sabdar}
1472654012Reza Sabdar
1482654012Reza Sabdarint
1492654012Reza Sabdarndmp_get_session_info(ndmp_session_info_t **sinfo, size_t *size)
1502654012Reza Sabdar{
1512654012Reza Sabdar	int status;
1522654012Reza Sabdar	int i, j;
1532654012Reza Sabdar	ndmp_session_info_t *sp;
1542654012Reza Sabdar	ndmp_dt_pval_t *ep;
1552654012Reza Sabdar	ndmp_dt_name_t *np;
1562654012Reza Sabdar	ndmp_dt_name_v3_t *npv3;
1572654012Reza Sabdar	int opcode = NDMP_SHOW;
1582654012Reza Sabdar
1592654012Reza Sabdar	(void) mutex_lock(&ndmp_lock);
1602654012Reza Sabdar	if (ndmp_door_setup(opcode))
1612654012Reza Sabdar		goto err;
1622654012Reza Sabdar
1632654012Reza Sabdar	if (ndmp_door_call())
1642654012Reza Sabdar		goto err;
1652654012Reza Sabdar
1662654012Reza Sabdar	/* number of sessions */
1672654012Reza Sabdar	*size = ndmp_door_get_int32(dec_ctx);
1682654012Reza Sabdar
1692654012Reza Sabdar	*sinfo = malloc((sizeof (ndmp_session_info_t)) * *size);
1702654012Reza Sabdar	if (!*sinfo) {
1712654012Reza Sabdar		free(buf);
1722654012Reza Sabdar		ndmp_errno = ENDMP_MEM_ALLOC;
1732654012Reza Sabdar		goto err;
1742654012Reza Sabdar	}
1752654012Reza Sabdar	sp = *sinfo;
1762654012Reza Sabdar	for (i = 0; i < *size; i++, sp++) {
1772654012Reza Sabdar		status = ndmp_door_get_int32(dec_ctx);
1782654012Reza Sabdar		if (status == NDMP_SESSION_NODATA)
1792654012Reza Sabdar			continue;
1802654012Reza Sabdar
1812654012Reza Sabdar		/* connection common info */
1822654012Reza Sabdar		sp->nsi_sid = ndmp_door_get_int32(dec_ctx);
1832654012Reza Sabdar		sp->nsi_pver = ndmp_door_get_int32(dec_ctx);
1842654012Reza Sabdar		sp->nsi_auth = ndmp_door_get_int32(dec_ctx);
1852654012Reza Sabdar		sp->nsi_eof = ndmp_door_get_int32(dec_ctx);
1862654012Reza Sabdar		sp->nsi_cl_addr = ndmp_door_get_string(dec_ctx);
1872654012Reza Sabdar		/*
1882654012Reza Sabdar		 * scsi and tape data are same for all version,
1892654012Reza Sabdar		 * so keep reading
1902654012Reza Sabdar		 */
1912654012Reza Sabdar		/* connection common scsi info.   */
1922654012Reza Sabdar		sp->nsi_scsi.ns_scsi_open = ndmp_door_get_int32(dec_ctx);
1932654012Reza Sabdar		sp->nsi_scsi.ns_adapter_name = ndmp_door_get_string(dec_ctx);
1942654012Reza Sabdar		sp->nsi_scsi.ns_valid_target_set = ndmp_door_get_int32(dec_ctx);
1952654012Reza Sabdar		if (sp->nsi_scsi.ns_valid_target_set) {
1962654012Reza Sabdar			sp->nsi_scsi.ns_scsi_id = ndmp_door_get_int32(dec_ctx);
1972654012Reza Sabdar			sp->nsi_scsi.ns_lun = ndmp_door_get_int32(dec_ctx);
1982654012Reza Sabdar		}
1992654012Reza Sabdar
2002654012Reza Sabdar		/* connection common tape info.   */
2012654012Reza Sabdar		sp->nsi_tape.nt_fd = ndmp_door_get_int32(dec_ctx);
2022654012Reza Sabdar		if (sp->nsi_tape.nt_fd != -1) {
2032654012Reza Sabdar			sp->nsi_tape.nt_rec_count =
2042654012Reza Sabdar			    ndmp_door_get_uint64(dec_ctx);
2052654012Reza Sabdar			sp->nsi_tape.nt_mode = ndmp_door_get_int32(dec_ctx);
2062654012Reza Sabdar			sp->nsi_tape.nt_dev_name =
2072654012Reza Sabdar			    ndmp_door_get_string(dec_ctx);
2082654012Reza Sabdar			sp->nsi_tape.nt_adapter_name =
2092654012Reza Sabdar			    ndmp_door_get_string(dec_ctx);
2102654012Reza Sabdar			sp->nsi_tape.nt_sid = ndmp_door_get_int32(dec_ctx);
2112654012Reza Sabdar			sp->nsi_tape.nt_lun = ndmp_door_get_int32(dec_ctx);
2122654012Reza Sabdar		}
2132654012Reza Sabdar		/* all the V2 mover data are same as V3/V4 */
2142654012Reza Sabdar		sp->nsi_mover.nm_state = ndmp_door_get_int32(dec_ctx);
2152654012Reza Sabdar		sp->nsi_mover.nm_mode = ndmp_door_get_int32(dec_ctx);
2162654012Reza Sabdar		sp->nsi_mover.nm_pause_reason = ndmp_door_get_int32(dec_ctx);
2172654012Reza Sabdar		sp->nsi_mover.nm_halt_reason = ndmp_door_get_int32(dec_ctx);
2182654012Reza Sabdar		sp->nsi_mover.nm_rec_size = ndmp_door_get_uint64(dec_ctx);
2192654012Reza Sabdar		sp->nsi_mover.nm_rec_num = ndmp_door_get_uint64(dec_ctx);
2202654012Reza Sabdar		sp->nsi_mover.nm_mov_pos = ndmp_door_get_uint64(dec_ctx);
2212654012Reza Sabdar		sp->nsi_mover.nm_window_offset = ndmp_door_get_uint64(dec_ctx);
2222654012Reza Sabdar		sp->nsi_mover.nm_window_length = ndmp_door_get_uint64(dec_ctx);
2232654012Reza Sabdar		sp->nsi_mover.nm_sock = ndmp_door_get_int32(dec_ctx);
2242654012Reza Sabdar
2252654012Reza Sabdar		/* Read V3/V4 mover info */
2262654012Reza Sabdar		if ((sp->nsi_pver == NDMP_V3) || (sp->nsi_pver == NDMP_V4)) {
2272654012Reza Sabdar			sp->nsi_mover.nm_listen_sock =
2282654012Reza Sabdar			    ndmp_door_get_int32(dec_ctx);
2292654012Reza Sabdar			sp->nsi_mover.nm_addr_type =
2302654012Reza Sabdar			    ndmp_door_get_int32(dec_ctx);
2312654012Reza Sabdar			sp->nsi_mover.nm_tcp_addr =
2322654012Reza Sabdar			    ndmp_door_get_string(dec_ctx);
2332654012Reza Sabdar		}
2342654012Reza Sabdar
2352654012Reza Sabdar		/* connection common data info */
2362654012Reza Sabdar		sp->nsi_data.nd_oper = ndmp_door_get_int32(dec_ctx);
2372654012Reza Sabdar		sp->nsi_data.nd_state = ndmp_door_get_int32(dec_ctx);
2382654012Reza Sabdar		sp->nsi_data.nd_halt_reason = ndmp_door_get_int32(dec_ctx);
2392654012Reza Sabdar		sp->nsi_data.nd_sock = ndmp_door_get_int32(dec_ctx);
2402654012Reza Sabdar		sp->nsi_data.nd_addr_type = ndmp_door_get_int32(dec_ctx);
2412654012Reza Sabdar		sp->nsi_data.nd_abort = ndmp_door_get_int32(dec_ctx);
2422654012Reza Sabdar		sp->nsi_data.nd_read_offset = ndmp_door_get_uint64(dec_ctx);
2432654012Reza Sabdar		sp->nsi_data.nd_read_length = ndmp_door_get_uint64(dec_ctx);
2442654012Reza Sabdar		sp->nsi_data.nd_total_size = ndmp_door_get_uint64(dec_ctx);
2452654012Reza Sabdar		sp->nsi_data.nd_env_len = ndmp_door_get_uint64(dec_ctx);
2462654012Reza Sabdar		sp->nsi_data.nd_env =
2472654012Reza Sabdar		    malloc(sizeof (ndmp_dt_pval_t) * sp->nsi_data.nd_env_len);
2482654012Reza Sabdar		if (!sp->nsi_data.nd_env) {
2492654012Reza Sabdar			free(buf);
2502654012Reza Sabdar			ndmp_errno = ENDMP_MEM_ALLOC;
2512654012Reza Sabdar			goto err;
2522654012Reza Sabdar		}
2532654012Reza Sabdar		ep = sp->nsi_data.nd_env;
2542654012Reza Sabdar		for (j = 0; j < sp->nsi_data.nd_env_len; j++, ep++) {
2552654012Reza Sabdar			ep->np_name = ndmp_door_get_string(dec_ctx);
2562654012Reza Sabdar			ep->np_value = ndmp_door_get_string(dec_ctx);
2572654012Reza Sabdar		}
2582654012Reza Sabdar		sp->nsi_data.nd_tcp_addr = ndmp_door_get_string(dec_ctx);
2592654012Reza Sabdar
2602654012Reza Sabdar		/* Read V2 data info */
2612654012Reza Sabdar		if (sp->nsi_pver == NDMP_V2) {
2622654012Reza Sabdar			sp->nsi_data.nld_nlist_len =
2632654012Reza Sabdar			    ndmp_door_get_int64(dec_ctx);
2642654012Reza Sabdar			sp->nsi_data.nd_nlist.nld_nlist =
2652654012Reza Sabdar			    malloc(sizeof (ndmp_dt_name_t) *
2662654012Reza Sabdar			    sp->nsi_data.nld_nlist_len);
2672654012Reza Sabdar			if (!sp->nsi_data.nd_nlist.nld_nlist) {
2682654012Reza Sabdar				free(buf);
2692654012Reza Sabdar				ndmp_errno = ENDMP_MEM_ALLOC;
2702654012Reza Sabdar				goto err;
2712654012Reza Sabdar			}
2722654012Reza Sabdar			np = sp->nsi_data.nd_nlist.nld_nlist;
2732654012Reza Sabdar
2742654012Reza Sabdar			for (j = 0; j < sp->nsi_data.nld_nlist_len; j++, np++) {
2752654012Reza Sabdar				np->nn_name = ndmp_door_get_string(dec_ctx);
2762654012Reza Sabdar				np->nn_dest = ndmp_door_get_string(dec_ctx);
2772654012Reza Sabdar			}
2782654012Reza Sabdar		} else if ((sp->nsi_pver == NDMP_V3) ||
2792654012Reza Sabdar		    (sp->nsi_pver == NDMP_V4)) {
2802654012Reza Sabdar			/* Read V3/V4 data info */
2812654012Reza Sabdar			sp->nsi_data.nd_nlist.nld_dt_v3.dv3_listen_sock =
2822654012Reza Sabdar			    ndmp_door_get_int32(dec_ctx);
2832654012Reza Sabdar			sp->nsi_data.nd_nlist.nld_dt_v3.dv3_bytes_processed =
2842654012Reza Sabdar			    ndmp_door_get_uint64(dec_ctx);
2852654012Reza Sabdar			sp->nsi_data.nld_nlist_len =
2862654012Reza Sabdar			    ndmp_door_get_uint64(dec_ctx);
2872654012Reza Sabdar			sp->nsi_data.nd_nlist.nld_dt_v3.dv3_nlist =
2882654012Reza Sabdar			    malloc(sizeof (ndmp_dt_name_v3_t) *
2892654012Reza Sabdar			    sp->nsi_data.nld_nlist_len);
2902654012Reza Sabdar			if (!sp->nsi_data.nd_nlist.nld_dt_v3.dv3_nlist) {
2912654012Reza Sabdar				free(buf);
2922654012Reza Sabdar				ndmp_errno = ENDMP_MEM_ALLOC;
2932654012Reza Sabdar				goto err;
2942654012Reza Sabdar			}
2952654012Reza Sabdar			npv3 = sp->nsi_data.nd_nlist.nld_dt_v3.dv3_nlist;
2962654012Reza Sabdar			for (j = 0; j < sp->nsi_data.nld_nlist_len;
2972654012Reza Sabdar			    j++, npv3++) {
2982654012Reza Sabdar				npv3->nn3_opath = ndmp_door_get_string(dec_ctx);
2992654012Reza Sabdar				npv3->nn3_dpath = ndmp_door_get_string(dec_ctx);
3002654012Reza Sabdar				npv3->nn3_node = ndmp_door_get_uint64(dec_ctx);
3012654012Reza Sabdar				npv3->nn3_fh_info =
3022654012Reza Sabdar				    ndmp_door_get_uint64(dec_ctx);
3032654012Reza Sabdar			}
3042654012Reza Sabdar		}
3052654012Reza Sabdar	}
3062654012Reza Sabdar
3072654012Reza Sabdar	if (ndmp_door_fini())
3082654012Reza Sabdar		goto err;
3092654012Reza Sabdar
3102654012Reza Sabdar	(void) mutex_unlock(&ndmp_lock);
3112654012Reza Sabdar	return (0);
3122654012Reza Sabdarerr:
3132654012Reza Sabdar	(void) mutex_unlock(&ndmp_lock);
3142654012Reza Sabdar	return (-1);
3152654012Reza Sabdar}
3162654012Reza Sabdar
3172654012Reza Sabdarvoid
3182654012Reza Sabdarndmp_get_session_info_free(ndmp_session_info_t *sinfo, size_t size)
3192654012Reza Sabdar{
3202654012Reza Sabdar	ndmp_session_info_t *sp;
3212654012Reza Sabdar	ndmp_dt_pval_t *ep;
3222654012Reza Sabdar	ndmp_dt_name_t *np;
3232654012Reza Sabdar	ndmp_dt_name_v3_t *npv3;
3242654012Reza Sabdar	int i, j;
3252654012Reza Sabdar
3262654012Reza Sabdar	sp = sinfo;
3272654012Reza Sabdar	for (i = 0; i < size; i++, sp++) {
3282654012Reza Sabdar		free(sp->nsi_cl_addr);
3292654012Reza Sabdar		free(sp->nsi_scsi.ns_adapter_name);
3302654012Reza Sabdar		if (sp->nsi_tape.nt_fd != -1) {
3312654012Reza Sabdar			free(sp->nsi_tape.nt_dev_name);
3322654012Reza Sabdar			free(sp->nsi_tape.nt_adapter_name);
3332654012Reza Sabdar		}
3342654012Reza Sabdar		if ((sp->nsi_pver == NDMP_V3) || (sp->nsi_pver == NDMP_V4))
3352654012Reza Sabdar			free(sp->nsi_mover.nm_tcp_addr);
3362654012Reza Sabdar
3372654012Reza Sabdar		ep = sp->nsi_data.nd_env;
3382654012Reza Sabdar		for (j = 0; j < sp->nsi_data.nd_env_len; j++, ep++) {
3392654012Reza Sabdar			free(ep->np_name);
3402654012Reza Sabdar			free(ep->np_value);
3412654012Reza Sabdar		}
3422654012Reza Sabdar		free(sp->nsi_data.nd_env);
3432654012Reza Sabdar		free(sp->nsi_data.nd_tcp_addr);
3442654012Reza Sabdar
3452654012Reza Sabdar		if (sp->nsi_pver == NDMP_V2) {
3462654012Reza Sabdar			np = sp->nsi_data.nd_nlist.nld_nlist;
3472654012Reza Sabdar			for (j = 0; j < sp->nsi_data.nld_nlist_len; j++, np++) {
3482654012Reza Sabdar				free(np->nn_name);
3492654012Reza Sabdar				free(np->nn_dest);
3502654012Reza Sabdar			}
3512654012Reza Sabdar			free(sp->nsi_data.nd_nlist.nld_nlist);
3522654012Reza Sabdar		} else if ((sp->nsi_pver == NDMP_V3) ||
3532654012Reza Sabdar		    (sp->nsi_pver == NDMP_V4)) {
3542654012Reza Sabdar			npv3 = sp->nsi_data.nd_nlist.nld_dt_v3.dv3_nlist;
3552654012Reza Sabdar			for (j = 0; j < sp->nsi_data.nld_nlist_len;
3562654012Reza Sabdar			    j++, npv3++) {
3572654012Reza Sabdar				free(npv3->nn3_opath);
3582654012Reza Sabdar				free(npv3->nn3_dpath);
3592654012Reza Sabdar			}
3602654012Reza Sabdar			free(sp->nsi_data.nd_nlist.nld_dt_v3.dv3_nlist);
3612654012Reza Sabdar		}
3622654012Reza Sabdar	}
3632654012Reza Sabdar	free(sinfo);
3642654012Reza Sabdar}
3652654012Reza Sabdar
3662654012Reza Sabdar/* ARGSUSED */
3672654012Reza Sabdarint
3682654012Reza Sabdarndmp_get_stats(ndmp_stat_t *statp)
3692654012Reza Sabdar{
3702654012Reza Sabdar	int opcode = NDMP_GET_STAT;
3712654012Reza Sabdar
3722654012Reza Sabdar	(void) mutex_lock(&ndmp_lock);
3732654012Reza Sabdar	if (!statp) {
3742654012Reza Sabdar		ndmp_errno = ENDMP_INVALID_ARG;
3752654012Reza Sabdar		goto err;
3762654012Reza Sabdar	}
3772654012Reza Sabdar
3782654012Reza Sabdar	if (ndmp_door_setup(opcode))
3792654012Reza Sabdar		goto err;
3802654012Reza Sabdar
3812654012Reza Sabdar	if (ndmp_door_call())
3822654012Reza Sabdar		goto err;
3832654012Reza Sabdar
3842654012Reza Sabdar	statp->ns_trun = ndmp_door_get_uint32(dec_ctx);
3852654012Reza Sabdar	statp->ns_twait = ndmp_door_get_uint32(dec_ctx);
3862654012Reza Sabdar	statp->ns_nbk = ndmp_door_get_uint32(dec_ctx);
3872654012Reza Sabdar	statp->ns_nrs = ndmp_door_get_uint32(dec_ctx);
3882654012Reza Sabdar	statp->ns_rfile = ndmp_door_get_uint32(dec_ctx);
3892654012Reza Sabdar	statp->ns_wfile = ndmp_door_get_uint32(dec_ctx);
3902654012Reza Sabdar	statp->ns_rdisk = ndmp_door_get_uint64(dec_ctx);
3912654012Reza Sabdar	statp->ns_wdisk = ndmp_door_get_uint64(dec_ctx);
3922654012Reza Sabdar	statp->ns_rtape = ndmp_door_get_uint64(dec_ctx);
3932654012Reza Sabdar	statp->ns_wtape = ndmp_door_get_uint64(dec_ctx);
3942654012Reza Sabdar
3952654012Reza Sabdar	if (ndmp_door_fini())
3962654012Reza Sabdar		goto err;
3972654012Reza Sabdar
3982654012Reza Sabdar	(void) mutex_unlock(&ndmp_lock);
3992654012Reza Sabdar	return (0);
4002654012Reza Sabdarerr:
4012654012Reza Sabdar	(void) mutex_unlock(&ndmp_lock);
4022654012Reza Sabdar	return (-1);
4032654012Reza Sabdar}
4042654012Reza Sabdar
4052654012Reza Sabdarint
4062654012Reza Sabdarndmp_door_status(void)
4072654012Reza Sabdar{
4082654012Reza Sabdar	int opcode = NDMP_GET_DOOR_STATUS;
4092654012Reza Sabdar
4102654012Reza Sabdar	(void) mutex_lock(&ndmp_lock);
4112654012Reza Sabdar	if (ndmp_door_setup(opcode))
4122654012Reza Sabdar		goto err;
4132654012Reza Sabdar
4142654012Reza Sabdar	if (ndmp_door_call())
4152654012Reza Sabdar		goto err;
4162654012Reza Sabdar
4172654012Reza Sabdar	if (ndmp_door_fini())
4182654012Reza Sabdar		goto err;
4192654012Reza Sabdar
4202654012Reza Sabdar	(void) mutex_unlock(&ndmp_lock);
4212654012Reza Sabdar	return (0);
4222654012Reza Sabdarerr:
4232654012Reza Sabdar	(void) mutex_unlock(&ndmp_lock);
4242654012Reza Sabdar	return (-1);
4252654012Reza Sabdar}
4262654012Reza Sabdar
4272654012Reza Sabdarstatic int
4282654012Reza Sabdarndmp_door_setup(int opcode)
4292654012Reza Sabdar{
4302654012Reza Sabdar	/* Open channel to NDMP service */
4312654012Reza Sabdar	if ((ndmp_door_fildes == -1) &&
4322654012Reza Sabdar	    (ndmp_door_fildes = open(NDMP_DOOR_SVC, O_RDONLY)) < 0) {
4332654012Reza Sabdar		ndmp_errno = ENDMP_DOOR_OPEN;
4342654012Reza Sabdar		return (-1);
4352654012Reza Sabdar	}
4362654012Reza Sabdar
4372654012Reza Sabdar	buf = malloc(NDMP_DOOR_SIZE);
4382654012Reza Sabdar	if (!buf) {
4392654012Reza Sabdar		ndmp_errno = ENDMP_MEM_ALLOC;
4402654012Reza Sabdar		return (-1);
4412654012Reza Sabdar	}
4422654012Reza Sabdar
4432654012Reza Sabdar	enc_ctx = ndmp_door_encode_start(buf, NDMP_DOOR_SIZE);
4442654012Reza Sabdar	if (enc_ctx == 0) {
4452654012Reza Sabdar		free(buf);
4462654012Reza Sabdar		ndmp_errno = ENDMP_DOOR_ENCODE_START;
4472654012Reza Sabdar		return (-1);
4482654012Reza Sabdar	}
4492654012Reza Sabdar	ndmp_door_put_uint32(enc_ctx, opcode);
4502654012Reza Sabdar	return (0);
4512654012Reza Sabdar}
4522654012Reza Sabdar
4532654012Reza Sabdarstatic int
4542654012Reza Sabdarndmp_door_call(void)
4552654012Reza Sabdar{
4562654012Reza Sabdar	uint32_t used;
4572654012Reza Sabdar	int rc;
4582654012Reza Sabdar
4592654012Reza Sabdar	if ((ndmp_door_encode_finish(enc_ctx, &used)) != 0) {
4602654012Reza Sabdar		free(buf);
4612654012Reza Sabdar		ndmp_errno = ENDMP_DOOR_ENCODE_FINISH;
4622654012Reza Sabdar		return (-1);
4632654012Reza Sabdar	}
4642654012Reza Sabdar
4652654012Reza Sabdar	arg.data_ptr = buf;
4662654012Reza Sabdar	arg.data_size = used;
4672654012Reza Sabdar	arg.desc_ptr = NULL;
4682654012Reza Sabdar	arg.desc_num = 0;
4692654012Reza Sabdar	arg.rbuf = buf;
4702654012Reza Sabdar	arg.rsize = NDMP_DOOR_SIZE;
4712654012Reza Sabdar
4722654012Reza Sabdar	if (door_call(ndmp_door_fildes, &arg) < 0) {
4732654012Reza Sabdar		free(buf);
4742654012Reza Sabdar		ndmp_errno = ENDMP_DOOR_SRV_TIMEOUT;
4752654012Reza Sabdar		(void) close(ndmp_door_fildes);
4762654012Reza Sabdar		ndmp_door_fildes = -1;
4772654012Reza Sabdar		return (-1);
4782654012Reza Sabdar	}
4792654012Reza Sabdar
4802654012Reza Sabdar	dec_ctx = ndmp_door_decode_start(arg.data_ptr, arg.data_size);
4812654012Reza Sabdar	rc = ndmp_door_get_uint32(dec_ctx);
4822654012Reza Sabdar	if (rc != NDMP_DOOR_SRV_SUCCESS) {
4832654012Reza Sabdar		free(buf);
4842654012Reza Sabdar		ndmp_errno = ENDMP_DOOR_SRV_OPERATION;
4852654012Reza Sabdar		return (-1);
4862654012Reza Sabdar	}
4872654012Reza Sabdar	return (0);
4882654012Reza Sabdar}
4892654012Reza Sabdar
4902654012Reza Sabdarstatic int
4912654012Reza Sabdarndmp_door_fini(void)
4922654012Reza Sabdar{
4932654012Reza Sabdar	if ((ndmp_door_decode_finish(dec_ctx)) != 0) {
4942654012Reza Sabdar		free(buf);
4952654012Reza Sabdar		ndmp_errno = ENDMP_DOOR_DECODE_FINISH;
4962654012Reza Sabdar		return (-1);
4972654012Reza Sabdar	}
4982654012Reza Sabdar	free(buf);
4992654012Reza Sabdar	return (0);
5002654012Reza Sabdar}
501