/* exclusively to be used by __al_write_transaction(), *drbd_bm_mark_for_writeout()and *anddrbd_bm_write_hinted()->bm_rw()calledfromthere.
*/ unsignedint n_bitmap_hints; unsignedint al_bitmap_hints[AL_UPDATES_PER_TRANSACTION];
/* see LIMITATIONS: above */
unsignedlong bm_set; /* nr of set bits; THINK maybe atomic_t? */ unsignedlong bm_bits;
size_t bm_words;
size_t bm_number_of_pages;
sector_t bm_dev_capacity; struct mutex bm_change; /* serializes resize operations */
wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */
enum bm_flag bm_flags;
/* debugging aid, in case we are still racy somewhere */ char *bm_why; struct task_struct *bm_task;
};
#define bm_print_lock_info(m) __bm_print_lock_info(m, __func__) staticvoid __bm_print_lock_info(struct drbd_device *device, constchar *func)
{ struct drbd_bitmap *b = device->bitmap; if (!drbd_ratelimit()) return;
drbd_err(device, "FIXME %s[%d] in %s, bitmap locked for '%s' by %s[%d]\n",
current->comm, task_pid_nr(current),
func, b->bm_why ?: "?",
b->bm_task->comm, task_pid_nr(b->bm_task));
}
/* we store some "meta" info about our pages in page->private */ /* at a granularity of 4k storage per bitmap bit: *onepetabytestorage:1<<50byte,1<<38*4kstorageblocks *1<<38bits, *1<<234kbitmappages. *Use24bitsaspageindex,covers2petabytestorage *atagranularityof4kperbit. *Usedtoreportthefailedpageidxonioerrorfromtheendiohandlers.
*/ #define BM_PAGE_IDX_MASK ((1UL<<24)-1) /* this page is currently read in, or written back */ #define BM_PAGE_IO_LOCK 31 /* if there has been an IO error for this page */ #define BM_PAGE_IO_ERROR 30 /* this is to be able to intelligently skip disk IO,
* set if bits have been set since last IO. */ #define BM_PAGE_NEED_WRITEOUT 29 /* to mark for lazy writeout once syncer cleared all clearable bits,
* we if bits have been cleared since last IO. */ #define BM_PAGE_LAZY_WRITEOUT 28 /* pages marked with this "HINT" will be considered for writeout
* on activity log transactions */ #define BM_PAGE_HINT_WRITEOUT 27
/* store_page_idx uses non-atomic assignment. It is only used directly after *allocatingthepage.Allotherbm_set_page_*andbm_clear_page_*needto *useatomicbitmanipulation,asset_out_of_sync(andthereforebitmap *changes)mayhappenfromvariouscontexts,andwait_on_bit/wake_up_bit
* requires it all to be atomic as well. */ staticvoid bm_store_page_idx(struct page *page, unsignedlong idx)
{
BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
set_page_private(page, idx);
}
/* As is very unlikely that the same page is under IO from more than one *context,wecangetawaywithabitperpageandonewaitqueueperbitmap.
*/ staticvoid bm_page_lock_io(struct drbd_device *device, int page_nr)
{ struct drbd_bitmap *b = device->bitmap; void *addr = &page_private(b->bm_pages[page_nr]);
wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
}
/* set _before_ submit_io, so it may be reset due to being changed
* while this page is in flight... will get submitted later again */ staticvoid bm_set_page_unchanged(struct page *page)
{ /* use cmpxchg? */
clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
}
/* long word offset of _bitmap_ sector */ #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL)) /* word offset from start of bitmap to word number _in_page_ *modulolongsperpage #defineMLPP(X)((X)%(PAGE_SIZE/sizeof(long)) hm,well,Philippthinksgccmightnotoptimizethe%into&(...-1) sodoitexplicitly:
*/ #define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
/* Long words per page */ #define LWPP (PAGE_SIZE/sizeof(long))
for (i = 0; i < number; i++) { if (!pages[i]) {
pr_alert("bm_free_pages tried to free a NULL pointer; i=%lu n=%lu\n",
i, number); continue;
}
__free_page(pages[i]);
pages[i] = NULL;
}
}
/* Trying kmalloc first, falling back to vmalloc. *GFP_NOIO,asthisiscalledwhiledrbdIOis"suspended", *andduringresizeorattachondisklessPrimary, *wemustnotblockonIOtoourselves.
* Context is receiver thread or dmsetup. */
bytes = sizeof(struct page *)*want;
new_pages = kzalloc(bytes, GFP_NOIO | __GFP_NOWARN); if (!new_pages) {
new_pages = __vmalloc(bytes, GFP_NOIO | __GFP_ZERO); if (!new_pages) return NULL;
}
if (want >= have) { for (i = 0; i < have; i++)
new_pages[i] = old_pages[i]; for (; i < want; i++) {
page = alloc_page(GFP_NOIO | __GFP_HIGHMEM); if (!page) {
bm_free_pages(new_pages + have, i - have);
bm_vk_free(new_pages); return NULL;
} /* we want to know which page it is
* from the endio handlers */
bm_store_page_idx(page, i);
new_pages[i] = page;
}
} else { for (i = 0; i < want; i++)
new_pages[i] = old_pages[i]; /* NOT HERE, we are outside the spinlock! bm_free_pages(old_pages+want,have-want);
*/
}
return new_pages;
}
/* *allocatesthedrbd_bitmapandstoresitindevice->bitmap.
*/ int drbd_bm_init(struct drbd_device *device)
{ struct drbd_bitmap *b = device->bitmap;
WARN_ON(b != NULL);
b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL); if (!b) return -ENOMEM;
spin_lock_init(&b->bm_lock);
mutex_init(&b->bm_change);
init_waitqueue_head(&b->bm_io_wait);
/* called on driver unload. TODO: call when a device is destroyed.
*/ void drbd_bm_cleanup(struct drbd_device *device)
{ if (!expect(device, device->bitmap)) return;
bm_free_pages(device->bitmap->bm_pages, device->bitmap->bm_number_of_pages);
bm_vk_free(device->bitmap->bm_pages);
kfree(device->bitmap);
device->bitmap = NULL;
}
/* number of bits modulo bits per page */
tmp = (b->bm_bits & BITS_PER_PAGE_MASK); /* mask the used bits of the word containing the last bit */
mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1; /* bitmap is always stored little endian,
* on disk and in core memory alike */
mask = cpu_to_lel(mask);
p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
bm = p_addr + (tmp/BITS_PER_LONG); if (mask) { /* If mask != 0, we are not exactly aligned, so bm now points *tothelongcontainingthelastbit. *Ifmask==0,bmalreadypointstothewordimmediately
* after the last (long word aligned) bit. */
cleared = hweight_long(*bm & ~mask);
*bm &= mask;
bm++;
}
if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) { /* on a 32bit arch, we may need to zero out
* a padding long to align with a 64bit remote */
cleared += hweight_long(*bm);
*bm = 0;
}
bm_unmap(p_addr); return cleared;
}
/* number of bits modulo bits per page */
tmp = (b->bm_bits & BITS_PER_PAGE_MASK); /* mask the used bits of the word containing the last bit */
mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1; /* bitmap is always stored little endian,
* on disk and in core memory alike */
mask = cpu_to_lel(mask);
p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
bm = p_addr + (tmp/BITS_PER_LONG); if (mask) { /* If mask != 0, we are not exactly aligned, so bm now points *tothelongcontainingthelastbit. *Ifmask==0,bmalreadypointstothewordimmediately
* after the last (long word aligned) bit. */
*bm |= ~mask;
bm++;
}
if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) { /* on a 32bit arch, we may need to zero out
* a padding long to align with a 64bit remote */
*bm = ~0UL;
}
bm_unmap(p_addr);
}
/* you better not modify the bitmap while this is running,
* or its results will be stale */ staticunsignedlong bm_count_bits(struct drbd_bitmap *b)
{ unsignedlong *p_addr; unsignedlong bits = 0; unsignedlong mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1; int idx, last_word;
/* all but last page */ for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) {
p_addr = __bm_map_pidx(b, idx);
bits += bitmap_weight(p_addr, BITS_PER_PAGE);
__bm_unmap(p_addr);
cond_resched();
} /* last (or only) page */
last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL;
p_addr = __bm_map_pidx(b, idx);
bits += bitmap_weight(p_addr, last_word * BITS_PER_LONG);
p_addr[last_word] &= cpu_to_lel(mask);
bits += hweight_long(p_addr[last_word]); /* 32bit arch, may have an unused padding long */ if (BITS_PER_LONG == 32 && (last_word & 1) == 0)
p_addr[last_word+1] = 0;
__bm_unmap(p_addr); return bits;
}
/* offset and len in long words.*/ staticvoid bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
{ unsignedlong *p_addr, *bm; unsignedint idx;
size_t do_now, end;
end = offset + len;
if (end > b->bm_words) {
pr_alert("bm_memset end > bm_words\n"); return;
}
/* if we would use words=ALIGN(bits,BITS_PER_LONG)>>LN2_BPL; a32bithostcouldpresentthewrongnumberofwords toa64bithost.
*/
words = ALIGN(bits, 64) >> LN2_BPL;
if (!expect(device, b)) return0; if (!expect(device, b->bm_pages)) return0;
spin_lock_irqsave(&b->bm_lock, flags);
s = b->bm_set;
spin_unlock_irqrestore(&b->bm_lock, flags);
return s;
}
unsignedlong drbd_bm_total_weight(struct drbd_device *device)
{ unsignedlong s; /* if I don't have a disk, I don't know about out-of-sync status */ if (!get_ldev_if_state(device, D_NEGOTIATING)) return0;
s = _drbd_bm_total_weight(device);
put_ldev(device); return s;
}
size_t drbd_bm_words(struct drbd_device *device)
{ struct drbd_bitmap *b = device->bitmap; if (!expect(device, b)) return0; if (!expect(device, b->bm_pages)) return0;
/* set all bits in the bitmap */ void drbd_bm_set_all(struct drbd_device *device)
{ struct drbd_bitmap *b = device->bitmap; if (!expect(device, b)) return; if (!expect(device, b->bm_pages)) return;
/* bv_page may be a copy, or may be the original */ staticvoid drbd_bm_endio(struct bio *bio)
{ struct drbd_bm_aio_ctx *ctx = bio->bi_private; struct drbd_device *device = ctx->device; struct drbd_bitmap *b = device->bitmap; unsignedint idx = bm_page_to_idx(bio_first_page_all(bio));
if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 &&
!bm_test_page_unchanged(b->bm_pages[idx]))
drbd_warn(device, "bitmap page idx %u changed during IO!\n", idx);
if (bio->bi_status) { /* ctx error will hold the completed-last non-zero error code,
* in case error codes differ. */
ctx->error = blk_status_to_errno(bio->bi_status);
bm_set_page_io_err(b->bm_pages[idx]); /* Not identical to on disk version of it.
* Is BM_PAGE_IO_ERROR enough? */ if (drbd_ratelimit())
drbd_err(device, "IO ERROR %d on bitmap page idx %u\n",
bio->bi_status, idx);
} else {
bm_clear_page_io_err(b->bm_pages[idx]);
dynamic_drbd_dbg(device, "bitmap page idx %u completed\n", idx);
}
bm_page_unlock_io(device, idx);
if (ctx->flags & BM_AIO_COPY_PAGES)
mempool_free(bio->bi_io_vec[0].bv_page, &drbd_md_io_page_pool);
/* this might happen with very small *flexibleexternalmetadatadevice,
* or with PAGE_SIZE > 4k */
last_bm_sect = drbd_md_last_bitmap_sector(device->ldev); if (first_bm_sect <= on_disk_sector && last_bm_sect >= on_disk_sector) {
sector_t len_sect = last_bm_sect - on_disk_sector + 1; if (len_sect < PAGE_SIZE/SECTOR_SIZE)
len = (unsignedint)len_sect*SECTOR_SIZE; else
len = PAGE_SIZE;
} else { if (drbd_ratelimit()) {
drbd_err(device, "Invalid offset during on-disk bitmap access: " "page idx %u, sector %llu\n", page_nr, on_disk_sector);
}
ctx->error = -EIO;
bm_set_page_io_err(b->bm_pages[page_nr]); if (atomic_dec_and_test(&ctx->in_flight)) {
ctx->done = 1;
wake_up(&device->misc_wait);
kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy);
} return;
}
/* serialize IO on this page */
bm_page_lock_io(device, page_nr); /* before memcpy and submit,
* so it can be redirtied any time */
bm_set_page_unchanged(b->bm_pages[page_nr]);
if (drbd_insert_fault(device, (op == REQ_OP_WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
bio_io_error(bio);
} else {
submit_bio(bio); /* this should not count as user activity and cause the
* resync to throttle -- see drbd_rs_should_slow_down(). */
atomic_add(len >> 9, &device->rs_sect_ev);
}
}
if (!get_ldev_if_state(device, D_ATTACHING)) { /* put is in drbd_bm_aio_ctx_destroy() */
drbd_err(device, "ASSERT FAILED: get_ldev_if_state() == 1 in bm_rw()\n");
kfree(ctx); return -ENODEV;
} /* Here D_ATTACHING is sufficient since drbd_bm_read() is called only from
drbd_adm_attach(), after device->ldev was assigned. */
if (0 == (ctx->flags & ~BM_AIO_READ))
WARN_ON(!(BM_LOCKED_MASK & b->bm_flags));
/* let the layers below us try to merge these bios... */
if (flags & BM_AIO_READ) { for (i = 0; i < num_pages; i++) {
atomic_inc(&ctx->in_flight);
bm_page_io_async(ctx, i);
++count;
cond_resched();
}
} elseif (flags & BM_AIO_WRITE_HINTED) { /* ASSERT: BM_AIO_WRITE_ALL_PAGES is not set. */ unsignedint hint; for (hint = 0; hint < b->n_bitmap_hints; hint++) {
i = b->al_bitmap_hints[hint]; if (i >= num_pages) /* == -1U: no hint here. */ continue; /* Several AL-extents may point to the same page. */ if (!test_and_clear_bit(BM_PAGE_HINT_WRITEOUT,
&page_private(b->bm_pages[i]))) continue; /* Has it even changed? */ if (bm_test_page_unchanged(b->bm_pages[i])) continue;
atomic_inc(&ctx->in_flight);
bm_page_io_async(ctx, i);
++count;
}
} else { for (i = 0; i < num_pages; i++) { /* ignore completely unchanged pages */ if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx) break; if (!(flags & BM_AIO_WRITE_ALL_PAGES) &&
bm_test_page_unchanged(b->bm_pages[i])) {
dynamic_drbd_dbg(device, "skipped bm write for idx %u\n", i); continue;
} /* during lazy writeout,
* ignore those pages not marked for lazy writeout. */ if (lazy_writeout_upper_idx &&
!bm_test_page_lazy_writeout(b->bm_pages[i])) {
dynamic_drbd_dbg(device, "skipped bm lazy write for idx %u\n", i); continue;
}
atomic_inc(&ctx->in_flight);
bm_page_io_async(ctx, i);
++count;
cond_resched();
}
}
/* summary for global bitmap IO */ if (flags == 0) { unsignedint ms = jiffies_to_msecs(jiffies - now); if (ms > 5) {
drbd_info(device, "bitmap %s of %u pages took %u ms\n",
(flags & BM_AIO_READ) ? "READ" : "WRITE",
count, ms);
}
}
if (ctx->error) {
drbd_alert(device, "we had at least one MD IO ERROR during bitmap IO\n");
drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR);
err = -EIO; /* ctx->error ? */
}
if (atomic_read(&ctx->in_flight))
err = -EIO; /* Disk timeout/force-detach during IO... */
now = jiffies; if (flags & BM_AIO_READ) {
b->bm_set = bm_count_bits(b);
drbd_info(device, "recounting of set bits took additional %lu jiffies\n",
jiffies - now);
}
now = b->bm_set;
if ((flags & ~BM_AIO_READ) == 0)
drbd_info(device, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
if (bm_fo > b->bm_bits) {
drbd_err(device, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
bm_fo = DRBD_END_OF_BITMAP;
} else { while (bm_fo < b->bm_bits) { /* bit offset of the first bit in the page */
bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo));
if (find_zero_bit)
i = find_next_zero_bit_le(p_addr,
PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK); else
i = find_next_bit_le(p_addr,
PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
/* does not spin_lock_irqsave.
* you must take drbd_bm_lock() first */ unsignedlong _drbd_bm_find_next(struct drbd_device *device, unsignedlong bm_fo)
{ /* WARN_ON(!(BM_DONT_SET & device->b->bm_flags)); */ return __bm_find_next(device, bm_fo, 0);
}
/* returns number of bits changed 0 -> 1 */ int drbd_bm_set_bits(struct drbd_device *device, constunsignedlong s, constunsignedlong e)
{ return bm_change_bits_to(device, s, e, 1);
}
/* returns number of bits changed 1 -> 0 */ int drbd_bm_clear_bits(struct drbd_device *device, constunsignedlong s, constunsignedlong e)
{ return -bm_change_bits_to(device, s, e, 0);
}
/* sets all bits in full words,
* from first_word up to, but not including, last_word */ staticinlinevoid bm_set_full_words_within_one_page(struct drbd_bitmap *b, int page_nr, int first_word, int last_word)
{ int i; int bits; int changed = 0; unsignedlong *paddr = kmap_atomic(b->bm_pages[page_nr]);
/* I think it is more cache line friendly to hweight_long then set to ~0UL,
* than to first bitmap_weight() all words, then bitmap_fill() all words */ for (i = first_word; i < last_word; i++) {
bits = hweight_long(paddr[i]);
paddr[i] = ~0UL;
changed += BITS_PER_LONG - bits;
}
kunmap_atomic(paddr); if (changed) { /* We only need lazy writeout, the information is still in the *remotebitmapaswell,andisreconstructedduringthenext
* bitmap exchange, if lost locally due to a crash. */
bm_set_page_lazy_writeout(b->bm_pages[page_nr]);
b->bm_set += changed;
}
}
/* Same thing as drbd_bm_set_bits, *butmoreefficientforalargebitrange. *Youmustfirstdrbd_bm_lock(). *Canbecalledtosetthewholebitmapinonego.
* Sets bits from s to e _inclusive_. */ void _drbd_bm_set_bits(struct drbd_device *device, constunsignedlong s, constunsignedlong e)
{ /* First set_bit from the first bit (s) *uptothenextlongboundary(sl), *thenassignfullwordsuptothelastlongboundary(el), *thenset_bituptoandincludingthelastbit(e). * *Donotusememset,becausewemustaccountforchanges, *soweneedtoloopoverthewordswithhweight()anyways.
*/ struct drbd_bitmap *b = device->bitmap; unsignedlong sl = ALIGN(s,BITS_PER_LONG); unsignedlong el = (e+1) & ~((unsignedlong)BITS_PER_LONG-1); int first_page; int last_page; int page_nr; int first_word; int last_word;
if (e - s <= 3*BITS_PER_LONG) { /* don't bother; el and sl may even be wrong. */
spin_lock_irq(&b->bm_lock);
__bm_change_bits_to(device, s, e, 1);
spin_unlock_irq(&b->bm_lock); return;
}
/* difference is large enough that we can trust sl and el */
spin_lock_irq(&b->bm_lock);
/* bits filling the current long */ if (sl)
__bm_change_bits_to(device, s, sl-1, 1);
/* MLPP: modulo longs per page */ /* LWPP: long words per page */
first_word = MLPP(sl >> LN2_BPL);
last_word = LWPP;
/* first and full pages, unless first page == last page */ for (page_nr = first_page; page_nr < last_page; page_nr++) {
bm_set_full_words_within_one_page(device->bitmap, page_nr, first_word, last_word);
spin_unlock_irq(&b->bm_lock);
cond_resched();
first_word = 0;
spin_lock_irq(&b->bm_lock);
} /* last page (respectively only page, for first page == last page) */
last_word = MLPP(el >> LN2_BPL);
/* returns number of bits set in the range [s, e] */ int drbd_bm_count_bits(struct drbd_device *device, constunsignedlong s, constunsignedlong e)
{ unsignedlong flags; struct drbd_bitmap *b = device->bitmap; unsignedlong *p_addr = NULL; unsignedlong bitnr; unsignedint page_nr = -1U; int c = 0;
/* If this is called without a bitmap, that is a bug. But just to be *robustincasewescrewedupelsewhere,inthatcasepretendthere *wasonedirtybitintherequestedarea,sowewon'ttrytodoa
* local read there (no bitmap probably implies no disk) */ if (!expect(device, b)) return1; if (!expect(device, b->bm_pages)) return1;
spin_lock_irqsave(&b->bm_lock, flags); if (BM_DONT_TEST & b->bm_flags)
bm_print_lock_info(device); for (bitnr = s; bitnr <= e; bitnr++) { unsignedint idx = bm_bit_to_page_idx(b, bitnr); if (page_nr != idx) {
page_nr = idx; if (p_addr)
bm_unmap(p_addr);
p_addr = bm_map_pidx(b, idx);
} if (expect(device, bitnr < b->bm_bits))
c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr)); else
drbd_err(device, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
} if (p_addr)
bm_unmap(p_addr);
spin_unlock_irqrestore(&b->bm_lock, flags); return c;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.