libzfs_jni_pool.c revision c8e9ed14d97e244b9753db14caf8481f181f5750
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Common Development and Distribution License, Version 1.0 only
fa9e4066f08beec538e775443c5be79dd423fcabahrens * (the "License"). You may not use this file except in compliance
fa9e4066f08beec538e775443c5be79dd423fcabahrens * with the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fa9e4066f08beec538e775443c5be79dd423fcabahrens * See the License for the specific language governing permissions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * and limitations under the License.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * When distributing Covered Code, include this CDDL HEADER in each
fa9e4066f08beec538e775443c5be79dd423fcabahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * If applicable, add the following below this CDDL HEADER, with the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * fields enclosed by brackets "[]" replaced with your own identifying
fa9e4066f08beec538e775443c5be79dd423fcabahrens * information: Portions Copyright [yyyy] [name of copyright owner]
fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER END
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Use is subject to license terms.
fa9e4066f08beec538e775443c5be79dd423fcabahrens#pragma ident "%Z%%M% %I% %E% SMI"
c8e9ed14d97e244b9753db14caf8481f181f5750talleytypedef struct ImportablePoolBean {
fa9e4066f08beec538e775443c5be79dd423fcabahrenstypedef struct VirtualDeviceBean {
fa9e4066f08beec538e775443c5be79dd423fcabahrenstypedef struct DiskVirtualDeviceBean {
fa9e4066f08beec538e775443c5be79dd423fcabahrenstypedef struct FileVirtualDeviceBean {
fa9e4066f08beec538e775443c5be79dd423fcabahrenstypedef struct RAIDVirtualDeviceBean {
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Function prototypes
c8e9ed14d97e244b9753db14caf8481f181f5750talleystatic void new_ImportablePoolBean(JNIEnv *, ImportablePoolBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void new_VirtualDevice(JNIEnv *, VirtualDeviceBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void new_DiskVirtualDeviceBean(JNIEnv *, DiskVirtualDeviceBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void new_FileVirtualDeviceBean(JNIEnv *, FileVirtualDeviceBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void new_RAIDVirtualDeviceBean(JNIEnv *, RAIDVirtualDeviceBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void new_MirrorVirtualDeviceBean(JNIEnv *, MirrorVirtualDeviceBean_t *);
c8e9ed14d97e244b9753db14caf8481f181f5750talley JNIEnv *, ImportablePoolBean_t *, char *, uint64_t, uint64_t, char *);
fa9e4066f08beec538e775443c5be79dd423fcabahrens JNIEnv *, zpool_handle_t *, nvlist_t *, VirtualDeviceBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrens JNIEnv *, zpool_handle_t *, nvlist_t *, DiskVirtualDeviceBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrens JNIEnv *, zpool_handle_t *, nvlist_t *, FileVirtualDeviceBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrens JNIEnv *, zpool_handle_t *, nvlist_t *, RAIDVirtualDeviceBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrens JNIEnv *, zpool_handle_t *, nvlist_t *, MirrorVirtualDeviceBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Static functions
c8e9ed14d97e244b9753db14caf8481f181f5750talley/* Create a ImportablePoolBean */
c8e9ed14d97e244b9753db14caf8481f181f5750talleynew_ImportablePoolBean(JNIEnv *env, ImportablePoolBean_t *bean)
c8e9ed14d97e244b9753db14caf8481f181f5750talley (*env)->GetMethodID(env, object->class, "<init>", "()V");
c8e9ed14d97e244b9753db14caf8481f181f5750talley (*env)->NewObject(env, object->class, object->constructor);
c8e9ed14d97e244b9753db14caf8481f181f5750talley env, object->class, "setName", "(Ljava/lang/String;)V");
c8e9ed14d97e244b9753db14caf8481f181f5750talley env, object->class, "setHealth", "(Ljava/lang/String;)V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a VirtualDeviceBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrensnew_VirtualDevice(JNIEnv *env, VirtualDeviceBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->GetMethodID(env, object->class, "<init>", "()V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->NewObject(env, object->class, object->constructor);
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->class, "setPoolName", "(Ljava/lang/String;)V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a DiskVirtualDeviceBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrensnew_DiskVirtualDeviceBean(JNIEnv *env, DiskVirtualDeviceBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->GetMethodID(env, object->class, "<init>", "()V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->NewObject(env, object->class, object->constructor);
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->class, "setDiskName", "(Ljava/lang/String;)V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a FileVirtualDeviceBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrensnew_FileVirtualDeviceBean(JNIEnv *env, FileVirtualDeviceBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->GetMethodID(env, object->class, "<init>", "()V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->NewObject(env, object->class, object->constructor);
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->class, "setFileName", "(Ljava/lang/String;)V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a RAIDVirtualDeviceBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrensnew_RAIDVirtualDeviceBean(JNIEnv *env, RAIDVirtualDeviceBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->GetMethodID(env, object->class, "<init>", "()V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->NewObject(env, object->class, object->constructor);
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a MirrorVirtualDeviceBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrensnew_MirrorVirtualDeviceBean(JNIEnv *env, MirrorVirtualDeviceBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->GetMethodID(env, object->class, "<init>", "()V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->NewObject(env, object->class, object->constructor);
c8e9ed14d97e244b9753db14caf8481f181f5750talley jmethodID method_valueOf = (*env)->GetStaticMethodID(
c8e9ed14d97e244b9753db14caf8481f181f5750talleypopulate_ImportablePoolBean(JNIEnv *env, ImportablePoolBean_t *bean,
c8e9ed14d97e244b9753db14caf8481f181f5750talley char *name, uint64_t guid, uint64_t pool_state, char *health)
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Set name */
c8e9ed14d97e244b9753db14caf8481f181f5750talley (*env)->CallVoidMethod(env, object->object, bean->method_setName,
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Set state */
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Set guid */
c8e9ed14d97e244b9753db14caf8481f181f5750talley env, object->object, bean->method_setId, (jlong)guid);
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Set health */
c8e9ed14d97e244b9753db14caf8481f181f5750talley (*env)->CallVoidMethod(env, object->object, bean->method_setHealth,
c8e9ed14d97e244b9753db14caf8481f181f5750talley return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrenspopulate_VirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Set pool name */
fa9e4066f08beec538e775443c5be79dd423fcabahrens jstring poolUTF = (*env)->NewStringUTF(env, zpool_get_name(zhp));
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->object, bean->method_setPoolName, poolUTF);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Get index */
fa9e4066f08beec538e775443c5be79dd423fcabahrens result = nvlist_lookup_uint64(vdev, ZPOOL_CONFIG_GUID, &vdev_id);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (result != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens "could not retrieve virtual device ID (pool %s)",
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->object, bean->method_setIndex, (jlong)vdev_id);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Set used space */
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->object, bean->method_setUsed, (jlong)used);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Set available space */
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->object, bean->method_setSize, (jlong)total);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (result != 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrenspopulate_DiskVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Set path */
fa9e4066f08beec538e775443c5be79dd423fcabahrens result = nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (result != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens "could not retrive path from disk virtual device (pool %s)",
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->CallVoidMethod(env, ((zjni_Object_t *)bean)->object,
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (result != 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrenspopulate_FileVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Set path */
fa9e4066f08beec538e775443c5be79dd423fcabahrens result = nvlist_lookup_string(vdev, ZPOOL_CONFIG_PATH, &path);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (result != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens "could not retrive path from disk virtual device (pool %s)",
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->CallVoidMethod(env, ((zjni_Object_t *)bean)->object,
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (result != 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrenspopulate_RAIDVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
fa9e4066f08beec538e775443c5be79dd423fcabahrenspopulate_MirrorVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp,
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Construct ImportablePoolBean */
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrenscreate_DiskVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp, nvlist_t *vdev)
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Construct DiskVirtualDeviceBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens result = populate_DiskVirtualDeviceBean(env, zhp, vdev, bean);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrenscreate_FileVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp, nvlist_t *vdev)
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Construct FileVirtualDeviceBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens result = populate_FileVirtualDeviceBean(env, zhp, vdev, bean);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrenscreate_RAIDVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp, nvlist_t *vdev)
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Construct RAIDVirtualDeviceBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens result = populate_RAIDVirtualDeviceBean(env, zhp, vdev, bean);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrenscreate_MirrorVirtualDeviceBean(JNIEnv *env, zpool_handle_t *zhp, nvlist_t *vdev)
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Construct MirrorVirtualDeviceBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens result = populate_MirrorVirtualDeviceBean(env, zhp, vdev, bean);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Package-private functions
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Gets the root vdev (an nvlist_t *) for the given pool.
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (result != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Gets the vdev (an nvlist_t *) with the given vdev_id, below the
fa9e4066f08beec538e775443c5be79dd423fcabahrens * given vdev. If the given vdev is NULL, all vdevs within the given
fa9e4066f08beec538e775443c5be79dd423fcabahrens * pool are searched.
fa9e4066f08beec538e775443c5be79dd423fcabahrenszjni_get_vdev(zpool_handle_t *zhp, nvlist_t *vdev_parent,
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Was a vdev specified? */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* No -- retrieve the top-level pool vdev */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Get index of this vdev and compare with vdev_id_to_find */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Get the vdevs under this vdev */
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_parent, ZPOOL_CONFIG_CHILDREN, &children, &nelem);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (result == 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* For each vdev child... */
fa9e4066f08beec538e775443c5be79dd423fcabahrens for (i = 0; i < nelem; i++) {
fa9e4066f08beec538e775443c5be79dd423fcabahrenszjni_get_VirtualDevice_from_vdev(JNIEnv *env, zpool_handle_t *zhp,
fa9e4066f08beec538e775443c5be79dd423fcabahrens int result = nvlist_lookup_string(vdev, ZPOOL_CONFIG_TYPE, &type);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (result == 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens obj = create_MirrorVirtualDeviceBean(env, zhp, vdev);
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Get the vdevs under this vdev */
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Get last vdev child (replacement device) */
fa9e4066f08beec538e775443c5be79dd423fcabahrenszjni_get_VirtualDevices_from_vdev(JNIEnv *env, zpool_handle_t *zhp,
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Create an array list for the vdevs */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Was a vdev specified? */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* No -- retrieve the top-level pool vdev */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Get the vdevs under this vdev */
fa9e4066f08beec538e775443c5be79dd423fcabahrens vdev_parent, ZPOOL_CONFIG_CHILDREN, &children, &nelem);
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (result == 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* For each vdev child... */
fa9e4066f08beec538e775443c5be79dd423fcabahrens for (i = 0; i < nelem; i++) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Create a Java object from this vdev */
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Must not call any more Java methods
fa9e4066f08beec538e775443c5be79dd423fcabahrens * to preserve exception
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Add child to child vdev list */
c8e9ed14d97e244b9753db14caf8481f181f5750talley uint64_t guid, uint64_t pool_state, char *health, void *data) {
c8e9ed14d97e244b9753db14caf8481f181f5750talley JNIEnv *env = ((zjni_ArrayCallbackData_t *)data)->env;
c8e9ed14d97e244b9753db14caf8481f181f5750talley zjni_Collection_t *list = ((zjni_ArrayCallbackData_t *)data)->list;
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Construct ImportablePool object */
c8e9ed14d97e244b9753db14caf8481f181f5750talley return (-1);
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Add bean to list */
c8e9ed14d97e244b9753db14caf8481f181f5750talley (*env)->CallBooleanMethod(env, ((zjni_Object_t *)list)->object,
c8e9ed14d97e244b9753db14caf8481f181f5750talley return (0);
c8e9ed14d97e244b9753db14caf8481f181f5750talley * Extern functions
c8e9ed14d97e244b9753db14caf8481f181f5750talley * Iterates through each importable pool on the system. For each
c8e9ed14d97e244b9753db14caf8481f181f5750talley * importable pool, runs the given function with the given void as the
c8e9ed14d97e244b9753db14caf8481f181f5750talley * last arg.
c8e9ed14d97e244b9753db14caf8481f181f5750talleyzjni_ipool_iter(int argc, char **argv, zjni_ipool_iter_f func, void *data)
c8e9ed14d97e244b9753db14caf8481f181f5750talley while ((elem = nvlist_next_nvpair(pools, elem)) != NULL) {
c8e9ed14d97e244b9753db14caf8481f181f5750talley return (-1);
c8e9ed14d97e244b9753db14caf8481f181f5750talley /* Run the given function */
c8e9ed14d97e244b9753db14caf8481f181f5750talley return (-1);
c8e9ed14d97e244b9753db14caf8481f181f5750talley return (0);