X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=fusefile.c;h=30ec7fa284c7ce1f330daaede1de2fb1835ee9b5;hb=6c93ae8d4f1955020adedfa67a43478dba854ab0;hp=8371d1f6c9448d538daa7b4ba1f2fc89feb89676;hpb=a31db9efc8aa6da6cffecf4cff409730e96cbb86;p=rrq%2Ffusefile.git diff --git a/fusefile.c b/fusefile.c index 8371d1f..30ec7fa 100644 --- a/fusefile.c +++ b/fusefile.c @@ -34,12 +34,18 @@ #include #include +struct Region { + off_t pos; + size_t size; +}; + struct Source { char *filename; ssize_t from; ssize_t to; ssize_t start; // starting position in concatenated file int fd; + int dirty; }; static struct { @@ -54,7 +60,180 @@ static struct { time_t ctime; } times; -static struct Source overlay; +/** + * Overlay + */ +static struct { + struct Source source; + struct Region *table; + size_t count; + size_t limit; +} overlay; + +static void usage(); + +#define FRAG(m) (overlay.table+m) +#define BEG(m) (FRAG(m)->pos) +#define END(m) (FRAG(m)->pos + FRAG(m)->size) + +static ssize_t overlay_prior_fragment(off_t pos) { + size_t lo = 0, hi = overlay.count; + while ( lo < hi ) { + size_t m = ( lo + hi ) / 2; + if ( m == lo ) { + return BEG( m ) < pos? m : -1; + } + if ( BEG( m ) <= pos ) { + lo = m; + } else { + hi = m; + } + } + return -1; +} + +static void overlay_save_count() { + lseek( overlay.source.fd, overlay.source.to, SEEK_SET ); + size_t size = sizeof( overlay.count ); + char *p = (char *) &overlay.count ; + while ( size > 0 ) { + size_t n = write( overlay.source.fd, p, size ); + if ( n < 0 ) { + perror( overlay.source.filename ); + exit( 1 ); + } + size -= n; + p += n; + } + if ( overlay.source.dirty++ > 1000 ) { + fsync( overlay.source.fd ); + overlay.source.dirty = 0; + } +} + +static void overlay_save_table(size_t lo,size_t hi) { + char *p = (char *) FRAG(lo); + size_t pos = overlay.source.to + sizeof( overlay.count ) + + lo * sizeof( struct Region ); + size_t size = ( hi - lo ) * sizeof( struct Region ); + if ( pos != lseek( overlay.source.fd, pos, SEEK_SET ) ) { + fprintf( stderr, "%s: seek error\n", overlay.source.filename ); + exit( 1 ); + } + while ( size > 0 ) { + size_t n = write( overlay.source.fd, p, size ); + if ( n < 0 ) { + perror( overlay.source.filename ); + exit( 1 ); + } + size -= n; + p += n; + } + if ( overlay.source.dirty++ > 1000 ) { + fsync( overlay.source.fd ); + overlay.source.dirty = 0; + } +} + +static void overlay_insert(size_t p,off_t pos,size_t size) { + size_t bytes; + if ( overlay.count >= overlay.limit ) { + overlay.limit = overlay.count + 10; + bytes = overlay.limit * sizeof( struct Region ); + overlay.table = overlay.table? + realloc( overlay.table, bytes ) : malloc( bytes ); + } + bytes = ( overlay.count++ - p ) * sizeof( struct Region ); + if ( bytes ) { + memmove( FRAG( p+1 ), FRAG( p ), bytes ); + } + FRAG( p )->pos = pos; + FRAG( p )->size = size; + overlay_save_count(); +} + +static void overlay_delete(size_t p) { + if ( p < --overlay.count ) { + size_t size = ( overlay.count - p ) * sizeof( struct Region ); + memmove( FRAG(p), FRAG(p+1), size ); + } + overlay_save_count(); +} + +static void overlay_mark(off_t pos,size_t size) { +#if DEBUG + fprintf( stderr, "overlay_mark( %ld, %ld )\n", pos, size ); +#endif + int deleted = 0; + ssize_t q; + ssize_t p = overlay_prior_fragment( pos ); + // p is the nearest region below pos (or -1) + if ( p >= 0 && pos <= END(p) ) { + // p overlaps mark region + if ( END(p) >= pos + size ) { +#if DEBUG + fprintf( stderr, "overlay size 1( %ld )\n", FRAG(p)->size ); +#endif + return; // new mark within existing. + } + // new mark region extends existing + FRAG(p)->size = pos + size - BEG(p); + q = p+1; + while ( q < overlay.count && BEG(q) <= END(p) ) { + if ( END(q) > END(p) ) { + FRAG(p)->size = END(q) - BEG(p); + } + overlay_delete( q ); + deleted++; + } + overlay_save_table( p, deleted? overlay.count : q ); +#if DEBUG + fprintf( stderr, "overlay size 2( %ld ) deleted %d\n", + FRAG(p)->size, deleted ); +#endif + return; + } + // The region p does not expand into new mark region + p++; // subsequent region + if ( p >= overlay.count || BEG(p) > pos + size ) { + // New mark is separate region at p + overlay_insert( p, pos, size); +#if DEBUG + fprintf( stderr, "overlay size 4( %ld )\n", FRAG(p)->size ); +#endif + overlay_save_table( p, overlay.count ); + return; + } + // New marks start before and overlap with the region + if ( BEG(p) + FRAG(p)->size < pos + size ) { + FRAG(p)->size = size; // new mark covers old region + } else { + FRAG(p)->size += BEG(p) - pos; + } + BEG(p) = pos; + q = p+1; + while ( q < overlay.count && BEG(q) <= END(p) ) { + if ( END(q) > END(p) ) { + FRAG(p)->size = END(q) - BEG(p); + } + overlay_delete( q ); + deleted++; + } + overlay_save_table( p, deleted? overlay.count : q ); +#if DEBUG + fprintf( stderr, "overlay size 4( %ld ) deleted %d\n", + FRAG(p)->size, deleted ); +#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 ); + usage(); + } +} #if DEBUG static void print_source(struct Source *p) { @@ -69,17 +248,6 @@ static int RANGE(int s,int n ) { return ( s == n ) && *(range+c) == 0; } -static void usage(); - -static void setup_overlay(char *filename) { - overlay.filename = filename; - overlay.fd = open( filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR ); - if ( overlay.fd < 0 ) { - perror( filename ); - usage(); - } -} - static int setup_sources(char **argv,int i,int n) { sources.array = calloc( n, sizeof( struct Source ) ); if ( sources.array == 0 ) { @@ -238,45 +406,38 @@ static int find_source(off_t offset) { return lo; } -#define OBUFSZ 1048576 static int overlay_merge(char *buf,off_t off,size_t size) { - static char obuf[ OBUFSZ ]; #if DEBUG fprintf( stderr, "merge %ld %ld\n", off, size ); #endif - while ( size > 0 ) { - size_t n = size < OBUFSZ? size : OBUFSZ; - off_t ox = lseek( overlay.fd, off, SEEK_SET ); -#if DEBUG - fprintf( stderr, " seek %ld %ld %ld\n", off, ox, n ); -#endif - if ( ox < 0 ) { - perror( overlay.filename ); - return -ENOENT; - } - if ( ox < off ) { - break; + // Find nearest overlay data before or at off + ssize_t p = overlay_prior_fragment( off ); + if ( p < 0 ) { + p = 0; + } + for ( ; p < overlay.count && BEG(p) < off+size; p++ ) { + if ( END(p) < off ) { + continue; } - n = read( overlay.fd, obuf, n ); -#if DEBUG - fprintf( stderr, " got %ld\n", n ); -#endif - if ( n < 0 ) { - perror( overlay.filename ); - return -ENOENT; + size_t delta = FRAG(p)->size; + if ( BEG(p) < off ) { + delta -= off - BEG(p); + } else { + size_t skip = BEG(p) - off; + off += skip; + size -= skip; + buf += skip; } - if ( n == 0 ) { - break; + if ( delta > size ) { + delta = size; } - char *p = obuf; - while ( n-- > 0 ) { - if ( *p ) { - *buf = *p; - } - p++; - buf++; - size--; - off++; + lseek( overlay.source.fd, off, SEEK_SET ); + while ( delta > 0 ) { + size_t n = read( overlay.source.fd, buf, delta ); + off += n; + size -= n; + delta -= n; + buf += n; } } #if DEBUG @@ -319,6 +480,10 @@ static int fusefile_read(const char *path, char *buf, size_t size, if ( n > size ) { n = size; } + if ( sources.array[i].dirty ) { + 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 @@ -331,12 +496,6 @@ static int fusefile_read(const char *path, char *buf, size_t size, n, sources.array[i].fd ); #endif ssize_t r = read( sources.array[i].fd, buf + rr, n ); - if ( overlay.filename ) { - int x = overlay_merge( buf + rr, off + rr, r ); - if ( x ) { - return x; - } - } #if DEBUG fprintf( stderr, " got %ld bytes\n", r ); #endif @@ -347,6 +506,16 @@ static int fusefile_read(const char *path, char *buf, size_t size, if ( r == 0 ) { break; } + if ( overlay.source.filename ) { + if ( overlay.source.dirty ) { + fsync( overlay.source.fd ); + overlay.source.dirty = 0; + } + int x = overlay_merge( buf + rr, off + rr, r ); + if ( x ) { + return x; + } + } rr += r; off += r; size -= r; @@ -375,6 +544,30 @@ int fusefile_poll(const char *path, struct fuse_file_info *fi, return 0; } +static void overlay_load() { + lseek( overlay.source.fd, overlay.source.to, SEEK_SET ); + size_t x = 0; + size_t size = sizeof( overlay.count ); + if ( read( overlay.source.fd, &x, size ) != size ) { + return; + } +#if DEBUG + fprintf( stderr, "overlay: %s with %ld regions\n", + overlay.source.filename, x ); +#endif + struct Region f = { 0, 0 }; + size = sizeof( struct Region ); + while ( x-- > 0 ) { + if ( read( overlay.source.fd, &f, size ) != size ) { + fprintf( stderr, "%s: bad meta data\n", overlay.source.filename ); + exit( 1 ); + } +#if DEBUG + fprintf( stderr, "overlay region: %ld %ld\n", f.pos, f.size ); +#endif + overlay_mark( f.pos, f.size ); + } +} /** * Write a full block of data over the sources at the offset @@ -383,13 +576,16 @@ static int write_block(off_t off,const char *buf,size_t size) { #if DEBUG fprintf( stderr, "write_block( %ld, ?, %ld )\n", off, size ); #endif + if ( overlay.source.filename ) { + overlay_mark( off, size ); // Mark region as written + } while ( size > 0 ) { int index = find_source( off ); // index of source file if ( index < 0 ) { return -EIO; // past EOF } - struct Source *source = - overlay.filename? &overlay : &sources.array[ index ]; + struct Source *source = overlay.source.filename? + &overlay.source : &sources.array[ index ]; off_t from = off - source->start + source->from; off_t max = source->to - from; if ( lseek( source->fd, from, SEEK_SET ) < 0 ) { @@ -407,6 +603,10 @@ static int write_block(off_t off,const char *buf,size_t size) { size -= n; off += n; } + if ( source->dirty++ >= 1000 ) { + fsync( source->fd ); + source->dirty = 0; + } } return 0; } @@ -470,6 +670,20 @@ static void fusefile_destroy(void *data) { } } +static void fsync_all_dirty() { + int i = 0; + for ( ; i < sources.count; i++ ) { + if ( sources.array[i].dirty ) { + fsync( sources.array[i].fd ); + sources.array[i].dirty = 0; + } + } + if ( overlay.source.filename && overlay.source.dirty ) { + fsync( overlay.source.fd ); + overlay.source.dirty = 0; + } +} + static int fusefile_flush(const char *path, struct fuse_file_info *info) { #if DEBUG fprintf( stderr, "fusefile_flush( %s )\n", path ); @@ -477,6 +691,7 @@ static int fusefile_flush(const char *path, struct fuse_file_info *info) { if ( strcmp( path, "/" ) != 0 ) { return -ENOENT; } + fsync_all_dirty(); return 0; } @@ -497,6 +712,7 @@ static int fusefile_fsync(const char *path, int x, struct fuse_file_info *fi) { if ( strcmp( path, "/" ) != 0 ) { return -ENOENT; } + fsync_all_dirty(); return 0; } @@ -618,7 +834,10 @@ int main(int argc, char *argv[]) if ( setup_sources( argv, i, argc-i ) ) { return 1; } - overlay.to = sources.size; // Register total size. + if ( overlay.source.filename ) { + overlay.source.to = sources.size; // Register total size. + overlay_load(); + } if ( stat( mnt, &stbuf ) == -1 ) { int fd = open( mnt, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR ); if ( fd < 0 ) {