Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions client/src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ if USE_PMPI_WRAPPERS
lib_LTLIBRARIES += libunifyfs_mpi_gotcha.la
endif #USE_PMPI_WRAPPERS

if ENABLE_LD_PRELOAD
lib_LTLIBRARIES += libunifyfs_preload_gotcha.la
endif #ENABLE_LD_PRELOAD

if HAVE_FORTRAN
lib_LTLIBRARIES += libunifyfsf.la
endif #HAVE_FORTRAN
Expand Down Expand Up @@ -96,6 +100,8 @@ PMPI_SRC_FILES = \
pmpi_wrappers.c \
pmpi_wrappers.h

PRELOAD_SRC_FILES = \
preload.c

# Per-target flags begin here

Expand Down Expand Up @@ -137,6 +143,14 @@ libunifyfs_gotcha_la_SOURCES = \
$(POSIX_CLIENT_SRC_FILES) \
gotcha_map_unifyfs_list.c

if ENABLE_LD_PRELOAD
libunifyfs_preload_gotcha_la_CPPFLAGS = $(CLIENT_COMMON_CPPFLAGS)
libunifyfs_preload_gotcha_la_CFLAGS = $(CLIENT_COMMON_CFLAGS)
libunifyfs_preload_gotcha_la_LDFLAGS = $(CLIENT_COMMON_LDFLAGS)
libunifyfs_preload_gotcha_la_LIBADD = libunifyfs_gotcha.la
libunifyfs_preload_gotcha_la_SOURCES = $(PRELOAD_SRC_FILES)
endif #ENABLE_LD_PRELOAD

