1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include "libzfs_jni_disk.h" 30 31 /* 32 * Function prototypes 33 */ 34 35 static jobject create_DiskDeviceBean(JNIEnv *, dmgt_disk_t *); 36 static jobject get_SliceUsage_Use(JNIEnv *, char *); 37 static jobject create_SliceUsage(JNIEnv *env, dmgt_slice_t *sp); 38 static jobject create_SliceDeviceBean(JNIEnv *env, dmgt_slice_t *sp); 39 static jobjectArray create_SliceDeviceBean_array(JNIEnv *, dmgt_slice_t **); 40 41 /* 42 * Static functions 43 */ 44 45 static jobject 46 create_DiskDeviceBean(JNIEnv *env, dmgt_disk_t *dp) 47 { 48 jobject disk = NULL; 49 50 int naliases = zjni_count_elements((void **)dp->aliases); 51 jobjectArray aliases = zjni_string_array_to_String_array( 52 env, dp->aliases, naliases); 53 if (aliases != NULL) { 54 jobjectArray slices = create_SliceDeviceBean_array(env, 55 dp->slices); 56 if (slices != NULL) { 57 jstring nameUTF = (*env)->NewStringUTF(env, dp->name); 58 59 jclass class_DiskDeviceBean = (*env)->FindClass( 60 env, ZFSJNI_PACKAGE_DATA "DiskDeviceBean"); 61 62 jmethodID constructor = 63 (*env)->GetMethodID(env, class_DiskDeviceBean, 64 "<init>", 65 "(JLjava/lang/String;[Ljava/lang/String;[L" 66 ZFSJNI_PACKAGE_DATA "SliceDeviceBean;)V"); 67 68 disk = (*env)->NewObject(env, class_DiskDeviceBean, 69 constructor, dp->size, nameUTF, aliases, slices); 70 } 71 } 72 73 return (disk); 74 } 75 76 static jobject 77 get_SliceUsage_Use(JNIEnv *env, char *dm_usage) 78 { 79 jobject enumVal = NULL; 80 81 if (dm_usage != NULL) { 82 jclass class_SliceUsage_Use = (*env)->FindClass( 83 env, ZFSJNI_PACKAGE_DATA "SliceUsage$Use"); 84 85 jfieldID id = (*env)->GetStaticFieldID(env, 86 class_SliceUsage_Use, 87 dm_usage, "L" ZFSJNI_PACKAGE_DATA "SliceUsage$Use;"); 88 89 if (id != NULL) { 90 /* Retrieve the proper SliceUsage$Use enum value */ 91 enumVal = (*env)->GetStaticObjectField( 92 env, class_SliceUsage_Use, id); 93 #ifdef DEBUG 94 } else { 95 (void) fprintf(stderr, "Unknown slice usage: %s\n", 96 dm_usage); 97 #endif /* DEBUG */ 98 } 99 } 100 101 return (enumVal); 102 } 103 104 static jobject 105 create_SliceUsage(JNIEnv *env, dmgt_slice_t *sp) 106 { 107 jobject usage = NULL; 108 if (sp->used_name != NULL) { 109 jobject use = get_SliceUsage_Use(env, sp->used_name); 110 111 if (use != NULL) { 112 jstring usedByUTF = 113 (*env)->NewStringUTF(env, sp->used_by); 114 115 jclass class_SliceUsage = (*env)->FindClass( 116 env, ZFSJNI_PACKAGE_DATA "SliceUsage"); 117 118 jmethodID constructor = 119 (*env)->GetMethodID(env, class_SliceUsage, "<init>", 120 "(L" ZFSJNI_PACKAGE_DATA 121 "SliceUsage$Use;Ljava/lang/String;)V"); 122 123 usage = (*env)->NewObject(env, 124 class_SliceUsage, constructor, use, usedByUTF); 125 } 126 } 127 128 return (usage); 129 } 130 131 static jobject 132 create_SliceDeviceBean(JNIEnv *env, dmgt_slice_t *sp) 133 { 134 jobject slice = NULL; 135 136 /* May be NULL if unused */ 137 jobject usage = create_SliceUsage(env, sp); 138 139 jstring nameUTF = (*env)->NewStringUTF(env, sp->name); 140 141 jclass class_SliceDeviceBean = (*env)->FindClass( 142 env, ZFSJNI_PACKAGE_DATA "SliceDeviceBean"); 143 144 jmethodID constructor = 145 (*env)->GetMethodID(env, class_SliceDeviceBean, "<init>", 146 "(JLjava/lang/String;JL" ZFSJNI_PACKAGE_DATA "SliceUsage;)V"); 147 148 slice = (*env)->NewObject(env, class_SliceDeviceBean, 149 constructor, sp->size, nameUTF, sp->start, usage); 150 151 return (slice); 152 } 153 154 static jobjectArray 155 create_SliceDeviceBean_array(JNIEnv *env, dmgt_slice_t **slices) 156 { 157 /* Create an array list */ 158 zjni_ArrayList_t list_class = {0}; 159 zjni_ArrayList_t *list_class_p = &list_class; 160 zjni_new_ArrayList(env, list_class_p); 161 162 if (slices != NULL) { 163 int i; 164 for (i = 0; slices[i] != NULL; i++) { 165 dmgt_slice_t *slice = slices[i]; 166 jobject obj; 167 obj = create_SliceDeviceBean(env, slice); 168 if (obj != NULL) { 169 (*env)->CallBooleanMethod(env, 170 ((zjni_Object_t *)list_class_p)->object, 171 ((zjni_Collection_t *)list_class_p)-> 172 method_add, obj); 173 } 174 } 175 } 176 177 return (zjni_Collection_to_array( 178 env, (zjni_Collection_t *)list_class_p, 179 ZFSJNI_PACKAGE_DATA "SliceDeviceBean")); 180 } 181 182 /* 183 * Package-private functions 184 */ 185 186 int 187 zjni_create_add_DiskDevice(dmgt_disk_t *dp, void *data) 188 { 189 JNIEnv *env = ((zjni_ArrayCallbackData_t *)data)->env; 190 zjni_Collection_t *list = ((zjni_ArrayCallbackData_t *)data)->list; 191 jobject disk = create_DiskDeviceBean(env, dp); 192 193 /* Add disk to zjni_ArrayList */ 194 (*env)->CallBooleanMethod(env, ((zjni_Object_t *)list)->object, 195 ((zjni_Collection_t *)list)->method_add, disk); 196 197 return (0); 198 } 199