FUSE (Filesystem in Userspace) enables the development of custom filesystems without kernel programming, making it ideal for enterprise storage solutions. This comprehensive guide explores advanced FUSE programming techniques, from basic file operations to sophisticated distributed storage architectures.

FUSE Architecture and Advanced Programming

Section 1: Enterprise FUSE Filesystem Foundation

Building production-ready FUSE filesystems requires understanding the VFS interface, implementing efficient caching strategies, and handling concurrent operations safely.

Advanced FUSE Framework Implementation

// enterprise_fuse.c - Production-grade FUSE filesystem framework
#define FUSE_USE_VERSION 35
#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include <assert.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/xattr.h>
#include <dirent.h>

#define ENTERPRISE_FS_MAGIC 0xDEADBEEF
#define MAX_PATH_LEN 4096
#define MAX_OPEN_FILES 65536
#define CACHE_TIMEOUT 30
#define BLOCK_SIZE 4096

// Filesystem statistics
struct fs_stats {
    uint64_t total_files;
    uint64_t total_dirs;
    uint64_t total_size;
    uint64_t read_ops;
    uint64_t write_ops;
    uint64_t lookup_ops;
    uint64_t create_ops;
    uint64_t delete_ops;
    uint64_t cache_hits;
    uint64_t cache_misses;
    time_t start_time;
};

// File handle structure for open files
struct file_handle {
    int fd;
    char *path;
    int flags;
    mode_t mode;
    off_t size;
    time_t atime;
    time_t mtime;
    time_t ctime;
    bool dirty;
    pthread_mutex_t mutex;
    int ref_count;
};

// Directory handle structure
struct dir_handle {
    DIR *dp;
    char *path;
    struct dirent *last_entry;
    off_t offset;
    pthread_mutex_t mutex;
};

// Metadata cache entry
struct cache_entry {
    char *path;
    struct stat stat_buf;
    time_t expire_time;
    bool valid;
    pthread_rwlock_t lock;
    struct cache_entry *next;
    struct cache_entry *prev;
};

// Main filesystem context
struct enterprise_fs {
    char *root_path;
    char *mount_point;
    
    // Open file table
    struct file_handle *file_table[MAX_OPEN_FILES];
    pthread_mutex_t file_table_mutex;
    
    // Metadata cache
    struct cache_entry *cache_head;
    struct cache_entry *cache_tail;
    size_t cache_size;
    size_t max_cache_entries;
    pthread_rwlock_t cache_lock;
    
    // Statistics
    struct fs_stats stats;
    pthread_mutex_t stats_mutex;
    
    // Configuration
    bool use_cache;
    bool direct_io;
    bool async_ops;
    int cache_timeout;
    
    // Background threads
    pthread_t cache_cleaner_thread;
    pthread_t stats_thread;
    volatile bool running;
};

static struct enterprise_fs *g_fs = NULL;

// Utility functions
static char *make_full_path(const char *path)
{
    char *full_path = malloc(strlen(g_fs->root_path) + strlen(path) + 1);
    if (full_path) {
        strcpy(full_path, g_fs->root_path);
        strcat(full_path, path);
    }
    return full_path;
}

static void update_stats(const char *operation)
{
    pthread_mutex_lock(&g_fs->stats_mutex);
    
    if (strcmp(operation, "read") == 0) {
        g_fs->stats.read_ops++;
    } else if (strcmp(operation, "write") == 0) {
        g_fs->stats.write_ops++;
    } else if (strcmp(operation, "lookup") == 0) {
        g_fs->stats.lookup_ops++;
    } else if (strcmp(operation, "create") == 0) {
        g_fs->stats.create_ops++;
    } else if (strcmp(operation, "delete") == 0) {
        g_fs->stats.delete_ops++;
    }
    
    pthread_mutex_unlock(&g_fs->stats_mutex);
}

// Metadata cache implementation
static struct cache_entry *cache_lookup(const char *path)
{
    struct cache_entry *entry;
    time_t now = time(NULL);
    
    pthread_rwlock_rdlock(&g_fs->cache_lock);
    
