From b0a5fe94d04b4f6c1aaf401fc157fd134b4340f7 Mon Sep 17 00:00:00 2001 From: Noah Watkins Date: Thu, 27 Dec 2012 15:55:07 -0800 Subject: [PATCH] java: support ceph_get_file_pool_name Signed-off-by: Noah Watkins --- src/java/java/com/ceph/fs/CephMount.java | 17 ++++++ src/java/native/libcephfs_jni.cc | 61 ++++++++++++++++++++ src/java/test/com/ceph/fs/CephMountTest.java | 17 ++++++ 3 files changed, 95 insertions(+) diff --git a/src/java/java/com/ceph/fs/CephMount.java b/src/java/java/com/ceph/fs/CephMount.java index e1e1466a24d3c..6051cf635d0c3 100644 --- a/src/java/java/com/ceph/fs/CephMount.java +++ b/src/java/java/com/ceph/fs/CephMount.java @@ -885,6 +885,23 @@ public class CephMount { private static native int native_ceph_get_file_stripe_unit(long mountp, int fd); + /** + * Get the name of the pool a file is stored in. + * + * @param fd An open file descriptor. + * @return The pool name. + */ + public String get_file_pool_name(int fd) { + rlock.lock(); + try { + return native_ceph_get_file_pool_name(instance_ptr, fd); + } finally { + rlock.unlock(); + } + } + + private static native String native_ceph_get_file_pool_name(long mountp, int fd); + /** * Get the replication of a file. * diff --git a/src/java/native/libcephfs_jni.cc b/src/java/native/libcephfs_jni.cc index ff979f0b168f2..b04dc3f0aee09 100644 --- a/src/java/native/libcephfs_jni.cc +++ b/src/java/native/libcephfs_jni.cc @@ -2456,6 +2456,67 @@ JNIEXPORT jint JNICALL Java_com_ceph_fs_CephMount_native_1ceph_1get_1file_1repli return ret; } +/* + * Class: com_ceph_fs_CephMount + * Method: native_ceph_get_file_pool_name + * Signature: (JI)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_com_ceph_fs_CephMount_native_1ceph_1get_1file_1pool_1name + (JNIEnv *env, jclass clz, jlong j_mntp, jint j_fd) +{ + struct ceph_mount_info *cmount = get_ceph_mount(j_mntp); + CephContext *cct = ceph_get_mount_context(cmount); + jstring pool = NULL; + int ret, buflen = 0; + char *buf = NULL; + + CHECK_MOUNTED(cmount, NULL); + + ldout(cct, 10) << "jni: get_file_pool_name: fd " << (int)j_fd << dendl; + + for (;;) { + /* get pool name length (len==0) */ + ret = ceph_get_file_pool_name(cmount, (int)j_fd, NULL, 0); + if (ret < 0) + break; + + /* allocate buffer */ + if (buf) + delete [] buf; + buflen = ret; + buf = new (std::nothrow) char[buflen+1]; /* +1 for '\0' */ + if (!buf) { + cephThrowOutOfMemory(env, "head allocation failed"); + goto out; + } + memset(buf, 0, (buflen+1)*sizeof(*buf)); + + /* handle zero-length pool name!? */ + if (buflen == 0) + break; + + /* fill buffer */ + ret = ceph_get_file_pool_name(cmount, (int)j_fd, buf, buflen); + if (ret == -ERANGE) /* size changed! */ + continue; + else + break; + } + + ldout(cct, 10) << "jni: get_file_pool_name: ret " << ret << dendl; + + if (ret < 0) + handle_error(env, ret); + else + pool = env->NewStringUTF(buf); + +out: + if (buf) + delete [] buf; + + return pool; +} + /* * Class: com_ceph_fs_CephMount * Method: native_ceph_localize_reads diff --git a/src/java/test/com/ceph/fs/CephMountTest.java b/src/java/test/com/ceph/fs/CephMountTest.java index ca0db76255e0a..9d205121cc52b 100644 --- a/src/java/test/com/ceph/fs/CephMountTest.java +++ b/src/java/test/com/ceph/fs/CephMountTest.java @@ -931,4 +931,21 @@ public class CephMountTest { assertTrue(poolid >= 0); assertTrue(mount.get_pool_replication(poolid) > 0); } + + @Test + public void test_get_file_pool_name() throws Exception { + String path = makePath(); + int fd = createFile(path, 1); + String pool = mount.get_file_pool_name(fd); + mount.close(fd); + assertTrue(pool != null); + /* assumes using default data pool "data" */ + assertTrue(pool.compareTo("data") == 0); + mount.unlink(path); + } + + @Test(expected=IOException.class) + public void test_get_file_pool_name_ebadf() throws Exception { + String pool = mount.get_file_pool_name(-40); + } } -- 2.39.5