fa9e4066f08beec538e775443c5be79dd423fcabahrens * CDDL HEADER START
fa9e4066f08beec538e775443c5be79dd423fcabahrens * The contents of this file are subject to the terms of the
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley * Common Development and Distribution License (the "License").
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley * You may not use this file except in compliance 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
990b4856d0eaada6f8140335733a1b1771ed2746lling * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Use is subject to license terms.
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens * Copyright (c) 2015 by Delphix. All rights reserved.
fa9e4066f08beec538e775443c5be79dd423fcabahrens#define REGEX_ZFS_NAME "^((([^/]*)(/.+)?)[/@])?([^/]+)/*"
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Function prototypes
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void new_DatasetBean(JNIEnv *, DatasetBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void new_FileSystemBean(JNIEnv *, FileSystemBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void new_SnapshotBean(JNIEnv *, SnapshotBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void new_FileSystemSnapshotBean(JNIEnv *, FileSystemSnapshotBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic void new_VolumeSnapshotBean(JNIEnv *, VolumeSnapshotBean_t *);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talleystatic int set_name_in_DatasetBean(JNIEnv *, char *, DatasetBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic int populate_DatasetBean(JNIEnv *, zfs_handle_t *, DatasetBean_t *);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley JNIEnv *, zpool_handle_t *, zfs_handle_t *, PoolBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic int populate_SnapshotBean(JNIEnv *, zfs_handle_t *, SnapshotBean_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrens JNIEnv *, zfs_handle_t *, FileSystemSnapshotBean_t *);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talleystatic jobject create_PoolBean(JNIEnv *, zpool_handle_t *, zfs_handle_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic jobject create_FileSystemBean(JNIEnv *, zfs_handle_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic jobject create_VolumeBean(JNIEnv *, zfs_handle_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic jobject create_FileSystemSnapshotBean(JNIEnv *, zfs_handle_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic jobject create_VolumeSnapshotBean(JNIEnv *, zfs_handle_t *);
fa9e4066f08beec538e775443c5be79dd423fcabahrensstatic jobject create_DatasetBean(JNIEnv *, zfs_handle_t *);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talleystatic int is_pool_name(const char *);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Static functions
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a DatasetBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->FindClass(env, ZFSJNI_PACKAGE_DATA "DatasetBean");
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 env, object->class, "setParentName", "(Ljava/lang/String;)V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->class, "setBaseName", "(Ljava/lang/String;)V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a PoolBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->FindClass(env, ZFSJNI_PACKAGE_DATA "PoolBean");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->GetMethodID(env, object->class, "<init>", "()V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->NewObject(env, object->class, object->constructor);
fbfd10ff571cfd0139aa5127460f1b8a53dac971talley new_PoolStats(env, &(bean->interface_PoolStats), object);
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a FileSystemBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrensnew_FileSystemBean(JNIEnv *env, FileSystemBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->GetMethodID(env, object->class, "<init>", "()V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->NewObject(env, object->class, object->constructor);
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a VolumeBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->GetMethodID(env, object->class, "<init>", "()V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->NewObject(env, object->class, object->constructor);
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a SnapshotBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->GetMethodID(env, object->class, "<init>", "()V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->NewObject(env, object->class, object->constructor);
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a FileSystemSnapshotBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrensnew_FileSystemSnapshotBean(JNIEnv *env, FileSystemSnapshotBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->GetMethodID(env, object->class, "<init>", "()V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->NewObject(env, object->class, object->constructor);
fa9e4066f08beec538e775443c5be79dd423fcabahrens/* Create a VolumeSnapshotBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrensnew_VolumeSnapshotBean(JNIEnv *env, VolumeSnapshotBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->GetMethodID(env, object->class, "<init>", "()V");
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->NewObject(env, object->class, object->constructor);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talleyset_name_in_DatasetBean(JNIEnv *env, char *name, DatasetBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens * zhp->zfs_name has the format
fa9e4066f08beec538e775443c5be79dd423fcabahrens * <pool>[[/<container...>]/<dataset>[@<snapshot>]]
fa9e4066f08beec538e775443c5be79dd423fcabahrens if (regcomp(&re, REGEX_ZFS_NAME, REG_EXTENDED) != 0 ||
fa9e4066f08beec538e775443c5be79dd423fcabahrens regexec(&re, name, REGEX_ZFS_NAME_NGROUPS, matches, 0) != 0) {
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Set names */
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->object, bean->method_setPoolName, poolUTF);
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->object, bean->method_setBaseName, baseUTF);
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->object, bean->method_setParentName, parentUTF);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley return (0);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talleypopulate_DatasetBean(JNIEnv *env, zfs_handle_t *zhp, DatasetBean_t *bean)
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley if (result != 0) {
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Must not call any more Java methods to preserve exception */
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens env, object->object, bean->method_setProperties, properties);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talleypopulate_PoolBean(JNIEnv *env, zpool_handle_t *zphp, zfs_handle_t *zhp,
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley PoolStatsBean_t *pool_stats = &(bean->interface_PoolStats);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley DeviceStatsBean_t *dev_stats = (DeviceStatsBean_t *)pool_stats;
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley populate_DeviceStatsBean(env, devices, dev_stats, object)) {
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Override value set in populate_DeviceStatsBean */
a55b6846f87afedf14b3f9b64fbb8c0d0a3f2fe2cristian zpool_get_prop_int(zphp, ZPOOL_PROP_VERSION, NULL));
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley * If a root file system does not exist for this pool, the pool
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley * is likely faulted, so just set its name in the Java object.
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley * Otherwise, populate all fields of the Java object.
fbfd10ff571cfd0139aa5127460f1b8a53dac971talley return (result != 0);
fa9e4066f08beec538e775443c5be79dd423fcabahrenspopulate_FileSystemBean(JNIEnv *env, zfs_handle_t *zhp, FileSystemBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (populate_DatasetBean(env, zhp, (DatasetBean_t *)bean));
fa9e4066f08beec538e775443c5be79dd423fcabahrenspopulate_VolumeBean(JNIEnv *env, zfs_handle_t *zhp, VolumeBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (populate_DatasetBean(env, zhp, (DatasetBean_t *)bean));
fa9e4066f08beec538e775443c5be79dd423fcabahrenspopulate_SnapshotBean(JNIEnv *env, zfs_handle_t *zhp, SnapshotBean_t *bean)
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (populate_DatasetBean(env, zhp, (DatasetBean_t *)bean));
fa9e4066f08beec538e775443c5be79dd423fcabahrenspopulate_FileSystemSnapshotBean(JNIEnv *env, zfs_handle_t *zhp,
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (populate_SnapshotBean(env, zhp, (SnapshotBean_t *)bean));
fa9e4066f08beec538e775443c5be79dd423fcabahrenspopulate_VolumeSnapshotBean(JNIEnv *env, zfs_handle_t *zhp,
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (populate_SnapshotBean(env, zhp, (SnapshotBean_t *)bean));
32f884e0ef76c8ab2a6e26895696620c8d49ad05talleycreate_PoolBean(JNIEnv *env, zpool_handle_t *zphp, zfs_handle_t *zhp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Construct PoolBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Construct FileSystemBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Construct VolumeBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrenscreate_FileSystemSnapshotBean(JNIEnv *env, zfs_handle_t *zhp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Construct FileSystemSnapshotBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens result = populate_FileSystemSnapshotBean(env, zhp, bean);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrenscreate_VolumeSnapshotBean(JNIEnv *env, zfs_handle_t *zhp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Construct VolumeSnapshotBean */
fa9e4066f08beec538e775443c5be79dd423fcabahrens result = populate_VolumeSnapshotBean(env, zhp, bean);
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Must not call any more Java methods to preserve exception */
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Determines whether the given snapshot is a snapshot of a file
fa9e4066f08beec538e775443c5be79dd423fcabahrens * system or of a volume.
fa9e4066f08beec538e775443c5be79dd423fcabahrens * 0 if it is a volume snapshot
fa9e4066f08beec538e775443c5be79dd423fcabahrens * 1 if it is a file system snapshot
fa9e4066f08beec538e775443c5be79dd423fcabahrens * -1 on error
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
990b4856d0eaada6f8140335733a1b1771ed2746lling parent_zhp = zfs_open(g_zfs, parent, ZFS_TYPE_DATASET);
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
fa9e4066f08beec538e775443c5be79dd423fcabahrens isfs = zfs_get_type(parent_zhp) == ZFS_TYPE_FILESYSTEM;
389056bd4327caca73482e60b80da00f4eaf3de9tomee return (strchr(name, '/') == NULL && strchr(name, '@') == NULL);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley * Package-private functions
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley * Callback function for zpool_iter(). Creates a Pool and adds it to
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley * the given zjni_ArrayList.
32f884e0ef76c8ab2a6e26895696620c8d49ad05talleyzjni_create_add_Pool(zpool_handle_t *zphp, void *data)
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley JNIEnv *env = ((zjni_ArrayCallbackData_t *)data)->env;
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley zjni_Collection_t *list = ((zjni_ArrayCallbackData_t *)data)->list;
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Get root fs for this pool -- may be NULL if pool is faulted */
99653d4ee642c6528e88224f12409a5f23060994eschrock zfs_handle_t *zhp = zfs_open(g_zfs, zpool_get_name(zphp),
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Must not call any more Java methods to preserve exception */
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley return (-1);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Add pool to zjni_ArrayList */
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley (*env)->CallBooleanMethod(env, ((zjni_Object_t *)list)->object,
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Callback function for zfs_iter_children(). Creates the appropriate
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Dataset and adds it to the given zjni_ArrayList. Per the contract
fa9e4066f08beec538e775443c5be79dd423fcabahrens * with zfs_iter_children(), calls zfs_close() on the given
fa9e4066f08beec538e775443c5be79dd423fcabahrens * zfs_handle_t.
fa9e4066f08beec538e775443c5be79dd423fcabahrenszjni_create_add_Dataset(zfs_handle_t *zhp, void *data)
fa9e4066f08beec538e775443c5be79dd423fcabahrens JNIEnv *env = ((zjni_ArrayCallbackData_t *)data)->env;
fa9e4066f08beec538e775443c5be79dd423fcabahrens zjni_Collection_t *list = ((zjni_ArrayCallbackData_t *)data)->list;
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Only add allowed types */
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Must not call any more Java methods to preserve
fa9e4066f08beec538e775443c5be79dd423fcabahrens * exception
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (-1);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Add Dataset to zjni_ArrayList */
fa9e4066f08beec538e775443c5be79dd423fcabahrens (*env)->CallBooleanMethod(env, ((zjni_Object_t *)list)->object,
fa9e4066f08beec538e775443c5be79dd423fcabahrens return (0);
fa9e4066f08beec538e775443c5be79dd423fcabahrenszjni_get_Datasets_below(JNIEnv *env, jstring parentUTF,
fa9e4066f08beec538e775443c5be79dd423fcabahrens zfs_type_t parent_typemask, zfs_type_t child_typemask, char *arrayClass)
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Create an array list to hold the children */
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Retrieve parent dataset */
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley (void) zfs_iter_children(zhp, zjni_create_add_Dataset,
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Parent is not a dataset -- see if it's a faulted pool */
99653d4ee642c6528e88224f12409a5f23060994eschrock zpool_handle_t *zphp = zpool_open_canfail(g_zfs, name);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* A faulted pool has no datasets */
fa9e4066f08beec538e775443c5be79dd423fcabahrenszjni_get_Datasets_dependents(JNIEnv *env, jobjectArray paths)
fa9e4066f08beec538e775443c5be79dd423fcabahrens /* Create a list to hold the children */
fa9e4066f08beec538e775443c5be79dd423fcabahrens for (i = 0; i < npaths; i++) {
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Add all dependents of this Dataset to list */
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Add this Dataset to list (and close zhp) */
3ccfa83cd9cddd1e34808ba18082c156758c5ec8ahrens * Path is not a dataset -
3ccfa83cd9cddd1e34808ba18082c156758c5ec8ahrens * see if it's a faulted pool
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley * Add this Pool to list (and
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley * close zphp)
fa9e4066f08beec538e775443c5be79dd423fcabahrens array = zjni_Collection_to_array(env, (zjni_Collection_t *)list,
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Gets a Dataset of the given name and type, or NULL if no such
fa9e4066f08beec538e775443c5be79dd423fcabahrens * Dataset exists.
fa9e4066f08beec538e775443c5be79dd423fcabahrenszjni_get_Dataset(JNIEnv *env, jstring nameUTF, zfs_type_t typemask)
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley const char *name = (*env)->GetStringUTFChars(env, nameUTF, NULL);
99653d4ee642c6528e88224f12409a5f23060994eschrock zfs_handle_t *zhp = zfs_open(g_zfs, name, typemask);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley if ((typemask & ZFS_TYPE_FILESYSTEM) && is_pool_name(name)) {
99653d4ee642c6528e88224f12409a5f23060994eschrock zpool_handle_t *zphp = zpool_open_canfail(g_zfs, name);
32f884e0ef76c8ab2a6e26895696620c8d49ad05talley /* Creates a Dataset object of the appropriate class */