Commit cd31f8d2 authored by sevenuz's avatar sevenuz Committed by sevenuz
Browse files

hybrid: optional open of regular files through FUSE (LIBGKFS_OPEN_VIA_FUSE)

Split-point ablation for the hybrid interface. With LIBGKFS_OPEN_VIA_FUSE=1
the hybrid client opens (and creates/truncates) regular files through the
FUSE mount instead of the interception path, registers the returned kernel
fd in the open-file map and keeps read/write/lseek/fstat on it intercepted.
This models DAOS libpil4dfs with D_IL_COMPATIBLE=1 (open/openat/opendir via
dfuse, data intercepted). Default off: without the variable the behaviour
is unchanged (directories via FUSE, files intercepted).

Limitations: dup() of such an fd yields an intercepted placeholder fd that
is not backed by the kernel fd.
parent 8a207c5f
Loading
Loading
Loading
Loading
+4
Changes for include/client/env.hpp: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -95,6 +95,10 @@ static constexpr auto CREATE_WRITE_OPTIMIZATION =
        ADD_PREFIX("CREATE_WRITE_OPTIMIZATION");
static constexpr auto READ_INLINE_PREFETCH = ADD_PREFIX("READ_INLINE_PREFETCH");
static constexpr auto ENABLE_FORK = ADD_PREFIX("ENABLE_FORK");
// hybrid only: open regular files through the FUSE mount as well (kernel fd),
// data I/O on that fd stays intercepted. Models DAOS libpil4dfs
// D_IL_COMPATIBLE=1 (split-point ablation). Default off.
static constexpr auto OPEN_VIA_FUSE = ADD_PREFIX("OPEN_VIA_FUSE");

} // namespace gkfs::env

+71
Changes for src/client/hooks.cpp: 71 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -44,11 +44,16 @@
#include <client/gkfs_functions.hpp>
#include <client/path.hpp>
#include <client/open_dir.hpp>
#include <client/env.hpp>
#include <client/open_file_map.hpp>

#include <common/path_util.hpp>

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <mutex>
#include <unordered_set>
#include <string>
#include <vector>

@@ -72,6 +77,35 @@ with_errno(T ret) {

constexpr size_t k_fd_copy_chunk_size = 1UL * 1024UL * 1024UL;

// Split-point ablation (LIBGKFS_OPEN_VIA_FUSE=1): regular files are opened
// through the FUSE mount, so the open/create cost is FUSE's, and the returned
// kernel fd is registered in the file map so read/write/lseek/fstat on it stay
// intercepted. These fds are tracked here so that close() also releases the
// kernel fd in every fd mode.
bool
open_via_fuse() {
    static const bool enabled = [] {
        const char* v = std::getenv(gkfs::env::OPEN_VIA_FUSE);
        return v != nullptr && v[0] != '\0' && v[0] != '0';
    }();
    return enabled;
}

std::mutex kernel_backed_mutex;
std::unordered_set<int> kernel_backed_fds;

void
kernel_backed_add(int fd) {
    std::lock_guard<std::mutex> lock(kernel_backed_mutex);
    kernel_backed_fds.insert(fd);
}

bool
kernel_backed_take(int fd) {
    std::lock_guard<std::mutex> lock(kernel_backed_mutex);
    return kernel_backed_fds.erase(fd) > 0;
}

bool
kernel_fd_targets_dev_null(int fd) {
    char proc_fd_path[64];
@@ -225,6 +259,32 @@ hook_openat(int dirfd, const char* cpath, int flags, mode_t mode) {
                        fd, std::make_shared<gkfs::filemap::OpenDir>(resolved));
                return fd;
            }
            if(open_via_fuse()) {
                // Split-point ablation: create/lookup/truncate through FUSE.
                int kfd = gsl::narrow_cast<int>(syscall_no_intercept_wrapper(
                        SYS_openat, dirfd, cpath, flags, mode));
                if(kfd < 0) {
                    return kfd;
                }
                struct stat st {};
                if(syscall_no_intercept_wrapper(SYS_fstat, kfd, &st) != 0) {
                    return kfd; // leave it to the kernel
                }
                if(S_ISREG(st.st_mode)) {
                    // FUSE already created/truncated the file; keep the
                    // access mode and O_APPEND for the intercepted data path.
                    CTX->file_map()->add(
                            kfd, std::make_shared<gkfs::filemap::OpenFile>(
                                         resolved, flags & ~(O_CREAT | O_EXCL |
                                                             O_TRUNC)));
                    kernel_backed_add(kfd);
                } else if(S_ISDIR(st.st_mode)) {
                    CTX->file_map()->add(
                            kfd, std::make_shared<gkfs::filemap::OpenDir>(
                                         resolved));
                }
                return kfd;
            }
            if(flags & O_CREAT) {
                // O_CREAT always creates a new file in the intercept namespace.
                return with_errno(
@@ -274,6 +334,17 @@ hook_close(int fd) {

    LOG(DEBUG, "{}() called with fd: {}", __func__, fd);

    if(kernel_backed_take(fd)) {
        // flush caches, drop the map entry; in placeholder fd mode
        // OpenFileMap::remove() closes the fd itself
        gkfs::syscall::gkfs_close(fd);
        if(CTX->protect_fds() || CTX->range_fd()) {
            return gsl::narrow_cast<int>(
                    syscall_no_intercept_wrapper(SYS_close, fd));
        }
        return 0;
    }

    auto f = CTX->file_map()->get_dir(fd);
    if(f != nullptr) {
        // OpenFileMap::remove() already closes the kernel fd (it assumes a