test of asynchronous write/read
[rrq/fusefile.git] / fusefile.c
index 9e5cb45d835b771e73548b8b443461474fcaa487..2c7bc8da769a74e7d701da846afa86b9ba0de0e4 100644 (file)
@@ -25,8 +25,8 @@
 
 #define FUSE_USE_VERSION 33
 
-#include <fuse.h>
-#include <fuse/fuse_lowlevel.h>
+#include <fuse3/fuse.h>
+#include <fuse3/fuse_lowlevel.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -41,6 +41,8 @@ struct Region {
     off_t end;
 };
 
+#define REGIONKEEP(r) ((r,beg == r.end)?: 0 : 1)
+
 struct Source {
     char *filename;
     ssize_t from;
@@ -50,6 +52,8 @@ struct Source {
     int dirty;
 };
 
+#define ENDSOURCE( S ) ( S.start + ( S.to - S.from ) )
+
 static struct {
     struct Source *array;
     int count;
@@ -65,12 +69,12 @@ static struct {
 /**
  * Overlay
  */
-static struct {
+static struct Overlay {
     struct Source source;
     struct Region *table;
     size_t count;
     size_t limit;
-} overlay;
+} overlay; // The current overlay.
 
 static void usage();
 
@@ -270,13 +274,25 @@ static void overlay_mark(off_t beg,off_t end) {
 #endif
 }
 
-static void setup_overlay(char *filename) {
-    overlay.source.filename = filename;
-    overlay.source.fd = open( filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
-    if ( overlay.source.fd < 0 ) {
-       perror( filename );
+/**
+ * Capture overlay filenames for processing after source fragments.
+ */
+static void overlay_setup(char *filenames) {
+    overlay.source.filename = filenames;
+}
+
+// Duplicate the source record data at the given index.
+static void dup_source_item(int index) {
+    sources.count++;
+    sources.array = realloc(
+       sources.array, sources.count * sizeof( struct Source ) );
+    if ( sources.array == 0 ) {
+       fprintf( stderr, "** OOM when expanding frament table\n" );
        usage();
     }
+    // copy elements from [index] to [count-1] one element down
+    size_t size = ( sources.count - index - 1 ) * sizeof( struct Source ); 
+    memmove( &sources.array[ index+1 ], &sources.array[ index ], size );
 }
 
 #if DEBUG
@@ -313,7 +329,7 @@ static int setup_source(struct Source *p,char *frag) {
        perror( p->filename );
        return 1; // Error return
     }
-    if ( ( range == 0 ) && stat( p->filename, &filestat ) ) {
+    if ( ( range != 0 ) && stat( p->filename, &filestat ) ) {
        perror( p->filename );
        return 1; 
     }
@@ -399,7 +415,9 @@ static int setup_sources(char **argv,int i,int n) {
     return 0;
 }
 
-static int fusefile_getattr(const char *path,struct stat *stbuf) {
+static int fusefile_getattr(const char *path, struct stat *stbuf,
+                           struct fuse_file_info *ffi)
+{
 #if DEBUG
     fprintf( stderr, "fusefile_getattr( %s )\n", path );
 #endif
@@ -421,17 +439,19 @@ static int fusefile_getattr(const char *path,struct stat *stbuf) {
     return 0;
 }
 
-static int fusefile_chmod(const char *path,mode_t m) {
+static int fusefile_chmod(const char *path, mode_t m,
+                         struct fuse_file_info *ffi)
+{
 #if DEBUG
     fprintf( stderr, "fusefile_chmod( %s, %d )\n", path, m );
 #endif
     return -1;
 }
 
-static int fusefile_open(const char *path,struct fuse_file_info *fi) {
+static int fusefile_open(const char *path, struct fuse_file_info *ffi) {
 #if DEBUG
-    fprintf( stderr, "fusefile_open( %s, %d )\n", path, fi->flags );
-    fprintf( stderr, "fixing( %d )\n", fi->flags | O_CLOEXEC );
+    fprintf( stderr, "fusefile_open( %s, %d )\n", path, ffi->flags );
+    fprintf( stderr, "fixing( %d )\n", ffi->flags | O_CLOEXEC );
 #endif
     if ( strcmp( path, "/" ) != 0 ) {
        return -ENOENT;
@@ -447,28 +467,14 @@ static int find_source(off_t offset) {
     if ( offset >= sources.size ) {
        return -1;
     }
-#if DEBUG
-    fprintf( stderr, "find_source( %ld )\n", offset );
-#endif
     while ( lo + 1 < hi ) {
        int m = ( lo + hi ) / 2;
        if ( offset < sources.array[ m ].start ) {
-#if DEBUG
-           fprintf( stderr, "  offset < [%d].start: %ld\n",
-                    m, sources.array[ m ].start );
-#endif
            hi = m;
        } else {
-#if DEBUG
-           fprintf( stderr, "  offset >= [%d].start: %ld\n",
-                    m, sources.array[ m ].start );
-#endif
            lo = m;
        }
     }
-#if DEBUG
-    fprintf( stderr, "found %d\n", lo );
-#endif
     return lo;
 }
 
@@ -505,32 +511,27 @@ static int overlay_merge(char *buf,off_t beg,off_t end) {
 
 // Read <size> bytes from <offset> in file
 static int fusefile_read(const char *path, char *buf, size_t size,
-                        off_t off, struct fuse_file_info *fi)
+                        off_t off, struct fuse_file_info *ffi)
 {
-#if DEBUG
-    fprintf( stderr, "fusefile_read( %s )\n", path );
-#endif
     if( strcmp( path, "/" ) != 0 ) {
        return -ENOENT;
     }
-#if DEBUG
-    fprintf( stderr, "read %ld %ld\n", off, size );
-#endif
     size_t rr = 0; // total reading
-    while ( size > 0 ) {
 #if DEBUG
-       fprintf( stderr, "  find_source %ld %ld\n", off, size );
+    fprintf( stderr, "fusefile_read %ld + %ld\n", off, size );
 #endif
+    while ( size > 0 ) {
        int i = find_source( off );
        if ( i < 0 ) {
            return ( off == sources.size )? rr : -ENOENT;
        }
+#if DEBUG
+       fprintf( stderr, " item: %d ", i );
+       print_source(& sources.array[i] );
+#endif
        if ( sources.array[i].fd < 0 ) {
            return -ENOENT;
        }
-#if DEBUG
-       print_source( &sources.array[i] );
-#endif
        times.atime = time( 0 );
        size_t b = off - sources.array[i].start + sources.array[i].from;
        size_t n = sources.array[i].to - b;
@@ -541,20 +542,13 @@ static int fusefile_read(const char *path, char *buf, size_t size,
            fsync( sources.array[i].fd );
            sources.array[i].dirty = 0;
        }
-#if DEBUG
-       fprintf( stderr, "  seek fd=%d to %ld\n", sources.array[i].fd, b );
-#endif
        if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) {
            perror( sources.array[i].filename );
            return -ENOENT;
        }
-#if DEBUG
-       fprintf( stderr, "  now read %ld from fd=%d\n",
-                n, sources.array[i].fd );
-#endif
        ssize_t r = read( sources.array[i].fd, buf + rr, n );
 #if DEBUG
-       fprintf( stderr, "  got %ld bytes\n", r );
+       fprintf( stderr, " got: %ld bytes of %ld at %ld\n", r, n, rr );
 #endif
        if ( r < 0 ) {
            perror( sources.array[i].filename );
@@ -586,8 +580,8 @@ static int fusefile_read(const char *path, char *buf, size_t size,
 /**
  * Poll for IO readiness.
  */
-int fusefile_poll(const char *path, struct fuse_file_info *fi,
-                  struct fuse_pollhandle *ph, unsigned *reventsp )
+int fusefile_poll(const char *path, struct fuse_file_info *ffi,
+                 struct fuse_pollhandle *ph, unsigned *reventsp )
 {
 #if DEBUG
     fprintf( stderr, "fusefile_poll( %s ) %p %d\n", path, ph, *reventsp );
@@ -626,6 +620,145 @@ static void overlay_load() {
     }
 }
 
+/**
+ * Inject an overlay fragment into the source table, end return the
+ * end of the injected fragment.
+ */
+static off_t overlay_inject_from_region(off_t beg,off_t end) {
+    int index = find_source( beg );
+    if ( index < 0 ) {
+       fprintf( stderr, "** Injecting %s at %ld failed\n",
+                overlay.source.filename, beg );
+       usage();
+    }
+    if ( end > ENDSOURCE( sources.array[ index ] ) ) {
+       end = ENDSOURCE( sources.array[ index ] );
+    }
+    struct Region frags[3] = {
+       { sources.array[ index ].start, beg },
+       { beg, end },
+       { end, ENDSOURCE( sources.array[ index ] ) } };
+#if DEBUG
+    int i;
+    for ( i = 0; i < 3; i++ ) {
+       fprintf( stderr, "frags[%d] = (%ld, %ld)\n",
+                i, frags[i].beg, frags[i].end );
+    }
+#endif
+    ssize_t size = frags[0].end - frags[0].beg;
+    if ( size ) {
+       // Handle any portion before injection point.
+       dup_source_item( index );
+       off_t point = sources.array[ index ].from + size;
+       sources.array[ index ].to = point;
+#if DEBUG
+       fprintf( stderr, "item %d ", index );
+       print_source( &sources.array[ index ] );
+#endif
+       // Adjust item after injection point
+       index++;
+       sources.array[ index ].start = beg;
+       sources.array[ index ].from = point;
+#if DEBUG
+       fprintf( stderr, "item %d adjust ", index );
+       print_source( &sources.array[ index ] );
+#endif
+    }
+    size = frags[2].end        - frags[2].beg;
+    if ( size ) {
+       // Handle any remaining portion following injection fragment
+       dup_source_item( index );
+       sources.array[ index+1 ].start = frags[2].beg;
+       sources.array[ index+1 ].from += frags[1].end - frags[1].beg;
+#if DEBUG
+       fprintf( stderr, "item %d ", index+1 );
+       print_source( &sources.array[ index+1 ] );
+#endif
+    }
+    // Set up the injection fragment
+    sources.array[ index ].filename = overlay.source.filename;
+    sources.array[ index ].from = beg;
+    sources.array[ index ].to = end;
+    sources.array[ index ].fd = overlay.source.fd;
+#if DEBUG
+       fprintf( stderr, "item %d ", index );
+       print_source( &sources.array[ index ] );
+#endif
+    return end;
+}
+
+/**
+ * Inject the current (unopened) overlay into the source list.
+ */
+static void overlay_inject() {
+    overlay.source.fd = open( overlay.source.filename, O_RDONLY );
+    if ( overlay.source.fd < 0 ) {
+       perror( overlay.source.filename );
+       usage();
+    }
+    
+    if ( lseek( overlay.source.fd, overlay.source.to, SEEK_SET ) < 0 ) {
+       perror( overlay.source.filename );
+       usage();
+    }
+    size_t count = 0;
+    size_t size = sizeof( overlay.count );
+    size_t n;
+    if ( ( n = read( overlay.source.fd, &count, size ) ) != size ) {
+       if ( n != 0 ) {
+           fprintf( stderr, "** error injecting %s\n",
+                    overlay.source.filename );
+           usage();
+       }
+       fprintf( stderr, "** ignoring empty %s\n", overlay.source.filename );
+    }
+    if ( count == 0 ) {
+       close( overlay.source.fd );
+       return;
+    }
+    size = count * sizeof( struct Region );
+    overlay.table = calloc( sizeof( struct Region ), count );
+    if ( read( overlay.source.fd, overlay.table, size ) != size ) {
+       fprintf( stderr, "** error injecting %s\n", overlay.source.filename );
+       usage();
+    }
+    size_t i;
+    for ( i = 0; i < count; i++ ) {
+       off_t beg = overlay.table[i].beg;
+       while ( beg < overlay.table[i].end ) {
+#if DEBUG
+           fprintf( stderr, "inject [%ld,%ld] ", beg, overlay.table[i].end );
+           print_source( &overlay.source );
+#endif
+           
+           beg = overlay_inject_from_region( beg, overlay.table[i].end );
+       }
+    }
+    free( overlay.table );
+    overlay.table = 0;
+}
+
+/**
+ * Each stacked overlay file is nested into to source list
+ */
+static void overlay_post_setup() {
+    char *end;
+    while ( ( end = strchr( overlay.source.filename, ':' ) ) ) {
+       *end = 0; //
+       overlay_inject();
+       overlay.source.filename = end + 1;
+    }
+    if ( *overlay.source.filename ) {
+       overlay.source.fd = open( overlay.source.filename,
+                                 O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
+       if ( overlay.source.fd < 0 ) {
+           perror( overlay.source.filename );
+           usage();
+       }
+       overlay_load();
+    }
+}
+
 /**
  * Write a full block of data over the sources at the offset
  */
@@ -669,14 +802,13 @@ static int write_block(off_t off,const char *buf,size_t size) {
 }
 
 static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
-                             off_t off, struct fuse_file_info *fi) {
+                             off_t off, struct fuse_file_info *ffi) {
 #if DEBUG
     fprintf( stderr, "fusefile_write_buf( %s )\n", path );
 #endif
     if ( strcmp( path, "/" ) != 0 ) {
        return -ENOENT;
     }
-
     size_t size = 0;
     int i;
     for ( i = 0; i < buf->count; i++ ) {
@@ -702,7 +834,7 @@ static int fusefile_write_buf(const char *path, struct fuse_bufvec *buf,
  * Write a fragment at <off>. This overwrites files.
  */
 static int fusefile_write(const char *path, const char *buf, size_t size,
-                         off_t off, struct fuse_file_info *fi)
+                         off_t off, struct fuse_file_info *ffi)
 {
 #if DEBUG
     fprintf( stderr, "fusefile_write( %s %ld )\n", path, size );
@@ -717,6 +849,40 @@ static int fusefile_write(const char *path, const char *buf, size_t size,
     return size;
 }
 
+#define PUSHBUF 104857600
+/**
+ * Write data from overlay to source.
+ */
+static void push_oly(off_t beg, off_t end) {
+    static char * buffer = 0;
+    // Pretend that there isn't an overlay
+    char *filename = overlay.source.filename;
+    if ( buffer == 0 ) {
+       buffer = malloc( PUSHBUF );
+       if ( buffer == 0 ) {
+           fprintf( stderr, "** OOM!!\n" );
+           exit( 1 );
+       }
+    }
+    overlay.source.filename = 0;
+    while ( beg < end ) {
+       off_t size = end - beg;
+       if ( size > PUSHBUF ) {
+           size = PUSHBUF;
+       }
+       if ( lseek( overlay.source.fd, beg, SEEK_SET ) < 0 ) {
+           fprintf( stderr, "** Cannot seek overlay at %ld\n", beg );
+           break;
+       }
+       size = read( overlay.source.fd, buffer, size );
+       if ( write_block( beg, buffer, size ) < 0 ) {
+           fprintf( stderr, "** Cannot push %ld bytes at %ld\n", size, beg );
+       }
+       beg += size;
+    }
+    overlay.source.filename = filename;
+}
+
 static void fusefile_destroy(void *data) {
     char *mnt = (char*) data; // As passed to fuse_main
 #if DEBUG
@@ -741,7 +907,7 @@ static void fsync_all_dirty() {
     }
 }
 
-static int fusefile_flush(const char *path, struct fuse_file_info *info) {
+static int fusefile_flush(const char *path, struct fuse_file_info *ffi) {
 #if DEBUG
     fprintf( stderr, "fusefile_flush( %s )\n", path );
 #endif
@@ -752,7 +918,7 @@ static int fusefile_flush(const char *path, struct fuse_file_info *info) {
     return 0;
 }
 
-static int fusefile_release(const char *path, struct fuse_file_info *fi) {
+static int fusefile_release(const char *path, struct fuse_file_info *ffi) {
 #if DEBUG
     fprintf( stderr, "fusefile_release( %s, %d )\n", path, fi->flags );
 #endif
@@ -762,7 +928,8 @@ static int fusefile_release(const char *path, struct fuse_file_info *fi) {
     return 0;
 }
 
-static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
+static int fusefile_fsync(const char *path, int x,
+                         struct fuse_file_info *ffi) {
 #if DEBUG
     fprintf( stderr, "fusefile_fsync( %s, %d )\n", path, x );
 #endif
@@ -776,7 +943,8 @@ static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) {
 /**
  * 
  */
-static int fusefile_truncate(const char *path, off_t len) {
+static int fusefile_truncate(const char *path, off_t len,
+                            struct fuse_file_info *ffi) {
 #if DEBUG
     fprintf( stderr, "fusefile_truncate( %s, %ld )\n", path, len );
 #endif
@@ -786,25 +954,22 @@ static int fusefile_truncate(const char *path, off_t len) {
     return -EIO;
 }
 
-void *fusefile_init(struct fuse_conn_info *fci) {
+void *fusefile_init(struct fuse_conn_info *fci, struct fuse_config *fc) {
 #if DEBUG
     fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
 #endif
-    // Disable asynchronous reading
-    fci->async_read = 0;
-    fci->want &= ~FUSE_CAP_ASYNC_READ;
+    // Disable asynchronous operations, both reading and direct I/O
+    fci->want &= ~ ( FUSE_CAP_ASYNC_READ | FUSE_CAP_ASYNC_DIO );
 #if DEBUG
     fprintf( stderr, "fusefile_init( %d, %d )\n", fci->async_read, fci->want );
 #endif
     return 0;
 }
 
-#define ENDSOURCE( S ) ( S.start + ( S.to - S.from ) )
-
 /**
  * Dump the current fragmentation to stdout.
  */
-static int dump_fragments() {
+static int dump_fragments(int push) {
     int oly = 0;
     int src = 0;
     size_t pos = 0;
@@ -814,25 +979,37 @@ static int dump_fragments() {
        for ( ; src < sources.count && 
                  ENDSOURCE( sources.array[ src ] ) <= x; src++ ) {
            // Dump sources.array[src] in full
-           fprintf( stdout, "%s/%ld:%ld\n",
-                    sources.array[ src ].filename,
-                    pos - sources.array[ src ].start,
-                    sources.array[ src ].to );
+           if ( !push ) {
+               fprintf( stdout, "%s/%ld:%ld\n",
+                        sources.array[ src ].filename,
+                        pos - sources.array[ src ].start +
+                        sources.array[ src ].from,
+                        sources.array[ src ].to );
+           }
            pos = ENDSOURCE( sources.array[ src ] );
        }
        if ( ( src < sources.count ) && ( sources.array[ src ].start < x ) ) {
            // Dump sources.array[src] up to x;
-           fprintf( stdout, "%s/%ld:%ld\n",
-                    sources.array[ src ].filename,
-                    pos - sources.array[ src ].start,
-                    x - sources.array[ src ].start );
+           if ( !push ) {
+               fprintf( stdout, "%s/%ld:%ld\n",
+                        sources.array[ src ].filename,
+                        sources.array[ src ].from +
+                        pos - sources.array[ src ].start,
+                        x - sources.array[ src ].start +
+                        sources.array[ src ].from
+                        );
+           }
            pos = ENDSOURCE( sources.array[ src ] );
        }
        if ( oly < overlay.count ) {
-           fprintf( stdout, "%s/%ld:%ld\n",
-                    overlay.source.filename,
-                    overlay.table[ oly ].beg,
-                    overlay.table[ oly ].end );
+           if ( !push ) {
+               fprintf( stdout, "%s/%ld:%ld\n",
+                        overlay.source.filename,
+                        overlay.table[ oly ].beg,
+                        overlay.table[ oly ].end );
+           } else {
+               push_oly( overlay.table[ oly ].beg, overlay.table[ oly ].end );
+           }
            pos = overlay.table[ oly++ ].end;
        }
        for ( ; src < sources.count &&
@@ -859,7 +1036,6 @@ static struct fuse_operations fusefile_oper = {
     .fsync = fusefile_fsync,
     // NYI .ftruncate = fusefile_ftruncate,
     .truncate = fusefile_truncate,
-    //.truncate = fusefile_truncate,
     //.release = fusefile_release,
     .init = fusefile_init,
 };
@@ -881,6 +1057,7 @@ static int setup_argv(int argc,char ***argv) {
     // note: (*argv)[ argc ] is the mount point argument
     char *OURS[] = {
        "-odefault_permissions",
+       //"-s", // Forced single-threading
        (*argv)[ argc ]
     };
 #define OURSN ( sizeof( OURS ) / sizeof( char* ) )
@@ -909,8 +1086,6 @@ static int setup_argv(int argc,char ***argv) {
 int main(int argc, char *argv[])
 {
     char *mnt;
-    int mt;
-    int fg;
     int i;
     int fuseargc;
     struct stat stbuf;
@@ -926,11 +1101,10 @@ int main(int argc, char *argv[])
     }
     fuseargc = i;
     mnt = argv[ i++ ]; // First non-option argument is the mount pount
-    char *overlaytag = "-overlay:";
-    int overlaytagsize = strlen( overlaytag );
-    if ( strncmp( argv[i], overlaytag, overlaytagsize ) == 0 ) {
-       // consume "-overlay:filename"
-       setup_overlay( argv[i++] + overlaytagsize ); // Need a writable file
+    #define OVERLAYTAG "-overlay:"
+    if ( strncmp( argv[i], OVERLAYTAG, strlen( OVERLAYTAG ) ) == 0 ) {
+       // consume "-overlay:filename[,filename]*"
+       overlay_setup( argv[i++] + strlen( OVERLAYTAG ) );
        if ( i >= argc ) {
            usage();
        }
@@ -940,7 +1114,7 @@ int main(int argc, char *argv[])
     }
     if ( overlay.source.filename ) {
        overlay.source.to = sources.size; // Register total size.
-       overlay_load();
+       overlay_post_setup();
     }
     if ( stat( mnt, &stbuf ) == -1 ) {
        int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR );
@@ -975,16 +1149,17 @@ int main(int argc, char *argv[])
     }
     fuseargc = setup_argv( fuseargc, &argv );
     if ( strcmp( "-dump", argv[ 1 ] ) == 0 ) {
-       return dump_fragments();
+       return dump_fragments( 0 );
+    }
+    if ( strcmp( "-push", argv[ 1 ] ) == 0 ) {
+       return dump_fragments( 1 );
     }
     struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv );
-    if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {
+    struct fuse_cmdline_opts opts = { 0 };
+    if ( fuse_parse_cmdline( &args, &opts ) ) {
        return 1;
     }
     fuse_opt_free_args( &args );
-    if ( ! mnt ) {
-       fprintf( stderr, "missing mountpoint parameter\n" );
-       return 1;
-    }
-    return fuse_main( fuseargc, argv, &fusefile_oper, temporary? mnt : NULL );
+    return fuse_main( fuseargc, argv, &fusefile_oper,
+                     temporary? opts.mountpoint : NULL );
 }