    for (entry = g_fs->cache_head; entry; entry = entry->next) {
        if (strcmp(entry->path, path) == 0) {
            if (entry->valid && entry->expire_time > now) {
                // Move to head (LRU)
                if (entry != g_fs->cache_head) {
                    if (entry->next) entry->next->prev = entry->prev;
                    if (entry->prev) entry->prev->next = entry->next;
                    if (entry == g_fs->cache_tail) g_fs->cache_tail = entry->prev;
                    
                    entry->next = g_fs->cache_head;
                    entry->prev = NULL;
                    if (g_fs->cache_head) g_fs->cache_head->prev = entry;
                    g_fs->cache_head = entry;
                    if (!g_fs->cache_tail) g_fs->cache_tail = entry;
                }
                
                pthread_rwlock_unlock(&g_fs->cache_lock);
                pthread_mutex_lock(&g_fs->stats_mutex);
                g_fs->stats.cache_hits++;
                pthread_mutex_unlock(&g_fs->stats_mutex);
                return entry;
            } else {
                // Expired entry
                entry->valid = false;
            }
        }
    }
    
    pthread_rwlock_unlock(&g_fs->cache_lock);
    pthread_mutex_lock(&g_fs->stats_mutex);
    g_fs->stats.cache_misses++;
    pthread_mutex_unlock(&g_fs->stats_mutex);
    return NULL;
}

static void cache_insert(const char *path, const struct stat *stbuf)
{
    struct cache_entry *entry;
    
    if (!g_fs->use_cache) return;
    
    pthread_rwlock_wrlock(&g_fs->cache_lock);
    
    // Check if entry already exists
    for (entry = g_fs->cache_head; entry; entry = entry->next) {
        if (strcmp(entry->path, path) == 0) {
            entry->stat_buf = *stbuf;
            entry->expire_time = time(NULL) + g_fs->cache_timeout;
            entry->valid = true;
            pthread_rwlock_unlock(&g_fs->cache_lock);
            return;
        }
    }
    
    // Create new entry
    entry = calloc(1, sizeof(*entry));
    if (!entry) {
        pthread_rwlock_unlock(&g_fs->cache_lock);
        return;
    }
    
    entry->path = strdup(path);
    entry->stat_buf = *stbuf;
    entry->expire_time = time(NULL) + g_fs->cache_timeout;
    entry->valid = true;
    pthread_rwlock_init(&entry->lock, NULL);
    
    // Add to head
    entry->next = g_fs->cache_head;
    if (g_fs->cache_head) g_fs->cache_head->prev = entry;
    g_fs->cache_head = entry;
    if (!g_fs->cache_tail) g_fs->cache_tail = entry;
    
    g_fs->cache_size++;
    
    // Remove oldest entries if cache is full
    while (g_fs->cache_size > g_fs->max_cache_entries && g_fs->cache_tail) {
        struct cache_entry *old = g_fs->cache_tail;
        g_fs->cache_tail = old->prev;
        if (g_fs->cache_tail) g_fs->cache_tail->next = NULL;
        else g_fs->cache_head = NULL;
        
        free(old->path);
        pthread_rwlock_destroy(&old->lock);
        free(old);
        g_fs->cache_size--;
    }
    
    pthread_rwlock_unlock(&g_fs->cache_lock);
}

static void cache_invalidate(const char *path)
{
    struct cache_entry *entry;
    
    pthread_rwlock_wrlock(&g_fs->cache_lock);
    
    for (entry = g_fs->cache_head; entry; entry = entry->next) {
        if (strcmp(entry->path, path) == 0) {
            entry->valid = false;
            break;
        }
    }
    
    pthread_rwlock_unlock(&g_fs->cache_lock);
}

// File handle management
static struct file_handle *alloc_file_handle(const char *path, int flags, mode_t mode)
{
    struct file_handle *fh = calloc(1, sizeof(*fh));
    if (!fh) return NULL;
    
    fh->path = strdup(path);
    fh->flags = flags;
    fh->mode = mode;
    fh->fd = -1;
    fh->ref_count = 1;
    pthread_mutex_init(&fh->mutex, NULL);
    
