master - bcache: use indirection table for fd
Gitweb: https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=1570e76233398957911c3559b40de6e03c37df9a Commit: 1570e76233398957911c3559b40de6e03c37df9a Parent: 4b07ae55f1a1e7ad33b2fb0067112ed074f30bf4 Author: David Teigland <[email protected]> AuthorDate: Thu Sep 17 09:40:18 2020 -0500 Committer: David Teigland <[email protected]> CommitterDate: Fri Sep 18 15:10:11 2020 -0500
bcache: use indirection table for fd
Add a "device index" (di) for each device, and use this in the bcache api to the rest of lvm. This replaces the file descriptor (fd) in the api. The rest of lvm uses new functions bcache_set_fd(), bcache_clear_fd(), and bcache_change_fd() to control which fd bcache uses for io to a particular device.
. lvm opens a dev and gets and fd.
fd = open(dev);
. lvm passes fd to the bcache layer and gets a di
to use in the bcache api for the dev.
di = bcache_set_fd(fd);
. lvm uses bcache functions, passing di for the dev.
bcache_write_bytes(di, ...), etc.
. bcache translates di to fd to do io.
. lvm closes the device and clears the di/fd bcache state.
close(fd);
bcache_clear_fd(di);
In the bcache layer, a di-to-fd translation table (int *_fd_table) is added. When bcache needs to perform io on a di, it uses _fd_table[di].
In the following commit, lvm will make use of the new bcache_change_fd() function to change the fd that bcache uses for the dev, without dropping cached blocks.
--- lib/device/bcache-utils.c | 64 ++++++------ lib/device/bcache.c | 188 +++++++++++++++++++++++++---------- lib/device/bcache.h | 36 ++++--- lib/device/dev-cache.c | 1 + lib/device/device.h | 1 + lib/label/label.c | 115 +++++++++++---------- lib/metadata/metadata.c | 1 + test/unit/bcache_t.c | 242 ++++++++++++++++++++++----------------------- test/unit/bcache_utils_t.c | 18 ++-- test/unit/io_engine_t.c | 24 +++-- tools/toollib.c | 2 +- 11 files changed, 406 insertions(+), 286 deletions(-)
lib/device/bcache-utils.c +32 -32
@@ -39,32 +39,32 @@ static uint64_t _min(uint64_t lhs, uint64_t rhs)//---------------------------------------------------------------- -void bcache_prefetch_bytes (struct bcache *cache,int fd,uint64_t start,size_t len) +void bcache_prefetch_bytes (struct bcache *cache,int di,uint64_t start,size_t len) {block_address bb, be;byte_range_to_block_range (cache, start, len, &bb, &be);while (bb < be) { -bcache_prefetch (cache, fd, bb); +bcache_prefetch (cache, di, bb); bb++; } }//---------------------------------------------------------------- -bool bcache_read_bytes (struct bcache *cache,int fd,uint64_t start,size_t len,void *data) +bool bcache_read_bytes (struct bcache *cache,int di,uint64_t start,size_t len,void *data) {struct block *b;block_address bb, be;uint64_t block_size =bcache_block_sectors (cache) <<SECTOR_SHIFT ;uint64_t block_offset = start % block_size; -bcache_prefetch_bytes (cache, fd, start, len); +bcache_prefetch_bytes (cache, di, start, len);byte_range_to_block_range (cache, start, len, &bb, &be);for (; bb != be; bb++) { -if (!bcache_get (cache, fd, bb,0 , &b)) +if (!bcache_get (cache, di, bb,0 , &b))return false;size_t blen =_min (block_size - block_offset, len);
@@ -79,7 +79,7 @@ bool bcache_read_bytes(struct bcache *cache, int fd, uint64_t start, size_t len,return true; } -bool bcache_invalidate_bytes (struct bcache *cache,int fd,uint64_t start,size_t len) +bool bcache_invalidate_bytes (struct bcache *cache,int di,uint64_t start,size_t len) {block_address bb, be;bool result = true;
@@ -87,7 +87,7 @@ bool bcache_invalidate_bytes(struct bcache *cache, int fd, uint64_t start, size_byte_range_to_block_range (cache, start, len, &bb, &be);for (; bb != be; bb++) { -if (!bcache_invalidate (cache, fd, bb)) +if (!bcache_invalidate (cache, di, bb)) result = false; }
@@ -101,8 +101,8 @@ bool bcache_invalidate_bytes(struct bcache *cache, int fd, uint64_t start, size_struct updater ; -typedef bool (*partial_update_fn )(struct updater *u,int fd,block_address bb,uint64_t offset,size_t len); -typedef bool (*whole_update_fn )(struct updater *u,int fd,block_address bb,block_address be); +typedef bool (*partial_update_fn )(struct updater *u,int di,block_address bb,uint64_t offset,size_t len); +typedef bool (*whole_update_fn )(struct updater *u,int di,block_address bb,block_address be);struct updater {struct bcache *cache;
@@ -111,7 +111,7 @@ struct updater {void *data; }; -static bool _update_bytes (struct updater *u,int fd,uint64_t start,size_t len) +static bool _update_bytes (struct updater *u,int di,uint64_t start,size_t len) {struct bcache *cache = u->cache;block_address bb, be;
@@ -124,12 +124,12 @@ static bool _update_bytes(struct updater *u, int fd, uint64_t start, size_t len)// If the last block is partial, we will require a read, so let's // prefetch it. if ((start + len) % block_size) -bcache_prefetch (cache, fd, (start + len) / block_size); +bcache_prefetch (cache, di, (start + len) / block_size);// First block may be partial if (block_offset) {size_t blen =_min (block_size - block_offset, len); -if (!u->partial_fn (u, fd, bb, block_offset, blen)) +if (!u->partial_fn (u, di, bb, block_offset, blen))return false; len -= blen;
@@ -141,7 +141,7 @@ static bool _update_bytes(struct updater *u, int fd, uint64_t start, size_t len)// Now we write out a set of whole blocks nr_whole = len / block_size; -if (!u->whole_fn (u, fd, bb, bb + nr_whole)) +if (!u->whole_fn (u, di, bb, bb + nr_whole))return false; bb += nr_whole;
@@ -151,17 +151,17 @@ static bool _update_bytes(struct updater *u, int fd, uint64_t start, size_t len)return true;// Finally we write a partial end block -return u->partial_fn (u, fd, bb,0 , len); +return u->partial_fn (u, di, bb,0 , len); }//---------------------------------------------------------------- -static bool _write_partial (struct updater *u,int fd,block_address bb, +static bool _write_partial (struct updater *u,int di,block_address bb,uint64_t offset,size_t len) {struct block *b; -if (!bcache_get (u->cache, fd, bb,GF_DIRTY , &b)) +if (!bcache_get (u->cache, di, bb,GF_DIRTY , &b))return false;memcpy (((unsigned char *) b->data) + offset, u->data, len);
@@ -171,7 +171,7 @@ static bool _write_partial(struct updater *u, int fd, block_address bb,return true; } -static bool _write_whole (struct updater *u,int fd,block_address bb,block_address be) +static bool _write_whole (struct updater *u,int di,block_address bb,block_address be) {struct block *b;uint64_t block_size =bcache_block_sectors (u->cache) <<SECTOR_SHIFT ;
@@ -179,7 +179,7 @@ static bool _write_whole(struct updater *u, int fd, block_address bb, block_addrfor (; bb != be; bb++) {// We don't need to read the block since we are overwriting // it completely. -if (!bcache_get (u->cache, fd, bb,GF_ZERO , &b)) +if (!bcache_get (u->cache, di, bb,GF_ZERO , &b))return false;memcpy (b->data, u->data, block_size); u->data = ((unsigned char *) u->data) + block_size;
@@ -189,7 +189,7 @@ static bool _write_whole(struct updater *u, int fd, block_address bb, block_addrreturn true; } -bool bcache_write_bytes (struct bcache *cache,int fd,uint64_t start,size_t len,void *data) +bool bcache_write_bytes (struct bcache *cache,int di,uint64_t start,size_t len,void *data) {struct updater u;
@@ -198,16 +198,16 @@ bool bcache_write_bytes(struct bcache *cache, int fd, uint64_t start, size_t len u.whole_fn = _write_whole; u.data = data; -return _update_bytes (&u, fd, start, len); +return _update_bytes (&u, di, start, len); }//---------------------------------------------------------------- -static bool _zero_partial (struct updater *u,int fd,block_address bb,uint64_t offset,size_t len) +static bool _zero_partial (struct updater *u,int di,block_address bb,uint64_t offset,size_t len) {struct block *b; -if (!bcache_get (u->cache, fd, bb,GF_DIRTY , &b)) +if (!bcache_get (u->cache, di, bb,GF_DIRTY , &b))return false;memset (((unsigned char *) b->data) + offset,0 , len);
@@ -216,12 +216,12 @@ static bool _zero_partial(struct updater *u, int fd, block_address bb, uint64_treturn true; } -static bool _zero_whole (struct updater *u,int fd,block_address bb,block_address be) +static bool _zero_whole (struct updater *u,int di,block_address bb,block_address be) {struct block *b;for (; bb != be; bb++) { -if (!bcache_get (u->cache, fd, bb,GF_ZERO , &b)) +if (!bcache_get (u->cache, di, bb,GF_ZERO , &b))return false;bcache_put (b); }
@@ -229,7 +229,7 @@ static bool _zero_whole(struct updater *u, int fd, block_address bb, block_addrereturn true; } -bool bcache_zero_bytes (struct bcache *cache,int fd,uint64_t start,size_t len) +bool bcache_zero_bytes (struct bcache *cache,int di,uint64_t start,size_t len) {struct updater u;
@@ -238,17 +238,17 @@ bool bcache_zero_bytes(struct bcache *cache, int fd, uint64_t start, size_t len) u.whole_fn = _zero_whole; u.data =NULL ; -return _update_bytes (&u, fd, start, len); +return _update_bytes (&u, di, start, len); }//---------------------------------------------------------------- -static bool _set_partial (struct updater *u,int fd,block_address bb,uint64_t offset,size_t len) +static bool _set_partial (struct updater *u,int di,block_address bb,uint64_t offset,size_t len) {struct block *b;uint8_t val = *((uint8_t *) u->data); -if (!bcache_get (u->cache, fd, bb,GF_DIRTY , &b)) +if (!bcache_get (u->cache, di, bb,GF_DIRTY , &b))return false;memset (((unsigned char *) b->data) + offset, val, len);
@@ -257,14 +257,14 @@ static bool _set_partial(struct updater *u, int fd, block_address bb, uint64_t oreturn true; } -static bool _set_whole (struct updater *u,int fd,block_address bb,block_address be) +static bool _set_whole (struct updater *u,int di,block_address bb,block_address be) {struct block *b;uint8_t val = *((uint8_t *) u->data);uint64_t len =bcache_block_sectors (u->cache) *512 ;for (; bb != be; bb++) { -if (!bcache_get (u->cache, fd, bb,GF_ZERO , &b)) +if (!bcache_get (u->cache, di, bb,GF_ZERO , &b))return false;memset ((unsigned char *) b->data, val, len);bcache_put (b);
@@ -273,7 +273,7 @@ static bool _set_whole(struct updater *u, int fd, block_address bb, block_addresreturn true; } -bool bcache_set_bytes (struct bcache *cache,int fd,uint64_t start,size_t len,uint8_t val) +bool bcache_set_bytes (struct bcache *cache,int di,uint64_t start,size_t len,uint8_t val) {struct updater u;
@@ -282,6 +282,6 @@ bool bcache_set_bytes(struct bcache *cache, int fd, uint64_t start, size_t len, u.whole_fn = _set_whole; u.data = &val; -return _update_bytes (&u, fd, start, len); +return _update_bytes (&u, di, start, len); }
lib/device/bcache.c +135 -53
437 lines not on this page · raw message
lib/device/bcache.h +20 -16
@@ -34,7 +34,7 @@ typedef void io_complete_fn(void *context, int io_error);struct io_engine {void (*destroy)(struct io_engine *e); -bool (*issue)(struct io_engine *e,enum dir d,int fd, +bool (*issue)(struct io_engine *e,enum dir d,int di,sector_t sb,sector_t se,void *data,void *context);bool (*wait)(struct io_engine *e,io_complete_fn fn);unsigned (*max_io)(struct io_engine *e);
@@ -48,7 +48,7 @@ struct io_engine *create_sync_io_engine(void);struct bcache ;struct block {/* clients may only access these three fields */ -int fd; +int di;uint64_t index;void *data;
@@ -106,12 +106,12 @@ unsigned bcache_max_prefetches(struct bcache *cache);* they complete. But we're talking a very small difference, and it's worth it * to keep callbacks out of this interface. */ -void bcache_prefetch (struct bcache *cache,int fd,block_address index); +void bcache_prefetch (struct bcache *cache,int di,block_address index);/* * Returns true on success. */ -bool bcache_get (struct bcache *cache,int fd,block_address index, +bool bcache_get (struct bcache *cache,int di,block_address index,unsigned flags,struct block **result);void bcache_put (struct block *b);
@@ -129,38 +129,42 @@ bool bcache_flush(struct bcache *cache);* * If the block is currently held false will be returned. */ -bool bcache_invalidate (struct bcache *cache,int fd,block_address index); +bool bcache_invalidate (struct bcache *cache,int di,block_address index);/* * Invalidates all blocks on the given descriptor. Call this before closing * the descriptor to make sure everything is written back. */ -bool bcache_invalidate_fd (struct bcache *cache,int fd); +bool bcache_invalidate_di (struct bcache *cache,int di);/* * Call this function if flush, or invalidate fail and you do not * wish to retry the writes. This will throw away any dirty data -* not written. If any blocks for fd are held, then it will call +* not written. If any blocks for di are held, then it will call * abort(). */ -void bcache_abort_fd (struct bcache *cache,int fd); +void bcache_abort_di (struct bcache *cache,int di);//---------------------------------------------------------------- // The next four functions are utilities written in terms of the above api. // Prefetches the blocks neccessary to satisfy a byte range. -void bcache_prefetch_bytes (struct bcache *cache,int fd,uint64_t start,size_t len); +void bcache_prefetch_bytes (struct bcache *cache,int di,uint64_t start,size_t len);// Reads, writes and zeroes bytes. Returns false if errors occur. -bool bcache_read_bytes (struct bcache *cache,int fd,uint64_t start,size_t len,void *data); -bool bcache_write_bytes (struct bcache *cache,int fd,uint64_t start,size_t len,void *data); -bool bcache_zero_bytes (struct bcache *cache,int fd,uint64_t start,size_t len); -bool bcache_set_bytes (struct bcache *cache,int fd,uint64_t start,size_t len,uint8_t val); -bool bcache_invalidate_bytes (struct bcache *cache,int fd,uint64_t start,size_t len); +bool bcache_read_bytes (struct bcache *cache,int di,uint64_t start,size_t len,void *data); +bool bcache_write_bytes (struct bcache *cache,int di,uint64_t start,size_t len,void *data); +bool bcache_zero_bytes (struct bcache *cache,int di,uint64_t start,size_t len); +bool bcache_set_bytes (struct bcache *cache,int di,uint64_t start,size_t len,uint8_t val); +bool bcache_invalidate_bytes (struct bcache *cache,int di,uint64_t start,size_t len); -void bcache_set_last_byte (struct bcache *cache,int fd,uint64_t offset,int sector_size); -void bcache_unset_last_byte (struct bcache *cache,int fd); +void bcache_set_last_byte (struct bcache *cache,int di,uint64_t offset,int sector_size); +void bcache_unset_last_byte (struct bcache *cache,int di);//---------------------------------------------------------------- +int bcache_set_fd (int fd);/* returns di */ +void bcache_clear_fd (int di); +int bcache_change_fd (int di,int fd); +#endif
lib/device/dev-cache.c +1 -0
@@ -66,6 +66,7 @@ static void _dev_init(struct device *dev) { dev->fd =-1 ; dev->bcache_fd =-1 ; + dev->bcache_di =-1 ; dev->read_ahead =-1 ; dev->ext.enabled =0 ;
lib/device/device.h +1 -0
@@ -71,6 +71,7 @@ struct device {int logical_block_size;/* From BLKSSZGET: lowest possible block size that the storage device can address */ int read_ahead;int bcache_fd; +int bcache_di;uint32_t flags;unsigned size_seqno;uint64_t size;
lib/label/label.c +65 -50
303 lines not on this page · raw message
lib/metadata/metadata.c +1 -0
@@ -4732,6 +4732,7 @@ static struct volume_group *_vg_read(struct cmd_context *cmd,* we check that they are unchanged in all mdas. This added checking is * probably unnecessary; all commands could likely just check a single mda. */ +if (lvmcache_scan_mismatch (cmd, vgname, vgid) ||_scan_text_mismatch (cmd, vgname, vgid)) {log_debug_metadata ("Rescanning devices for %s %s" , vgname, writing ?"rw" :"" );if (writing)
test/unit/bcache_t.c +121 -121
629 lines not on this page · raw message
test/unit/bcache_utils_t.c +12 -6
@@ -32,6 +32,7 @@struct fixture {int fd; +int di;char fname[32 ];struct bcache *cache; };
@@ -84,6 +85,8 @@ static void *_fix_init(struct io_engine *engine) f->cache =bcache_create (T_BLOCK_SIZE /512 ,NR_BLOCKS , engine);T_ASSERT (f->cache); + f->di =bcache_set_fd (f->fd); +return f; }
@@ -107,6 +110,7 @@ static void _fix_exit(void *fixture)bcache_destroy (f->cache);close (f->fd); +bcache_clear_fd (f->di);unlink (f->fname);free (f); }
@@ -140,7 +144,7 @@ static void _verify(struct fixture *f, uint64_t byte_b, uint64_t byte_e, uint8_tunsigned i;size_t len2 = byte_e - byte_b;uint8_t *buffer =malloc (len2); -T_ASSERT (bcache_read_bytes (f->cache, f->fd, byte_b, len2, buffer)); +T_ASSERT (bcache_read_bytes (f->cache, f->di, byte_b, len2, buffer));for (i =0 ; i < len; i++)T_ASSERT_EQUAL (buffer[i],_pattern_at (pat, byte_b + i));free (buffer);
@@ -148,7 +152,7 @@ static void _verify(struct fixture *f, uint64_t byte_b, uint64_t byte_e, uint8_t// Verify again, driving bcache directly for (; bb != be; bb++) { -T_ASSERT (bcache_get (f->cache, f->fd, bb,0 , &b)); +T_ASSERT (bcache_get (f->cache, f->di, bb,0 , &b)); blen =_min (T_BLOCK_SIZE - offset, len);_verify_bytes (b, bb *T_BLOCK_SIZE , offset, blen, pat);
@@ -170,7 +174,7 @@ static void _verify_set(struct fixture *f, uint64_t byte_b, uint64_t byte_e, uinuint64_t blen, len = byte_e - byte_b;for (; bb != be; bb++) { -T_ASSERT (bcache_get (f->cache, f->fd, bb,0 , &b)); +T_ASSERT (bcache_get (f->cache, f->di, bb,0 , &b)); blen =_min (T_BLOCK_SIZE - offset, len);for (i =0 ; i < blen; i++)
@@ -198,18 +202,18 @@ static void _do_write(struct fixture *f, uint64_t byte_b, uint64_t byte_e, uint8for (i =0 ; i < len; i++) buffer[i] =_pattern_at (pat, byte_b + i); -T_ASSERT (bcache_write_bytes (f->cache, f->fd, byte_b, byte_e - byte_b, buffer)); +T_ASSERT (bcache_write_bytes (f->cache, f->di, byte_b, byte_e - byte_b, buffer));free (buffer); }static void _do_zero (struct fixture *f,uint64_t byte_b,uint64_t byte_e) { -T_ASSERT (bcache_zero_bytes (f->cache, f->fd, byte_b, byte_e - byte_b)); +T_ASSERT (bcache_zero_bytes (f->cache, f->di, byte_b, byte_e - byte_b)); }static void _do_set (struct fixture *f,uint64_t byte_b,uint64_t byte_e,uint8_t val) { -T_ASSERT (bcache_set_bytes (f->cache, f->fd, byte_b, byte_e - byte_b, val)); +T_ASSERT (bcache_set_bytes (f->cache, f->di, byte_b, byte_e - byte_b, val)); }static void _reopen (struct fixture *f)
@@ -222,6 +226,8 @@ static void _reopen(struct fixture *f) f->cache =bcache_create (T_BLOCK_SIZE /512 ,NR_BLOCKS , engine);T_ASSERT (f->cache); + + f->di =bcache_set_fd (f->fd); }//----------------------------------------------------------------
test/unit/io_engine_t.c +17 -7
@@ -35,6 +35,7 @@ struct fixture {char fname[64 ];int fd; +int di; };static void _fill_buffer (uint8_t *buffer,uint8_t seed,size_t count)
@@ -102,6 +103,7 @@ static void _fix_exit(void *fixture)struct fixture *f = fixture;close (f->fd); +bcache_clear_fd (f->di);unlink (f->fname);free (f->data);if (f->e)
@@ -135,11 +137,14 @@ static void _complete_io(void *context, int io_error)static void _test_read (void *fixture) {struct fixture *f = fixture; -struct io io; +struct bcache *cache =bcache_create (8 ,BLOCK_SIZE_SECTORS , f->e); +T_ASSERT (cache); + + f->di =bcache_set_fd (f->fd);_io_init (&io); -T_ASSERT (f->e->issue (f->e,DIR_READ , f->fd,0 ,BLOCK_SIZE_SECTORS , f->data, &io)); +T_ASSERT (f->e->issue (f->e,DIR_READ , f->di,0 ,BLOCK_SIZE_SECTORS , f->data, &io));T_ASSERT (f->e->wait (f->e, _complete_io));T_ASSERT (io.completed);T_ASSERT (!io.error);
@@ -150,11 +155,14 @@ static void _test_read(void *fixture)static void _test_write (void *fixture) {struct fixture *f = fixture; -struct io io; +struct bcache *cache =bcache_create (8 ,BLOCK_SIZE_SECTORS , f->e); +T_ASSERT (cache); + + f->di =bcache_set_fd (f->fd);_io_init (&io); -T_ASSERT (f->e->issue (f->e,DIR_WRITE , f->fd,0 ,BLOCK_SIZE_SECTORS , f->data, &io)); +T_ASSERT (f->e->issue (f->e,DIR_WRITE , f->di,0 ,BLOCK_SIZE_SECTORS , f->data, &io));T_ASSERT (f->e->wait (f->e, _complete_io));T_ASSERT (io.completed);T_ASSERT (!io.error);
@@ -170,10 +178,12 @@ static void _test_write_bytes(void *fixture)struct bcache *cache =bcache_create (8 ,BLOCK_SIZE_SECTORS , f->e);T_ASSERT (cache); -// T_ASSERT(bcache_read_bytes(cache, f->fd, offset, sizeof(buf_in), buf_in)); + f->di =bcache_set_fd (f->fd); + +// T_ASSERT(bcache_read_bytes(cache, f->di, offset, sizeof(buf_in), buf_in)); _fill_buffer ((uint8_t *) buf_out,234 ,sizeof (buf_out)); -T_ASSERT (bcache_write_bytes (cache, f->fd, offset,sizeof (buf_out), buf_out)); -T_ASSERT (bcache_read_bytes (cache, f->fd, offset,sizeof (buf_in), buf_in)); +T_ASSERT (bcache_write_bytes (cache, f->di, offset,sizeof (buf_out), buf_out)); +T_ASSERT (bcache_read_bytes (cache, f->di, offset,sizeof (buf_in), buf_in));_print_buffer ("buf_out" , (uint8_t *) buf_out,sizeof (buf_out));_print_buffer ("buf_in" , (uint8_t *) buf_in,sizeof (buf_in));
tools/toollib.c +1 -1
@@ -5579,7 +5579,7 @@ do_command:* Wipe signatures on devices being created. */ dm_list_iterate_items_safe(pd, pd2, &pp->arg_create) { -label_scan_open (pd->dev); +label_scan_open_excl (pd->dev);log_verbose ("Wiping signatures on new PV %s." , pd->name);