Skip to content

Commit

Permalink
Merge pull request #304 from HDFGroup/chogan/issue_272
Browse files Browse the repository at this point in the history
Support fstat in posix adapter
  • Loading branch information
ChristopherHogan committed Dec 10, 2021
2 parents 68850c7 + 6af9b53 commit e56dcf5
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 4 deletions.
6 changes: 5 additions & 1 deletion adapter/include/hermes/adapter/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
*/
#include <fcntl.h>
#include <stdarg.h>
#include <sys/stat.h>

/**
* Dependent library headers
*/
#include "glog/logging.h"

/**
* Internal headers
*/
Expand Down Expand Up @@ -60,11 +63,12 @@ HERMES_FORWARD_DECL(pwrite64, ssize_t,
(int fd, const void *buf, size_t count, off64_t offset));
HERMES_FORWARD_DECL(lseek, off_t, (int fd, off_t offset, int whence));
HERMES_FORWARD_DECL(lseek64, off64_t, (int fd, off64_t offset, int whence));
HERMES_FORWARD_DECL(__fxstat, int, (int version, int fd, struct stat *buf));
HERMES_FORWARD_DECL(fsync, int, (int fd));
HERMES_FORWARD_DECL(fdatasync, int, (int fd));
HERMES_FORWARD_DECL(close, int, (int fd));
/**
* MPI functions declarations
* MPI function declarations
*/
HERMES_FORWARD_DECL(MPI_Init, int, (int *argc, char ***argv));
HERMES_FORWARD_DECL(MPI_Finalize, int, (void));
Expand Down
3 changes: 2 additions & 1 deletion adapter/src/hermes/adapter/posix/metadata_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ std::pair<AdapterStat, bool> MetadataManager::Find(int fh) {

FileID MetadataManager::Convert(int fd) {
struct stat st;
int status = fstat(fd, &st);
MAP_OR_FAIL(__fxstat);
int status = real___fxstat_(_STAT_VER, fd, &st);
if (status == 0) {
return FileID(st.st_dev, st.st_ino);
} else {
Expand Down
52 changes: 51 additions & 1 deletion adapter/src/hermes/adapter/posix/posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ int simple_open(int ret, const std::string &path_str, int flags) {
if (!existing.second) {
LOG(INFO) << "File not opened before by adapter" << std::endl;
struct stat st;
int status = fstat(ret, &st);
MAP_OR_FAIL(__fxstat);
int status = real___fxstat_(_STAT_VER, ret, &st);
if (status == 0) {
AdapterStat stat(st);
stat.ref_count = 1;
Expand Down Expand Up @@ -396,6 +397,7 @@ int HERMES_DECL(MPI_Finalize)(void) {
int status = real_MPI_Finalize_();
return status;
}

/**
* POSIX
*/
Expand Down Expand Up @@ -429,6 +431,7 @@ int HERMES_DECL(open)(const char *path, int flags, ...) {
}
return (ret);
}

int HERMES_DECL(open64)(const char *path, int flags, ...) {
int ret;
int mode = 0;
Expand Down Expand Up @@ -459,6 +462,7 @@ int HERMES_DECL(open64)(const char *path, int flags, ...) {
}
return (ret);
}

int HERMES_DECL(__open_2)(const char *path, int oflag) {
int ret;
if (hermes::adapter::IsTracked(path)) {
Expand All @@ -478,6 +482,7 @@ int HERMES_DECL(__open_2)(const char *path, int oflag) {
}
return (ret);
}

int HERMES_DECL(creat)(const char *path, mode_t mode) {
int ret;
std::string path_str(path);
Expand All @@ -491,6 +496,7 @@ int HERMES_DECL(creat)(const char *path, mode_t mode) {
}
return (ret);
}

int HERMES_DECL(creat64)(const char *path, mode_t mode) {
int ret;
std::string path_str(path);
Expand All @@ -504,6 +510,7 @@ int HERMES_DECL(creat64)(const char *path, mode_t mode) {
}
return (ret);
}

ssize_t HERMES_DECL(read)(int fd, void *buf, size_t count) {
size_t ret;
if (hermes::adapter::IsTracked(fd)) {
Expand All @@ -523,6 +530,7 @@ ssize_t HERMES_DECL(read)(int fd, void *buf, size_t count) {
}
return (ret);
}

ssize_t HERMES_DECL(write)(int fd, const void *buf, size_t count) {
size_t ret;
if (hermes::adapter::IsTracked(fd)) {
Expand All @@ -541,6 +549,7 @@ ssize_t HERMES_DECL(write)(int fd, const void *buf, size_t count) {
}
return (ret);
}

ssize_t HERMES_DECL(pread)(int fd, void *buf, size_t count, off_t offset) {
size_t ret;
if (hermes::adapter::IsTracked(fd)) {
Expand All @@ -562,6 +571,7 @@ ssize_t HERMES_DECL(pread)(int fd, void *buf, size_t count, off_t offset) {
}
return (ret);
}

ssize_t HERMES_DECL(pwrite)(int fd, const void *buf, size_t count,
off_t offset) {
size_t ret;
Expand All @@ -584,6 +594,7 @@ ssize_t HERMES_DECL(pwrite)(int fd, const void *buf, size_t count,
}
return (ret);
}

ssize_t HERMES_DECL(pread64)(int fd, void *buf, size_t count, off64_t offset) {
size_t ret;
if (hermes::adapter::IsTracked(fd)) {
Expand All @@ -605,6 +616,7 @@ ssize_t HERMES_DECL(pread64)(int fd, void *buf, size_t count, off64_t offset) {
}
return (ret);
}

ssize_t HERMES_DECL(pwrite64)(int fd, const void *buf, size_t count,
off64_t offset) {
size_t ret;
Expand Down Expand Up @@ -715,6 +727,44 @@ off64_t HERMES_DECL(lseek64)(int fd, off64_t offset, int whence) {
}
return (ret);
}

int HERMES_DECL(__fxstat)(int version, int fd, struct stat *buf) {
int result = 0;
if (hermes::adapter::IsTracked(fd)) {
LOG(INFO) << "Intercepted fstat." << std::endl;
auto mdm = hermes::adapter::Singleton<MetadataManager>::GetInstance();
auto existing = mdm->Find(fd);
if (existing.second) {
AdapterStat &astat = existing.first;
// TODO(chogan): st_dev and st_ino need to be assigned by us, but
// currently we get them by calling the real fstat on open.
buf->st_dev = 0;
buf->st_ino = 0;
buf->st_mode = astat.st_mode;
buf->st_nlink = 0;
buf->st_uid = astat.st_uid;
buf->st_gid = astat.st_gid;
buf->st_rdev = 0;
buf->st_size = astat.st_size;
buf->st_blksize = astat.st_blksize;
buf->st_blocks = 0;
buf->st_atime = astat.st_atime;
buf->st_mtime = astat.st_mtime;
buf->st_ctime = astat.st_ctime;
} else {
result = -1;
errno = EBADF;
LOG(ERROR) << "File with descriptor" << fd
<< "does not exist in Hermes\n";
}
} else {
MAP_OR_FAIL(__fxstat);
result = real___fxstat_(version, fd, buf);
}

return result;
}

int HERMES_DECL(fsync)(int fd) {
int ret;
if (hermes::adapter::IsTracked(fd)) {
Expand Down
2 changes: 1 addition & 1 deletion adapter/test/mpiio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ add_dependencies(hermes_mpiio_adapter_test hermes_daemon)
set_target_properties(hermes_mpiio_adapter_test PROPERTIES COMPILE_FLAGS "-DHERMES_INTERCEPT=1")

mpi_daemon(hermes_mpiio_adapter_test 2 "[synchronicity=async]" "async" 1)
mpi_daemon(hermes_mpiio_adapter_test 2 "[synchronicity=sync]" "sync" 1)
mpi_daemon(hermes_mpiio_adapter_test 2 "[synchronicity=sync]" "sync" 1)
23 changes: 23 additions & 0 deletions adapter/test/posix/posix_adapter_basic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* have access to the file, you may request a copy from help@hdfgroup.org. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include <sys/stat.h>

TEST_CASE("Open", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=single_open]"
Expand Down Expand Up @@ -1451,3 +1453,24 @@ TEST_CASE("SingleMixed", "[process=" + std::to_string(info.comm_size) +
}
posttest();
}

TEST_CASE("fstat") {
pretest();

SECTION("fstat on new file") {
test::test_open(info.new_file.c_str(), O_WRONLY | O_CREAT | O_EXCL, 0600);
REQUIRE(test::fh_orig != -1);
test::test_write(info.write_data.data(), args.request_size);
REQUIRE(test::size_written_orig == args.request_size);

struct stat buf = {};
int result = fstat(test::fh_orig, &buf);
REQUIRE(result == 0);
REQUIRE(buf.st_size == (off_t)test::size_written_orig);

test::test_close();
REQUIRE(test::status_orig == 0);
}

posttest();
}

0 comments on commit e56dcf5

Please sign in to comment.