From 9e278fdbf927b2b8644a252465851034cc3927f7 Mon Sep 17 00:00:00 2001 From: dongdong Date: Sat, 29 Jul 2017 18:31:54 +0800 Subject: [PATCH] Client: Add function "get_default_data_pool_name" for java client Fix: now java client trying to get the default pool througth the root dir "/", but the thing is when java client is mounted to a sub dir and the sub dir's pool_id is always -1, this will cause unexpected failure. Signed-off-by: dongdong tao --- src/client/Client.cc | 6 ++++ src/client/Client.h | 3 ++ src/include/cephfs/libcephfs.h | 11 ++++++ src/java/java/com/ceph/fs/CephMount.java | 16 +++++++++ src/java/native/libcephfs_jni.cc | 45 ++++++++++++++++++++++++ src/libcephfs.cc | 15 ++++++++ 6 files changed, 96 insertions(+) diff --git a/src/client/Client.cc b/src/client/Client.cc index 6d9346e7b9dc8..0ec811fe6232f 100644 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -12693,6 +12693,12 @@ int Client::fdescribe_layout(int fd, file_layout_t *lp) return 0; } +int64_t Client::get_default_pool_id() +{ + Mutex::Locker lock(client_lock); + /* first data pool is the default */ + return mdsmap->get_first_data_pool(); +} // expose osdmap diff --git a/src/client/Client.h b/src/client/Client.h index b6369a29363e1..beefa1eba5c8a 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -1101,6 +1101,9 @@ public: int get_file_stripe_address(int fd, loff_t offset, vector& address); int get_file_extent_osds(int fd, loff_t off, loff_t *len, vector& osds); int get_osd_addr(int osd, entity_addr_t& addr); + + // expose mdsmap + int64_t get_default_pool_id(); // expose osdmap int get_local_osd(); diff --git a/src/include/cephfs/libcephfs.h b/src/include/cephfs/libcephfs.h index dfb31a1007b8e..45cef34eb47ce 100644 --- a/src/include/cephfs/libcephfs.h +++ b/src/include/cephfs/libcephfs.h @@ -1242,6 +1242,17 @@ int ceph_get_pool_name(struct ceph_mount_info *cmount, int pool, char *buf, size */ int ceph_get_path_pool_name(struct ceph_mount_info *cmount, const char *path, char *buf, size_t buflen); +/** + * Get the default pool name of cephfs + * Write the name of the default pool to the buffer. If buflen is 0, return + * a suggested length for the buffer. + * @param cmount the ceph mount handle to use. + * @param buf buffer to store the name in + * @param buflen size of the buffer + * @returns length in bytes of the pool name, or -ERANGE if the buffer is not large enough. + */ +int ceph_get_default_data_pool_name(struct ceph_mount_info *cmount, char *buf, size_t buflen); + /** * Get the file layout from an open file descriptor. * diff --git a/src/java/java/com/ceph/fs/CephMount.java b/src/java/java/com/ceph/fs/CephMount.java index d1267142c3ecf..786a6ece2f588 100644 --- a/src/java/java/com/ceph/fs/CephMount.java +++ b/src/java/java/com/ceph/fs/CephMount.java @@ -938,6 +938,22 @@ public class CephMount { } private static native String native_ceph_get_file_pool_name(long mountp, int fd); + + /** + * Get the default data pool of cephfs. + * + * @return The pool name. + */ + public String get_default_data_pool_name() { + rlock.lock(); + try { + return native_ceph_get_default_data_pool_name(instance_ptr); + } finally { + rlock.unlock(); + } + } + + private static native String native_ceph_get_default_data_pool_name(long mountp); /** * Get the replication of a file. diff --git a/src/java/native/libcephfs_jni.cc b/src/java/native/libcephfs_jni.cc index 975a092dace5a..080ec3cbda4ef 100644 --- a/src/java/native/libcephfs_jni.cc +++ b/src/java/native/libcephfs_jni.cc @@ -2580,6 +2580,51 @@ out: return pool; } +/** + * Class: com_ceph_fs_CephMount + * Method: native_ceph_get_default_data_pool_name + * Signature: (J)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_com_ceph_fs_CephMount_native_1ceph_1get_1default_1data_1pool_1name + (JNIEnv *env, jclass clz, jlong j_mntp) +{ + 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_default_data_pool_name" << dendl; + + ret = ceph_get_default_data_pool_name(cmount, NULL, 0); + if (ret < 0) + goto out; + 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)); + ret = ceph_get_default_data_pool_name(cmount, buf, buflen); + + ldout(cct, 10) << "jni: get_default_data_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/libcephfs.cc b/src/libcephfs.cc index bfacd1bae51b1..74652da147280 100644 --- a/src/libcephfs.cc +++ b/src/libcephfs.cc @@ -1074,6 +1074,21 @@ extern "C" int ceph_get_path_pool_name(struct ceph_mount_info *cmount, const cha return name.length(); } +extern "C" int ceph_get_default_data_pool_name(struct ceph_mount_info *cmount, char *buf, size_t len) +{ + if (!cmount->is_mounted()) + return -ENOTCONN; + int64_t pool_id = cmount->get_client()->get_default_pool_id(); + + string name = cmount->get_client()->get_pool_name(pool_id); + if (len == 0) + return name.length(); + if (name.length() > len) + return -ERANGE; + strncpy(buf, name.c_str(), len); + return name.length(); +} + extern "C" int ceph_get_file_layout(struct ceph_mount_info *cmount, int fh, int *stripe_unit, int *stripe_count, int *object_size, int *pg_pool) { file_layout_t l; -- 2.39.5