1fa9e406ahrens/*
2fa9e406ahrens * CDDL HEADER START
3fa9e406ahrens *
4fa9e406ahrens * The contents of this file are subject to the terms of the
5bfe5f5atalley * Common Development and Distribution License (the "License").
6bfe5f5atalley * You may not use this file except in compliance with the License.
7fa9e406ahrens *
8fa9e406ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e406ahrens * or http://www.opensolaris.org/os/licensing.
10fa9e406ahrens * See the License for the specific language governing permissions
11fa9e406ahrens * and limitations under the License.
12fa9e406ahrens *
13fa9e406ahrens * When distributing Covered Code, include this CDDL HEADER in each
14fa9e406ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e406ahrens * If applicable, add the following below this CDDL HEADER, with the
16fa9e406ahrens * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e406ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e406ahrens *
19fa9e406ahrens * CDDL HEADER END
20fa9e406ahrens */
21bfe5f5atalley
22fa9e406ahrens/*
233f9d6adLin Ling * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24d8ab6e1Don Brady * Copyright 2020 Joyent, Inc.
25fa9e406ahrens */
26fa9e406ahrens
2799653d4eschrock#include "libzfs_jni_util.h"
28fa9e406ahrens#include "libzfs_jni_pool.h"
29d8ab6e1Don Brady#include <libzutil.h>
30fa9e406ahrens#include <strings.h>
31fa9e406ahrens
32fa9e406ahrens/*
33fa9e406ahrens * Types
34fa9e406ahrens */
35fa9e406ahrens
36c8e9ed1talleytypedef struct ImportablePoolBean {
37c8e9ed1talley	zjni_Object_t super;
38fbfd10ftalley	PoolStatsBean_t interface_PoolStats;
39c8e9ed1talley
40c8e9ed1talley	jmethodID method_setName;
41c8e9ed1talley	jmethodID method_setId;
42c8e9ed1talley} ImportablePoolBean_t;
43c8e9ed1talley
44fa9e406ahrenstypedef struct VirtualDeviceBean {
45fa9e406ahrens	zjni_Object_t super;
46fbfd10ftalley	DeviceStatsBean_t interface_DeviceStats;
47fa9e406ahrens
48fa9e406ahrens	jmethodID method_setPoolName;
49bfe5f5atalley	jmethodID method_setParentIndex;
50fa9e406ahrens	jmethodID method_setIndex;
51fa9e406ahrens} VirtualDeviceBean_t;
52fa9e406ahrens
53a1a659atalleytypedef struct LeafVirtualDeviceBean {
54fa9e406ahrens	VirtualDeviceBean_t super;
55fa9e406ahrens
56a1a659atalley	jmethodID method_setName;
57a1a659atalley} LeafVirtualDeviceBean_t;
58a1a659atalley
59a1a659atalleytypedef struct DiskVirtualDeviceBean {
60a1a659atalley	LeafVirtualDeviceBean_t super;
61fa9e406ahrens} DiskVirtualDeviceBean_t;
62fa9e406ahrens
63a1a659atalleytypedef struct SliceVirtualDeviceBean {
64a1a659atalley	LeafVirtualDeviceBean_t super;
65a1a659atalley} SliceVirtualDeviceBean_t;
66fa9e406ahrens
67a1a659atalleytypedef struct FileVirtualDeviceBean {
68a1a659atalley	LeafVirtualDeviceBean_t super;
69fa9e406ahrens} FileVirtualDeviceBean_t;
70fa9e406ahrens
71fa9e406ahrenstypedef struct RAIDVirtualDeviceBean {
72fa9e406ahrens	VirtualDeviceBean_t super;
731e995cfrl
741e995cfrl	jmethodID method_setParity;
75fa9e406ahrens} RAIDVirtualDeviceBean_t;
76fa9e406ahrens
77fa9e406ahrenstypedef struct MirrorVirtualDeviceBean {
78fa9e406ahrens	VirtualDeviceBean_t super;
79fa9e406ahrens} MirrorVirtualDeviceBean_t;
80fa9e406ahrens
81fa9e406ahrens/*
82fbfd10ftalley * Data
83fbfd10ftalley */
84fbfd10ftalley
85fbfd10ftalley/* vdev_state_t to DeviceStats$DeviceState map */
86fbfd10ftalleystatic zjni_field_mapping_t vdev_state_map[] = {
87fbfd10ftalley	{ VDEV_STATE_CANT_OPEN, "VDEV_STATE_CANT_OPEN" },
88fbfd10ftalley	{ VDEV_STATE_CLOSED, "VDEV_STATE_CLOSED" },
89fbfd10ftalley	{ VDEV_STATE_DEGRADED, "VDEV_STATE_DEGRADED" },
90fbfd10ftalley	{ VDEV_STATE_HEALTHY, "VDEV_STATE_HEALTHY" },
91fbfd10ftalley	{ VDEV_STATE_OFFLINE, "VDEV_STATE_OFFLINE" },
92fbfd10ftalley	{ VDEV_STATE_UNKNOWN, "VDEV_STATE_UNKNOWN" },
93fbfd10ftalley	{ -1, NULL },
94fbfd10ftalley};
95fbfd10ftalley
96fbfd10ftalley/* vdev_aux_t to DeviceStats$DeviceStatus map */
97fbfd10ftalleystatic zjni_field_mapping_t vdev_aux_map[] = {
98fbfd10ftalley	{ VDEV_AUX_NONE, "VDEV_AUX_NONE" },
99fbfd10ftalley	{ VDEV_AUX_OPEN_FAILED, "VDEV_AUX_OPEN_FAILED" },
100fbfd10ftalley	{ VDEV_AUX_CORRUPT_DATA, "VDEV_AUX_CORRUPT_DATA" },
101fbfd10ftalley	{ VDEV_AUX_NO_REPLICAS, "VDEV_AUX_NO_REPLICAS" },
102fbfd10ftalley	{ VDEV_AUX_BAD_GUID_SUM, "VDEV_AUX_BAD_GUID_SUM" },
103fbfd10ftalley	{ VDEV_AUX_TOO_SMALL, "VDEV_AUX_TOO_SMALL" },
104fbfd10ftalley	{ VDEV_AUX_BAD_LABEL, "VDEV_AUX_BAD_LABEL" },
105fbfd10ftalley	{ -1, NULL },
106fbfd10ftalley};
107fbfd10ftalley
108fbfd10ftalley/* zpool_state_t to PoolStats$PoolState map */
109fbfd10ftalleystatic zjni_field_mapping_t pool_state_map[] = {
110fbfd10ftalley	{ POOL_STATE_ACTIVE, "POOL_STATE_ACTIVE" },
111fbfd10ftalley	{ POOL_STATE_EXPORTED, "POOL_STATE_EXPORTED" },
112fbfd10ftalley	{ POOL_STATE_DESTROYED, "POOL_STATE_DESTROYED" },
113a55b684cristian	{ POOL_STATE_SPARE, "POOL_STATE_SPARE" },
114fbfd10ftalley	{ POOL_STATE_UNINITIALIZED, "POOL_STATE_UNINITIALIZED" },
115fbfd10ftalley	{ POOL_STATE_UNAVAIL, "POOL_STATE_UNAVAIL" },
116a55b684cristian	{ POOL_STATE_POTENTIALLY_ACTIVE, "POOL_STATE_POTENTIALLY_ACTIVE" },
117fbfd10ftalley	{ -1, NULL },
118fbfd10ftalley};
119fbfd10ftalley
120fbfd10ftalley/* zpool_status_t to PoolStats$PoolStatus map */
121fbfd10ftalleystatic zjni_field_mapping_t zpool_status_map[] = {
122a55b684cristian	{ ZPOOL_STATUS_CORRUPT_CACHE, "ZPOOL_STATUS_CORRUPT_CACHE" },
123a55b684cristian	{ ZPOOL_STATUS_MISSING_DEV_R, "ZPOOL_STATUS_MISSING_DEV_R" },
124a55b684cristian	{ ZPOOL_STATUS_MISSING_DEV_NR, "ZPOOL_STATUS_MISSING_DEV_NR" },
125a55b684cristian	{ ZPOOL_STATUS_CORRUPT_LABEL_R, "ZPOOL_STATUS_CORRUPT_LABEL_R" },
126a55b684cristian	{ ZPOOL_STATUS_CORRUPT_LABEL_NR, "ZPOOL_STATUS_CORRUPT_LABEL_NR" },
127fbfd10ftalley	{ ZPOOL_STATUS_BAD_GUID_SUM, "ZPOOL_STATUS_BAD_GUID_SUM" },
128fbfd10ftalley	{ ZPOOL_STATUS_CORRUPT_POOL, "ZPOOL_STATUS_CORRUPT_POOL" },
129fbfd10ftalley	{ ZPOOL_STATUS_CORRUPT_DATA, "ZPOOL_STATUS_CORRUPT_DATA" },
130fbfd10ftalley	{ ZPOOL_STATUS_FAILING_DEV, "ZPOOL_STATUS_FAILING_DEV" },
131a55b684cristian	{ ZPOOL_STATUS_VERSION_NEWER, "ZPOOL_STATUS_VERSION_NEWER" },
132a55b684cristian	{ ZPOOL_STATUS_HOSTID_MISMATCH, "ZPOOL_STATUS_HOSTID_MISMATCH" },
133a55b684cristian	{ ZPOOL_STATUS_FAULTED_DEV_R, "ZPOOL_STATUS_FAULTED_DEV_R" },
134a55b684cristian	{ ZPOOL_STATUS_FAULTED_DEV_NR, "ZPOOL_STATUS_FAULTED_DEV_NR" },
135b87f3afperrin	{ ZPOOL_STATUS_BAD_LOG, "ZPOOL_STATUS_BAD_LOG" },
136a55b684cristian	{ ZPOOL_STATUS_VERSION_OLDER, "ZPOOL_STATUS_VERSION_OLDER" },
137fbfd10ftalley	{ ZPOOL_STATUS_RESILVERING, "ZPOOL_STATUS_RESILVERING" },
138fbfd10ftalley	{ ZPOOL_STATUS_OFFLINE_DEV, "ZPOOL_STATUS_OFFLINE_DEV" },
139c25309dGeorge Wilson	{ ZPOOL_STATUS_REMOVED_DEV, "ZPOOL_STATUS_REMOVED_DEV" },
140fbfd10ftalley	{ ZPOOL_STATUS_OK, "ZPOOL_STATUS_OK" },
141a55b684cristian	{ -1, NULL }
142fbfd10ftalley};
143fbfd10ftalley
144fbfd10ftalley/*
145fa9e406ahrens * Function prototypes
146fa9e406ahrens */
147fa9e406ahrens
148c8e9ed1talleystatic void new_ImportablePoolBean(JNIEnv *, ImportablePoolBean_t *);
149fa9e406ahrensstatic void new_VirtualDevice(JNIEnv *, VirtualDeviceBean_t *);
150a1a659atalleystatic void new_LeafVirtualDevice(JNIEnv *, LeafVirtualDeviceBean_t *);
151fa9e406ahrensstatic void new_DiskVirtualDeviceBean(JNIEnv *, DiskVirtualDeviceBean_t *);
152a1a659atalleystatic void new_SliceVirtualDeviceBean(JNIEnv *, SliceVirtualDeviceBean_t *);
153fa9e406ahrensstatic void new_FileVirtualDeviceBean(JNIEnv *, FileVirtualDeviceBean_t *);
154fa9e406ahrensstatic void new_RAIDVirtualDeviceBean(JNIEnv *, RAIDVirtualDeviceBean_t *);
155fa9e406ahrensstatic void new_MirrorVirtualDeviceBean(JNIEnv *, MirrorVirtualDeviceBean_t *);
156c8e9ed1talleystatic int populate_ImportablePoolBean(
157fbfd10ftalley    JNIEnv *, ImportablePoolBean_t *, nvlist_t *);
158bfe5f5atalleystatic int populate_VirtualDeviceBean(JNIEnv *, zpool_handle_t *,
159bfe5f5atalley    nvlist_t *, uint64_t *p_vdev_id, VirtualDeviceBean_t *);
160bfe5f5atalleystatic int populate_LeafVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
161bfe5f5atalley    nvlist_t *, uint64_t *p_vdev_id, LeafVirtualDeviceBean_t *);
162bfe5f5atalleystatic int populate_DiskVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
163bfe5f5atalley    nvlist_t *, uint64_t *p_vdev_id, DiskVirtualDeviceBean_t *);
164bfe5f5atalleystatic int populate_SliceVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
165bfe5f5atalley    nvlist_t *, uint64_t *p_vdev_id, SliceVirtualDeviceBean_t *);
166bfe5f5atalleystatic int populate_FileVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
167bfe5f5atalley    nvlist_t *, uint64_t *p_vdev_id, FileVirtualDeviceBean_t *);
168bfe5f5atalleystatic int populate_RAIDVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
169bfe5f5atalley    nvlist_t *, uint64_t *p_vdev_id, RAIDVirtualDeviceBean_t *);
170bfe5f5atalleystatic int populate_MirrorVirtualDeviceBean(JNIEnv *, zpool_handle_t *,
171bfe5f5atalley    nvlist_t *, uint64_t *p_vdev_id, MirrorVirtualDeviceBean_t *);
172fbfd10ftalleystatic jobject create_ImportablePoolBean(JNIEnv *, nvlist_t *);
173fa9e406ahrensstatic jobject create_DiskVirtualDeviceBean(
174bfe5f5atalley    JNIEnv *, zpool_handle_t *, nvlist_t *, uint64_t *p_vdev_id);
175a1a659atalleystatic jobject create_SliceVirtualDeviceBean(
176bfe5f5atalley    JNIEnv *, zpool_handle_t *, nvlist_t *, uint64_t *p_vdev_id);
177fa9e406ahrensstatic jobject create_FileVirtualDeviceBean(
178bfe5f5atalley    JNIEnv *, zpool_handle_t *, nvlist_t *, uint64_t *p_vdev_id);
179fa9e406ahrensstatic jobject create_RAIDVirtualDeviceBean(
180bfe5f5atalley    JNIEnv *, zpool_handle_t *, nvlist_t *, uint64_t *p_vdev_id);
181fa9e406ahrensstatic jobject create_MirrorVirtualDeviceBean(
182bfe5f5atalley    JNIEnv *, zpool_handle_t *, nvlist_t *, uint64_t *p_vdev_id);
183fbfd10ftalleystatic char *find_field(const zjni_field_mapping_t *, int);
184fbfd10ftalleystatic jobject zjni_vdev_state_to_obj(JNIEnv *, vdev_state_t);
185fbfd10ftalleystatic jobject zjni_vdev_aux_to_obj(JNIEnv *, vdev_aux_t);
186fa9e406ahrens
187fa9e406ahrens/*
188fa9e406ahrens * Static functions
189fa9e406ahrens */
190fa9e406ahrens
191c8e9ed1talley/* Create a ImportablePoolBean */
192c8e9ed1talleystatic void
193c8e9ed1talleynew_ImportablePoolBean(JNIEnv *env, ImportablePoolBean_t *bean)
194c8e9ed1talley{
195c8e9ed1talley	zjni_Object_t *object = (zjni_Object_t *)bean;
196c8e9ed1talley
197c8e9ed1talley	if (object->object == NULL) {
198c8e9ed1talley		object->class =
199c8e9ed1talley		    (*env)->FindClass(env,
2001e995cfrl		    ZFSJNI_PACKAGE_DATA "ImportablePoolBean");
201c8e9ed1talley
202c8e9ed1talley		object->constructor =
203c8e9ed1talley		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
204c8e9ed1talley
205c8e9ed1talley		object->object =
206c8e9ed1talley		    (*env)->NewObject(env, object->class, object->constructor);
207c8e9ed1talley	}
208c8e9ed1talley
209fbfd10ftalley	new_PoolStats(env, &(bean->interface_PoolStats), object);
210fbfd10ftalley
211c8e9ed1talley	bean->method_setName = (*env)->GetMethodID(
212c8e9ed1talley	    env, object->class, "setName", "(Ljava/lang/String;)V");
213c8e9ed1talley
214c8e9ed1talley	bean->method_setId = (*env)->GetMethodID(
215c8e9ed1talley	    env, object->class, "setId", "(J)V");
216c8e9ed1talley}
217c8e9ed1talley
218fa9e406ahrens/* Create a VirtualDeviceBean */
219fa9e406ahrensstatic void
220fa9e406ahrensnew_VirtualDevice(JNIEnv *env, VirtualDeviceBean_t *bean)
221fa9e406ahrens{
222fa9e406ahrens	zjni_Object_t *object = (zjni_Object_t *)bean;
223fa9e406ahrens
224fa9e406ahrens	if (object->object == NULL) {
225fa9e406ahrens		object->class =
226fa9e406ahrens		    (*env)->FindClass(env,
2271e995cfrl		    ZFSJNI_PACKAGE_DATA "VirtualDeviceBean");
228fa9e406ahrens
229fa9e406ahrens		object->constructor =
230fa9e406ahrens		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
231fa9e406ahrens
232fa9e406ahrens		object->object =
233fa9e406ahrens		    (*env)->NewObject(env, object->class, object->constructor);
234fa9e406ahrens	}
235fa9e406ahrens
236fbfd10ftalley	new_DeviceStats(env, &(bean->interface_DeviceStats), object);
237fbfd10ftalley
238fa9e406ahrens	bean->method_setPoolName = (*env)->GetMethodID(
239fa9e406ahrens	    env, object->class, "setPoolName", "(Ljava/lang/String;)V");
240fa9e406ahrens
241bfe5f5atalley	bean->method_setParentIndex = (*env)->GetMethodID(
242bfe5f5atalley	    env, object->class, "setParentIndex", "(Ljava/lang/Long;)V");
243bfe5f5atalley
244fa9e406ahrens	bean->method_setIndex = (*env)->GetMethodID(
245fa9e406ahrens	    env, object->class, "setIndex", "(J)V");
246fa9e406ahrens}
247fa9e406ahrens
248a1a659atalley/* Create a LeafVirtualDeviceBean */
249a1a659atalleystatic void
250a1a659atalleynew_LeafVirtualDevice(JNIEnv *env, LeafVirtualDeviceBean_t *bean)
251a1a659atalley{
252a1a659atalley	zjni_Object_t *object = (zjni_Object_t *)bean;
253a1a659atalley
254a1a659atalley	if (object->object == NULL) {
255a1a659atalley		object->class =
256a1a659atalley		    (*env)->FindClass(env,
2571e995cfrl		    ZFSJNI_PACKAGE_DATA "LeafVirtualDeviceBean");
258a1a659atalley
259a1a659atalley		object->constructor =
260a1a659atalley		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
261a1a659atalley
262a1a659atalley		object->object =
263a1a659atalley		    (*env)->NewObject(env, object->class, object->constructor);
264a1a659atalley	}
265a1a659atalley
266a1a659atalley	new_VirtualDevice(env, (VirtualDeviceBean_t *)bean);
267a1a659atalley
268a1a659atalley	bean->method_setName = (*env)->GetMethodID(
269a1a659atalley	    env, object->class, "setName", "(Ljava/lang/String;)V");
270a1a659atalley}
271a1a659atalley
272fa9e406ahrens/* Create a DiskVirtualDeviceBean */
273fa9e406ahrensstatic void
274fa9e406ahrensnew_DiskVirtualDeviceBean(JNIEnv *env, DiskVirtualDeviceBean_t *bean)
275fa9e406ahrens{
276fa9e406ahrens	zjni_Object_t *object = (zjni_Object_t *)bean;
277fa9e406ahrens
278fa9e406ahrens	if (object->object == NULL) {
279fa9e406ahrens		object->class = (*env)->FindClass(
280fa9e406ahrens		    env, ZFSJNI_PACKAGE_DATA "DiskVirtualDeviceBean");
281fa9e406ahrens
282fa9e406ahrens		object->constructor =
283fa9e406ahrens		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
284fa9e406ahrens
285fa9e406ahrens		object->object =
286fa9e406ahrens		    (*env)->NewObject(env, object->class, object->constructor);
287fa9e406ahrens	}
288fa9e406ahrens
289a1a659atalley	new_LeafVirtualDevice(env, (LeafVirtualDeviceBean_t *)bean);
290a1a659atalley}
291fa9e406ahrens
292a1a659atalley/* Create a SliceVirtualDeviceBean */
293a1a659atalleystatic void
294a1a659atalleynew_SliceVirtualDeviceBean(JNIEnv *env, SliceVirtualDeviceBean_t *bean)
295a1a659atalley{
296a1a659atalley	zjni_Object_t *object = (zjni_Object_t *)bean;
297fa9e406ahrens
298a1a659atalley	if (object->object == NULL) {
299a1a659atalley		object->class = (*env)->FindClass(
300a1a659atalley		    env, ZFSJNI_PACKAGE_DATA "SliceVirtualDeviceBean");
301a1a659atalley
302a1a659atalley		object->constructor =
303a1a659atalley		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
304a1a659atalley
305a1a659atalley		object->object =
306a1a659atalley		    (*env)->NewObject(env, object->class, object->constructor);
307a1a659atalley	}
308a1a659atalley
309a1a659atalley	new_LeafVirtualDevice(env, (LeafVirtualDeviceBean_t *)bean);
310fa9e406ahrens}
311fa9e406ahrens
312fa9e406ahrens/* Create a FileVirtualDeviceBean */
313fa9e406ahrensstatic void
314fa9e406ahrensnew_FileVirtualDeviceBean(JNIEnv *env, FileVirtualDeviceBean_t *bean)
315fa9e406ahrens{
316fa9e406ahrens	zjni_Object_t *object = (zjni_Object_t *)bean;
317fa9e406ahrens
318fa9e406ahrens	if (object->object == NULL) {
319fa9e406ahrens		object->class = (*env)->FindClass(
320fa9e406ahrens		    env, ZFSJNI_PACKAGE_DATA "FileVirtualDeviceBean");
321fa9e406ahrens
322fa9e406ahrens		object->constructor =
323fa9e406ahrens		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
324fa9e406ahrens
325fa9e406ahrens		object->object =
326fa9e406ahrens		    (*env)->NewObject(env, object->class, object->constructor);
327fa9e406ahrens	}
328fa9e406ahrens
329a1a659atalley	new_LeafVirtualDevice(env, (LeafVirtualDeviceBean_t *)bean);
330fa9e406ahrens}
331fa9e406ahrens
332fa9e406ahrens/* Create a RAIDVirtualDeviceBean */
333fa9e406ahrensstatic void
334fa9e406ahrensnew_RAIDVirtualDeviceBean(JNIEnv *env, RAIDVirtualDeviceBean_t *bean)
335fa9e406ahrens{
336fa9e406ahrens	zjni_Object_t *object = (zjni_Object_t *)bean;
337fa9e406ahrens
338fa9e406ahrens	if (object->object == NULL) {
339fa9e406ahrens
340fa9e406ahrens		object->class = (*env)->FindClass(
341fa9e406ahrens		    env, ZFSJNI_PACKAGE_DATA "RAIDVirtualDeviceBean");
342fa9e406ahrens
343fa9e406ahrens		object->constructor =
344fa9e406ahrens		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
345fa9e406ahrens
346fa9e406ahrens		object->object =
347fa9e406ahrens		    (*env)->NewObject(env, object->class, object->constructor);
348fa9e406ahrens	}
349fa9e406ahrens
350fa9e406ahrens	new_VirtualDevice(env, (VirtualDeviceBean_t *)bean);
3511e995cfrl
3521e995cfrl	bean->method_setParity = (*env)->GetMethodID(
3531e995cfrl	    env, object->class, "setParity", "(J)V");
354fa9e406ahrens}
355fa9e406ahrens
356fa9e406ahrens/* Create a MirrorVirtualDeviceBean */
357fa9e406ahrensstatic void
358fa9e406ahrensnew_MirrorVirtualDeviceBean(JNIEnv *env, MirrorVirtualDeviceBean_t *bean)
359fa9e406ahrens{
360fa9e406ahrens	zjni_Object_t *object = (zjni_Object_t *)bean;
361fa9e406ahrens
362fa9e406ahrens	if (object->object == NULL) {
363fa9e406ahrens		object->class = (*env)->FindClass(
364fa9e406ahrens		    env, ZFSJNI_PACKAGE_DATA "MirrorVirtualDeviceBean");
365fa9e406ahrens
366fa9e406ahrens		object->constructor =
367fa9e406ahrens		    (*env)->GetMethodID(env, object->class, "<init>", "()V");
368fa9e406ahrens
369fa9e406ahrens		object->object =
370fa9e406ahrens		    (*env)->NewObject(env, object->class, object->constructor);
371fa9e406ahrens	}
372fa9e406ahrens
373fa9e406ahrens	new_VirtualDevice(env, (VirtualDeviceBean_t *)bean);
374fa9e406ahrens}
375fa9e406ahrens
376c8e9ed1talleystatic int
377c8e9ed1talleypopulate_ImportablePoolBean(JNIEnv *env, ImportablePoolBean_t *bean,
378fbfd10ftalley    nvlist_t *config)
379c8e9ed1talley{
380fbfd10ftalley	char *c;
381fbfd10ftalley	char *name;
382fbfd10ftalley	uint64_t guid;
383fbfd10ftalley	uint64_t state;
384a55b684cristian	uint64_t version;
385fbfd10ftalley	nvlist_t *devices;
386fbfd10ftalley
387c8e9ed1talley	zjni_Object_t *object = (zjni_Object_t *)bean;
388fbfd10ftalley	PoolStatsBean_t *pool_stats = &(bean->interface_PoolStats);
389fbfd10ftalley	DeviceStatsBean_t *dev_stats = (DeviceStatsBean_t *)pool_stats;
390fbfd10ftalley
391fbfd10ftalley	if (nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME, &name) ||
392fbfd10ftalley	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) ||
393fbfd10ftalley	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_STATE, &state) ||
394a55b684cristian	    nvlist_lookup_uint64(config, ZPOOL_CONFIG_VERSION, &version) ||
395fbfd10ftalley	    nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, &devices) ||
396fbfd10ftalley	    populate_DeviceStatsBean(env, devices, dev_stats, object)) {
397fbfd10ftalley		return (-1);
398fbfd10ftalley	}
399c8e9ed1talley
400fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
401fbfd10ftalley	    bean->method_setName, (*env)->NewStringUTF(env, name));
402c8e9ed1talley
403fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
404fbfd10ftalley	    bean->method_setId, (jlong)guid);
405c8e9ed1talley
406fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
407fbfd10ftalley	    pool_stats->method_setPoolState,
408fbfd10ftalley	    zjni_pool_state_to_obj(env, (pool_state_t)state));
409c8e9ed1talley
410fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
411fbfd10ftalley	    pool_stats->method_setPoolStatus,
412eb63303Tom Caputi	    zjni_pool_status_to_obj(env, zpool_import_status(config, &c,
413eb63303Tom Caputi	    NULL)));
414c8e9ed1talley
415a55b684cristian	(*env)->CallVoidMethod(env, object->object,
416a55b684cristian	    pool_stats->method_setPoolVersion, (jlong)version);
417a55b684cristian
418c8e9ed1talley	return (0);
419c8e9ed1talley}
420c8e9ed1talley
421fa9e406ahrensstatic int
422fa9e406ahrenspopulate_VirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
423bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id, VirtualDeviceBean_t *bean)
424fa9e406ahrens{
425fa9e406ahrens	int result;
426fa9e406ahrens	uint64_t vdev_id;
427fbfd10ftalley	jstring poolUTF;
428fbfd10ftalley
429fa9e406ahrens	zjni_Object_t *object = (zjni_Object_t *)bean;
430fbfd10ftalley	DeviceStatsBean_t *stats = &(bean->interface_DeviceStats);
431fbfd10ftalley
432fbfd10ftalley	result = populate_DeviceStatsBean(env, vdev, stats, object);
433fbfd10ftalley	if (result != 0) {
434fbfd10ftalley		return (1);
435fbfd10ftalley	}
436fa9e406ahrens
437fa9e406ahrens	/* Set pool name */
438fbfd10ftalley	poolUTF = (*env)->NewStringUTF(env, zpool_get_name(zhp));
439fa9e406ahrens	(*env)->CallVoidMethod(
440fa9e406ahrens	    env, object->object, bean->method_setPoolName, poolUTF);
441fa9e406ahrens
442bfe5f5atalley	/* Set parent vdev index */
443bfe5f5atalley	(*env)->CallVoidMethod(
444bfe5f5atalley	    env, object->object, bean->method_setParentIndex,
445bfe5f5atalley	    p_vdev_id == NULL ? NULL :
446bfe5f5atalley	    zjni_long_to_Long(env, *p_vdev_id));
447bfe5f5atalley
448fa9e406ahrens	/* Get index */
449fa9e406ahrens	result = nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_GUID, &vdev_id);
450fa9e406ahrens	if (result != 0) {
451fa9e406ahrens		zjni_throw_exception(env,
452fa9e406ahrens		    "could not retrieve virtual device ID (pool %s)",
453fa9e406ahrens		    zpool_get_name(zhp));
454fbfd10ftalley		return (1);
455fa9e406ahrens	}
456fa9e406ahrens
457fbfd10ftalley	(*env)->CallVoidMethod(
458fbfd10ftalley	    env, object->object, bean->method_setIndex, (jlong)vdev_id);
459fbfd10ftalley
460fbfd10ftalley	return (0);
461fa9e406ahrens}
462fa9e406ahrens
463fa9e406ahrensstatic int
464a1a659atalleypopulate_LeafVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
465bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id, LeafVirtualDeviceBean_t *bean)
466a1a659atalley{
467a1a659atalley	return (populate_VirtualDeviceBean(
468bfe5f5atalley	    env, zhp, vdev, p_vdev_id, (VirtualDeviceBean_t *)bean));
469a1a659atalley}
470a1a659atalley
471a1a659atalleystatic int
472fa9e406ahrenspopulate_DiskVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
473bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id, DiskVirtualDeviceBean_t *bean)
474fa9e406ahrens{
475fa9e406ahrens	char *path;
476a1a659atalley	int result = populate_LeafVirtualDeviceBean(
477bfe5f5atalley	    env, zhp, vdev, p_vdev_id, (LeafVirtualDeviceBean_t *)bean);
478fa9e406ahrens
479fa9e406ahrens	if (result) {
480fa9e406ahrens		/* Must not call any more Java methods to preserve exception */
481fa9e406ahrens		return (-1);
482fa9e406ahrens	}
483fa9e406ahrens
484fa9e406ahrens	/* Set path */
485fa9e406ahrens	result = nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path);
486fa9e406ahrens	if (result != 0) {
487fa9e406ahrens		zjni_throw_exception(env,
488d7d4af5mmusante		    "could not retrieve path from disk virtual device "
489d7d4af5mmusante		    "(pool %s)", zpool_get_name(zhp));
490fa9e406ahrens	} else {
491fa9e406ahrens
492a1a659atalley		regex_t re;
493a1a659atalley		regmatch_t matches[2];
494a1a659atalley		jstring pathUTF = NULL;
495a1a659atalley
496a1a659atalley		/* Strip off slice portion of name, if applicable */
497a1a659atalley		if (regcomp(&re, "^(/dev/dsk/.*)s[0-9]+$", REG_EXTENDED) == 0) {
498a1a659atalley			if (regexec(&re, path, 2, matches, 0) == 0) {
499a1a659atalley				regmatch_t *match = matches + 1;
500a1a659atalley				if (match->rm_so != -1 && match->rm_eo != -1) {
501a1a659atalley					char *tmp = strdup(path);
502a1a659atalley					if (tmp != NULL) {
503a1a659atalley						char *end = tmp + match->rm_eo;
504a1a659atalley						*end = '\0';
505a1a659atalley						pathUTF = (*env)->NewStringUTF(
506a1a659atalley						    env, tmp);
507a1a659atalley						free(tmp);
508a1a659atalley					}
509a1a659atalley				}
510a1a659atalley			}
511a1a659atalley			regfree(&re);
512a1a659atalley		}
5133fdda49John Harres		if (regcomp(&re, "^(/dev/dsk/.*)s[0-9]+/old$", REG_EXTENDED) ==
5143fdda49John Harres		    0) {
5153fdda49John Harres			if (regexec(&re, path, 2, matches, 0) == 0) {
5163fdda49John Harres				regmatch_t *match = matches + 1;
5173fdda49John Harres				if (match->rm_so != -1 && match->rm_eo != -1) {
5183fdda49John Harres					char *tmp = strdup(path);
5193fdda49John Harres					if (tmp != NULL) {
5203fdda49John Harres						(void) strcpy(tmp +
5213fdda49John Harres						    match->rm_eo, "/old");
5223fdda49John Harres						pathUTF = (*env)->NewStringUTF(
5233fdda49John Harres						    env, tmp);
5243fdda49John Harres						free(tmp);
5253fdda49John Harres					}
5263fdda49John Harres				}
5273fdda49John Harres			}
5283fdda49John Harres			regfree(&re);
5293fdda49John Harres		}
530a1a659atalley
531a1a659atalley		if (pathUTF == NULL) {
5321e995cfrl			pathUTF = (*env)->NewStringUTF(env, path);
533a1a659atalley		}
534a1a659atalley
535a1a659atalley		(*env)->CallVoidMethod(env, ((zjni_Object_t *)bean)->object,
536a1a659atalley		    ((LeafVirtualDeviceBean_t *)bean)->method_setName, pathUTF);
537a1a659atalley	}
538a1a659atalley
539a1a659atalley	return (result != 0);
540a1a659atalley}
541a1a659atalley
542a1a659atalleystatic int
543a1a659atalleypopulate_SliceVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
544bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id, SliceVirtualDeviceBean_t *bean)
545a1a659atalley{
546a1a659atalley	char *path;
547a1a659atalley	int result = populate_LeafVirtualDeviceBean(
548bfe5f5atalley	    env, zhp, vdev, p_vdev_id, (LeafVirtualDeviceBean_t *)bean);
549a1a659atalley
550a1a659atalley	if (result) {
551a1a659atalley		/* Must not call any more Java methods to preserve exception */
552a1a659atalley		return (-1);
553a1a659atalley	}
554a1a659atalley
555a1a659atalley	/* Set path */
556a1a659atalley	result = nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path);
557a1a659atalley	if (result != 0) {
558a1a659atalley		zjni_throw_exception(env,
559d7d4af5mmusante		    "could not retrieve path from slice virtual device (pool "
560a1a659atalley		    "%s)", zpool_get_name(zhp));
561a1a659atalley	} else {
562a1a659atalley
563fa9e406ahrens		jstring pathUTF = (*env)->NewStringUTF(env, path);
564fa9e406ahrens		(*env)->CallVoidMethod(env, ((zjni_Object_t *)bean)->object,
565bfe5f5atalley		    ((LeafVirtualDeviceBean_t *)bean)->method_setName,
566bfe5f5atalley		    pathUTF);
567fa9e406ahrens	}
568fa9e406ahrens
569fa9e406ahrens	return (result != 0);
570fa9e406ahrens}
571fa9e406ahrens
572fa9e406ahrensstatic int
573fa9e406ahrenspopulate_FileVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
574bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id, FileVirtualDeviceBean_t *bean)
575fa9e406ahrens{
576fa9e406ahrens	char *path;
577a1a659atalley	int result = populate_LeafVirtualDeviceBean(
578bfe5f5atalley	    env, zhp, vdev, p_vdev_id, (LeafVirtualDeviceBean_t *)bean);
579fa9e406ahrens
580fa9e406ahrens	if (result) {
581fa9e406ahrens		/* Must not call any more Java methods to preserve exception */
582fa9e406ahrens		return (-1);
583fa9e406ahrens	}
584fa9e406ahrens
585fa9e406ahrens	/* Set path */
586fa9e406ahrens	result = nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path);
587fa9e406ahrens	if (result != 0) {
588fa9e406ahrens		zjni_throw_exception(env,
589d7d4af5mmusante		    "could not retrieve path from disk virtual device "
590d7d4af5mmusante		    "(pool %s)", zpool_get_name(zhp));
591fa9e406ahrens	} else {
592fa9e406ahrens
593fa9e406ahrens		jstring pathUTF = (*env)->NewStringUTF(env, path);
594fa9e406ahrens		(*env)->CallVoidMethod(env, ((zjni_Object_t *)bean)->object,
595a1a659atalley		    ((LeafVirtualDeviceBean_t *)bean)->method_setName, pathUTF);
596fa9e406ahrens	}
597fa9e406ahrens
598fa9e406ahrens	return (result != 0);
599fa9e406ahrens}
600fa9e406ahrens
601fa9e406ahrensstatic int
602fa9e406ahrenspopulate_RAIDVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
603bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id, RAIDVirtualDeviceBean_t *bean)
604fa9e406ahrens{
605bfe5f5atalley	return (populate_VirtualDeviceBean(env, zhp, vdev, p_vdev_id,
606fa9e406ahrens	    (VirtualDeviceBean_t *)bean));
607fa9e406ahrens}
608fa9e406ahrens
609fa9e406ahrensstatic int
610fa9e406ahrenspopulate_MirrorVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
611bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id, MirrorVirtualDeviceBean_t *bean)
612fa9e406ahrens{
613bfe5f5atalley	return (populate_VirtualDeviceBean(env, zhp, vdev, p_vdev_id,
614fa9e406ahrens	    (VirtualDeviceBean_t *)bean));
615fa9e406ahrens}
616fa9e406ahrens
617fa9e406ahrensstatic jobject
618fbfd10ftalleycreate_ImportablePoolBean(JNIEnv *env, nvlist_t *config)
619c8e9ed1talley{
620c8e9ed1talley	int result;
621c8e9ed1talley	ImportablePoolBean_t bean_obj = {0};
622c8e9ed1talley	ImportablePoolBean_t *bean = &bean_obj;
623c8e9ed1talley
624c8e9ed1talley	/* Construct ImportablePoolBean */
625c8e9ed1talley	new_ImportablePoolBean(env, bean);
626c8e9ed1talley
627fbfd10ftalley	result = populate_ImportablePoolBean(env, bean, config);
628c8e9ed1talley	if (result) {
629c8e9ed1talley		/* Must not call any more Java methods to preserve exception */
630c8e9ed1talley		return (NULL);
631c8e9ed1talley	}
632c8e9ed1talley
633c8e9ed1talley	return (((zjni_Object_t *)bean)->object);
634c8e9ed1talley}
635c8e9ed1talley
636c8e9ed1talleystatic jobject
637bfe5f5atalleycreate_DiskVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
638bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id)
639fa9e406ahrens{
640fa9e406ahrens	int result;
641fa9e406ahrens	DiskVirtualDeviceBean_t bean_obj = {0};
642fa9e406ahrens	DiskVirtualDeviceBean_t *bean = &bean_obj;
643fa9e406ahrens
644fa9e406ahrens	/* Construct DiskVirtualDeviceBean */
645fa9e406ahrens	new_DiskVirtualDeviceBean(env, bean);
646fa9e406ahrens
647bfe5f5atalley	result = populate_DiskVirtualDeviceBean(
648bfe5f5atalley	    env, zhp, vdev, p_vdev_id, bean);
649fa9e406ahrens	if (result) {
650fa9e406ahrens		/* Must not call any more Java methods to preserve exception */
651fa9e406ahrens		return (NULL);
652fa9e406ahrens	}
653fa9e406ahrens
654fa9e406ahrens	return (((zjni_Object_t *)bean)->object);
655fa9e406ahrens}
656fa9e406ahrens
657fa9e406ahrensstatic jobject
658bfe5f5atalleycreate_SliceVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
659bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id)
660a1a659atalley{
661a1a659atalley	int result;
662a1a659atalley	SliceVirtualDeviceBean_t bean_obj = {0};
663a1a659atalley	SliceVirtualDeviceBean_t *bean = &bean_obj;
664a1a659atalley
665a1a659atalley	/* Construct SliceVirtualDeviceBean */
666a1a659atalley	new_SliceVirtualDeviceBean(env, bean);
667a1a659atalley
668bfe5f5atalley	result = populate_SliceVirtualDeviceBean(
669bfe5f5atalley	    env, zhp, vdev, p_vdev_id, bean);
670a1a659atalley	if (result) {
671a1a659atalley		/* Must not call any more Java methods to preserve exception */
672a1a659atalley		return (NULL);
673a1a659atalley	}
674a1a659atalley
675a1a659atalley	return (((zjni_Object_t *)bean)->object);
676a1a659atalley}
677a1a659atalley
678a1a659atalleystatic jobject
679bfe5f5atalleycreate_FileVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
680bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id)
681fa9e406ahrens{
682fa9e406ahrens	int result;
683fa9e406ahrens	FileVirtualDeviceBean_t bean_obj = {0};
684fa9e406ahrens	FileVirtualDeviceBean_t *bean = &bean_obj;
685fa9e406ahrens
686fa9e406ahrens	/* Construct FileVirtualDeviceBean */
687fa9e406ahrens	new_FileVirtualDeviceBean(env, bean);
688fa9e406ahrens
689bfe5f5atalley	result = populate_FileVirtualDeviceBean(
690bfe5f5atalley	    env, zhp, vdev, p_vdev_id, bean);
691fa9e406ahrens	if (result) {
692fa9e406ahrens		/* Must not call any more Java methods to preserve exception */
693fa9e406ahrens		return (NULL);
694fa9e406ahrens	}
695fa9e406ahrens
696fa9e406ahrens	return (((zjni_Object_t *)bean)->object);
697fa9e406ahrens}
698fa9e406ahrens
699fa9e406ahrensstatic jobject
700bfe5f5atalleycreate_RAIDVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
701bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id)
702fa9e406ahrens{
703fa9e406ahrens	int result;
7041e995cfrl	uint64_t parity;
705fa9e406ahrens	RAIDVirtualDeviceBean_t bean_obj = {0};
706fa9e406ahrens	RAIDVirtualDeviceBean_t *bean = &bean_obj;
707fa9e406ahrens
708fa9e406ahrens	((zjni_Object_t *)bean)->object = NULL;
709fa9e406ahrens
710fa9e406ahrens	/* Construct RAIDVirtualDeviceBean */
711fa9e406ahrens	new_RAIDVirtualDeviceBean(env, bean);
712fa9e406ahrens
7131e995cfrl	/* Set parity bit */
7141e995cfrl	result = nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_NPARITY,
7151e995cfrl	    &parity);
7161e995cfrl	if (result) {
7171e995cfrl		/* Default to RAID-Z1 in case of error */
7181e995cfrl		parity = 1;
7191e995cfrl	}
7201e995cfrl
7211e995cfrl	(*env)->CallVoidMethod(
7221e995cfrl	    env, ((zjni_Object_t *)bean)->object, bean->method_setParity,
7231e995cfrl	    (jlong)parity);
7241e995cfrl
7251e995cfrl
726bfe5f5atalley	result = populate_RAIDVirtualDeviceBean(
727bfe5f5atalley	    env, zhp, vdev, p_vdev_id, bean);
728fa9e406ahrens	if (result) {
729fa9e406ahrens		/* Must not call any more Java methods to preserve exception */
730fa9e406ahrens		return (NULL);
731fa9e406ahrens	}
732fa9e406ahrens
733fa9e406ahrens	return (((zjni_Object_t *)bean)->object);
734fa9e406ahrens}
735fa9e406ahrens
736fa9e406ahrensstatic jobject
737bfe5f5atalleycreate_MirrorVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
738bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id)
739fa9e406ahrens{
740fa9e406ahrens	int result;
741fa9e406ahrens	MirrorVirtualDeviceBean_t bean_obj = {0};
742fa9e406ahrens	MirrorVirtualDeviceBean_t *bean = &bean_obj;
743fa9e406ahrens
744fa9e406ahrens	/* Construct MirrorVirtualDeviceBean */
745fa9e406ahrens	new_MirrorVirtualDeviceBean(env, bean);
746fa9e406ahrens
747bfe5f5atalley	result = populate_MirrorVirtualDeviceBean(
748bfe5f5atalley	    env, zhp, vdev, p_vdev_id, bean);
749fa9e406ahrens	if (result) {
750fa9e406ahrens		/* Must not call any more Java methods to preserve exception */
751fa9e406ahrens		return (NULL);
752fa9e406ahrens	}
753fa9e406ahrens
754fa9e406ahrens	return (((zjni_Object_t *)bean)->object);
755fa9e406ahrens}
756fa9e406ahrens
757fbfd10ftalleystatic char *
758d8ab6e1Don Bradyfind_field(const zjni_field_mapping_t *mapping, int value)
759d8ab6e1Don Brady{
760fbfd10ftalley	int i;
761fbfd10ftalley	for (i = 0; mapping[i].name != NULL; i++) {
762fbfd10ftalley		if (value == mapping[i].value) {
763fbfd10ftalley			return (mapping[i].name);
764fbfd10ftalley		}
765fbfd10ftalley	}
766fbfd10ftalley	return (NULL);
767fbfd10ftalley}
768fbfd10ftalley
769fbfd10ftalley/*
770fbfd10ftalley * Converts a vdev_state_t to a Java DeviceStats$DeviceState object.
771fbfd10ftalley */
772fbfd10ftalleystatic jobject
773fbfd10ftalleyzjni_vdev_state_to_obj(JNIEnv *env, vdev_state_t state)
774fbfd10ftalley{
775fbfd10ftalley	return (zjni_int_to_enum(env, state,
776fbfd10ftalley	    ZFSJNI_PACKAGE_DATA "DeviceStats$DeviceState",
777fbfd10ftalley	    "VDEV_STATE_UNKNOWN", vdev_state_map));
778fbfd10ftalley}
779fbfd10ftalley
780fbfd10ftalley/*
781fbfd10ftalley * Converts a vdev_aux_t to a Java DeviceStats$DeviceStatus object.
782fbfd10ftalley */
783fbfd10ftalleystatic jobject
784fbfd10ftalleyzjni_vdev_aux_to_obj(JNIEnv *env, vdev_aux_t aux)
785fbfd10ftalley{
786fbfd10ftalley	return (zjni_int_to_enum(env, aux,
787fbfd10ftalley	    ZFSJNI_PACKAGE_DATA "DeviceStats$DeviceStatus",
788fbfd10ftalley	    "VDEV_AUX_NONE", vdev_aux_map));
789fbfd10ftalley}
790fbfd10ftalley
791fa9e406ahrens/*
792fa9e406ahrens * Package-private functions
793fa9e406ahrens */
794fa9e406ahrens
795fbfd10ftalley/* Create a DeviceStatsBean */
796fbfd10ftalleyvoid
797fbfd10ftalleynew_DeviceStats(JNIEnv *env, DeviceStatsBean_t *bean, zjni_Object_t *object)
798fbfd10ftalley{
799fbfd10ftalley	bean->method_setSize = (*env)->GetMethodID(
800fbfd10ftalley	    env, object->class, "setSize", "(J)V");
801fbfd10ftalley
802a1a659atalley	bean->method_setReplacementSize = (*env)->GetMethodID(
803a1a659atalley	    env, object->class, "setReplacementSize", "(J)V");
804a1a659atalley
805fbfd10ftalley	bean->method_setUsed = (*env)->GetMethodID(
806fbfd10ftalley	    env, object->class, "setUsed", "(J)V");
807fbfd10ftalley
808fbfd10ftalley	bean->method_setReadBytes = (*env)->GetMethodID(
809fbfd10ftalley	    env, object->class, "setReadBytes", "(J)V");
810fbfd10ftalley
811fbfd10ftalley	bean->method_setWriteBytes = (*env)->GetMethodID(
812fbfd10ftalley	    env, object->class, "setWriteBytes", "(J)V");
813fbfd10ftalley
814fbfd10ftalley	bean->method_setReadOperations = (*env)->GetMethodID(
815fbfd10ftalley	    env, object->class, "setReadOperations", "(J)V");
816fbfd10ftalley
817fbfd10ftalley	bean->method_setWriteOperations = (*env)->GetMethodID(
818fbfd10ftalley	    env, object->class, "setWriteOperations", "(J)V");
819fbfd10ftalley
820fbfd10ftalley	bean->method_setReadErrors = (*env)->GetMethodID(
821fbfd10ftalley	    env, object->class, "setReadErrors", "(J)V");
822fbfd10ftalley
823fbfd10ftalley	bean->method_setWriteErrors = (*env)->GetMethodID(
824fbfd10ftalley	    env, object->class, "setWriteErrors", "(J)V");
825fbfd10ftalley
826fbfd10ftalley	bean->method_setChecksumErrors = (*env)->GetMethodID(
827fbfd10ftalley	    env, object->class, "setChecksumErrors", "(J)V");
828fbfd10ftalley
829fbfd10ftalley	bean->method_setDeviceState = (*env)->GetMethodID(
830fbfd10ftalley	    env, object->class, "setDeviceState",
831fbfd10ftalley	    "(L" ZFSJNI_PACKAGE_DATA "DeviceStats$DeviceState;)V");
832fbfd10ftalley
833fbfd10ftalley	bean->method_setDeviceStatus = (*env)->GetMethodID(
834fbfd10ftalley	    env, object->class, "setDeviceStatus",
835fbfd10ftalley	    "(L" ZFSJNI_PACKAGE_DATA "DeviceStats$DeviceStatus;)V");
836fbfd10ftalley}
837fbfd10ftalley
838fbfd10ftalley/* Create a PoolStatsBean */
839fbfd10ftalleyvoid
840fbfd10ftalleynew_PoolStats(JNIEnv *env, PoolStatsBean_t *bean, zjni_Object_t *object)
841fbfd10ftalley{
842fbfd10ftalley	new_DeviceStats(env, (DeviceStatsBean_t *)bean, object);
843fbfd10ftalley
844fbfd10ftalley	bean->method_setPoolState = (*env)->GetMethodID(
845fbfd10ftalley	    env, object->class, "setPoolState",
846fbfd10ftalley	    "(L" ZFSJNI_PACKAGE_DATA "PoolStats$PoolState;)V");
847fbfd10ftalley
848fbfd10ftalley	bean->method_setPoolStatus = (*env)->GetMethodID(
849fbfd10ftalley	    env, object->class, "setPoolStatus",
850fbfd10ftalley	    "(L" ZFSJNI_PACKAGE_DATA "PoolStats$PoolStatus;)V");
851a55b684cristian
852a55b684cristian	bean->method_setPoolVersion = (*env)->GetMethodID(
853a55b684cristian	    env, object->class, "setPoolVersion", "(J)V");
854fbfd10ftalley}
855fbfd10ftalley
856fa9e406ahrens/*
857fa9e406ahrens * Gets the root vdev (an nvlist_t *) for the given pool.
858fa9e406ahrens */
859fa9e406ahrensnvlist_t *
860fa9e406ahrenszjni_get_root_vdev(zpool_handle_t *zhp)
861fa9e406ahrens{
862fa9e406ahrens	nvlist_t *root = NULL;
863fa9e406ahrens
864fa9e406ahrens	if (zhp != NULL) {
865088e9d4eschrock		nvlist_t *attrs = zpool_get_config(zhp, NULL);
866fa9e406ahrens
867fa9e406ahrens		if (attrs != NULL) {
868fa9e406ahrens			int result = nvlist_lookup_nvlist(
869fa9e406ahrens			    attrs, ZPOOL_CONFIG_VDEV_TREE, &root);
870fa9e406ahrens			if (result != 0) {
871fa9e406ahrens				root = NULL;
872fa9e406ahrens			}
873fa9e406ahrens		}
874fa9e406ahrens	}
875fa9e406ahrens
876fa9e406ahrens	return (root);
877fa9e406ahrens}
878fa9e406ahrens
879fa9e406ahrens/*
880fa9e406ahrens * Gets the vdev (an nvlist_t *) with the given vdev_id, below the
881fa9e406ahrens * given vdev.  If the given vdev is NULL, all vdevs within the given
882fa9e406ahrens * pool are searched.
883bfe5f5atalley *
884bfe5f5atalley * If p_vdev_id is not NULL, it will be set to the ID of the parent
885bfe5f5atalley * vdev, if any, or to vdev_id_to_find if the searched-for vdev is a
886bfe5f5atalley * toplevel vdev.
887fa9e406ahrens */
888fa9e406ahrensnvlist_t *
889fa9e406ahrenszjni_get_vdev(zpool_handle_t *zhp, nvlist_t *vdev_parent,
890bfe5f5atalley    uint64_t vdev_id_to_find, uint64_t *p_vdev_id)
891fa9e406ahrens{
892fa9e406ahrens	int result;
893bfe5f5atalley	uint64_t id = vdev_id_to_find;
894fa9e406ahrens
895fa9e406ahrens	/* Was a vdev specified? */
896fa9e406ahrens	if (vdev_parent == NULL) {
897fa9e406ahrens		/* No -- retrieve the top-level pool vdev */
898fa9e406ahrens		vdev_parent = zjni_get_root_vdev(zhp);
899fa9e406ahrens	} else {
900fa9e406ahrens		/* Get index of this vdev and compare with vdev_id_to_find */
901fa9e406ahrens		result = nvlist_lookup_uint64(
902fa9e406ahrens		    vdev_parent, ZPOOL_CONFIG_GUID, &id);
903fa9e406ahrens		if (result == 0 && id == vdev_id_to_find) {
904fa9e406ahrens			return (vdev_parent);
905fa9e406ahrens		}
906fa9e406ahrens	}
907fa9e406ahrens
908fa9e406ahrens	if (vdev_parent != NULL) {
909fa9e406ahrens
910fa9e406ahrens		nvlist_t **children;
911fa9e406ahrens		uint_t nelem = 0;
912fa9e406ahrens
913fa9e406ahrens		/* Get the vdevs under this vdev */
914fa9e406ahrens		result = nvlist_lookup_nvlist_array(
915fa9e406ahrens		    vdev_parent, ZPOOL_CONFIG_CHILDREN, &children, &nelem);
916fa9e406ahrens
917fa9e406ahrens		if (result == 0) {
918fa9e406ahrens
919fa9e406ahrens			int i;
920fa9e406ahrens			nvlist_t *child;
921fa9e406ahrens
922fa9e406ahrens			/* For each vdev child... */
923fa9e406ahrens			for (i = 0; i < nelem; i++) {
924bfe5f5atalley				if (p_vdev_id != NULL) {
925bfe5f5atalley					/* Save parent vdev id */
926bfe5f5atalley					*p_vdev_id = id;
927bfe5f5atalley				}
928bfe5f5atalley
929fa9e406ahrens				child = zjni_get_vdev(zhp, children[i],
930bfe5f5atalley				    vdev_id_to_find, p_vdev_id);
931fa9e406ahrens				if (child != NULL) {
932fa9e406ahrens					return (child);
933fa9e406ahrens				}
934fa9e406ahrens			}
935fa9e406ahrens		}
936fa9e406ahrens	}
937fa9e406ahrens
938fa9e406ahrens	return (NULL);
939fa9e406ahrens}
940fa9e406ahrens
941fa9e406ahrensjobject
942fa9e406ahrenszjni_get_VirtualDevice_from_vdev(JNIEnv *env, zpool_handle_t *zhp,
943bfe5f5atalley    nvlist_t *vdev, uint64_t *p_vdev_id)
944fa9e406ahrens{
945fa9e406ahrens	jobject obj = NULL;
946fa9e406ahrens	char *type = NULL;
947fa9e406ahrens	int result = nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE, &type);
948fa9e406ahrens
949fa9e406ahrens	if (result == 0) {
950fa9e406ahrens		if (strcmp(type, VDEV_TYPE_DISK) == 0) {
951a1a659atalley			uint64_t wholedisk;
952a1a659atalley			if (nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_WHOLE_DISK,
953a1a659atalley			    &wholedisk) == 0 && wholedisk) {
954a1a659atalley				obj = create_DiskVirtualDeviceBean(
955bfe5f5atalley				    env, zhp, vdev, p_vdev_id);
956a1a659atalley			} else {
957a1a659atalley				obj = create_SliceVirtualDeviceBean(
958bfe5f5atalley				    env, zhp, vdev, p_vdev_id);
959a1a659atalley			}
960fa9e406ahrens		} else if (strcmp(type, VDEV_TYPE_FILE) == 0) {
961bfe5f5atalley			obj = create_FileVirtualDeviceBean(
962bfe5f5atalley			    env, zhp, vdev, p_vdev_id);
963fa9e406ahrens		} else if (strcmp(type, VDEV_TYPE_RAIDZ) == 0) {
964bfe5f5atalley			obj = create_RAIDVirtualDeviceBean(
965bfe5f5atalley			    env, zhp, vdev, p_vdev_id);
966fa9e406ahrens		} else if (strcmp(type, VDEV_TYPE_MIRROR) == 0) {
967bfe5f5atalley			obj = create_MirrorVirtualDeviceBean(
968bfe5f5atalley			    env, zhp, vdev, p_vdev_id);
969c8e9ed1talley		} else if (strcmp(type, VDEV_TYPE_REPLACING) == 0) {
970c8e9ed1talley
971c8e9ed1talley			/* Get the vdevs under this vdev */
972c8e9ed1talley			nvlist_t **children;
973c8e9ed1talley			uint_t nelem = 0;
974c8e9ed1talley			int result = nvlist_lookup_nvlist_array(
975c8e9ed1talley			    vdev, ZPOOL_CONFIG_CHILDREN, &children, &nelem);
976c8e9ed1talley
977c8e9ed1talley			if (result == 0 && nelem > 0) {
978c8e9ed1talley
979c8e9ed1talley				/* Get last vdev child (replacement device) */
980c8e9ed1talley				nvlist_t *child = children[nelem - 1];
981c8e9ed1talley
982c8e9ed1talley				obj = zjni_get_VirtualDevice_from_vdev(env,
983bfe5f5atalley				    zhp, child, p_vdev_id);
984c8e9ed1talley			}
985fa9e406ahrens		}
986fa9e406ahrens	}
987fa9e406ahrens
988fa9e406ahrens	return (obj);
989fa9e406ahrens}
990fa9e406ahrens
991fa9e406ahrensjobject
992fa9e406ahrenszjni_get_VirtualDevices_from_vdev(JNIEnv *env, zpool_handle_t *zhp,
993bfe5f5atalley    nvlist_t *vdev_parent, uint64_t *p_vdev_id)
994fa9e406ahrens{
995fa9e406ahrens	/* Create an array list for the vdevs */
996fa9e406ahrens	zjni_ArrayList_t list_class = {0};
997fa9e406ahrens	zjni_ArrayList_t *list_class_p = &list_class;
998fa9e406ahrens	zjni_new_ArrayList(env, list_class_p);
999fa9e406ahrens
1000fa9e406ahrens	/* Was a vdev specified? */
1001fa9e406ahrens	if (vdev_parent == NULL) {
1002fa9e406ahrens		/* No -- retrieve the top-level pool vdev */
1003fa9e406ahrens		vdev_parent = zjni_get_root_vdev(zhp);
1004fa9e406ahrens	}
1005fa9e406ahrens
1006fa9e406ahrens	if (vdev_parent != NULL) {
1007fa9e406ahrens
1008fa9e406ahrens		/* Get the vdevs under this vdev */
1009fa9e406ahrens		nvlist_t **children;
1010fa9e406ahrens		uint_t nelem = 0;
1011fa9e406ahrens		int result = nvlist_lookup_nvlist_array(
1012fa9e406ahrens		    vdev_parent, ZPOOL_CONFIG_CHILDREN, &children, &nelem);
1013fa9e406ahrens
1014fa9e406ahrens		if (result == 0) {
1015fa9e406ahrens
1016fa9e406ahrens			/* For each vdev child... */
1017fa9e406ahrens			int i;
1018fa9e406ahrens			for (i = 0; i < nelem; i++) {
1019fa9e406ahrens				nvlist_t *child = children[i];
1020fa9e406ahrens
1021fa9e406ahrens				/* Create a Java object from this vdev */
1022fa9e406ahrens				jobject obj =
1023fa9e406ahrens				    zjni_get_VirtualDevice_from_vdev(env,
10241e995cfrl				    zhp, child, p_vdev_id);
1025fa9e406ahrens
1026c8e9ed1talley				if ((*env)->ExceptionOccurred(env) != NULL) {
1027fa9e406ahrens					/*
1028fa9e406ahrens					 * Must not call any more Java methods
1029fa9e406ahrens					 * to preserve exception
1030fa9e406ahrens					 */
1031fa9e406ahrens					return (NULL);
1032fa9e406ahrens				}
1033fa9e406ahrens
1034c8e9ed1talley				if (obj != NULL) {
1035c8e9ed1talley				    /* Add child to child vdev list */
10361e995cfrl					(*env)->CallBooleanMethod(env,
10371e995cfrl					    ((zjni_Object_t *)
10381e995cfrl					    list_class_p)->object,
10391e995cfrl					    ((zjni_Collection_t *)
10401e995cfrl					    list_class_p)->method_add, obj);
1041c8e9ed1talley				}
1042fa9e406ahrens			}
1043fa9e406ahrens		}
1044fa9e406ahrens	}
1045fa9e406ahrens
1046fa9e406ahrens	return (zjni_Collection_to_array(
1047fa9e406ahrens	    env, (zjni_Collection_t *)list_class_p,
1048fa9e406ahrens	    ZFSJNI_PACKAGE_DATA "VirtualDevice"));
1049fa9e406ahrens}
1050c8e9ed1talley
1051c8e9ed1talleyint
1052d8ab6e1Don Bradyzjni_create_add_ImportablePool(nvlist_t *config, void *data)
1053d8ab6e1Don Brady{
1054c8e9ed1talley
1055c8e9ed1talley	JNIEnv *env = ((zjni_ArrayCallbackData_t *)data)->env;
1056c8e9ed1talley	zjni_Collection_t *list = ((zjni_ArrayCallbackData_t *)data)->list;
1057c8e9ed1talley
1058c8e9ed1talley	/* Construct ImportablePool object */
1059fbfd10ftalley	jobject bean = create_ImportablePoolBean(env, config);
1060c8e9ed1talley	if (bean == NULL) {
1061c8e9ed1talley		return (-1);
1062c8e9ed1talley	}
1063c8e9ed1talley
1064c8e9ed1talley	/* Add bean to list */
1065c8e9ed1talley	(*env)->CallBooleanMethod(env, ((zjni_Object_t *)list)->object,
1066c8e9ed1talley	    ((zjni_Collection_t *)list)->method_add, bean);
1067c8e9ed1talley
1068c8e9ed1talley	return (0);
1069c8e9ed1talley}
1070c8e9ed1talley
1071fbfd10ftalleyint
1072fbfd10ftalleypopulate_DeviceStatsBean(JNIEnv *env, nvlist_t *vdev,
1073fbfd10ftalley    DeviceStatsBean_t *bean, zjni_Object_t *object)
1074fbfd10ftalley{
1075fbfd10ftalley	uint_t c;
1076fbfd10ftalley	vdev_stat_t *vs;
1077fbfd10ftalley
1078fbfd10ftalley	int result = nvlist_lookup_uint64_array(
10793f9d6adLin Ling	    vdev, ZPOOL_CONFIG_VDEV_STATS, (uint64_t **)&vs, &c);
1080fbfd10ftalley	if (result != 0) {
1081fbfd10ftalley		zjni_throw_exception(env,
1082fbfd10ftalley		    "could not retrieve virtual device statistics");
1083fbfd10ftalley		return (1);
1084fbfd10ftalley	}
1085fbfd10ftalley
1086fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
1087fbfd10ftalley	    bean->method_setUsed, (jlong)vs->vs_alloc);
1088fbfd10ftalley
1089fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
1090fbfd10ftalley	    bean->method_setSize, (jlong)vs->vs_space);
1091fbfd10ftalley
1092fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
1093a1a659atalley	    bean->method_setReplacementSize, (jlong)vs->vs_rsize);
1094a1a659atalley
1095a1a659atalley	(*env)->CallVoidMethod(env, object->object,
1096fbfd10ftalley	    bean->method_setReadBytes, (jlong)vs->vs_bytes[ZIO_TYPE_READ]);
1097fbfd10ftalley
1098fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
1099fbfd10ftalley	    bean->method_setWriteBytes, (jlong)vs->vs_bytes[ZIO_TYPE_WRITE]);
1100fbfd10ftalley
1101fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
1102fbfd10ftalley	    bean->method_setReadOperations, (jlong)vs->vs_ops[ZIO_TYPE_READ]);
1103fbfd10ftalley
1104fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
1105fbfd10ftalley	    bean->method_setWriteOperations, (jlong)vs->vs_ops[ZIO_TYPE_WRITE]);
1106fbfd10ftalley
1107fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
1108fbfd10ftalley	    bean->method_setReadErrors, (jlong)vs->vs_read_errors);
1109fbfd10ftalley
1110fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
1111fbfd10ftalley	    bean->method_setWriteErrors, (jlong)vs->vs_write_errors);
1112fbfd10ftalley
1113fbfd10ftalley	(*env)->CallVoidMethod(env, object->object,
1114fbfd10ftalley