xref: /onnv-gate/usr/src/lib/libzfs_jni/common/libzfs_jni_pool.c (revision 12383:c8a3959209ef)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51499Stalley  * Common Development and Distribution License (the "License").
61499Stalley  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
211499Stalley 
22789Sahrens /*
2312296SLin.Ling@Sun.COM  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24789Sahrens  */
25789Sahrens 
262082Seschrock #include "libzfs_jni_util.h"
27789Sahrens #include "libzfs_jni_pool.h"
28789Sahrens #include <strings.h>
29789Sahrens 
30789Sahrens /*
31789Sahrens  * Types
32789Sahrens  */
33789Sahrens 
34894Stalley typedef struct ImportablePoolBean {
35894Stalley 	zjni_Object_t super;
361206Stalley 	PoolStatsBean_t interface_PoolStats;
37894Stalley 
38894Stalley 	jmethodID method_setName;
39894Stalley 	jmethodID method_setId;
40894Stalley } ImportablePoolBean_t;
41894Stalley 
42789Sahrens typedef struct VirtualDeviceBean {
43789Sahrens 	zjni_Object_t super;
441206Stalley 	DeviceStatsBean_t interface_DeviceStats;
45789Sahrens 
46789Sahrens 	jmethodID method_setPoolName;
471499Stalley 	jmethodID method_setParentIndex;
48789Sahrens 	jmethodID method_setIndex;
49789Sahrens } VirtualDeviceBean_t;
50789Sahrens 
511328Stalley typedef struct LeafVirtualDeviceBean {
52789Sahrens 	VirtualDeviceBean_t super;
53789Sahrens 
541328Stalley 	jmethodID method_setName;
551328Stalley } LeafVirtualDeviceBean_t;
561328Stalley 
571328Stalley typedef struct DiskVirtualDeviceBean {
581328Stalley 	LeafVirtualDeviceBean_t super;
59789Sahrens } DiskVirtualDeviceBean_t;
60789Sahrens 
611328Stalley typedef struct SliceVirtualDeviceBean {
621328Stalley 	LeafVirtualDeviceBean_t super;
631328Stalley } SliceVirtualDeviceBean_t;
641328Stalley 
65789Sahrens typedef struct FileVirtualDeviceBean {
661328Stalley 	LeafVirtualDeviceBean_t super;
67789Sahrens } FileVirtualDeviceBean_t;
68789Sahrens 
69789Sahrens typedef struct RAIDVirtualDeviceBean {
70789Sahrens 	VirtualDeviceBean_t super;
715400Srl193873 
725400Srl193873 	jmethodID method_setParity;
73789Sahrens } RAIDVirtualDeviceBean_t;
74789Sahrens 
75789Sahrens typedef struct MirrorVirtualDeviceBean {
76789Sahrens 	VirtualDeviceBean_t super;
77789Sahrens } MirrorVirtualDeviceBean_t;
78789Sahrens 
79789Sahrens /*
801206Stalley  * Data
811206Stalley  */
821206Stalley 
831206Stalley /* vdev_state_t to DeviceStats$DeviceState map */
841206Stalley static zjni_field_mapping_t vdev_state_map[] = {
851206Stalley 	{ VDEV_STATE_CANT_OPEN, "VDEV_STATE_CANT_OPEN" },
861206Stalley 	{ VDEV_STATE_CLOSED, "VDEV_STATE_CLOSED" },
871206Stalley 	{ VDEV_STATE_DEGRADED, "VDEV_STATE_DEGRADED" },
881206Stalley 	{ VDEV_STATE_HEALTHY, "VDEV_STATE_HEALTHY" },
891206Stalley 	{ VDEV_STATE_OFFLINE, "VDEV_STATE_OFFLINE" },
901206Stalley 	{ VDEV_STATE_UNKNOWN, "VDEV_STATE_UNKNOWN" },
911206Stalley 	{ -1, NULL },
921206Stalley };
931206Stalley 
941206Stalley /* vdev_aux_t to DeviceStats$DeviceStatus map */
951206Stalley static zjni_field_mapping_t vdev_aux_map[] = {
961206Stalley 	{ VDEV_AUX_NONE, "VDEV_AUX_NONE" },
971206Stalley 	{ VDEV_AUX_OPEN_FAILED, "VDEV_AUX_OPEN_FAILED" },
981206Stalley 	{ VDEV_AUX_CORRUPT_DATA, "VDEV_AUX_CORRUPT_DATA" },
991206Stalley 	{ VDEV_AUX_NO_REPLICAS, "VDEV_AUX_NO_REPLICAS" },
1001206Stalley 	{ VDEV_AUX_BAD_GUID_SUM, "VDEV_AUX_BAD_GUID_SUM" },
1011206Stalley 	{ VDEV_AUX_TOO_SMALL, "VDEV_AUX_TOO_SMALL" },
1021206Stalley 	{ VDEV_AUX_BAD_LABEL, "VDEV_AUX_BAD_LABEL" },
1031206Stalley 	{ -1, NULL },
1041206Stalley };
1051206Stalley 
1061206Stalley /* zpool_state_t to PoolStats$PoolState map */
1071206Stalley static zjni_field_mapping_t pool_state_map[] = {
1081206Stalley 	{ POOL_STATE_ACTIVE, "POOL_STATE_ACTIVE" },
1091206Stalley 	{ POOL_STATE_EXPORTED, "POOL_STATE_EXPORTED" },
1101206Stalley 	{ POOL_STATE_DESTROYED, "POOL_STATE_DESTROYED" },
1115582Scristian 	{ POOL_STATE_SPARE, "POOL_STATE_SPARE" },
1121206Stalley 	{ POOL_STATE_UNINITIALIZED, "POOL_STATE_UNINITIALIZED" },
1131206Stalley 	{ POOL_STATE_UNAVAIL, "POOL_STATE_UNAVAIL" },
1145582Scristian 	{ POOL_STATE_POTENTIALLY_ACTIVE, "POOL_STATE_POTENTIALLY_ACTIVE" },
1151206Stalley 	{ -1, NULL },
1161206Stalley };
1171206Stalley 
1181206Stalley /* zpool_status_t to PoolStats$PoolStatus map */
1191206Stalley static zjni_field_mapping_t zpool_status_map[] = {
1205582Scristian 	{ ZPOOL_STATUS_CORRUPT_CACHE, "ZPOOL_STATUS_CORRUPT_CACHE" },
1215582Scristian 	{ ZPOOL_STATUS_MISSING_DEV_R, "ZPOOL_STATUS_MISSING_DEV_R" },
1225582Scristian 	{ ZPOOL_STATUS_MISSING_DEV_NR, "ZPOOL_STATUS_MISSING_DEV_NR" },
1235582Scristian 	{ ZPOOL_STATUS_CORRUPT_LABEL_R, "ZPOOL_STATUS_CORRUPT_LABEL_R" },
1245582Scristian 	{ ZPOOL_STATUS_CORRUPT_LABEL_NR, "ZPOOL_STATUS_CORRUPT_LABEL_NR" },
1251206Stalley 	{ ZPOOL_STATUS_BAD_GUID_SUM, "ZPOOL_STATUS_BAD_GUID_SUM" },
1261206Stalley 	{ ZPOOL_STATUS_CORRUPT_POOL, "ZPOOL_STATUS_CORRUPT_POOL" },
1271206Stalley 	{ ZPOOL_STATUS_CORRUPT_DATA, "ZPOOL_STATUS_CORRUPT_DATA" },
1281206Stalley 	{ ZPOOL_STATUS_FAILING_DEV, "ZPOOL_STATUS_FAILING_DEV" },
1295582Scristian 	{ ZPOOL_STATUS_VERSION_NEWER, "ZPOOL_STATUS_VERSION_NEWER" },
1305582Scristian 	{ ZPOOL_STATUS_HOSTID_MISMATCH, "ZPOOL_STATUS_HOSTID_MISMATCH" },
1315582Scristian 	{ ZPOOL_STATUS_FAULTED_DEV_R, "ZPOOL_STATUS_FAULTED_DEV_R" },
1325582Scristian 	{ ZPOOL_STATUS_FAULTED_DEV_NR, "ZPOOL_STATUS_FAULTED_DEV_NR" },
1337294Sperrin 	{ ZPOOL_STATUS_BAD_LOG, "ZPOOL_STATUS_BAD_LOG" },
1345582Scristian 	{ ZPOOL_STATUS_VERSION_OLDER, "ZPOOL_STATUS_VERSION_OLDER" },
1351206Stalley 	{ ZPOOL_STATUS_RESILVERING, "ZPOOL_STATUS_RESILVERING" },
1361206Stalley 	{ ZPOOL_STATUS_OFFLINE_DEV, "ZPOOL_STATUS_OFFLINE_DEV" },
13710151SGeorge.Wilson@Sun.COM 	{ ZPOOL_STATUS_REMOVED_DEV, "ZPOOL_STATUS_REMOVED_DEV" },
1381206Stalley 	{ ZPOOL_STATUS_OK, "ZPOOL_STATUS_OK" },
1395582Scristian 	{ -1, NULL }
1401206Stalley };
1411206Stalley 
1421206Stalley /*
143789Sahrens  * Function prototypes
144789Sahrens  */
145789Sahrens 
146894Stalley static void new_ImportablePoolBean(JNIEnv *, ImportablePoolBean_t *);
147789Sahrens static void new_VirtualDevice(JNIEnv *, VirtualDeviceBean_t *);
1481328Stalley static void new_LeafVirtualDevice(JNIEnv *, LeafVirtualDeviceBean_t *);
149789Sahrens static void new_DiskVirtualDeviceBean(JNIEnv *, DiskVirtualDeviceBean_t *);
1501328Stalley static void new_SliceVirtualDeviceBean(JNIEnv *, SliceVirtualDeviceBean_t *);
151789Sahrens static void new_FileVirtualDeviceBean(JNIEnv *, FileVirtualDeviceBean_t *);
152789Sahrens static void new_RAIDVirtualDeviceBean(JNIEnv *, RAIDVirtualDeviceBean_t *);
153789Sahrens static void new_MirrorVirtualDeviceBean(JNIEnv *, MirrorVirtualDeviceBean_t *);
154894Stalley static int populate_ImportablePoolBean(
1551206Stalley     JNIEnv *, ImportablePoolBean_t *, nvlist_t *);
1561499Stalley static int populate_VirtualDeviceBean(JNIEnv *, zpool_handle_t *,
1571499Stalley     nvlist_t *, uint64_t *p_vdev_id, VirtualDeviceBean_t *);
1581499Stalley static int populate_LeafVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
1591499Stalley     nvlist_t *, uint64_t *p_vdev_id, LeafVirtualDeviceBean_t *);
1601499Stalley static int populate_DiskVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
1611499Stalley     nvlist_t *, uint64_t *p_vdev_id, DiskVirtualDeviceBean_t *);
1621499Stalley static int populate_SliceVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
1631499Stalley     nvlist_t *, uint64_t *p_vdev_id, SliceVirtualDeviceBean_t *);
1641499Stalley static int populate_FileVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
1651499Stalley     nvlist_t *, uint64_t *p_vdev_id, FileVirtualDeviceBean_t *);
1661499Stalley static int populate_RAIDVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
1671499Stalley     nvlist_t *, uint64_t *p_vdev_id, RAIDVirtualDeviceBean_t *);
1681499Stalley static int populate_MirrorVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
1691499Stalley     nvlist_t *, uint64_t *p_vdev_id, MirrorVirtualDeviceBean_t *);
1701206Stalley static jobject create_ImportablePoolBean(JNIEnv *, nvlist_t *);
171789Sahrens static jobject create_DiskVirtualDeviceBean(
1721499Stalley     JNIEnv *, zpool_handle_t *, nvlist_t *, uint64_t *p_vdev_id);
1731328Stalley static jobject create_SliceVirtualDeviceBean(
1741499Stalley     JNIEnv *, zpool_handle_t *, nvlist_t *, uint64_t *p_vdev_id);
175789Sahrens static jobject create_FileVirtualDeviceBean(
1761499Stalley     JNIEnv *, zpool_handle_t *, nvlist_t *, uint64_t *p_vdev_id);
177789Sahrens static jobject create_RAIDVirtualDeviceBean(
1781499Stalley     JNIEnv *, zpool_handle_t *, nvlist_t *, uint64_t *p_vdev_id);
179789Sahrens static jobject create_MirrorVirtualDeviceBean(
1801499Stalley     JNIEnv *, zpool_handle_t *, nvlist_t *, uint64_t *p_vdev_id);
1811206Stalley static char *find_field(const zjni_field_mapping_t *, int);
1821206Stalley static jobject zjni_vdev_state_to_obj(JNIEnv *, vdev_state_t);
1831206Stalley static jobject zjni_vdev_aux_to_obj(JNIEnv *, vdev_aux_t);
184789Sahrens 
185789Sahrens /*
186789Sahrens  * Static functions
187789Sahrens  */
188789Sahrens 
189894Stalley /* Create a ImportablePoolBean */
190894Stalley static void
new_ImportablePoolBean(JNIEnv * env,ImportablePoolBean_t * bean)191894Stalley new_ImportablePoolBean(JNIEnv *env, ImportablePoolBean_t *bean)
192894Stalley {
193894Stalley 	zjni_Object_t *object = (zjni_Object_t *)bean;
194894Stalley 
195894Stalley 	if (object->object == NULL) {
196894Stalley 		object->class =
197894Stalley 		    (*env)->FindClass(env,
1985400Srl193873 		    ZFSJNI_PACKAGE_DATA "ImportablePoolBean");
199894Stalley 
200894Stalley 		object->constructor =
201894Stalley 		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
202894Stalley 
203894Stalley 		object->object =
204894Stalley 		    (*env)->NewObject(env, object->class, object->constructor);
205894Stalley 	}
206894Stalley 
2071206Stalley 	new_PoolStats(env, &(bean->interface_PoolStats), object);
2081206Stalley 
209894Stalley 	bean->method_setName = (*env)->GetMethodID(
210894Stalley 	    env, object->class, "setName", "(Ljava/lang/String;)V");
211894Stalley 
212894Stalley 	bean->method_setId = (*env)->GetMethodID(
213894Stalley 	    env, object->class, "setId", "(J)V");
214894Stalley }
215894Stalley 
216789Sahrens /* Create a VirtualDeviceBean */
217789Sahrens static void
new_VirtualDevice(JNIEnv * env,VirtualDeviceBean_t * bean)218789Sahrens new_VirtualDevice(JNIEnv *env, VirtualDeviceBean_t *bean)
219789Sahrens {
220789Sahrens 	zjni_Object_t *object = (zjni_Object_t *)bean;
221789Sahrens 
222789Sahrens 	if (object->object == NULL) {
223789Sahrens 		object->class =
224789Sahrens 		    (*env)->FindClass(env,
2255400Srl193873 		    ZFSJNI_PACKAGE_DATA "VirtualDeviceBean");
226789Sahrens 
227789Sahrens 		object->constructor =
228789Sahrens 		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
229789Sahrens 
230789Sahrens 		object->object =
231789Sahrens 		    (*env)->NewObject(env, object->class, object->constructor);
232789Sahrens 	}
233789Sahrens 
2341206Stalley 	new_DeviceStats(env, &(bean->interface_DeviceStats), object);
2351206Stalley 
236789Sahrens 	bean->method_setPoolName = (*env)->GetMethodID(
237789Sahrens 	    env, object->class, "setPoolName", "(Ljava/lang/String;)V");
238789Sahrens 
2391499Stalley 	bean->method_setParentIndex = (*env)->GetMethodID(
2401499Stalley 	    env, object->class, "setParentIndex", "(Ljava/lang/Long;)V");
2411499Stalley 
242789Sahrens 	bean->method_setIndex = (*env)->GetMethodID(
243789Sahrens 	    env, object->class, "setIndex", "(J)V");
244789Sahrens }
245789Sahrens 
2461328Stalley /* Create a LeafVirtualDeviceBean */
2471328Stalley static void
new_LeafVirtualDevice(JNIEnv * env,LeafVirtualDeviceBean_t * bean)2481328Stalley new_LeafVirtualDevice(JNIEnv *env, LeafVirtualDeviceBean_t *bean)
2491328Stalley {
2501328Stalley 	zjni_Object_t *object = (zjni_Object_t *)bean;
2511328Stalley 
2521328Stalley 	if (object->object == NULL) {
2531328Stalley 		object->class =
2541328Stalley 		    (*env)->FindClass(env,
2555400Srl193873 		    ZFSJNI_PACKAGE_DATA "LeafVirtualDeviceBean");
2561328Stalley 
2571328Stalley 		object->constructor =
2581328Stalley 		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
2591328Stalley 
2601328Stalley 		object->object =
2611328Stalley 		    (*env)->NewObject(env, object->class, object->constructor);
2621328Stalley 	}
2631328Stalley 
2641328Stalley 	new_VirtualDevice(env, (VirtualDeviceBean_t *)bean);
2651328Stalley 
2661328Stalley 	bean->method_setName = (*env)->GetMethodID(
2671328Stalley 	    env, object->class, "setName", "(Ljava/lang/String;)V");
2681328Stalley }
2691328Stalley 
270789Sahrens /* Create a DiskVirtualDeviceBean */
271789Sahrens static void
new_DiskVirtualDeviceBean(JNIEnv * env,DiskVirtualDeviceBean_t * bean)272789Sahrens new_DiskVirtualDeviceBean(JNIEnv *env, DiskVirtualDeviceBean_t *bean)
273789Sahrens {
274789Sahrens 	zjni_Object_t *object = (zjni_Object_t *)bean;
275789Sahrens 
276789Sahrens 	if (object->object == NULL) {
277789Sahrens 		object->class = (*env)->FindClass(
278789Sahrens 		    env, ZFSJNI_PACKAGE_DATA "DiskVirtualDeviceBean");
279789Sahrens 
280789Sahrens 		object->constructor =
281789Sahrens 		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
282789Sahrens 
283789Sahrens 		object->object =
284789Sahrens 		    (*env)->NewObject(env, object->class, object->constructor);
285789Sahrens 	}
286789Sahrens 
2871328Stalley 	new_LeafVirtualDevice(env, (LeafVirtualDeviceBean_t *)bean);
2881328Stalley }
2891328Stalley 
2901328Stalley /* Create a SliceVirtualDeviceBean */
2911328Stalley static void
new_SliceVirtualDeviceBean(JNIEnv * env,SliceVirtualDeviceBean_t * bean)2921328Stalley new_SliceVirtualDeviceBean(JNIEnv *env, SliceVirtualDeviceBean_t *bean)
2931328Stalley {
2941328Stalley 	zjni_Object_t *object = (zjni_Object_t *)bean;
295789Sahrens 
2961328Stalley 	if (object->object == NULL) {
2971328Stalley 		object->class = (*env)->FindClass(
2981328Stalley 		    env, ZFSJNI_PACKAGE_DATA "SliceVirtualDeviceBean");
299789Sahrens 
3001328Stalley 		object->constructor =
3011328Stalley 		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
3021328Stalley 
3031328Stalley 		object->object =
3041328Stalley 		    (*env)->NewObject(env, object->class, object->constructor);
3051328Stalley 	}
3061328Stalley 
3071328Stalley 	new_LeafVirtualDevice(env, (LeafVirtualDeviceBean_t *)bean);
308789Sahrens }
309789Sahrens 
310789Sahrens /* Create a FileVirtualDeviceBean */
311789Sahrens static void
new_FileVirtualDeviceBean(JNIEnv * env,FileVirtualDeviceBean_t * bean)312789Sahrens new_FileVirtualDeviceBean(JNIEnv *env, FileVirtualDeviceBean_t *bean)
313789Sahrens {
314789Sahrens 	zjni_Object_t *object = (zjni_Object_t *)bean;
315789Sahrens 
316789Sahrens 	if (object->object == NULL) {
317789Sahrens 		object->class = (*env)->FindClass(
318789Sahrens 		    env, ZFSJNI_PACKAGE_DATA "FileVirtualDeviceBean");
319789Sahrens 
320789Sahrens 		object->constructor =
321789Sahrens 		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
322789Sahrens 
323789Sahrens 		object->object =
324789Sahrens 		    (*env)->NewObject(env, object->class, object->constructor);
325789Sahrens 	}
326789Sahrens 
3271328Stalley 	new_LeafVirtualDevice(env, (LeafVirtualDeviceBean_t *)bean);
328789Sahrens }
329789Sahrens 
330789Sahrens /* Create a RAIDVirtualDeviceBean */
331789Sahrens static void
new_RAIDVirtualDeviceBean(JNIEnv * env,RAIDVirtualDeviceBean_t * bean)332789Sahrens new_RAIDVirtualDeviceBean(JNIEnv *env, RAIDVirtualDeviceBean_t *bean)
333789Sahrens {
334789Sahrens 	zjni_Object_t *object = (zjni_Object_t *)bean;
335789Sahrens 
336789Sahrens 	if (object->object == NULL) {
337789Sahrens 
338789Sahrens 		object->class = (*env)->FindClass(
339789Sahrens 		    env, ZFSJNI_PACKAGE_DATA "RAIDVirtualDeviceBean");
340789Sahrens 
341789Sahrens 		object->constructor =
342789Sahrens 		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
343789Sahrens 
344789Sahrens 		object->object =
345789Sahrens 		    (*env)->NewObject(env, object->class, object->constructor);
346789Sahrens 	}
347789Sahrens 
348789Sahrens 	new_VirtualDevice(env, (VirtualDeviceBean_t *)bean);
3495400Srl193873 
3505400Srl193873 	bean->method_setParity = (*env)->GetMethodID(
3515400Srl193873 	    env, object->class, "setParity", "(J)V");
352789Sahrens }
353789Sahrens 
354789Sahrens /* Create a MirrorVirtualDeviceBean */
355789Sahrens static void
new_MirrorVirtualDeviceBean(JNIEnv * env,MirrorVirtualDeviceBean_t * bean)356789Sahrens new_MirrorVirtualDeviceBean(JNIEnv *env, MirrorVirtualDeviceBean_t *bean)
357789Sahrens {
358789Sahrens 	zjni_Object_t *object = (zjni_Object_t *)bean;
359789Sahrens 
360789Sahrens 	if (object->object == NULL) {
361789Sahrens 		object->class = (*env)->FindClass(
362789Sahrens 		    env, ZFSJNI_PACKAGE_DATA "MirrorVirtualDeviceBean");
363789Sahrens 
364789Sahrens 		object->constructor =
365789Sahrens 		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
366789Sahrens 
367789Sahrens 		object->object =
368789Sahrens 		    (*env)->NewObject(env, object->class, object->constructor);
369789Sahrens 	}
370789Sahrens 
371789Sahrens 	new_VirtualDevice(env, (VirtualDeviceBean_t *)bean);
372789Sahrens }
373789Sahrens 
3741206Stalley static int
populate_ImportablePoolBean(JNIEnv * env,ImportablePoolBean_t * bean,nvlist_t * config)3751206Stalley populate_ImportablePoolBean(JNIEnv *env, ImportablePoolBean_t *bean,
3761206Stalley     nvlist_t *config)
377894Stalley {
3781206Stalley 	char *c;
3791206Stalley 	char *name;
3801206Stalley 	uint64_t guid;
3811206Stalley 	uint64_t state;
3825582Scristian 	uint64_t version;
3831206Stalley 	nvlist_t *devices;
384894Stalley 
3851206Stalley 	zjni_Object_t *object = (zjni_Object_t *)bean;
3861206Stalley 	PoolStatsBean_t *pool_stats = &(bean->interface_PoolStats);
3871206Stalley 	DeviceStatsBean_t *dev_stats = (DeviceStatsBean_t *)pool_stats;
388894Stalley 
3891206Stalley 	if (nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name) ||
3901206Stalley 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) ||
3911206Stalley 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state) ||
3925582Scristian 	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) ||
3931206Stalley 	    nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &devices) ||
3941206Stalley 	    populate_DeviceStatsBean(env, devices, dev_stats, object)) {
3951206Stalley 		return (-1);
396894Stalley 	}
397894Stalley 
3981206Stalley 	(*env)->CallVoidMethod(env, object->object,
3991206Stalley 	    bean->method_setName, (*env)->NewStringUTF(env, name));
400894Stalley 
4011206Stalley 	(*env)->CallVoidMethod(env, object->object,
4021206Stalley 	    bean->method_setId, (jlong)guid);
403894Stalley 
4041206Stalley 	(*env)->CallVoidMethod(env, object->object,
4051206Stalley 	    pool_stats->method_setPoolState,
4061206Stalley 	    zjni_pool_state_to_obj(env, (pool_state_t)state));
407894Stalley 
4081206Stalley 	(*env)->CallVoidMethod(env, object->object,
4091206Stalley 	    pool_stats->method_setPoolStatus,
4101206Stalley 	    zjni_pool_status_to_obj(env, zpool_import_status(config, &c)));
411894Stalley 
4125582Scristian 	(*env)->CallVoidMethod(env, object->object,
4135582Scristian 	    pool_stats->method_setPoolVersion, (jlong)version);
4145582Scristian 
415894Stalley 	return (0);
416894Stalley }
417894Stalley 
418789Sahrens static int
populate_VirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id,VirtualDeviceBean_t * bean)419789Sahrens populate_VirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
4201499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id, VirtualDeviceBean_t *bean)
421789Sahrens {
422789Sahrens 	int result;
423789Sahrens 	uint64_t vdev_id;
4241206Stalley 	jstring poolUTF;
4251206Stalley 
426789Sahrens 	zjni_Object_t *object = (zjni_Object_t *)bean;
4271206Stalley 	DeviceStatsBean_t *stats = &(bean->interface_DeviceStats);
4281206Stalley 
4291206Stalley 	result = populate_DeviceStatsBean(env, vdev, stats, object);
4301206Stalley 	if (result != 0) {
4311206Stalley 		return (1);
4321206Stalley 	}
433789Sahrens 
434789Sahrens 	/* Set pool name */
4351206Stalley 	poolUTF = (*env)->NewStringUTF(env, zpool_get_name(zhp));
436789Sahrens 	(*env)->CallVoidMethod(
437789Sahrens 	    env, object->object, bean->method_setPoolName, poolUTF);
438789Sahrens 
4391499Stalley 	/* Set parent vdev index */
4401499Stalley 	(*env)->CallVoidMethod(
4411499Stalley 	    env, object->object, bean->method_setParentIndex,
4421499Stalley 	    p_vdev_id == NULL ? NULL :
4431499Stalley 	    zjni_long_to_Long(env, *p_vdev_id));
4441499Stalley 
445789Sahrens 	/* Get index */
446789Sahrens 	result = nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_GUID, &vdev_id);
447789Sahrens 	if (result != 0) {
448789Sahrens 		zjni_throw_exception(env,
449789Sahrens 		    "could not retrieve virtual device ID (pool %s)",
450789Sahrens 		    zpool_get_name(zhp));
4511206Stalley 		return (1);
452789Sahrens 	}
453789Sahrens 
4541206Stalley 	(*env)->CallVoidMethod(
4551206Stalley 	    env, object->object, bean->method_setIndex, (jlong)vdev_id);
4561206Stalley 
4571206Stalley 	return (0);
458789Sahrens }
459789Sahrens 
460789Sahrens static int
populate_LeafVirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id,LeafVirtualDeviceBean_t * bean)4611328Stalley populate_LeafVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
4621499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id, LeafVirtualDeviceBean_t *bean)
4631328Stalley {
4641328Stalley 	return (populate_VirtualDeviceBean(
4651499Stalley 	    env, zhp, vdev, p_vdev_id, (VirtualDeviceBean_t *)bean));
4661328Stalley }
4671328Stalley 
4681328Stalley static int
populate_DiskVirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id,DiskVirtualDeviceBean_t * bean)469789Sahrens populate_DiskVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
4701499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id, DiskVirtualDeviceBean_t *bean)
471789Sahrens {
472789Sahrens 	char *path;
4731328Stalley 	int result = populate_LeafVirtualDeviceBean(
4741499Stalley 	    env, zhp, vdev, p_vdev_id, (LeafVirtualDeviceBean_t *)bean);
4751328Stalley 
4761328Stalley 	if (result) {
4771328Stalley 		/* Must not call any more Java methods to preserve exception */
4781328Stalley 		return (-1);
4791328Stalley 	}
4801328Stalley 
4811328Stalley 	/* Set path */
4821328Stalley 	result = nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path);
4831328Stalley 	if (result != 0) {
4841328Stalley 		zjni_throw_exception(env,
4853413Smmusante 		    "could not retrieve path from disk virtual device "
4863413Smmusante 		    "(pool %s)", zpool_get_name(zhp));
4871328Stalley 	} else {
4881328Stalley 
4891328Stalley 		regex_t re;
4901328Stalley 		regmatch_t matches[2];
4911328Stalley 		jstring pathUTF = NULL;
4921328Stalley 
4931328Stalley 		/* Strip off slice portion of name, if applicable */
4941328Stalley 		if (regcomp(&re, "^(/dev/dsk/.*)s[0-9]+$", REG_EXTENDED) == 0) {
4951328Stalley 			if (regexec(&re, path, 2, matches, 0) == 0) {
4961328Stalley 				regmatch_t *match = matches + 1;
4971328Stalley 				if (match->rm_so != -1 && match->rm_eo != -1) {
4981328Stalley 					char *tmp = strdup(path);
4991328Stalley 					if (tmp != NULL) {
5001328Stalley 						char *end = tmp + match->rm_eo;
5011328Stalley 						*end = '\0';
5021328Stalley 						pathUTF = (*env)->NewStringUTF(
5031328Stalley 						    env, tmp);
5041328Stalley 						free(tmp);
5051328Stalley 					}
5061328Stalley 				}
5071328Stalley 			}
5081328Stalley 			regfree(&re);
5091328Stalley 		}
510*12383SJohn.Harres@Sun.COM 		if (regcomp(&re, "^(/dev/dsk/.*)s[0-9]+/old$", REG_EXTENDED) ==
511*12383SJohn.Harres@Sun.COM 		    0) {
512*12383SJohn.Harres@Sun.COM 			if (regexec(&re, path, 2, matches, 0) == 0) {
513*12383SJohn.Harres@Sun.COM 				regmatch_t *match = matches + 1;
514*12383SJohn.Harres@Sun.COM 				if (match->rm_so != -1 && match->rm_eo != -1) {
515*12383SJohn.Harres@Sun.COM 					char *tmp = strdup(path);
516*12383SJohn.Harres@Sun.COM 					if (tmp != NULL) {
517*12383SJohn.Harres@Sun.COM 						(void) strcpy(tmp +
518*12383SJohn.Harres@Sun.COM 						    match->rm_eo, "/old");
519*12383SJohn.Harres@Sun.COM 						pathUTF = (*env)->NewStringUTF(
520*12383SJohn.Harres@Sun.COM 						    env, tmp);
521*12383SJohn.Harres@Sun.COM 						free(tmp);
522*12383SJohn.Harres@Sun.COM 					}
523*12383SJohn.Harres@Sun.COM 				}
524*12383SJohn.Harres@Sun.COM 			}
525*12383SJohn.Harres@Sun.COM 			regfree(&re);
526*12383SJohn.Harres@Sun.COM 		}
5271328Stalley 
5281328Stalley 		if (pathUTF == NULL) {
5295400Srl193873 			pathUTF = (*env)->NewStringUTF(env, path);
5301328Stalley 		}
5311328Stalley 
5321328Stalley 		(*env)->CallVoidMethod(env, ((zjni_Object_t *)bean)->object,
5331328Stalley 		    ((LeafVirtualDeviceBean_t *)bean)->method_setName, pathUTF);
5341328Stalley 	}
5351328Stalley 
5361328Stalley 	return (result != 0);
5371328Stalley }
5381328Stalley 
5391328Stalley static int
populate_SliceVirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id,SliceVirtualDeviceBean_t * bean)5401328Stalley populate_SliceVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
5411499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id, SliceVirtualDeviceBean_t *bean)
5421328Stalley {
5431328Stalley 	char *path;
5441328Stalley 	int result = populate_LeafVirtualDeviceBean(
5451499Stalley 	    env, zhp, vdev, p_vdev_id, (LeafVirtualDeviceBean_t *)bean);
5461328Stalley 
5471328Stalley 	if (result) {
5481328Stalley 		/* Must not call any more Java methods to preserve exception */
5491328Stalley 		return (-1);
5501328Stalley 	}
5511328Stalley 
5521328Stalley 	/* Set path */
5531328Stalley 	result = nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path);
5541328Stalley 	if (result != 0) {
5551328Stalley 		zjni_throw_exception(env,
5563413Smmusante 		    "could not retrieve path from slice virtual device (pool "
5571328Stalley 		    "%s)", zpool_get_name(zhp));
5581328Stalley 	} else {
5591328Stalley 
5601328Stalley 		jstring pathUTF = (*env)->NewStringUTF(env, path);
5611328Stalley 		(*env)->CallVoidMethod(env, ((zjni_Object_t *)bean)->object,
5621499Stalley 		    ((LeafVirtualDeviceBean_t *)bean)->method_setName,
5631499Stalley 		    pathUTF);
5641328Stalley 	}
5651328Stalley 
5661328Stalley 	return (result != 0);
5671328Stalley }
5681328Stalley 
5691328Stalley static int
populate_FileVirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id,FileVirtualDeviceBean_t * bean)5701328Stalley populate_FileVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
5711499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id, FileVirtualDeviceBean_t *bean)
5721328Stalley {
5731328Stalley 	char *path;
5741328Stalley 	int result = populate_LeafVirtualDeviceBean(
5751499Stalley 	    env, zhp, vdev, p_vdev_id, (LeafVirtualDeviceBean_t *)bean);
576789Sahrens 
577789Sahrens 	if (result) {
578789Sahrens 		/* Must not call any more Java methods to preserve exception */
579789Sahrens 		return (-1);
580789Sahrens 	}
581789Sahrens 
582789Sahrens 	/* Set path */
583789Sahrens 	result = nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path);
584789Sahrens 	if (result != 0) {
585789Sahrens 		zjni_throw_exception(env,
5863413Smmusante 		    "could not retrieve path from disk virtual device "
5873413Smmusante 		    "(pool %s)", zpool_get_name(zhp));
588789Sahrens 	} else {
589789Sahrens 
590789Sahrens 		jstring pathUTF = (*env)->NewStringUTF(env, path);
591789Sahrens 		(*env)->CallVoidMethod(env, ((zjni_Object_t *)bean)->object,
5921328Stalley 		    ((LeafVirtualDeviceBean_t *)bean)->method_setName, pathUTF);
593789Sahrens 	}
594789Sahrens 
595789Sahrens 	return (result != 0);
596789Sahrens }
597789Sahrens 
598789Sahrens static int
populate_RAIDVirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id,RAIDVirtualDeviceBean_t * bean)599789Sahrens populate_RAIDVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
6001499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id, RAIDVirtualDeviceBean_t *bean)
601789Sahrens {
6021499Stalley 	return (populate_VirtualDeviceBean(env, zhp, vdev, p_vdev_id,
603789Sahrens 	    (VirtualDeviceBean_t *)bean));
604789Sahrens }
605789Sahrens 
606789Sahrens static int
populate_MirrorVirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id,MirrorVirtualDeviceBean_t * bean)607789Sahrens populate_MirrorVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
6081499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id, MirrorVirtualDeviceBean_t *bean)
609789Sahrens {
6101499Stalley 	return (populate_VirtualDeviceBean(env, zhp, vdev, p_vdev_id,
611789Sahrens 	    (VirtualDeviceBean_t *)bean));
612789Sahrens }
613789Sahrens 
614789Sahrens static jobject
create_ImportablePoolBean(JNIEnv * env,nvlist_t * config)6151206Stalley create_ImportablePoolBean(JNIEnv *env, nvlist_t *config)
616894Stalley {
617894Stalley 	int result;
618894Stalley 	ImportablePoolBean_t bean_obj = {0};
619894Stalley 	ImportablePoolBean_t *bean = &bean_obj;
620894Stalley 
621894Stalley 	/* Construct ImportablePoolBean */
622894Stalley 	new_ImportablePoolBean(env, bean);
623894Stalley 
6241206Stalley 	result = populate_ImportablePoolBean(env, bean, config);
625894Stalley 	if (result) {
626894Stalley 		/* Must not call any more Java methods to preserve exception */
627894Stalley 		return (NULL);
628894Stalley 	}
629894Stalley 
630894Stalley 	return (((zjni_Object_t *)bean)->object);
631894Stalley }
632894Stalley 
633894Stalley static jobject
create_DiskVirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id)6341499Stalley create_DiskVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
6351499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id)
636789Sahrens {
637789Sahrens 	int result;
638789Sahrens 	DiskVirtualDeviceBean_t bean_obj = {0};
639789Sahrens 	DiskVirtualDeviceBean_t *bean = &bean_obj;
640789Sahrens 
641789Sahrens 	/* Construct DiskVirtualDeviceBean */
642789Sahrens 	new_DiskVirtualDeviceBean(env, bean);
643789Sahrens 
6441499Stalley 	result = populate_DiskVirtualDeviceBean(
6451499Stalley 	    env, zhp, vdev, p_vdev_id, bean);
646789Sahrens 	if (result) {
647789Sahrens 		/* Must not call any more Java methods to preserve exception */
648789Sahrens 		return (NULL);
649789Sahrens 	}
650789Sahrens 
651789Sahrens 	return (((zjni_Object_t *)bean)->object);
652789Sahrens }
653789Sahrens 
654789Sahrens static jobject
create_SliceVirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id)6551499Stalley create_SliceVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
6561499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id)
6571328Stalley {
6581328Stalley 	int result;
6591328Stalley 	SliceVirtualDeviceBean_t bean_obj = {0};
6601328Stalley 	SliceVirtualDeviceBean_t *bean = &bean_obj;
6611328Stalley 
6621328Stalley 	/* Construct SliceVirtualDeviceBean */
6631328Stalley 	new_SliceVirtualDeviceBean(env, bean);
6641328Stalley 
6651499Stalley 	result = populate_SliceVirtualDeviceBean(
6661499Stalley 	    env, zhp, vdev, p_vdev_id, bean);
6671328Stalley 	if (result) {
6681328Stalley 		/* Must not call any more Java methods to preserve exception */
6691328Stalley 		return (NULL);
6701328Stalley 	}
6711328Stalley 
6721328Stalley 	return (((zjni_Object_t *)bean)->object);
6731328Stalley }
6741328Stalley 
6751328Stalley static jobject
create_FileVirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id)6761499Stalley create_FileVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
6771499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id)
678789Sahrens {
679789Sahrens 	int result;
680789Sahrens 	FileVirtualDeviceBean_t bean_obj = {0};
681789Sahrens 	FileVirtualDeviceBean_t *bean = &bean_obj;
682789Sahrens 
683789Sahrens 	/* Construct FileVirtualDeviceBean */
684789Sahrens 	new_FileVirtualDeviceBean(env, bean);
685789Sahrens 
6861499Stalley 	result = populate_FileVirtualDeviceBean(
6871499Stalley 	    env, zhp, vdev, p_vdev_id, bean);
688789Sahrens 	if (result) {
689789Sahrens 		/* Must not call any more Java methods to preserve exception */
690789Sahrens 		return (NULL);
691789Sahrens 	}
692789Sahrens 
693789Sahrens 	return (((zjni_Object_t *)bean)->object);
694789Sahrens }
695789Sahrens 
696789Sahrens static jobject
create_RAIDVirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id)6971499Stalley create_RAIDVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
6981499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id)
699789Sahrens {
700789Sahrens 	int result;
7015400Srl193873 	uint64_t parity;
702789Sahrens 	RAIDVirtualDeviceBean_t bean_obj = {0};
703789Sahrens 	RAIDVirtualDeviceBean_t *bean = &bean_obj;
704789Sahrens 
705789Sahrens 	((zjni_Object_t *)bean)->object = NULL;
706789Sahrens 
707789Sahrens 	/* Construct RAIDVirtualDeviceBean */
708789Sahrens 	new_RAIDVirtualDeviceBean(env, bean);
709789Sahrens 
7105400Srl193873 	/* Set parity bit */
7115400Srl193873 	result = nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_NPARITY,
7125400Srl193873 	    &parity);
7135400Srl193873 	if (result) {
7145400Srl193873 		/* Default to RAID-Z1 in case of error */
7155400Srl193873 		parity = 1;
7165400Srl193873 	}
7175400Srl193873 
7185400Srl193873 	(*env)->CallVoidMethod(
7195400Srl193873 	    env, ((zjni_Object_t *)bean)->object, bean->method_setParity,
7205400Srl193873 	    (jlong)parity);
7215400Srl193873 
7225400Srl193873 
7231499Stalley 	result = populate_RAIDVirtualDeviceBean(
7241499Stalley 	    env, zhp, vdev, p_vdev_id, bean);
725789Sahrens 	if (result) {
726789Sahrens 		/* Must not call any more Java methods to preserve exception */
727789Sahrens 		return (NULL);
728789Sahrens 	}
729789Sahrens 
730789Sahrens 	return (((zjni_Object_t *)bean)->object);
731789Sahrens }
732789Sahrens 
733789Sahrens static jobject
create_MirrorVirtualDeviceBean(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id)7341499Stalley create_MirrorVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
7351499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id)
736789Sahrens {
737789Sahrens 	int result;
738789Sahrens 	MirrorVirtualDeviceBean_t bean_obj = {0};
739789Sahrens 	MirrorVirtualDeviceBean_t *bean = &bean_obj;
740789Sahrens 
741789Sahrens 	/* Construct MirrorVirtualDeviceBean */
742789Sahrens 	new_MirrorVirtualDeviceBean(env, bean);
743789Sahrens 
7441499Stalley 	result = populate_MirrorVirtualDeviceBean(
7451499Stalley 	    env, zhp, vdev, p_vdev_id, bean);
746789Sahrens 	if (result) {
747789Sahrens 		/* Must not call any more Java methods to preserve exception */
748789Sahrens 		return (NULL);
749789Sahrens 	}
750789Sahrens 
751789Sahrens 	return (((zjni_Object_t *)bean)->object);
752789Sahrens }
753789Sahrens 
7541206Stalley static char *
find_field(const zjni_field_mapping_t * mapping,int value)7551206Stalley find_field(const zjni_field_mapping_t *mapping, int value) {
7561206Stalley 	int i;
7571206Stalley 	for (i = 0; mapping[i].name != NULL; i++) {
7581206Stalley 		if (value == mapping[i].value) {
7591206Stalley 			return (mapping[i].name);
7601206Stalley 		}
7611206Stalley 	}
7621206Stalley 	return (NULL);
7631206Stalley }
7641206Stalley 
7651206Stalley /*
7661206Stalley  * Converts a vdev_state_t to a Java DeviceStats$DeviceState object.
7671206Stalley  */
7681206Stalley static jobject
zjni_vdev_state_to_obj(JNIEnv * env,vdev_state_t state)7691206Stalley zjni_vdev_state_to_obj(JNIEnv *env, vdev_state_t state)
7701206Stalley {
7711206Stalley 	return (zjni_int_to_enum(env, state,
7721206Stalley 	    ZFSJNI_PACKAGE_DATA "DeviceStats$DeviceState",
7731206Stalley 	    "VDEV_STATE_UNKNOWN", vdev_state_map));
7741206Stalley }
7751206Stalley 
7761206Stalley /*
7771206Stalley  * Converts a vdev_aux_t to a Java DeviceStats$DeviceStatus object.
7781206Stalley  */
7791206Stalley static jobject
zjni_vdev_aux_to_obj(JNIEnv * env,vdev_aux_t aux)7801206Stalley zjni_vdev_aux_to_obj(JNIEnv *env, vdev_aux_t aux)
7811206Stalley {
7821206Stalley 	return (zjni_int_to_enum(env, aux,
7831206Stalley 	    ZFSJNI_PACKAGE_DATA "DeviceStats$DeviceStatus",
7841206Stalley 	    "VDEV_AUX_NONE", vdev_aux_map));
7851206Stalley }
7861206Stalley 
787789Sahrens /*
788789Sahrens  * Package-private functions
789789Sahrens  */
790789Sahrens 
7911206Stalley /* Create a DeviceStatsBean */
7921206Stalley void
new_DeviceStats(JNIEnv * env,DeviceStatsBean_t * bean,zjni_Object_t * object)7931206Stalley new_DeviceStats(JNIEnv *env, DeviceStatsBean_t *bean, zjni_Object_t *object)
7941206Stalley {
7951206Stalley 	bean->method_setSize = (*env)->GetMethodID(
7961206Stalley 	    env, object->class, "setSize", "(J)V");
7971206Stalley 
7981328Stalley 	bean->method_setReplacementSize = (*env)->GetMethodID(
7991328Stalley 	    env, object->class, "setReplacementSize", "(J)V");
8001328Stalley 
8011206Stalley 	bean->method_setUsed = (*env)->GetMethodID(
8021206Stalley 	    env, object->class, "setUsed", "(J)V");
8031206Stalley 
8041206Stalley 	bean->method_setReadBytes = (*env)->GetMethodID(
8051206Stalley 	    env, object->class, "setReadBytes", "(J)V");
8061206Stalley 
8071206Stalley 	bean->method_setWriteBytes = (*env)->GetMethodID(
8081206Stalley 	    env, object->class, "setWriteBytes", "(J)V");
8091206Stalley 
8101206Stalley 	bean->method_setReadOperations = (*env)->GetMethodID(
8111206Stalley 	    env, object->class, "setReadOperations", "(J)V");
8121206Stalley 
8131206Stalley 	bean->method_setWriteOperations = (*env)->GetMethodID(
8141206Stalley 	    env, object->class, "setWriteOperations", "(J)V");
8151206Stalley 
8161206Stalley 	bean->method_setReadErrors = (*env)->GetMethodID(
8171206Stalley 	    env, object->class, "setReadErrors", "(J)V");
8181206Stalley 
8191206Stalley 	bean->method_setWriteErrors = (*env)->GetMethodID(
8201206Stalley 	    env, object->class, "setWriteErrors", "(J)V");
8211206Stalley 
8221206Stalley 	bean->method_setChecksumErrors = (*env)->GetMethodID(
8231206Stalley 	    env, object->class, "setChecksumErrors", "(J)V");
8241206Stalley 
8251206Stalley 	bean->method_setDeviceState = (*env)->GetMethodID(
8261206Stalley 	    env, object->class, "setDeviceState",
8271206Stalley 	    "(L" ZFSJNI_PACKAGE_DATA "DeviceStats$DeviceState;)V");
8281206Stalley 
8291206Stalley 	bean->method_setDeviceStatus = (*env)->GetMethodID(
8301206Stalley 	    env, object->class, "setDeviceStatus",
8311206Stalley 	    "(L" ZFSJNI_PACKAGE_DATA "DeviceStats$DeviceStatus;)V");
8321206Stalley }
8331206Stalley 
8341206Stalley /* Create a PoolStatsBean */
8351206Stalley void
new_PoolStats(JNIEnv * env,PoolStatsBean_t * bean,zjni_Object_t * object)8361206Stalley new_PoolStats(JNIEnv *env, PoolStatsBean_t *bean, zjni_Object_t *object)
8371206Stalley {
8381206Stalley 	new_DeviceStats(env, (DeviceStatsBean_t *)bean, object);
8391206Stalley 
8401206Stalley 	bean->method_setPoolState = (*env)->GetMethodID(
8411206Stalley 	    env, object->class, "setPoolState",
8421206Stalley 	    "(L" ZFSJNI_PACKAGE_DATA "PoolStats$PoolState;)V");
8431206Stalley 
8441206Stalley 	bean->method_setPoolStatus = (*env)->GetMethodID(
8451206Stalley 	    env, object->class, "setPoolStatus",
8461206Stalley 	    "(L" ZFSJNI_PACKAGE_DATA "PoolStats$PoolStatus;)V");
8475582Scristian 
8485582Scristian 	bean->method_setPoolVersion = (*env)->GetMethodID(
8495582Scristian 	    env, object->class, "setPoolVersion", "(J)V");
8501206Stalley }
8511206Stalley 
852789Sahrens /*
853789Sahrens  * Gets the root vdev (an nvlist_t *) for the given pool.
854789Sahrens  */
855789Sahrens nvlist_t *
zjni_get_root_vdev(zpool_handle_t * zhp)856789Sahrens zjni_get_root_vdev(zpool_handle_t *zhp)
857789Sahrens {
858789Sahrens 	nvlist_t *root = NULL;
859789Sahrens 
860789Sahrens 	if (zhp != NULL) {
861952Seschrock 		nvlist_t *attrs = zpool_get_config(zhp, NULL);
862789Sahrens 
863789Sahrens 		if (attrs != NULL) {
864789Sahrens 			int result = nvlist_lookup_nvlist(
865789Sahrens 			    attrs, ZPOOL_CONFIG_VDEV_TREE, &root);
866789Sahrens 			if (result != 0) {
867789Sahrens 				root = NULL;
868789Sahrens 			}
869789Sahrens 		}
870789Sahrens 	}
871789Sahrens 
872789Sahrens 	return (root);
873789Sahrens }
874789Sahrens 
875789Sahrens /*
876789Sahrens  * Gets the vdev (an nvlist_t *) with the given vdev_id, below the
877789Sahrens  * given vdev.  If the given vdev is NULL, all vdevs within the given
878789Sahrens  * pool are searched.
8791499Stalley  *
8801499Stalley  * If p_vdev_id is not NULL, it will be set to the ID of the parent
8811499Stalley  * vdev, if any, or to vdev_id_to_find if the searched-for vdev is a
8821499Stalley  * toplevel vdev.
883789Sahrens  */
884789Sahrens nvlist_t *
zjni_get_vdev(zpool_handle_t * zhp,nvlist_t * vdev_parent,uint64_t vdev_id_to_find,uint64_t * p_vdev_id)885789Sahrens zjni_get_vdev(zpool_handle_t *zhp, nvlist_t *vdev_parent,
8861499Stalley     uint64_t vdev_id_to_find, uint64_t *p_vdev_id)
887789Sahrens {
888789Sahrens 	int result;
8891499Stalley 	uint64_t id = vdev_id_to_find;
890789Sahrens 
891789Sahrens 	/* Was a vdev specified? */
892789Sahrens 	if (vdev_parent == NULL) {
893789Sahrens 		/* No -- retrieve the top-level pool vdev */
894789Sahrens 		vdev_parent = zjni_get_root_vdev(zhp);
895789Sahrens 	} else {
896789Sahrens 		/* Get index of this vdev and compare with vdev_id_to_find */
897789Sahrens 		result = nvlist_lookup_uint64(
898789Sahrens 		    vdev_parent, ZPOOL_CONFIG_GUID, &id);
899789Sahrens 		if (result == 0 && id == vdev_id_to_find) {
900789Sahrens 			return (vdev_parent);
901789Sahrens 		}
902789Sahrens 	}
903789Sahrens 
904789Sahrens 	if (vdev_parent != NULL) {
905789Sahrens 
906789Sahrens 		nvlist_t **children;
907789Sahrens 		uint_t nelem = 0;
908789Sahrens 
909789Sahrens 		/* Get the vdevs under this vdev */
910789Sahrens 		result = nvlist_lookup_nvlist_array(
911789Sahrens 		    vdev_parent, ZPOOL_CONFIG_CHILDREN, &children, &nelem);
912789Sahrens 
913789Sahrens 		if (result == 0) {
914789Sahrens 
915789Sahrens 			int i;
916789Sahrens 			nvlist_t *child;
917789Sahrens 
918789Sahrens 			/* For each vdev child... */
919789Sahrens 			for (i = 0; i < nelem; i++) {
9201499Stalley 				if (p_vdev_id != NULL) {
9211499Stalley 					/* Save parent vdev id */
9221499Stalley 					*p_vdev_id = id;
9231499Stalley 				}
9241499Stalley 
925789Sahrens 				child = zjni_get_vdev(zhp, children[i],
9261499Stalley 				    vdev_id_to_find, p_vdev_id);
927789Sahrens 				if (child != NULL) {
928789Sahrens 					return (child);
929789Sahrens 				}
930789Sahrens 			}
931789Sahrens 		}
932789Sahrens 	}
933789Sahrens 
934789Sahrens 	return (NULL);
935789Sahrens }
936789Sahrens 
937789Sahrens jobject
zjni_get_VirtualDevice_from_vdev(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev,uint64_t * p_vdev_id)938789Sahrens zjni_get_VirtualDevice_from_vdev(JNIEnv *env, zpool_handle_t *zhp,
9391499Stalley     nvlist_t *vdev, uint64_t *p_vdev_id)
940789Sahrens {
941789Sahrens 	jobject obj = NULL;
942789Sahrens 	char *type = NULL;
943789Sahrens 	int result = nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE, &type);
944789Sahrens 
945789Sahrens 	if (result == 0) {
946789Sahrens 		if (strcmp(type, VDEV_TYPE_DISK) == 0) {
9471328Stalley 			uint64_t wholedisk;
9481328Stalley 			if (nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
9491328Stalley 			    &wholedisk) == 0 && wholedisk) {
9501328Stalley 				obj = create_DiskVirtualDeviceBean(
9511499Stalley 				    env, zhp, vdev, p_vdev_id);
9521328Stalley 			} else {
9531328Stalley 				obj = create_SliceVirtualDeviceBean(
9541499Stalley 				    env, zhp, vdev, p_vdev_id);
9551328Stalley 			}
956789Sahrens 		} else if (strcmp(type, VDEV_TYPE_FILE) == 0) {
9571499Stalley 			obj = create_FileVirtualDeviceBean(
9581499Stalley 			    env, zhp, vdev, p_vdev_id);
959789Sahrens 		} else if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
9601499Stalley 			obj = create_RAIDVirtualDeviceBean(
9611499Stalley 			    env, zhp, vdev, p_vdev_id);
962789Sahrens 		} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
9631499Stalley 			obj = create_MirrorVirtualDeviceBean(
9641499Stalley 			    env, zhp, vdev, p_vdev_id);
965894Stalley 		} else if (strcmp(type, VDEV_TYPE_REPLACING) == 0) {
966894Stalley 
967894Stalley 			/* Get the vdevs under this vdev */
968894Stalley 			nvlist_t **children;
969894Stalley 			uint_t nelem = 0;
970894Stalley 			int result = nvlist_lookup_nvlist_array(
971894Stalley 			    vdev, ZPOOL_CONFIG_CHILDREN, &children, &nelem);
972894Stalley 
973894Stalley 			if (result == 0 && nelem > 0) {
974894Stalley 
975894Stalley 				/* Get last vdev child (replacement device) */
976894Stalley 				nvlist_t *child = children[nelem - 1];
977894Stalley 
978894Stalley 				obj = zjni_get_VirtualDevice_from_vdev(env,
9791499Stalley 				    zhp, child, p_vdev_id);
980894Stalley 			}
981789Sahrens 		}
982789Sahrens 	}
983789Sahrens 
984789Sahrens 	return (obj);
985789Sahrens }
986789Sahrens 
987789Sahrens jobject
zjni_get_VirtualDevices_from_vdev(JNIEnv * env,zpool_handle_t * zhp,nvlist_t * vdev_parent,uint64_t * p_vdev_id)988789Sahrens zjni_get_VirtualDevices_from_vdev(JNIEnv *env, zpool_handle_t *zhp,
9891499Stalley     nvlist_t *vdev_parent, uint64_t *p_vdev_id)
990789Sahrens {
991789Sahrens 	/* Create an array list for the vdevs */
992789Sahrens 	zjni_ArrayList_t list_class = {0};
993789Sahrens 	zjni_ArrayList_t *list_class_p = &list_class;
994789Sahrens 	zjni_new_ArrayList(env, list_class_p);
995789Sahrens 
996789Sahrens 	/* Was a vdev specified? */
997789Sahrens 	if (vdev_parent == NULL) {
998789Sahrens 		/* No -- retrieve the top-level pool vdev */
999789Sahrens 		vdev_parent = zjni_get_root_vdev(zhp);
1000789Sahrens 	}
1001789Sahrens 
1002789Sahrens 	if (vdev_parent != NULL) {
1003789Sahrens 
1004789Sahrens 		/* Get the vdevs under this vdev */
1005789Sahrens 		nvlist_t **children;
1006789Sahrens 		uint_t nelem = 0;
1007789Sahrens 		int result = nvlist_lookup_nvlist_array(
1008789Sahrens 		    vdev_parent, ZPOOL_CONFIG_CHILDREN, &children, &nelem);
1009789Sahrens 
1010789Sahrens 		if (result == 0) {
1011789Sahrens 
1012789Sahrens 			/* For each vdev child... */
1013789Sahrens 			int i;
1014789Sahrens 			for (i = 0; i < nelem; i++) {
1015789Sahrens 				nvlist_t *child = children[i];
1016789Sahrens 
1017789Sahrens 				/* Create a Java object from this vdev */
1018789Sahrens 				jobject obj =
1019789Sahrens 				    zjni_get_VirtualDevice_from_vdev(env,
10205400Srl193873 				    zhp, child, p_vdev_id);
1021789Sahrens 
1022894Stalley 				if ((*env)->ExceptionOccurred(env) != NULL) {
1023789Sahrens 					/*
1024789Sahrens 					 * Must not call any more Java methods
1025789Sahrens 					 * to preserve exception
1026789Sahrens 					 */
1027789Sahrens 					return (NULL);
1028789Sahrens 				}
1029789Sahrens 
1030894Stalley 				if (obj != NULL) {
1031894Stalley 				    /* Add child to child vdev list */
10325400Srl193873 					(*env)->CallBooleanMethod(env,
10335400Srl193873 					    ((zjni_Object_t *)
10345400Srl193873 					    list_class_p)->object,
10355400Srl193873 					    ((zjni_Collection_t *)
10365400Srl193873 					    list_class_p)->method_add, obj);
1037894Stalley 				}
1038789Sahrens 			}
1039789Sahrens 		}
1040789Sahrens 	}
1041789Sahrens 
1042789Sahrens 	return (zjni_Collection_to_array(
1043789Sahrens 	    env, (zjni_Collection_t *)list_class_p,
1044789Sahrens 	    ZFSJNI_PACKAGE_DATA "VirtualDevice"));
1045789Sahrens }
1046894Stalley 
1047894Stalley int
zjni_create_add_ImportablePool(nvlist_t * config,void * data)10481206Stalley zjni_create_add_ImportablePool(nvlist_t *config, void *data) {
1049894Stalley 
1050894Stalley 	JNIEnv *env = ((zjni_ArrayCallbackData_t *)data)->env;
1051894Stalley 	zjni_Collection_t *list = ((zjni_ArrayCallbackData_t *)data)->list;
1052894Stalley 
1053894Stalley 	/* Construct ImportablePool object */
10541206Stalley 	jobject bean = create_ImportablePoolBean(env, config);
1055894Stalley 	if (bean == NULL) {
1056894Stalley 		return (-1);
1057894Stalley 	}
1058894Stalley 
1059894Stalley 	/* Add bean to list */
1060894Stalley 	(*env)->CallBooleanMethod(env, ((zjni_Object_t *)list)->object,
1061894Stalley 	    ((zjni_Collection_t *)list)->method_add, bean);
1062894Stalley 
1063894Stalley 	return (0);
1064894Stalley }
1065894Stalley 
10661206Stalley int
populate_DeviceStatsBean(JNIEnv * env,nvlist_t * vdev,DeviceStatsBean_t * bean,zjni_Object_t * object)10671206Stalley populate_DeviceStatsBean(JNIEnv *env, nvlist_t *vdev,
10681206Stalley     DeviceStatsBean_t *bean, zjni_Object_t *object)
10691206Stalley {
10701206Stalley 	uint_t c;
10711206Stalley 	vdev_stat_t *vs;
10721206Stalley 
10731206Stalley 	int result = nvlist_lookup_uint64_array(
107412296SLin.Ling@Sun.COM 	    vdev, ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &c);
10751206Stalley 	if (result != 0) {
10761206Stalley 		zjni_throw_exception(env,
10771206Stalley 		    "could not retrieve virtual device statistics");
10781206Stalley 		return (1);
10791206Stalley 	}
10801206Stalley 
10811206Stalley 	(*env)->CallVoidMethod(env, object->object,
10821206Stalley 	    bean->method_setUsed, (jlong)vs->vs_alloc);
10831206Stalley 
10841206Stalley 	(*env)->CallVoidMethod(env, object->object,
10851206Stalley 	    bean->method_setSize, (jlong)vs->vs_space);
10861206Stalley 
10871206Stalley 	(*env)->CallVoidMethod(env, object->object,
10881328Stalley 	    bean->method_setReplacementSize, (jlong)vs->vs_rsize);
10891328Stalley 
10901328Stalley 	(*env)->CallVoidMethod(env, object->object,
10911206Stalley 	    bean->method_setReadBytes, (jlong)vs->vs_bytes[ZIO_TYPE_READ]);
10921206Stalley 
10931206Stalley 	(*env)->CallVoidMethod(env, object->object,
10941206Stalley 	    bean->method_setWriteBytes, (jlong)vs->vs_bytes[ZIO_TYPE_WRITE]);
10951206Stalley 
10961206Stalley 	(*env)->CallVoidMethod(env, object->object,
10971206Stalley 	    bean->method_setReadOperations, (jlong)vs->vs_ops[ZIO_TYPE_READ]);
10981206Stalley 
10991206Stalley 	(*env)->CallVoidMethod(env, object->object,
11001206Stalley 	    bean->method_setWriteOperations, (jlong)vs->vs_ops[ZIO_TYPE_WRITE]);
11011206Stalley 
11021206Stalley 	(*env)->CallVoidMethod(env, object->object,
11031206Stalley 	    bean->method_setReadErrors, (jlong)vs->vs_read_errors);
11041206Stalley 
11051206Stalley 	(*env)->CallVoidMethod(env, object->object,
11061206Stalley 	    bean->method_setWriteErrors, (jlong)vs->vs_write_errors);
11071206Stalley 
11081206Stalley 	(*env)->CallVoidMethod(env, object->object,
11091206Stalley 	    bean->method_setChecksumErrors, (jlong)vs->vs_checksum_errors);
11101206Stalley 
11111206Stalley 	(*env)->CallVoidMethod(env, object->object,
11121206Stalley 	    bean->method_setDeviceState,
11131206Stalley 	    zjni_vdev_state_to_obj(env, vs->vs_state));
11141206Stalley 
11151206Stalley 	(*env)->CallVoidMethod(env, object->object,
11161206Stalley 	    bean->method_setDeviceStatus,
11171206Stalley 	    zjni_vdev_aux_to_obj(env, vs->vs_aux));
11181206Stalley 
11191206Stalley 	return (0);
11201206Stalley }
11211206Stalley 
11221206Stalley /*
11231206Stalley  * Converts a pool_state_t to a Java PoolStats$PoolState object.
11241206Stalley  */
11251206Stalley jobject
zjni_pool_state_to_obj(JNIEnv * env,pool_state_t state)11261206Stalley zjni_pool_state_to_obj(JNIEnv *env, pool_state_t state)
11271206Stalley {
11281206Stalley 	return (zjni_int_to_enum(env, state,
11291206Stalley 	    ZFSJNI_PACKAGE_DATA "PoolStats$PoolState",
11301206Stalley 	    "POOL_STATE_ACTIVE", pool_state_map));
11311206Stalley }
11321206Stalley 
11331206Stalley /*
11341206Stalley  * Converts a zpool_status_t to a Java PoolStats$PoolStatus object.
11351206Stalley  */
11361206Stalley jobject
zjni_pool_status_to_obj(JNIEnv * env,zpool_status_t status)11371206Stalley zjni_pool_status_to_obj(JNIEnv *env, zpool_status_t status)
11381206Stalley {
11391206Stalley 	return (zjni_int_to_enum(env, status,
11401206Stalley 	    ZFSJNI_PACKAGE_DATA "PoolStats$PoolStatus",
11411206Stalley 	    "ZPOOL_STATUS_OK", zpool_status_map));
11421206Stalley }
11431206Stalley 
1144894Stalley /*
1145894Stalley  * Extern functions
1146894Stalley  */
1147894Stalley 
1148894Stalley /*
1149894Stalley  * Iterates through each importable pool on the system.  For each
1150894Stalley  * importable pool, runs the given function with the given void as the
1151894Stalley  * last arg.
1152894Stalley  */
1153894Stalley int
zjni_ipool_iter(int argc,char ** argv,zjni_ipool_iter_f func,void * data)1154894Stalley zjni_ipool_iter(int argc, char **argv, zjni_ipool_iter_f func, void *data)
1155894Stalley {
11566807Sck153898 	nvlist_t *pools = zpool_find_import(g_zfs, argc, argv);
1157894Stalley 
1158894Stalley 	if (pools != NULL) {
11591206Stalley 		nvpair_t *elem = NULL;
1160894Stalley 
1161894Stalley 		while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
1162894Stalley 			nvlist_t *config;
1163894Stalley 
1164894Stalley 			if (nvpair_value_nvlist(elem, &config) != 0 ||
11651206Stalley 			    func(config, data)) {
1166894Stalley 				return (-1);
1167894Stalley 			}
1168894Stalley 		}
1169894Stalley 	}
1170894Stalley 
1171894Stalley 	return (0);
1172894Stalley }
1173894Stalley 
1174894Stalley char *
zjni_vdev_state_to_str(vdev_state_t state)11751206Stalley zjni_vdev_state_to_str(vdev_state_t state) {
11761206Stalley 	return (find_field(vdev_state_map, state));
11771206Stalley }
1178894Stalley 
11791206Stalley char *
zjni_vdev_aux_to_str(vdev_aux_t aux)11801206Stalley zjni_vdev_aux_to_str(vdev_aux_t aux) {
11811206Stalley 	return (find_field(vdev_aux_map, aux));
11821206Stalley }
1183894Stalley 
11841206Stalley char *
zjni_pool_state_to_str(pool_state_t state)11851206Stalley zjni_pool_state_to_str(pool_state_t state) {
11861206Stalley 	return (find_field(pool_state_map, state));
11871206Stalley }
1188894Stalley 
11891206Stalley char *
zjni_pool_status_to_str(zpool_status_t status)11901206Stalley zjni_pool_status_to_str(zpool_status_t status) {
11911206Stalley 	return (find_field(zpool_status_map, status));
1192894Stalley }
1193