Skip to contentSkip to conversation

Adding ceph_readdir to libceph

PATCH Applied Brian Chrisman 2 participants 4 messages May 19, 2011

lore
Share & tools
lore
#0 lore raw

cover Adding ceph_readdir to libceph

Some applications (Samba-VFS layer included) expect readdir() rather than readdir_r(). Expanded Client & libceph to include readdir.
Expanded testceph to cover additional basic functionality.

Brian Chrisman (2):
add ceph_readdir() to libceph
expanding testceph to test open/readdir/telldir

 src/client/Client.cc       |   35 ++++++++++++++++-
 src/client/Client.h        |    1 +
 src/client/testceph.cc     |   88 ++++++++++++++++++++++++++++++++++---------
 src/include/ceph/libceph.h |    1 +
 src/libceph.cc             |    5 ++
 5 files changed, 109 insertions(+), 21 deletions(-)
#1 lore raw

PATCH 1/2 add ceph_readdir() to libceph

---
 src/client/Client.cc       |   35 +++++++++++++++++++++++++++++++++--
 src/client/Client.h        |    1 +
 src/include/ceph/libceph.h |    1 +
 src/libceph.cc             |    5 +++++
 4 files changed, 40 insertions(+), 2 deletions(-)
src/client/Client.cc +33 -2
index e4b400c..0284c03 100644
@@ -4298,8 +4298,6 @@ int Client::readdir_r_cb(dir_result_t *d, add_dirent_cb_t cb, void *p)
 }
 
 