if USE_PMPI_WRAPPERS
libunifyfs_mpi_gotcha_la_CPPFLAGS = $(CLIENT_COMMON_CPPFLAGS)
libunifyfs_mpi_gotcha_la_CFLAGS = $(CLIENT_COMMON_CFLAGS)
Expand Down
17 changes: 17 additions & 0 deletions client/src/gotcha_map_unifyfs_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ UNIFYFS_DEF(unlink, int,
UNIFYFS_DEF(remove, int,
(const char* path),
(path))
UNIFYFS_DEF(utimensat, int,
(int dirfd, const char* pathname, const struct timespec times[2],
int flags),
(dirfd, pathname, times, flags))
UNIFYFS_DEF(futimens, int,
(int fd, const struct timespec times[2]),
(fd, times))

UNIFYFS_DEF(stat, int,
(const char* path, struct stat* buf),
Expand Down Expand Up @@ -144,6 +151,12 @@ UNIFYFS_DEF(pwrite64, ssize_t,
UNIFYFS_DEF(close, int,
(int fd),
(fd))
UNIFYFS_DEF(dup, int,
(int fd),
(fd))
UNIFYFS_DEF(dup2, int,
(int fd, int desired_fd),
(fd, desired_fd))
UNIFYFS_DEF(fchdir, int,
(int fd),
(fd))
Expand Down Expand Up @@ -349,6 +362,8 @@ struct gotcha_binding_t unifyfs_wrappers[] = {
{ "truncate", UNIFYFS_WRAP(truncate), &wrappee_handle_truncate },
{ "unlink", UNIFYFS_WRAP(unlink), &wrappee_handle_unlink },
{ "remove", UNIFYFS_WRAP(remove), &wrappee_handle_remove },
{ "utimensat", UNIFYFS_WRAP(utimensat), &wrappee_handle_utimensat },
{ "futimens", UNIFYFS_WRAP(futimens), &wrappee_handle_futimens },
{ "stat", UNIFYFS_WRAP(stat), &wrappee_handle_stat },
{ "fstat", UNIFYFS_WRAP(fstat), &wrappee_handle_fstat },
{ "__xstat", UNIFYFS_WRAP(__xstat), &wrappee_handle___xstat },
Expand Down Expand Up @@ -386,6 +401,8 @@ struct gotcha_binding_t unifyfs_wrappers[] = {
{ "munmap", UNIFYFS_WRAP(munmap), &wrappee_handle_munmap },
{ "mmap64", UNIFYFS_WRAP(mmap64), &wrappee_handle_mmap64 },
{ "close", UNIFYFS_WRAP(close), &wrappee_handle_close },
{ "dup", UNIFYFS_WRAP(dup), &wrappee_handle_dup },
{ "dup2", UNIFYFS_WRAP(dup2), &wrappee_handle_dup2 },
{ "opendir", UNIFYFS_WRAP(opendir), &wrappee_handle_opendir },
{ "fdopendir", UNIFYFS_WRAP(fdopendir), &wrappee_handle_fdopendir },
{ "closedir", UNIFYFS_WRAP(closedir), &wrappee_handle_closedir },
Expand Down
44 changes: 35 additions & 9 deletions client/src/posix_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ int unifyfs_fpos_enabled = 1;
unifyfs_fd_t unifyfs_fds[UNIFYFS_CLIENT_MAX_FILES];
rlim_t unifyfs_fd_limit;

/* dup/dup2() support - given a regular fd with a value up to
* UNIFYFS_CLIENT_MAX_FILES, the array holds the index of the
* corresponding entry in unifyfs_fds[] */
int unifyfs_dup_fds[UNIFYFS_CLIENT_MAX_FILES];

/* array of file streams */
unifyfs_stream_t unifyfs_streams[UNIFYFS_CLIENT_MAX_FILES];

Expand Down Expand Up @@ -263,23 +268,35 @@ int unifyfs_intercept_path(const char* path, char* upath)
* convert fd to new fd value if needed */
int unifyfs_intercept_fd(int* fd)
{
int newfd;
int oldfd = *fd;

/* don't intercept anything until we're initialized */
if (!unifyfs_initialized) {
return 0;
}

if (oldfd < unifyfs_fd_limit) {
/* this fd is a real system fd, so leave it as is */
return 0;
} else if (oldfd < 0) {
if (oldfd < 0) {
/* this is an invalid fd, so we should not intercept it */
return 0;
} else {
} else if (oldfd < unifyfs_fd_limit) {
/* check if it has a valid entry in unifyfs_dup_fds[] */
if (oldfd < UNIFYFS_CLIENT_MAX_FILES) {
newfd = unifyfs_dup_fds[oldfd];
if (-1 != newfd) {
*fd = newfd;
LOGDBG("Changing fd from exposed dup2=%d to internal %d",
oldfd, newfd);
return 1;
}
}

/* this fd is a real system fd, so leave it as is */
return 0;
} else { /* >= unifyfs_fd_limit */
/* this is an fd we generated and returned to the user,
* so intercept the call and shift the fd */
int newfd = oldfd - unifyfs_fd_limit;
newfd = oldfd - unifyfs_fd_limit;
*fd = newfd;
LOGDBG("Changing fd from exposed %d to internal %d", oldfd, newfd);
return 1;
Expand Down Expand Up @@ -338,11 +355,19 @@ int unifyfs_fd_init(int fd)
/* set fid to -1 to indicate fd is not active,
* set file position to max value,
* disable read and write flags */
filedesc->fid = -1;
filedesc->pos = (off_t) -1;
filedesc->read = 0;
filedesc->fid = -1;
filedesc->pos = (off_t) -1;
filedesc->read = 0;
filedesc->write = 0;
filedesc->use_count = 0;

return UNIFYFS_SUCCESS;
}

/* initialize duped internal fd for given array index */
int unifyfs_dup_fd_init(int fd)
{
unifyfs_dup_fds[fd] = -1;
return UNIFYFS_SUCCESS;
}

Expand Down Expand Up @@ -482,6 +507,7 @@ int posix_client_init(void)
int num_fds = UNIFYFS_CLIENT_MAX_FILES;
for (i = 0; i < num_fds; i++) {
unifyfs_fd_init(i);
unifyfs_dup_fd_init(i);
}

/* initialize file stream structures */
Expand Down
17 changes: 11 additions & 6 deletions client/src/posix_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@

/* structure to represent file descriptors */
typedef struct {
int fid; /* local file id associated with fd */
off_t pos; /* current file pointer */
int read; /* whether file is opened for read */
int write; /* whether file is opened for write */
int append; /* whether file is opened for append */
off_t pos; /* current file pointer */
int read; /* whether file is opened for read */
int write; /* whether file is opened for write */
int append; /* whether file is opened for append */
int fid; /* local file id associated with fd */
int use_count; /* how many exposed fds refer to this */
} unifyfs_fd_t;

enum unifyfs_stream_orientation {
Expand Down Expand Up @@ -100,7 +101,8 @@ extern int unifyfs_mount_id; /* gfid of mountpoint */
/* whether we can use fgetpos/fsetpos */
extern int unifyfs_fpos_enabled;

/* array of file descriptors */
/* arrays of file descriptors */
extern int unifyfs_dup_fds[UNIFYFS_CLIENT_MAX_FILES];
extern unifyfs_fd_t unifyfs_fds[UNIFYFS_CLIENT_MAX_FILES];
extern rlim_t unifyfs_fd_limit;

Expand Down Expand Up @@ -175,6 +177,9 @@ int unifyfs_get_fid_from_fd(int fd);
/* initialze file descriptor structure corresponding to fd value */
int unifyfs_fd_init(int fd);

/* initialize unifyfs_dup_fds[] entry corresponding to fd value */
int unifyfs_dup_fd_init(int fd);

/* initialze file stream structure corresponding to id value */
int unifyfs_stream_init(int sid);

Expand Down
75 changes: 75 additions & 0 deletions client/src/preload.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2022, Lawrence Livermore National Security, LLC.
* Produced at the Lawrence Livermore National Laboratory.
*
* Copyright 2022, UT-Battelle, LLC.
*
* LLNL-CODE-741539
* All rights reserved.
*
* This is the license for UnifyFS.
* For details, see https://github.com/LLNL/UnifyFS.
* Please read https://github.com/LLNL/UnifyFS/LICENSE for full license text.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "unifyfs.h"
#include "unifyfs_rc.h"

static int preload_initialized; // = 0

void unifyfs_preload_init(void) __attribute__ ((constructor));
void unifyfs_preload_fini(void) __attribute__ ((destructor));

static void unifyfs_preload_mount(void)
{
if (preload_initialized) {
return;
}

char* mountpoint = getenv("UNIFYFS_PRELOAD_MOUNTPOINT");
if (NULL == mountpoint) {
mountpoint = strdup("/unifyfs");
}

int rank = 0;
int world_sz = 1;
int rc = unifyfs_mount(mountpoint, rank, (size_t)world_sz);
if (UNIFYFS_SUCCESS != rc) {
fprintf(stderr,
"UNIFYFS ERROR: unifyfs_mount(%s) failed with '%s'\n",
mountpoint, unifyfs_rc_enum_description((unifyfs_rc)rc));
} else {
fprintf(stderr, "DEBUG: mounted\n");
preload_initialized = 1;
}
}

static void unifyfs_preload_unmount(void)
{
if (preload_initialized) {
int rc = unifyfs_unmount();
if (UNIFYFS_SUCCESS != rc) {
fprintf(stderr,
"UNIFYFS ERROR: unifyfs_unmount() failed with '%s'\n",
unifyfs_rc_enum_description((unifyfs_rc)rc));
}
fprintf(stderr, "DEBUG: unmounted\n");
preload_initialized = 0;
}
}

void unifyfs_preload_init(void)
{
unifyfs_preload_mount();
fflush(NULL);
}

void unifyfs_preload_fini(void)
{
unifyfs_preload_unmount();
fflush(NULL);
}
Loading