X-Git-Url: https://git.rrq.au/?a=blobdiff_plain;f=fusefile.c;h=01f4c58400f0d4a2197797ddebb6a415e27b808a;hb=74baf758e5465bb0675fb7a8e134e29467cfa7b9;hp=9c4b9648d226d11fb6a42c43e56ab5ac78447079;hpb=7059a8375f259a9d739fe6a642d0162ee83c30c8;p=rrq%2Ffusefile.git diff --git a/fusefile.c b/fusefile.c index 9c4b964..01f4c58 100644 --- a/fusefile.c +++ b/fusefile.c @@ -34,12 +34,18 @@ #include #include +struct Region { + off_t beg; + off_t end; +}; + struct Source { char *filename; ssize_t from; ssize_t to; ssize_t start; // starting position in concatenated file int fd; + int dirty; }; static struct { @@ -53,7 +59,224 @@ static struct { time_t mtime; time_t ctime; } times; - + +/** + * Overlay + */ +static struct { + struct Source source; + struct Region *table; + size_t count; + size_t limit; +} overlay; + +static void usage(); + +/** + * Find the nearest overlay.table region below pos. Returns the index, + * or -1 if there is none, i.e. pos < overlay.table[0]. + */ +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 overlay.table[m].beg <= pos? m : -1; + } + if ( overlay.table[m].beg <= pos ) { + lo = m; + } else { + hi = m; + } + } + return -1; +} + +/** + * Save the entry count for overlay.table as 64-bit integer + * immediately following the overlay content at the index + * corresponding to the fused file size. + */ +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; + } +} + +/** + * Update the on-disk cache of overlay.table between the given + * indexes. The table is laid out immediately following the table + * count with each region saved as two 64-bit unsigned integers. + */ +static void overlay_save_table(size_t lo,size_t hi) { + char *p = (char *) &overlay.table[ 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; + } +} + +/** + * Insert a new region at index p, with previous portion [p,count] + * moved up to make space. + */ +static void overlay_insert(size_t p,off_t beg,off_t end) { + size_t bytes; + // Grow the table if needed + 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( (char*) &overlay.table[ p+1 ], + (char*) &overlay.table[ p ], + bytes ); + } + overlay.table[ p ].beg = beg; + overlay.table[ p ].end = end; + overlay_save_count(); +} + +/** + * Delete the region entry at p by moving the portion [p+1,count] + * down. + */ +static void overlay_delete(size_t p) { + size_t bytes = ( --overlay.count - p ) * sizeof( struct Region ); + if ( bytes ) { + memmove( (char*) &overlay.table[ p ], + (char*) &overlay.table[ p+1 ], + bytes ); + } +} + +/** + * Mark the given region as updated, i.e. written to the overlay. The + * mark region may attach to prior marked regions ro be a new, + * separate region. If attaching, it causes the prior regions to + * expand and the table adjusted by deleting any regions that become + * fully contained in other regions. + */ +static void overlay_mark(off_t beg,off_t end) { +#if DEBUG + fprintf( stderr, "overlay_mark( %ld, %ld )\n", beg, end ); +#endif + int deleted = 0; + ssize_t q; + ssize_t p = overlay_prior_fragment( beg ); + // p is the nearest region below or at beg (or -1) + if ( p >= 0 && beg <= overlay.table[p].end ) { + // p overlaps mark region + if ( end <= overlay.table[p].end ) { + // region p covers mark region already +#if DEBUG + fprintf( stderr, "overlay covering ( %ld %ld )\n", + overlay.table[p].beg, overlay.table[p].end ); +#endif + return; + } + // the new mark region extends region p + overlay.table[p].end = end; + q = p+1; + while ( q < overlay.count && + overlay.table[q].beg <= overlay.table[p].end ) { + // Extended region merges with subsequent region + if ( overlay.table[p].end < overlay.table[q].end ) { + overlay.table[p].end = overlay.table[q].end; + } + overlay_delete( q ); + deleted++; + } + if ( deleted ) { + overlay_save_count(); + q = overlay.count; + } + overlay_save_table( p, q ); +#if DEBUG + fprintf( stderr, "overlay expand ( %ld %ld ) deleted %d\n", + overlay.table[p].beg, overlay.table[p].end, deleted ); +#endif + return; + } + // The prior region p does not expand into new mark region + p++; // subsequent region + if ( p >= overlay.count || end < overlay.table[p].beg ) { + // New mark region is a separate region at p + overlay_insert( p, beg, end ); +#if DEBUG + fprintf( stderr, "overlay new ( %ld %ld )\n", + overlay.table[p].beg, overlay.table[p].end ); +#endif + overlay_save_table( p, overlay.count ); + return; + } + // New marks start before and overlap with region p => change p + // and handle any subsequent regions being covered + overlay.table[p].beg = beg; + q = p+1; + if ( overlay.table[p].end < end ) { + overlay.table[p].end = end; + while ( q < overlay.count && + overlay.table[q].beg <= overlay.table[p].end ) { + if ( overlay.table[p].end < overlay.table[q].end ) { + overlay.table[p].end = overlay.table[q].end; + } + overlay_delete( q ); + deleted++; + } + if ( deleted ) { + overlay_save_count(); + q = overlay.count; + } + } + overlay_save_table( p, q ); +#if DEBUG + fprintf( stderr, "overlay before ( %ld %ld ) deleted %d\n", + overlay.table[p].beg, overlay.table[p].end, 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) { fprintf( stderr, "%p { %s, %ld, %ld, %ld, %d }\n", @@ -117,10 +340,10 @@ static int setup_sources(char **argv,int i,int n) { } else if ( RANGE( sscanf( range, "%d:%n", &a, &c ), 1 )) { p->from = ( a < 0 )? ( p->to + a ) : a; } else if ( RANGE( sscanf( range, "%d%n", &a, &c ), 1 )) { - if ( a < 0 ) { - p->from = p->to + a; + if ( a >= 0 ) { + p->from = a; } else { - p->to = p->from + a; + p->from = p->to + a; } } else if ( RANGE( sscanf( range, ":%n", &c), 0 ) ) { // to end from start @@ -129,8 +352,16 @@ static int setup_sources(char **argv,int i,int n) { return 1; } } - if ( p->from >= p->to || - p->from >= filestat.st_size || p->to > filestat.st_size ) { + if ( ( filestat.st_mode & S_IFMT ) == S_IFCHR ) { + filestat.st_size = p->to; // Pretend size of character device + } + if ( p->from < 0 ) { + p->from = 0; + } + if ( p->to > filestat.st_size ) { + p->to = filestat.st_size; + } + if ( p->from >= p->to || p->from >= filestat.st_size ) { fprintf( stderr, "** BAD RANGE: %s [%ld:%ld]\n", argv[i], p->from, p->to ); return 1; @@ -192,17 +423,62 @@ 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; } +static int overlay_merge(char *buf,off_t beg,off_t end) { +#if DEBUG + fprintf( stderr, "merge %ld %ld\n", beg, end ); +#endif + // Find nearest overlay data before or at beg + ssize_t p = overlay_prior_fragment( beg ); + if ( p < 0 ) { + p = 0; + } + for ( ; p < overlay.count && overlay.table[p].beg < end; p++ ) { + if ( overlay.table[p].end < beg ) { + continue; + } + if ( overlay.table[p].beg > beg ) { + size_t delta = overlay.table[p].beg - beg; + buf += delta; + beg += delta; + } + size_t size = ( overlay.table[p].end <= end )? + ( overlay.table[p].end - beg ) : ( end - beg ); + lseek( overlay.source.fd, beg, SEEK_SET ); + while ( size > 0 ) { + size_t n = read( overlay.source.fd, buf, size ); + size -= n; + buf += n; + beg += n; // + } + } + return 0; +} + // Read bytes from in file static int fusefile_read(const char *path, char *buf, size_t size, off_t off, struct fuse_file_info *fi) @@ -216,10 +492,10 @@ static int fusefile_read(const char *path, char *buf, size_t size, #if DEBUG fprintf( stderr, "read %ld %ld\n", off, size ); #endif - size_t rr = 0; + size_t rr = 0; // total reading while ( size > 0 ) { #if DEBUG - fprintf( stderr, "find_source %ld %ld\n", off, size ); + fprintf( stderr, " find_source %ld %ld\n", off, size ); #endif int i = find_source( off ); if ( i < 0 ) { @@ -237,16 +513,24 @@ 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 if ( lseek( sources.array[i].fd, b, SEEK_SET ) < 0 ) { perror( sources.array[i].filename ); return -ENOENT; } #if DEBUG - fprintf( stderr, "get %ld bytes at %ld\n", n, rr ); + 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\n", r ); #endif if ( r < 0 ) { perror( sources.array[i].filename ); @@ -255,10 +539,23 @@ 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, off + rr + r ); + if ( x ) { + return x; + } + } rr += r; off += r; size -= r; } +#if DEBUG + fprintf( stderr, " total reading %ld bytes\n", rr ); +#endif return rr; } @@ -280,6 +577,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.beg, f.end ); +#endif + overlay_mark( f.beg, f.end ); + } +} /** * Write a full block of data over the sources at the offset @@ -288,12 +609,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, 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 = &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 ) { @@ -311,6 +636,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; } @@ -374,6 +703,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 ); @@ -381,6 +724,7 @@ static int fusefile_flush(const char *path, struct fuse_file_info *info) { if ( strcmp( path, "/" ) != 0 ) { return -ENOENT; } + fsync_all_dirty(); return 0; } @@ -401,6 +745,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; } @@ -451,7 +796,7 @@ static struct fuse_operations fusefile_oper = { static void usage() { char *usage = "Usage: fusefile [ ] ... \n" -"Mounts a virtual, read-only file that is a concatenation of file fragments\n" +"Mounts a virtual, file that is a concatenation of file fragments\n" ; fprintf( stderr, "%s", usage ); exit( 1 ); @@ -510,9 +855,22 @@ 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 + if ( i >= argc ) { + usage(); + } + } if ( setup_sources( argv, i, argc-i ) ) { return 1; } + 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 ) { @@ -534,6 +892,16 @@ int main(int argc, char *argv[]) times.ctime = stbuf.st_ctime; } + { + int fd = open( mnt, O_RDWR, S_IRUSR | S_IWUSR ); + if ( fd < 0 ) { + perror( mnt ); + return 1; + } + if ( lseek( fd, sources.size, SEEK_SET ) < 0 ) { + return -EIO; + } + } fuseargc = setup_argv( fuseargc, &argv ); struct fuse_args args = FUSE_ARGS_INIT( fuseargc, argv ); if ( fuse_parse_cmdline( &args, &mnt, &mt, &fg ) ) {