Commit 562321ab authored by Julius Athenstaedt's avatar Julius Athenstaedt Committed by Ramon Nou
Browse files

refactor fuse logging, so it is only working on DEBUG build

parent 273c220f
Loading
Loading
Loading
Loading
+55 −95
Original line number Diff line number Diff line
@@ -47,6 +47,17 @@ static std::unordered_map<std::string, struct stat> local_fifos;
static fuse_ino_t next_ino = 2; // reserve 1 for root
static const std::string fifo_path = "/tmp/gekkofs_fifos/";

#ifdef GKFS_DEBUG_BUILD
#define DEBUG_INFO(ud, fmt, ...)                                               \
    do {                                                                       \
        if((ud) && (ud)->debug) {                                              \
            fuse_log(FUSE_LOG_DEBUG, "[DEBUG] " fmt "\n", ##__VA_ARGS__);      \
        }                                                                      \
    } while(0)
#else
#define DEBUG_INFO(...) /* No debug output */
#endif

static fuse_ino_t
alloc_inode(const std::string& path) {
    std::lock_guard<std::mutex> lk(ino_mutex);
@@ -123,9 +134,8 @@ passthrough_ll_help(void) {
static void
init_handler(void* userdata, struct fuse_conn_info* conn) {
    struct u_data* ud = (struct u_data*) userdata;
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "init handler readahead %i direct_io %i \n",
                 ud->max_readahead, ud->direct_io);
    DEBUG_INFO(ud, "init handler readahead %i direct_io %i", ud->max_readahead,
               ud->direct_io);

    // TODO check other capabilities e.g. FUSE_CAP_READDIRPLUS
    if(ud->writeback) {
@@ -136,9 +146,8 @@ init_handler(void* userdata, struct fuse_conn_info* conn) {
        // for older fuse versions like on the ubuntu22
        conn->want |= FUSE_CAP_WRITEBACK_CACHE;
#endif
        if(ud->debug)
            fuse_log(FUSE_LOG_DEBUG,
                     "init_handler: try to activate writeback\n");
        DEBUG_INFO(ud, "init_handler: try to activate writeback",
                   ud->max_readahead, ud->direct_io);
    }
    // if(lo->flock && conn->capable & FUSE_CAP_FLOCK_LOCKS) {
    //     has_flag = fuse_set_feature_flag(conn, FUSE_CAP_FLOCK_LOCKS);
@@ -155,24 +164,21 @@ init_handler(void* userdata, struct fuse_conn_info* conn) {
static void
destroy_handler(void* userdata) {
    struct u_data* ud = (struct u_data*) userdata;
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "destroy handler \n");
    DEBUG_INFO(ud, "destroy handler");
    // userdata is GekkoFuse* if passed
}

static void
lookup_handler(fuse_req_t req, fuse_ino_t parent, const char* name) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "lookup handler ino %u\n", parent);
    DEBUG_INFO(ud, "lookup handler ino %u", parent);
    auto* parent_inode = get_inode(parent);
    if(!parent_inode) {
        fuse_reply_err(req, ENOENT);
        return;
    }
    std::string child = get_path(parent_inode, name);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "lookup %s\n", child.c_str());
    DEBUG_INFO(ud, "lookup %s", child.c_str());

    if(ud->fifo) {
        auto iit = local_fifos.find(child);
@@ -218,9 +224,7 @@ lookup_handler(fuse_req_t req, fuse_ino_t parent, const char* name) {
static void
getattr_handler(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
    auto* ud = udata(req);
    if(ud->debug) {
        fuse_log(FUSE_LOG_DEBUG, "getattr handler \n");
    }
    DEBUG_INFO(ud, "getattr handler");
    auto* inode = get_inode(ino);
    if(!inode) {
        fuse_reply_err(req, ENOENT);
@@ -230,9 +234,8 @@ getattr_handler(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
    struct stat st;
    int rc = gkfs::syscall::gkfs_stat(inode->path, &st);
    if(rc) {
        if(ud->debug)
            fuse_log(FUSE_LOG_DEBUG, "getattr error %u\n", rc);
        fuse_reply_err(req, ENOENT);
        DEBUG_INFO(ud, "getattr error %u", rc);
        fuse_reply_err(req, errno);
        return;
    }
    inode->st = st;
@@ -243,8 +246,7 @@ static void
setattr_handler(fuse_req_t req, fuse_ino_t ino, struct stat* attr, int to_set,
                struct fuse_file_info* fi) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "setattr handler ino %u\n", ino);
    DEBUG_INFO(ud, "setattr handler ino %u", ino);
    auto* inode = get_inode(ino);
    if(!inode) {
        fuse_reply_err(req, ENOENT);
@@ -255,8 +257,7 @@ setattr_handler(fuse_req_t req, fuse_ino_t ino, struct stat* attr, int to_set,
        off_t new_size = attr->st_size;
        int res = gkfs::syscall::gkfs_truncate(inode->path, new_size);
        if(res < 0) {
            if(ud->debug)
                fuse_log(FUSE_LOG_DEBUG, "setattr truncate failed on %s\n",
            DEBUG_INFO(ud, "setattr truncate failed on %s",
                       inode->path.c_str());
            fuse_reply_err(req, EIO);
            return;
@@ -279,8 +280,7 @@ setattr_handler(fuse_req_t req, fuse_ino_t ino, struct stat* attr, int to_set,
static void
open_handler(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "open handler \n");
    DEBUG_INFO(ud, "open handler");
    auto* inode = get_inode(ino);
    if(!inode) {
        fuse_reply_err(req, ENOENT);
@@ -302,8 +302,7 @@ static void
lseek_handler(fuse_req_t req, fuse_ino_t ino, off_t off, int whence,
              struct fuse_file_info* fi) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "lseek handler \n");
    DEBUG_INFO(ud, "lseek handler");
    int lc = gkfs::syscall::gkfs_lseek(fi->fh, off, whence);
    if(lc < 0) {
        fuse_reply_err(req, 1);
@@ -316,8 +315,7 @@ static void
read_handler(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
             struct fuse_file_info* fi) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "read handler \n");
    DEBUG_INFO(ud, "read handler");
    auto* inode = get_inode(ino);
    if(!inode) {
        fuse_reply_err(req, ENOENT);
@@ -326,8 +324,7 @@ read_handler(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
    std::vector<char> buf(size);
    int rc = gkfs::syscall::gkfs_pread(fi->fh, buf.data(), size, off);
    if(rc < 0) {
        if(ud->debug)
            fuse_log(FUSE_LOG_DEBUG, "read fail \n");
        DEBUG_INFO(ud, "read fail");
        fuse_reply_err(req, errno);
        return;
    }
@@ -338,8 +335,7 @@ static void
write_handler(fuse_req_t req, fuse_ino_t ino, const char* buf, size_t size,
              off_t off, struct fuse_file_info* fi) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "write handler \n");
    DEBUG_INFO(ud, "write handler");
    auto* inode = get_inode(ino);
    if(!inode) {
        fuse_reply_err(req, ENOENT);
@@ -347,8 +343,7 @@ write_handler(fuse_req_t req, fuse_ino_t ino, const char* buf, size_t size,
    }
    int rc = gkfs::syscall::gkfs_pwrite(fi->fh, buf, size, off);
    if(rc < 0) {
        if(ud->debug)
            fuse_log(FUSE_LOG_DEBUG, "write fail \n");
        DEBUG_INFO(ud, "write fail");
        fuse_reply_err(req, errno);
        return;
    }
@@ -365,12 +360,10 @@ create_handler(fuse_req_t req, fuse_ino_t parent, const char* name, mode_t mode,
        return;
    }
    std::string path = get_path(parent_inode, name);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "create handler %s\n", path.c_str());
    DEBUG_INFO(ud, "create handler %s", path.c_str());
    int fd = gkfs::syscall::gkfs_open(path, mode, fi->flags | O_CREAT);
    if(fd < 0) {
        if(ud->debug)
            fuse_log(FUSE_LOG_DEBUG, "create -> open failed errno %i\n", errno);
        DEBUG_INFO(ud, "create -> open failed errno %i", errno);
        fuse_reply_err(req, errno);
        return;
    }
@@ -383,8 +376,7 @@ create_handler(fuse_req_t req, fuse_ino_t parent, const char* name, mode_t mode,
        return;
    }
    fuse_ino_t ino = alloc_inode(path);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "create new inode ino %i\n", ino);
    DEBUG_INFO(ud, "create new inode ino %i", ino);
    ino_map[ino].st = st;
    fuse_entry_param e = {};
    e.ino = ino;
@@ -399,8 +391,7 @@ create_handler(fuse_req_t req, fuse_ino_t parent, const char* name, mode_t mode,
static void
unlink_handler(fuse_req_t req, fuse_ino_t parent, const char* name) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "unlink handler \n");
    DEBUG_INFO(ud, "unlink handler");
    auto* parent_inode = get_inode(parent);
    if(!parent_inode) {
        fuse_reply_err(req, ENOENT);
@@ -435,20 +426,17 @@ unlink_handler(fuse_req_t req, fuse_ino_t parent, const char* name) {
static void
opendir_handler(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "opendir handler \n");
    DEBUG_INFO(ud, "opendir handler");
    auto* inode = get_inode(ino);
    if(!inode) {
        fuse_reply_err(req, ENOTDIR);
        return;
    }
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "open dir %s \n", inode->path.c_str());
    DEBUG_INFO(ud, "open dir %s", inode->path.c_str());

    const int fd = gkfs::syscall::gkfs_opendir(inode->path);

    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "\t with fd %i \n", fd);
    DEBUG_INFO(ud, "\t with fd %i \n", fd);

    if(fd < 0) {
        fuse_reply_err(req, ENOTDIR);
@@ -463,13 +451,11 @@ static void
readdir_handler(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
                struct fuse_file_info* fi) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "readdir handler \n");
    DEBUG_INFO(ud, "readdir handler");

    auto open_dir = CTX->file_map()->get_dir(fi->fh);

    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "read dir %s \n", open_dir->path().c_str());
    DEBUG_INFO(ud, "read dir %s", open_dir->path().c_str());

    if(open_dir == nullptr) {
        fuse_reply_err(req, EBADF);
@@ -543,8 +529,7 @@ readdir_handler(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off,
static void
releasedir_handler(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "releasedir handler \n");
    DEBUG_INFO(ud, "releasedir handler");
    if(CTX->interception_enabled() && CTX->file_map()->exist(fi->fh)) {
        int ret = gkfs::syscall::gkfs_close(fd); // Close GekkoFS internal FD

@@ -558,24 +543,17 @@ releasedir_handler(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
static void
release_handler(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "release handler \n");
    DEBUG_INFO(ud, "release handler");
    auto* inode = get_inode(ino);
    if(!inode) {
        if(ud->debug)
            fuse_log(FUSE_LOG_DEBUG, "release here \n");
        fuse_reply_err(req, ENOENT);
        return;
    }
    int lc = gkfs::syscall::gkfs_close(fi->fh);
    if(lc < 0) {
        if(ud->debug)
            fuse_log(FUSE_LOG_DEBUG, "release there \n");
        fuse_reply_err(req, 1);
        return;
    }
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "release success \n");
    fuse_reply_err(req, 0);
}

@@ -583,8 +561,7 @@ release_handler(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
static void
forget_handler(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "forget handler \n");
    DEBUG_INFO(ud, "forget handler");

    auto it = ino_map.find(ino);
    if(it == ino_map.end()) {
@@ -601,8 +578,7 @@ forget_handler(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup) {
    if(inode.lookup_count == 0) { // && inode.open_count == 0
        path_map.erase(inode.path);
        ino_map.erase(it);
        if(ud->debug)
            fuse_log(FUSE_LOG_DEBUG, "reached lookup_count 0 \n");
        DEBUG_INFO(ud, "reached lookup_count 0");
    }

    fuse_reply_none(req);
@@ -612,8 +588,7 @@ forget_handler(fuse_req_t req, fuse_ino_t ino, uint64_t nlookup) {
static void
flush_handler(fuse_req_t req, fuse_ino_t ino, struct fuse_file_info* fi) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "flush handler \n");
    DEBUG_INFO(ud, "flush handler");
    auto* inode = get_inode(ino);
    if(!inode) {
        fuse_reply_err(req, ENOENT);
@@ -637,8 +612,7 @@ fsync_handler(fuse_req_t req, fuse_ino_t ino, int datasync,
static void
access_handler(fuse_req_t req, fuse_ino_t ino, int mask) {
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "access handler \n");
    DEBUG_INFO(ud, "access handler");
    if(ud->access && !ud->fifo) {
        auto* inode = get_inode(ino);
        if(!inode) {
@@ -668,9 +642,7 @@ mkdir_handler(fuse_req_t req, fuse_ino_t parent, const char* name,
        return;
    }
    std::string path = get_path(parent_inode, name);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "mkdir parent %s name %s\n",
                 parent_inode->path.c_str(), name);
    DEBUG_INFO(ud, "mkdir parent %s name %s", parent_inode->path.c_str(), name);
    int rc = gkfs::syscall::gkfs_create(path, mode | S_IFDIR);
    if(rc == -1) {
        fuse_reply_err(req, 1);
@@ -679,14 +651,11 @@ mkdir_handler(fuse_req_t req, fuse_ino_t parent, const char* name,
    struct stat st;
    int sc = gkfs::syscall::gkfs_stat(path, &st);
    if(sc == -1) {
        if(ud->debug)
            fuse_log(FUSE_LOG_DEBUG, "thats why its not allowed \n");
        fuse_reply_err(req, 1);
        return;
    }
    fuse_ino_t ino = alloc_inode(path);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "create new inode ino %i\n", ino);
    DEBUG_INFO(ud, "create new inode ino %i", ino);
    ino_map[ino].st = st;
    fuse_entry_param e = {};
    e.ino = ino;
@@ -705,8 +674,7 @@ rmdir_handler(fuse_req_t req, fuse_ino_t parent, const char* name) {
        return;
    }
    std::string path = get_path(parent_inode, name);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "rmdir  %s\n", path.c_str());
    DEBUG_INFO(ud, "rmdir %s", path.c_str());
    int rc = gkfs::syscall::gkfs_rmdir(path);
    if(rc == -1) {
        fuse_reply_err(req, errno);
@@ -746,13 +714,10 @@ symlink_handler(fuse_req_t req, const char* linkname, fuse_ino_t parent,
                const char* name) {
#ifdef HAS_SYMLINKS
    auto* ud = udata(req);
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "symlink handler linkname %s name %s\n",
                 linkname, name);
    DEBUG_INFO(ud, "symlink handler linkname %s name %s", linkname, name);
    auto* parent_inode = get_inode(parent);
    if(!parent_inode) {
        if(ud->debug)
            fuse_log(FUSE_LOG_DEBUG, "symlink parent inode ino %i\n", parent);
        DEBUG_INFO(ud, "symlink parent inode ino %i", parent);
        fuse_reply_err(req, ENOENT);
        return;
    }
@@ -766,9 +731,7 @@ symlink_handler(fuse_req_t req, const char* linkname, fuse_ino_t parent,
                          target.substr(strlen(ud->mountpoint)).c_str());
    }

    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "mk symlink path %s target %s\n", path.c_str(),
                 target.c_str());
    DEBUG_INFO(ud, "mk symlink path %s target %s", path.c_str());
    int rc = gkfs::syscall::gkfs_mk_symlink(path, target);
    if(rc < 0) {
        fuse_reply_err(req, 1);
@@ -778,14 +741,11 @@ symlink_handler(fuse_req_t req, const char* linkname, fuse_ino_t parent,
    // Stat the new symlink so we can reply with entry info
    struct stat st;
    if(gkfs::syscall::gkfs_stat(path, &st) < 0) {
        if(ud->debug)
            fuse_log(FUSE_LOG_DEBUG, "stat failed\n");
        DEBUG_INFO(ud, "stat failed %i", errno);
        fuse_reply_err(req, errno);
        return;
    }
    if(ud->debug)
        fuse_log(FUSE_LOG_DEBUG, "stat mode %i, iflink %i\n", st.st_mode,
                 S_IFLNK);
    DEBUG_INFO(ud, "stat mode %i, iflink %i", st.st_mode, S_IFLNK);
    // TODO this meta is not saved and therefore on restart gone
    // this shows the link on ls -l
    st.st_mode = S_IFLNK | 0777; // mark as symlink + full perms
@@ -980,7 +940,7 @@ init_ll_ops(fuse_lowlevel_ops* ops) {
    // ops->write_buf
    ops->lseek = lseek_handler;

    // xattr
    // xattr for arbitrary key value fields
    // ops->setxattr
    // ops->getxattr
    // ops->listxattr
@@ -1018,7 +978,7 @@ init_ll_ops(fuse_lowlevel_ops* ops) {
    // ops->retrive_reply
    // ops->fallocate
    // ops->copy_file_range
    // ops->statx
    // ops->statx // not supported in fuse < 3
}

void