    return fh;
}

static void free_file_handle(struct file_handle *fh)
{
    if (!fh) return;
    
    if (fh->fd >= 0) close(fh->fd);
    free(fh->path);
    pthread_mutex_destroy(&fh->mutex);
    free(fh);
}

// FUSE operations implementation
static int enterprise_getattr(const char *path, struct stat *stbuf,
                             struct fuse_file_info *fi)
{
    char *full_path;
    int res;
    struct cache_entry *cached;
    
    (void) fi;
    
    update_stats("lookup");
    
    // Check cache first
    cached = cache_lookup(path);
    if (cached) {
        pthread_rwlock_rdlock(&cached->lock);
        *stbuf = cached->stat_buf;
        pthread_rwlock_unlock(&cached->lock);
        return 0;
    }
    
    full_path = make_full_path(path);
    if (!full_path) return -ENOMEM;
    
    res = lstat(full_path, stbuf);
    if (res == -1) {
        free(full_path);
        return -errno;
    }
    
    // Cache the result
    cache_insert(path, stbuf);
    
    free(full_path);
    return 0;
}

static int enterprise_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
                             off_t offset, struct fuse_file_info *fi,
                             enum fuse_readdir_flags flags)
{
    DIR *dp;
    struct dirent *de;
    char *full_path;
    
    (void) offset;
    (void) fi;
    (void) flags;
    
    full_path = make_full_path(path);
    if (!full_path) return -ENOMEM;
    
    dp = opendir(full_path);
    if (dp == NULL) {
        free(full_path);
        return -errno;
    }
    
    while ((de = readdir(dp)) != NULL) {
        struct stat st;
        memset(&st, 0, sizeof(st));
        st.st_ino = de->d_ino;
        st.st_mode = de->d_type << 12;
        
        if (filler(buf, de->d_name, &st, 0, 0))
            break;
    }
    
    closedir(dp);
    free(full_path);
    return 0;
}

static int enterprise_open(const char *path, struct fuse_file_info *fi)
{
    char *full_path;
    int fd;
    struct file_handle *fh;
    
    full_path = make_full_path(path);
    if (!full_path) return -ENOMEM;
    
    fd = open(full_path, fi->flags);
    if (fd == -1) {
        free(full_path);
        return -errno;
    }
    
    fh = alloc_file_handle(path, fi->flags, 0);
    if (!fh) {
        close(fd);
        free(full_path);
        return -ENOMEM;
    }
    
    fh->fd = fd;
    fi->fh = (uint64_t)fh;
    
    // Set direct I/O if configured
    if (g_fs->direct_io) {
        fi->direct_io = 1;
    }
    
    free(full_path);
    return 0;
}

static int enterprise_read(const char *path, char *buf, size_t size, off_t offset,
                          struct fuse_file_info *fi)
{
    struct file_handle *fh = (struct file_handle *)fi->fh;
    int res;
    
    (void) path;
    
    update_stats("read");
    
    if (!fh) return -EBADF;
    
    pthread_mutex_lock(&fh->mutex);
    
    res = pread(fh->fd, buf, size, offset);
    if (res == -1) {
        res = -errno;
    } else {
        fh->atime = time(NULL);
    }
    
    pthread_mutex_unlock(&fh->mutex);
    
    return res;
}

static int enterprise_write(const char *path, const char *buf, size_t size,
                           off_t offset, struct fuse_file_info *fi)
{
    struct file_handle *fh = (struct file_handle *)fi->fh;
    int res;
    
    (void) path;
    
    update_stats("write");
    
    if (!fh) return -EBADF;
    
    pthread_mutex_lock(&fh->mutex);
    
    res = pwrite(fh->fd, buf, size, offset);
    if (res == -1) {
        res = -errno;
    } else {
        fh->mtime = time(NULL);
        fh->dirty = true;
        
        // Update size if we wrote past end
        if (offset + res > fh->size) {
            fh->size = offset + res;
        }
        
        // Invalidate cache
        cache_invalidate(path);
    }
    
    pthread_mutex_unlock(&fh->mutex);
    
    return res;
}

