From d2f5890f8451412ff21fd480fedc41eafc755d07 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Fri, 7 Dec 2012 01:16:53 -0800 Subject: [PATCH] client, libcephfs: add method to get the pool name for an open file Signed-off-by: Sage Weil --- src/client/Client.cc | 8 ++++++++ src/client/Client.h | 1 + src/include/cephfs/libcephfs.h | 14 ++++++++++++++ src/libcephfs.cc | 19 +++++++++++++++++++ src/test/libcephfs/test.cc | 6 ++++++ 5 files changed, 48 insertions(+) diff --git a/src/client/Client.cc b/src/client/Client.cc index ac23877ebc077..58d4292333f9c 100644 --- a/src/client/Client.cc +++ b/src/client/Client.cc @@ -7251,6 +7251,14 @@ int Client::describe_layout(int fd, ceph_file_layout *lp) // expose osdmap +string Client::get_pool_name(int64_t pool) +{ + Mutex::Locker lock(client_lock); + if (!osdmap->have_pg_pool(pool)) + return string(); + return osdmap->get_pool_name(pool); +} + int Client::get_pool_replication(int64_t pool) { Mutex::Locker lock(client_lock); diff --git a/src/client/Client.h b/src/client/Client.h index 125e194a9f4ab..b1d943f42e53d 100644 --- a/src/client/Client.h +++ b/src/client/Client.h @@ -654,6 +654,7 @@ public: // expose osdmap int get_local_osd(); int get_pool_replication(int64_t pool); + string get_pool_name(int64_t pool); int enumerate_layout(int fd, vector& result, loff_t length, loff_t offset); diff --git a/src/include/cephfs/libcephfs.h b/src/include/cephfs/libcephfs.h index adeea248dd2e3..7e5afb9b98508 100644 --- a/src/include/cephfs/libcephfs.h +++ b/src/include/cephfs/libcephfs.h @@ -841,6 +841,20 @@ int ceph_get_file_stripe_unit(struct ceph_mount_info *cmount, int fh); */ int ceph_get_file_pool(struct ceph_mount_info *cmount, int fh); +/** + * Get the name of the pool a file is stored in, + * + * Write the name of the file's pool to the buffer. If buflen is 0, return + * a suggested length for the buffer. + * + * @param cmount the ceph mount handle to use. + * @param fh the open file descriptor referring to the file + * @param buf buffer to store the name in + * @param buflen size of the buffer + * @returns length in bytes of the pool name + */ +int ceph_get_file_pool_name(struct ceph_mount_info *cmount, int fh, char *buf, size_t buflen); + /** * Get the file replication information. * @param cmount the ceph mount handle to use. diff --git a/src/libcephfs.cc b/src/libcephfs.cc index d085f846265b8..55bcfed1ff713 100644 --- a/src/libcephfs.cc +++ b/src/libcephfs.cc @@ -705,6 +705,25 @@ extern "C" int ceph_get_file_pool(struct ceph_mount_info *cmount, int fh) return l.fl_pg_pool; } +extern "C" int ceph_get_file_pool_name(struct ceph_mount_info *cmount, int fh, char *buf, size_t len) +{ + struct ceph_file_layout l; + int r; + + if (!cmount->is_mounted()) + return -ENOTCONN; + r = cmount->get_client()->describe_layout(fh, &l); + if (r < 0) + return r; + string name = cmount->get_client()->get_pool_name(l.fl_pg_pool); + 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_replication(struct ceph_mount_info *cmount, int fh) { struct ceph_file_layout l; diff --git a/src/test/libcephfs/test.cc b/src/test/libcephfs/test.cc index b2d22f9acebb2..f576fefd16447 100644 --- a/src/test/libcephfs/test.cc +++ b/src/test/libcephfs/test.cc @@ -160,6 +160,10 @@ TEST(LibCephFS, OpenLayout) { sprintf(test_layout_file, "test_layout_%d_b", getpid()); int fd = ceph_open_layout(cmount, test_layout_file, O_CREAT, 0666, (1<<20), 7, (1<<20), NULL); ASSERT_GT(fd, 0); + char poolname[80]; + ASSERT_LT(0, ceph_get_file_pool_name(cmount, fd, poolname, sizeof(poolname))); + ASSERT_EQ(4, ceph_get_file_pool_name(cmount, fd, poolname, 0)); + ASSERT_EQ(0, strcmp("data", poolname)); ceph_close(cmount, fd); /* invalid layout */ @@ -171,6 +175,8 @@ TEST(LibCephFS, OpenLayout) { sprintf(test_layout_file, "test_layout_%d_d", getpid()); fd = ceph_open_layout(cmount, test_layout_file, O_CREAT, 0666, (1<<20), 7, (1<<20), "data"); ASSERT_GT(fd, 0); + ASSERT_EQ(4, ceph_get_file_pool_name(cmount, fd, poolname, sizeof(poolname))); + ASSERT_EQ(0, strcmp("data", poolname)); ceph_close(cmount, fd); /* with metadata pool (invalid) */ -- 2.39.5