test of asynchronous write/read
[rrq/fusefile.git] / fusefile.c
index e05b90ebdf4c8a3de0c45d56a562dc71f4031fc4..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>
@@ -290,8 +290,9 @@ static void dup_source_item(int index) {
        fprintf( stderr, "** OOM when expanding frament table\n" );
        usage();
     }
-    memcpy( &sources.array[ index+1 ], &sources.array[ index ],
-           sizeof( struct Source ) );
+    // 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
@@ -328,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; 
     }
@@ -414,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
@@ -436,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;
@@ -462,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;
 }
 
@@ -520,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;
@@ -556,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 );
@@ -601,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 );
@@ -657,29 +636,54 @@ static off_t overlay_inject_from_region(off_t beg,off_t end) {
     }
     struct Region frags[3] = {
        { sources.array[ index ].start, beg },
-       { beg, ENDSOURCE( sources.array[ index ] ) },
-       { ENDSOURCE( sources.array[ index ] ), end } };
+       { 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 ) {
-       // "Duplicate" the indexed source data, copying the filename
+       // Handle any portion before injection point.
        dup_source_item( index );
-       sources.array[ index ].to = sources.array[ index ].from + size;
+       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 = sources.array[ index-1 ].to;
+       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 = sources.array[ index+1 ].to -size;
+       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
     }
-    // Replace the [index] fragment
+    // Set up the injection fragment
     sources.array[ index ].filename = overlay.source.filename;
-    sources.array[ index ].start = beg;
     sources.array[ index ].from = beg;
     sources.array[ index ].to = end;
-    sources.array[ index ].fd = overlay.source.fd; //?
+    sources.array[ index ].fd = overlay.source.fd;
+#if DEBUG
+       fprintf( stderr, "item %d ", index );
+       print_source( &sources.array[ index ] );
+#endif
     return end;
 }
 
@@ -699,9 +703,14 @@ static void overlay_inject() {
     }
     size_t count = 0;
     size_t size = sizeof( overlay.count );
-    if ( read( overlay.source.fd, &count, size ) != size ) {
-       fprintf( stderr, "** error injecting %s\n", overlay.source.filename );
-       usage();
+    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 );
@@ -717,12 +726,16 @@ static void overlay_inject() {
     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;
-    close( overlay.source.fd );
 }
 
 /**
@@ -731,7 +744,7 @@ static void overlay_inject() {
 static void overlay_post_setup() {
     char *end;
     while ( ( end = strchr( overlay.source.filename, ':' ) ) ) {
-       *end = 0; //  
+       *end = 0; //
        overlay_inject();
        overlay.source.filename = end + 1;
     }
@@ -789,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++ ) {
@@ -822,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 );
@@ -837,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
@@ -861,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
@@ -872,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
@@ -882,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
@@ -896,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
@@ -906,13 +954,12 @@ 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
@@ -922,7 +969,7 @@ void *fusefile_init(struct fuse_conn_info *fci) {
 /**
  * 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;
@@ -932,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 &&
@@ -977,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,
 };
@@ -999,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* ) )
@@ -1027,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;
@@ -1044,10 +1101,10 @@ int main(int argc, char *argv[])
     }
     fuseargc = i;
     mnt = argv[ i++ ]; // First non-option argument is the mount pount
-    if ( strcmp( argv[i], "-overlay:" ) == 0 ) {
+    #define OVERLAYTAG "-overlay:"
+    if ( strncmp( argv[i], OVERLAYTAG, strlen( OVERLAYTAG ) ) == 0 ) {
        // consume "-overlay:filename[,filename]*"
-       // Verify file access; to overlay must be writable.
-       overlay_setup( argv[i++] + strlen( "-overlay:" ) );
+       overlay_setup( argv[i++] + strlen( OVERLAYTAG ) );
        if ( i >= argc ) {
            usage();
        }
@@ -1092,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 );
 }