static int enterprise_release(const char *path, struct fuse_file_info *fi)
{
    struct file_handle *fh = (struct file_handle *)fi->fh;
    
    (void) path;
    
    if (fh) {
        free_file_handle(fh);
    }
    
    return 0;
}

static int enterprise_create(const char *path, mode_t mode,
                            struct fuse_file_info *fi)
{
    char *full_path;
    int fd;
    struct file_handle *fh;
    
    update_stats("create");
    
    full_path = make_full_path(path);
    if (!full_path) return -ENOMEM;
    
    fd = creat(full_path, mode);
    if (fd == -1) {
        free(full_path);
        return -errno;
    }
    
    fh = alloc_file_handle(path, fi->flags, mode);
    if (!fh) {
        close(fd);
        free(full_path);
        return -ENOMEM;
    }
    
    fh->fd = fd;
    fi->fh = (uint64_t)fh;
    
    // Update statistics
    pthread_mutex_lock(&g_fs->stats_mutex);
    g_fs->stats.total_files++;
    pthread_mutex_unlock(&g_fs->stats_mutex);
    
    free(full_path);
    return 0;
}

static int enterprise_unlink(const char *path)
{
    char *full_path;
    int res;
    
    update_stats("delete");
    
    full_path = make_full_path(path);
    if (!full_path) return -ENOMEM;
    
    res = unlink(full_path);
    if (res == -1) {
        free(full_path);
        return -errno;
    }
    
    // Invalidate cache
    cache_invalidate(path);
    
    // Update statistics
    pthread_mutex_lock(&g_fs->stats_mutex);
    g_fs->stats.total_files--;
    pthread_mutex_unlock(&g_fs->stats_mutex);
    
    free(full_path);
    return 0;
}

static int enterprise_mkdir(const char *path, mode_t mode)
{
    char *full_path;
    int res;
    
    full_path = make_full_path(path);
    if (!full_path) return -ENOMEM;
    
    res = mkdir(full_path, mode);
    if (res == -1) {
        free(full_path);
        return -errno;
    }
    
    // Update statistics
    pthread_mutex_lock(&g_fs->stats_mutex);
    g_fs->stats.total_dirs++;
    pthread_mutex_unlock(&g_fs->stats_mutex);
    
    free(full_path);
    return 0;
}

static int enterprise_rmdir(const char *path)
{
    char *full_path;
    int res;
    
    full_path = make_full_path(path);
    if (!full_path) return -ENOMEM;
    
    res = rmdir(full_path);
    if (res == -1) {
        free(full_path);
        return -errno;
    }
    
    // Invalidate cache
    cache_invalidate(path);
    
    // Update statistics
    pthread_mutex_lock(&g_fs->stats_mutex);
    g_fs->stats.total_dirs--;
    pthread_mutex_unlock(&g_fs->stats_mutex);
    
    free(full_path);
    return 0;
}

static int enterprise_flush(const char *path, struct fuse_file_info *fi)
{
    struct file_handle *fh = (struct file_handle *)fi->fh;
    int res = 0;
    
    (void) path;
    
    if (fh && fh->dirty) {
        pthread_mutex_lock(&fh->mutex);
        res = fsync(fh->fd);
        if (res == -1) {
            res = -errno;
        } else {
            fh->dirty = false;
        }
        pthread_mutex_unlock(&fh->mutex);
    }
    
    return res;
}

// Extended attributes support
static int enterprise_setxattr(const char *path, const char *name, const char *value,
                              size_t size, int flags)
{
    char *full_path;
    int res;
    
    full_path = make_full_path(path);
    if (!full_path) return -ENOMEM;
    
    res = setxattr(full_path, name, value, size, flags);
    if (res == -1) {
        free(full_path);
        return -errno;
    }
    
    free(full_path);
    return 0;
}