-
-
 int Client::readdir_r(dir_result_t *d, struct dirent *de)
 {  
   return readdirplus_r(d, de, 0, 0);
@@ -4337,6 +4335,39 @@ static int _readdir_single_dirent_cb(void *p, struct dirent *de, struct stat *st
   return 0;  
 }
 
+struct dirent * Client::readdir(dir_result_t *d)
+{
+  int ret;
+  static int stmask;
+  static struct dirent de;
+  static struct stat st;
+  single_readdir sr;
+  sr.de = &de;
+  sr.st = &st;
+  sr.stmask = &stmask;
+  sr.full = false;
+
+  /*
+   * Return mechanisms are non-obvious (callback appears intended for multi-read mechanism like cfuse)
+   * readdir_r_cb=0 end of directory reached on prior call
+   * readdir_r_cb=0 entry filled and offset now at end of the directory
+   * readdir_r_cb=-1 entry is filled successfully, not end of dir
+   * readdir_r_cb=-(other) on error
+   * callback leaves sr.full=false when 'offset is at end of directory'
+   * callback may leave sr.full=false on error
+   * callback sets sr.full=true on 'successfully read dirent'
+   */
+  ret = readdir_r_cb(d, _readdir_single_dirent_cb, (void *)&sr);
+  if (ret < -1) {
+    errno = -ret;
+    return (dirent *) NULL;
+  }
+  if (sr.full) {
+    return &de;
+  }
+  return (dirent *) NULL;
+}
+
 int Client::readdirplus_r(dir_result_t *d, struct dirent *de, struct stat *st, int *stmask)
 {  
   single_readdir sr;
src/client/Client.h +1 -0
index b36621d..e10979d 100644
@@ -1204,6 +1204,7 @@ public:
 
   int readdir_r_cb(dir_result_t *dirp, add_dirent_cb_t cb, void *p);
 
+  struct dirent * readdir(dir_result_t *d);
   int readdir_r(dir_result_t *dirp, struct dirent *de);
   int readdirplus_r(dir_result_t *dirp, struct dirent *de, struct stat *st, int *stmask);
 
src/include/ceph/libceph.h +1 -0
index f9fc29e..bf28402 100644
@@ -76,6 +76,7 @@ int ceph_chdir(struct ceph_mount_info *cmount, const char *s);
 
 int ceph_opendir(struct ceph_mount_info *cmount, const char *name, struct ceph_dir_result **dirpp);
 int ceph_closedir(struct ceph_mount_info *cmount, struct ceph_dir_result *dirp);
+struct dirent * ceph_readdir(struct ceph_mount_info *cmount, struct ceph_dir_result *dirp);
 int ceph_readdir_r(struct ceph_mount_info *cmount, struct ceph_dir_result *dirp, struct dirent *de);
 int ceph_readdirplus_r(struct ceph_mount_info *cmount, struct ceph_dir_result *dirp, struct dirent *de,
 		       struct stat *st, int *stmask);
src/libceph.cc +5 -0
index 6388bde..54993ac 100644
@@ -319,6 +319,11 @@ extern "C" int ceph_closedir(struct ceph_mount_info *cmount, struct ceph_dir_res
   return cmount->get_client()->closedir((dir_result_t*)dirp);
 }
 
+extern "C" struct dirent * ceph_readdir(struct ceph_mount_info *cmount, struct ceph_dir_result *dirp)
+{
+  return cmount->get_client()->readdir((dir_result_t*)dirp);
+}
+
 extern "C" int ceph_readdir_r(struct ceph_mount_info *cmount, struct ceph_dir_result *dirp, struct dirent *de)
 {
   return cmount->get_client()->readdir_r((dir_result_t*)dirp, de);
Signature · 1 line
1.7.1
#2 lore raw

PATCH 2/2 expanding testceph to test open/readdir/telldir

In reply to Brian Chrisman
---
 src/client/testceph.cc |   88 +++++++++++++++++++++++++++++++++++++----------
 1 files changed, 69 insertions(+), 19 deletions(-)
src/client/testceph.cc +69 -19
index c24cc03..413da10 100644
@@ -30,6 +30,7 @@ int main(int argc, const char **argv)
 {
   struct ceph_mount_info *cmount;
     cout << "calling ceph_create..." << std::endl;
+  int my_fd;
   int ret = ceph_create(&cmount, NULL);
   if (ret) {
     cerr << "ceph_create failed with error: " << ret << std::endl;
@@ -67,7 +68,12 @@ int main(int argc, const char **argv)
   } else {
     cout << "ceph_opendir: success" << std::endl;
   }
-
+  //ret = ceph_closedir(cmount, foo_dir);
+  //if (ret == 0) {
+  //  cerr << "ceph_closedir success" << std::endl;
+  //} else {
+  //  cerr << "ceph_closedir error: " << cpp_strerror(ret) << std::endl;
+  //}
   ret = ceph_mkdir(cmount, "foo",  0777);
   if (ret) {
     cerr << "ceph_mkdir error: " << cpp_strerror(ret) << std::endl;
@@ -129,7 +135,7 @@ int main(int argc, const char **argv)
   } else {
     cout << "ceph_rmdir: success" << std::endl;
   }
-  ret = ceph_open(cmount, "barfile", O_CREAT, 0666);
+  my_fd = ret = ceph_open(cmount, "barfile", O_CREAT, 0666);
   if (ret < 0) {
     cerr << "ceph_open O_CREAT error: " << cpp_strerror(ret) << std::endl;
     return 1;
@@ -175,6 +181,15 @@ int main(int argc, const char **argv)
   if (strncmp((char *) aybabtu, aybabtu_reference,7)) {
     cerr << "ceph_getxattr error: no match (" << aybabtu << ") should be (" << aybabtu_reference << cpp_strerror(ret) << std::endl;
   }
+  ret = ceph_close(cmount,my_fd);
+  if (ret < 0) {
+    cerr << "ceph_close error: " << cpp_strerror(ret) << std::endl;
+    return 1;
+  } else {
+    cout << "ceph_close: success" << std::endl;
+  }
+
+
   cout << "Attempting lstat on '/.'" << std::endl;
   ret = ceph_lstat(cmount, "/.", &stbuf);
   if (ret) {
@@ -191,40 +206,75 @@ int main(int argc, const char **argv)
   } else {
     cout << "ceph_lstat: success" << std::endl;
   }
-  cout << "Attempting readdir_r" << std::endl;
-  ret = ceph_mkdir(cmount, "readdir_r_test",  0777);
+  cout << "Setting up readdir test" << std::endl;
+  ret = ceph_mkdir(cmount, "readdir_test",  0777);
   if (ret) {
     cerr << "ceph_mkdir error: " << cpp_strerror(ret) << std::endl;
     return 1;
   } else {
     cout << "ceph_mkdir: success" << std::endl;
   }
-  struct ceph_dir_result *readdir_r_test_dir;
-  ret = ceph_opendir(cmount, "readdir_r_test", &readdir_r_test_dir);
-  if (ret != 0) {
-    cerr << "ceph_opendir error: unexpected result from trying to open readdir_r_test: "
-	 << cpp_strerror(ret) << std::endl;
+  my_fd = ret = ceph_open(cmount, "readdir_test/opened_file_1", O_CREAT, 0666);
+  if (ret < 0) {
+    cerr << "ceph_open O_CREAT error: " << cpp_strerror(ret) << std::endl;
     return 1;
   } else {
-    cout << "ceph_opendir: success" << std::endl;
+    cout << "ceph_open: success" << std::endl;
   }
-  ret = ceph_open(cmount, "readdir_r_test/opened_file", O_CREAT, 0666);
+  ret = ceph_close(cmount, my_fd);
   if (ret < 0) {
-    cerr << "ceph_open O_CREAT error: " << cpp_strerror(ret) << std::endl;
+    cerr << "ceph_close error: " << cpp_strerror(ret) << std::endl;
     return 1;
   } else {
-    cout << "ceph_open: success" << std::endl;
+    cout << "ceph_close: success" << std::endl;
   }
 
-  struct dirent * result;
-  result = (struct dirent *) malloc(sizeof(struct dirent));
-  ret = ceph_readdir_r(cmount, readdir_r_test_dir, result);
+  struct ceph_dir_result *readdir_test_dir;
+  ret = ceph_opendir(cmount, "readdir_test", &readdir_test_dir);
   if (ret != 0) {
-    cerr << "ceph_readdir_r: fail, returned: " << ret << std::endl;
-  } else {
-    cerr << "ceph_readdir_r: success: " << *result->d_name << std::endl;
+    cerr << "ceph_opendir error: unexpected result from trying to open readdir_test: "
+	 << cpp_strerror(ret) << std::endl;
     return 1;
+  } else {
+    cout << "ceph_opendir: success" << std::endl;
   }
+  cout << "Attempting readdir on opened directory..." << std::endl;
+  struct dirent * result;
+  //result = (struct dirent *) malloc(sizeof(struct dirent));
+  result = ceph_readdir(cmount, readdir_test_dir);
+  if (result == (dirent *) NULL) {
+    cout << "ceph_readdir: failed to read any entries" << std::endl;
+  }
+  loff_t telldir_result;
+  while ( result != (dirent *) NULL) {
+    cout << "ceph_readdir: dirent->d_name: (" << result->d_name << ")" << std::endl;
+    cout << "ceph_telldir: starting" << std::endl;
+    telldir_result = ceph_telldir(cmount, readdir_test_dir);
+    if (telldir_result > -1) {
+      cout << "ceph_telldir: offset: from return code:" << telldir_result << std::endl;
+    } else {
+      cout << "ceph_telldir: failed" << std::endl;
+    }
+    cout << "ceph_readdir: lookup success: trying for another..." << std::endl;
+    result = ceph_readdir(cmount, readdir_test_dir);
+  }
+  cout << "ceph_readdir: finished" << std::endl;
+
+  // tell us that we're at the end of the directory:
+  cout << "ceph_telldir: starting" << std::endl;
+  telldir_result = ceph_telldir(cmount, readdir_test_dir);
+  if (telldir_result > -1) {
+    cout << "ceph_telldir: offset: from return code:" << telldir_result << std::endl;
+  } else {
+    cout << "ceph_telldir: failed" << std::endl;
+  }
+
+  //ret = ceph_closedir(cmount,readdir_test_dir);
+  //if (ret == 0) {
+  //  cerr << "ceph_closedir success" << std::endl;
+  //} else {
+  //  cerr << "ceph_closedir error: " << cpp_strerror(ret) << std::endl;
+  //}
   
   ceph_shutdown(cmount);
 
Signature · 1 line
1.7.1
Applied
#3 lore raw
In reply to Brian Chrisman
Quoted 7 lines · Brian Chrisman

Some applications (Samba-VFS layer included) expect readdir() rather than readdir_r(). Expanded Client & libceph to include readdir.
Expanded testceph to cover additional basic functionality.

Brian Chrisman (2):
add ceph_readdir() to libceph
expanding testceph to test open/readdir/telldir

Applied, thanks!
sage

Quoted 14 lines
 src/client/Client.cc       |   35 ++++++++++++++++-
 src/client/Client.h        |    1 +
 src/client/testceph.cc     |   88 ++++++++++++++++++++++++++++++++++---------
 src/include/ceph/libceph.h |    1 +
 src/libceph.cc             |    5 ++
 5 files changed, 109 insertions(+), 21 deletions(-)

--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html