static int enterprise_getxattr(const char *path, const char *name, char *value,
                              size_t size)
{
    char *full_path;
    int res;
    
    full_path = make_full_path(path);
    if (!full_path) return -ENOMEM;
    
    res = getxattr(full_path, name, value, size);
    if (res == -1) {
        free(full_path);
        return -errno;
    }
    
    free(full_path);
    return res;
}

// Background cache cleaner thread
static void *cache_cleaner_thread(void *arg)
{
    (void) arg;
    
    while (g_fs->running) {
        time_t now = time(NULL);
        struct cache_entry *entry, *next;
        
        pthread_rwlock_wrlock(&g_fs->cache_lock);
        
        for (entry = g_fs->cache_head; entry; entry = next) {
            next = entry->next;
            
            if (entry->expire_time <= now) {
                // Remove expired entry
                if (entry->prev) entry->prev->next = entry->next;
                else g_fs->cache_head = entry->next;
                
                if (entry->next) entry->next->prev = entry->prev;
                else g_fs->cache_tail = entry->prev;
                
                free(entry->path);
                pthread_rwlock_destroy(&entry->lock);
                free(entry);
                g_fs->cache_size--;
            }
        }
        
        pthread_rwlock_unlock(&g_fs->cache_lock);
        
        sleep(10);  // Clean every 10 seconds
    }
    
    return NULL;
}

// FUSE operations structure
static struct fuse_operations enterprise_oper = {
    .getattr    = enterprise_getattr,
    .readdir    = enterprise_readdir,
    .open       = enterprise_open,
    .read       = enterprise_read,
    .write      = enterprise_write,
    .release    = enterprise_release,
    .create     = enterprise_create,
    .unlink     = enterprise_unlink,
    .mkdir      = enterprise_mkdir,
    .rmdir      = enterprise_rmdir,
    .flush      = enterprise_flush,
    .setxattr   = enterprise_setxattr,
    .getxattr   = enterprise_getxattr,
};

// Initialize filesystem
static int enterprise_fs_init(const char *root_path)
{
    g_fs = calloc(1, sizeof(*g_fs));
    if (!g_fs) return -ENOMEM;
    
    g_fs->root_path = strdup(root_path);
    g_fs->use_cache = true;
    g_fs->cache_timeout = CACHE_TIMEOUT;
    g_fs->max_cache_entries = 10000;
    g_fs->running = true;
    
    pthread_mutex_init(&g_fs->file_table_mutex, NULL);
    pthread_rwlock_init(&g_fs->cache_lock, NULL);
    pthread_mutex_init(&g_fs->stats_mutex, NULL);
    
    g_fs->stats.start_time = time(NULL);
    
    // Start background threads
    pthread_create(&g_fs->cache_cleaner_thread, NULL, cache_cleaner_thread, NULL);
    
    return 0;
}

// Main function
int main(int argc, char *argv[])
{
    if (argc < 3) {
        fprintf(stderr, "Usage: %s <root_directory> <mount_point> [fuse_options]\n", argv[0]);
        return 1;
    }
    
    if (enterprise_fs_init(argv[1]) != 0) {
        fprintf(stderr, "Failed to initialize filesystem\n");
        return 1;
    }
    
    // Remove program name and root directory from arguments
    argc -= 2;
    for (int i = 0; i < argc; i++) {
        argv[i] = argv[i + 2];
    }
    argv[argc] = NULL;
    
    printf("Starting Enterprise FUSE filesystem\n");
    printf("Root: %s\n", g_fs->root_path);
    printf("Mount: %s\n", argv[0]);
    
    int ret = fuse_main(argc, argv, &enterprise_oper, NULL);
    
    // Cleanup
    g_fs->running = false;
    pthread_join(g_fs->cache_cleaner_thread, NULL);
    
    free(g_fs->root_path);
    free(g_fs);
    
    return ret;
}

This comprehensive FUSE filesystem implementation demonstrates advanced techniques for building enterprise storage solutions. The framework includes sophisticated caching mechanisms, concurrent file handling, extended attributes support, and performance monitoring. These patterns enable the development of high-performance custom filesystems suitable for enterprise environments while maintaining compatibility with standard POSIX filesystem interfaces.