162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci drbd_bitmap.c 462306a36Sopenharmony_ci 562306a36Sopenharmony_ci This file is part of DRBD by Philipp Reisner and Lars Ellenberg. 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci Copyright (C) 2004-2008, LINBIT Information Technologies GmbH. 862306a36Sopenharmony_ci Copyright (C) 2004-2008, Philipp Reisner <philipp.reisner@linbit.com>. 962306a36Sopenharmony_ci Copyright (C) 2004-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include <linux/bitmap.h> 1662306a36Sopenharmony_ci#include <linux/vmalloc.h> 1762306a36Sopenharmony_ci#include <linux/string.h> 1862306a36Sopenharmony_ci#include <linux/drbd.h> 1962306a36Sopenharmony_ci#include <linux/slab.h> 2062306a36Sopenharmony_ci#include <linux/highmem.h> 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci#include "drbd_int.h" 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci/* OPAQUE outside this file! 2662306a36Sopenharmony_ci * interface defined in drbd_int.h 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci * convention: 2962306a36Sopenharmony_ci * function name drbd_bm_... => used elsewhere, "public". 3062306a36Sopenharmony_ci * function name bm_... => internal to implementation, "private". 3162306a36Sopenharmony_ci */ 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ci/* 3562306a36Sopenharmony_ci * LIMITATIONS: 3662306a36Sopenharmony_ci * We want to support >= peta byte of backend storage, while for now still using 3762306a36Sopenharmony_ci * a granularity of one bit per 4KiB of storage. 3862306a36Sopenharmony_ci * 1 << 50 bytes backend storage (1 PiB) 3962306a36Sopenharmony_ci * 1 << (50 - 12) bits needed 4062306a36Sopenharmony_ci * 38 --> we need u64 to index and count bits 4162306a36Sopenharmony_ci * 1 << (38 - 3) bitmap bytes needed 4262306a36Sopenharmony_ci * 35 --> we still need u64 to index and count bytes 4362306a36Sopenharmony_ci * (that's 32 GiB of bitmap for 1 PiB storage) 4462306a36Sopenharmony_ci * 1 << (35 - 2) 32bit longs needed 4562306a36Sopenharmony_ci * 33 --> we'd even need u64 to index and count 32bit long words. 4662306a36Sopenharmony_ci * 1 << (35 - 3) 64bit longs needed 4762306a36Sopenharmony_ci * 32 --> we could get away with a 32bit unsigned int to index and count 4862306a36Sopenharmony_ci * 64bit long words, but I rather stay with unsigned long for now. 4962306a36Sopenharmony_ci * We probably should neither count nor point to bytes or long words 5062306a36Sopenharmony_ci * directly, but either by bitnumber, or by page index and offset. 5162306a36Sopenharmony_ci * 1 << (35 - 12) 5262306a36Sopenharmony_ci * 22 --> we need that much 4KiB pages of bitmap. 5362306a36Sopenharmony_ci * 1 << (22 + 3) --> on a 64bit arch, 5462306a36Sopenharmony_ci * we need 32 MiB to store the array of page pointers. 5562306a36Sopenharmony_ci * 5662306a36Sopenharmony_ci * Because I'm lazy, and because the resulting patch was too large, too ugly 5762306a36Sopenharmony_ci * and still incomplete, on 32bit we still "only" support 16 TiB (minus some), 5862306a36Sopenharmony_ci * (1 << 32) bits * 4k storage. 5962306a36Sopenharmony_ci * 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci * bitmap storage and IO: 6262306a36Sopenharmony_ci * Bitmap is stored little endian on disk, and is kept little endian in 6362306a36Sopenharmony_ci * core memory. Currently we still hold the full bitmap in core as long 6462306a36Sopenharmony_ci * as we are "attached" to a local disk, which at 32 GiB for 1PiB storage 6562306a36Sopenharmony_ci * seems excessive. 6662306a36Sopenharmony_ci * 6762306a36Sopenharmony_ci * We plan to reduce the amount of in-core bitmap pages by paging them in 6862306a36Sopenharmony_ci * and out against their on-disk location as necessary, but need to make 6962306a36Sopenharmony_ci * sure we don't cause too much meta data IO, and must not deadlock in 7062306a36Sopenharmony_ci * tight memory situations. This needs some more work. 7162306a36Sopenharmony_ci */ 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci/* 7462306a36Sopenharmony_ci * NOTE 7562306a36Sopenharmony_ci * Access to the *bm_pages is protected by bm_lock. 7662306a36Sopenharmony_ci * It is safe to read the other members within the lock. 7762306a36Sopenharmony_ci * 7862306a36Sopenharmony_ci * drbd_bm_set_bits is called from bio_endio callbacks, 7962306a36Sopenharmony_ci * We may be called with irq already disabled, 8062306a36Sopenharmony_ci * so we need spin_lock_irqsave(). 8162306a36Sopenharmony_ci * And we need the kmap_atomic. 8262306a36Sopenharmony_ci */ 8362306a36Sopenharmony_cistruct drbd_bitmap { 8462306a36Sopenharmony_ci struct page **bm_pages; 8562306a36Sopenharmony_ci spinlock_t bm_lock; 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci /* exclusively to be used by __al_write_transaction(), 8862306a36Sopenharmony_ci * drbd_bm_mark_for_writeout() and 8962306a36Sopenharmony_ci * and drbd_bm_write_hinted() -> bm_rw() called from there. 9062306a36Sopenharmony_ci */ 9162306a36Sopenharmony_ci unsigned int n_bitmap_hints; 9262306a36Sopenharmony_ci unsigned int al_bitmap_hints[AL_UPDATES_PER_TRANSACTION]; 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci /* see LIMITATIONS: above */ 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */ 9762306a36Sopenharmony_ci unsigned long bm_bits; 9862306a36Sopenharmony_ci size_t bm_words; 9962306a36Sopenharmony_ci size_t bm_number_of_pages; 10062306a36Sopenharmony_ci sector_t bm_dev_capacity; 10162306a36Sopenharmony_ci struct mutex bm_change; /* serializes resize operations */ 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */ 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci enum bm_flag bm_flags; 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci /* debugging aid, in case we are still racy somewhere */ 10862306a36Sopenharmony_ci char *bm_why; 10962306a36Sopenharmony_ci struct task_struct *bm_task; 11062306a36Sopenharmony_ci}; 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci#define bm_print_lock_info(m) __bm_print_lock_info(m, __func__) 11362306a36Sopenharmony_cistatic void __bm_print_lock_info(struct drbd_device *device, const char *func) 11462306a36Sopenharmony_ci{ 11562306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 11662306a36Sopenharmony_ci if (!drbd_ratelimit()) 11762306a36Sopenharmony_ci return; 11862306a36Sopenharmony_ci drbd_err(device, "FIXME %s[%d] in %s, bitmap locked for '%s' by %s[%d]\n", 11962306a36Sopenharmony_ci current->comm, task_pid_nr(current), 12062306a36Sopenharmony_ci func, b->bm_why ?: "?", 12162306a36Sopenharmony_ci b->bm_task->comm, task_pid_nr(b->bm_task)); 12262306a36Sopenharmony_ci} 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_civoid drbd_bm_lock(struct drbd_device *device, char *why, enum bm_flag flags) 12562306a36Sopenharmony_ci{ 12662306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 12762306a36Sopenharmony_ci int trylock_failed; 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci if (!b) { 13062306a36Sopenharmony_ci drbd_err(device, "FIXME no bitmap in drbd_bm_lock!?\n"); 13162306a36Sopenharmony_ci return; 13262306a36Sopenharmony_ci } 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci trylock_failed = !mutex_trylock(&b->bm_change); 13562306a36Sopenharmony_ci 13662306a36Sopenharmony_ci if (trylock_failed) { 13762306a36Sopenharmony_ci drbd_warn(device, "%s[%d] going to '%s' but bitmap already locked for '%s' by %s[%d]\n", 13862306a36Sopenharmony_ci current->comm, task_pid_nr(current), 13962306a36Sopenharmony_ci why, b->bm_why ?: "?", 14062306a36Sopenharmony_ci b->bm_task->comm, task_pid_nr(b->bm_task)); 14162306a36Sopenharmony_ci mutex_lock(&b->bm_change); 14262306a36Sopenharmony_ci } 14362306a36Sopenharmony_ci if (BM_LOCKED_MASK & b->bm_flags) 14462306a36Sopenharmony_ci drbd_err(device, "FIXME bitmap already locked in bm_lock\n"); 14562306a36Sopenharmony_ci b->bm_flags |= flags & BM_LOCKED_MASK; 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci b->bm_why = why; 14862306a36Sopenharmony_ci b->bm_task = current; 14962306a36Sopenharmony_ci} 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_civoid drbd_bm_unlock(struct drbd_device *device) 15262306a36Sopenharmony_ci{ 15362306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 15462306a36Sopenharmony_ci if (!b) { 15562306a36Sopenharmony_ci drbd_err(device, "FIXME no bitmap in drbd_bm_unlock!?\n"); 15662306a36Sopenharmony_ci return; 15762306a36Sopenharmony_ci } 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci if (!(BM_LOCKED_MASK & device->bitmap->bm_flags)) 16062306a36Sopenharmony_ci drbd_err(device, "FIXME bitmap not locked in bm_unlock\n"); 16162306a36Sopenharmony_ci 16262306a36Sopenharmony_ci b->bm_flags &= ~BM_LOCKED_MASK; 16362306a36Sopenharmony_ci b->bm_why = NULL; 16462306a36Sopenharmony_ci b->bm_task = NULL; 16562306a36Sopenharmony_ci mutex_unlock(&b->bm_change); 16662306a36Sopenharmony_ci} 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci/* we store some "meta" info about our pages in page->private */ 16962306a36Sopenharmony_ci/* at a granularity of 4k storage per bitmap bit: 17062306a36Sopenharmony_ci * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks 17162306a36Sopenharmony_ci * 1<<38 bits, 17262306a36Sopenharmony_ci * 1<<23 4k bitmap pages. 17362306a36Sopenharmony_ci * Use 24 bits as page index, covers 2 peta byte storage 17462306a36Sopenharmony_ci * at a granularity of 4k per bit. 17562306a36Sopenharmony_ci * Used to report the failed page idx on io error from the endio handlers. 17662306a36Sopenharmony_ci */ 17762306a36Sopenharmony_ci#define BM_PAGE_IDX_MASK ((1UL<<24)-1) 17862306a36Sopenharmony_ci/* this page is currently read in, or written back */ 17962306a36Sopenharmony_ci#define BM_PAGE_IO_LOCK 31 18062306a36Sopenharmony_ci/* if there has been an IO error for this page */ 18162306a36Sopenharmony_ci#define BM_PAGE_IO_ERROR 30 18262306a36Sopenharmony_ci/* this is to be able to intelligently skip disk IO, 18362306a36Sopenharmony_ci * set if bits have been set since last IO. */ 18462306a36Sopenharmony_ci#define BM_PAGE_NEED_WRITEOUT 29 18562306a36Sopenharmony_ci/* to mark for lazy writeout once syncer cleared all clearable bits, 18662306a36Sopenharmony_ci * we if bits have been cleared since last IO. */ 18762306a36Sopenharmony_ci#define BM_PAGE_LAZY_WRITEOUT 28 18862306a36Sopenharmony_ci/* pages marked with this "HINT" will be considered for writeout 18962306a36Sopenharmony_ci * on activity log transactions */ 19062306a36Sopenharmony_ci#define BM_PAGE_HINT_WRITEOUT 27 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci/* store_page_idx uses non-atomic assignment. It is only used directly after 19362306a36Sopenharmony_ci * allocating the page. All other bm_set_page_* and bm_clear_page_* need to 19462306a36Sopenharmony_ci * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap 19562306a36Sopenharmony_ci * changes) may happen from various contexts, and wait_on_bit/wake_up_bit 19662306a36Sopenharmony_ci * requires it all to be atomic as well. */ 19762306a36Sopenharmony_cistatic void bm_store_page_idx(struct page *page, unsigned long idx) 19862306a36Sopenharmony_ci{ 19962306a36Sopenharmony_ci BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK)); 20062306a36Sopenharmony_ci set_page_private(page, idx); 20162306a36Sopenharmony_ci} 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_cistatic unsigned long bm_page_to_idx(struct page *page) 20462306a36Sopenharmony_ci{ 20562306a36Sopenharmony_ci return page_private(page) & BM_PAGE_IDX_MASK; 20662306a36Sopenharmony_ci} 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci/* As is very unlikely that the same page is under IO from more than one 20962306a36Sopenharmony_ci * context, we can get away with a bit per page and one wait queue per bitmap. 21062306a36Sopenharmony_ci */ 21162306a36Sopenharmony_cistatic void bm_page_lock_io(struct drbd_device *device, int page_nr) 21262306a36Sopenharmony_ci{ 21362306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 21462306a36Sopenharmony_ci void *addr = &page_private(b->bm_pages[page_nr]); 21562306a36Sopenharmony_ci wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr)); 21662306a36Sopenharmony_ci} 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_cistatic void bm_page_unlock_io(struct drbd_device *device, int page_nr) 21962306a36Sopenharmony_ci{ 22062306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 22162306a36Sopenharmony_ci void *addr = &page_private(b->bm_pages[page_nr]); 22262306a36Sopenharmony_ci clear_bit_unlock(BM_PAGE_IO_LOCK, addr); 22362306a36Sopenharmony_ci wake_up(&device->bitmap->bm_io_wait); 22462306a36Sopenharmony_ci} 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci/* set _before_ submit_io, so it may be reset due to being changed 22762306a36Sopenharmony_ci * while this page is in flight... will get submitted later again */ 22862306a36Sopenharmony_cistatic void bm_set_page_unchanged(struct page *page) 22962306a36Sopenharmony_ci{ 23062306a36Sopenharmony_ci /* use cmpxchg? */ 23162306a36Sopenharmony_ci clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page)); 23262306a36Sopenharmony_ci clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page)); 23362306a36Sopenharmony_ci} 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_cistatic void bm_set_page_need_writeout(struct page *page) 23662306a36Sopenharmony_ci{ 23762306a36Sopenharmony_ci set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page)); 23862306a36Sopenharmony_ci} 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_civoid drbd_bm_reset_al_hints(struct drbd_device *device) 24162306a36Sopenharmony_ci{ 24262306a36Sopenharmony_ci device->bitmap->n_bitmap_hints = 0; 24362306a36Sopenharmony_ci} 24462306a36Sopenharmony_ci 24562306a36Sopenharmony_ci/** 24662306a36Sopenharmony_ci * drbd_bm_mark_for_writeout() - mark a page with a "hint" to be considered for writeout 24762306a36Sopenharmony_ci * @device: DRBD device. 24862306a36Sopenharmony_ci * @page_nr: the bitmap page to mark with the "hint" flag 24962306a36Sopenharmony_ci * 25062306a36Sopenharmony_ci * From within an activity log transaction, we mark a few pages with these 25162306a36Sopenharmony_ci * hints, then call drbd_bm_write_hinted(), which will only write out changed 25262306a36Sopenharmony_ci * pages which are flagged with this mark. 25362306a36Sopenharmony_ci */ 25462306a36Sopenharmony_civoid drbd_bm_mark_for_writeout(struct drbd_device *device, int page_nr) 25562306a36Sopenharmony_ci{ 25662306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 25762306a36Sopenharmony_ci struct page *page; 25862306a36Sopenharmony_ci if (page_nr >= device->bitmap->bm_number_of_pages) { 25962306a36Sopenharmony_ci drbd_warn(device, "BAD: page_nr: %u, number_of_pages: %u\n", 26062306a36Sopenharmony_ci page_nr, (int)device->bitmap->bm_number_of_pages); 26162306a36Sopenharmony_ci return; 26262306a36Sopenharmony_ci } 26362306a36Sopenharmony_ci page = device->bitmap->bm_pages[page_nr]; 26462306a36Sopenharmony_ci BUG_ON(b->n_bitmap_hints >= ARRAY_SIZE(b->al_bitmap_hints)); 26562306a36Sopenharmony_ci if (!test_and_set_bit(BM_PAGE_HINT_WRITEOUT, &page_private(page))) 26662306a36Sopenharmony_ci b->al_bitmap_hints[b->n_bitmap_hints++] = page_nr; 26762306a36Sopenharmony_ci} 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_cistatic int bm_test_page_unchanged(struct page *page) 27062306a36Sopenharmony_ci{ 27162306a36Sopenharmony_ci volatile const unsigned long *addr = &page_private(page); 27262306a36Sopenharmony_ci return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0; 27362306a36Sopenharmony_ci} 27462306a36Sopenharmony_ci 27562306a36Sopenharmony_cistatic void bm_set_page_io_err(struct page *page) 27662306a36Sopenharmony_ci{ 27762306a36Sopenharmony_ci set_bit(BM_PAGE_IO_ERROR, &page_private(page)); 27862306a36Sopenharmony_ci} 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_cistatic void bm_clear_page_io_err(struct page *page) 28162306a36Sopenharmony_ci{ 28262306a36Sopenharmony_ci clear_bit(BM_PAGE_IO_ERROR, &page_private(page)); 28362306a36Sopenharmony_ci} 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_cistatic void bm_set_page_lazy_writeout(struct page *page) 28662306a36Sopenharmony_ci{ 28762306a36Sopenharmony_ci set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page)); 28862306a36Sopenharmony_ci} 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_cistatic int bm_test_page_lazy_writeout(struct page *page) 29162306a36Sopenharmony_ci{ 29262306a36Sopenharmony_ci return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page)); 29362306a36Sopenharmony_ci} 29462306a36Sopenharmony_ci 29562306a36Sopenharmony_ci/* on a 32bit box, this would allow for exactly (2<<38) bits. */ 29662306a36Sopenharmony_cistatic unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr) 29762306a36Sopenharmony_ci{ 29862306a36Sopenharmony_ci /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */ 29962306a36Sopenharmony_ci unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3); 30062306a36Sopenharmony_ci BUG_ON(page_nr >= b->bm_number_of_pages); 30162306a36Sopenharmony_ci return page_nr; 30262306a36Sopenharmony_ci} 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_cistatic unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr) 30562306a36Sopenharmony_ci{ 30662306a36Sopenharmony_ci /* page_nr = (bitnr/8) >> PAGE_SHIFT; */ 30762306a36Sopenharmony_ci unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3); 30862306a36Sopenharmony_ci BUG_ON(page_nr >= b->bm_number_of_pages); 30962306a36Sopenharmony_ci return page_nr; 31062306a36Sopenharmony_ci} 31162306a36Sopenharmony_ci 31262306a36Sopenharmony_cistatic unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx) 31362306a36Sopenharmony_ci{ 31462306a36Sopenharmony_ci struct page *page = b->bm_pages[idx]; 31562306a36Sopenharmony_ci return (unsigned long *) kmap_atomic(page); 31662306a36Sopenharmony_ci} 31762306a36Sopenharmony_ci 31862306a36Sopenharmony_cistatic unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx) 31962306a36Sopenharmony_ci{ 32062306a36Sopenharmony_ci return __bm_map_pidx(b, idx); 32162306a36Sopenharmony_ci} 32262306a36Sopenharmony_ci 32362306a36Sopenharmony_cistatic void __bm_unmap(unsigned long *p_addr) 32462306a36Sopenharmony_ci{ 32562306a36Sopenharmony_ci kunmap_atomic(p_addr); 32662306a36Sopenharmony_ci}; 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_cistatic void bm_unmap(unsigned long *p_addr) 32962306a36Sopenharmony_ci{ 33062306a36Sopenharmony_ci return __bm_unmap(p_addr); 33162306a36Sopenharmony_ci} 33262306a36Sopenharmony_ci 33362306a36Sopenharmony_ci/* long word offset of _bitmap_ sector */ 33462306a36Sopenharmony_ci#define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL)) 33562306a36Sopenharmony_ci/* word offset from start of bitmap to word number _in_page_ 33662306a36Sopenharmony_ci * modulo longs per page 33762306a36Sopenharmony_ci#define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long)) 33862306a36Sopenharmony_ci hm, well, Philipp thinks gcc might not optimize the % into & (... - 1) 33962306a36Sopenharmony_ci so do it explicitly: 34062306a36Sopenharmony_ci */ 34162306a36Sopenharmony_ci#define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1)) 34262306a36Sopenharmony_ci 34362306a36Sopenharmony_ci/* Long words per page */ 34462306a36Sopenharmony_ci#define LWPP (PAGE_SIZE/sizeof(long)) 34562306a36Sopenharmony_ci 34662306a36Sopenharmony_ci/* 34762306a36Sopenharmony_ci * actually most functions herein should take a struct drbd_bitmap*, not a 34862306a36Sopenharmony_ci * struct drbd_device*, but for the debug macros I like to have the device around 34962306a36Sopenharmony_ci * to be able to report device specific. 35062306a36Sopenharmony_ci */ 35162306a36Sopenharmony_ci 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_cistatic void bm_free_pages(struct page **pages, unsigned long number) 35462306a36Sopenharmony_ci{ 35562306a36Sopenharmony_ci unsigned long i; 35662306a36Sopenharmony_ci if (!pages) 35762306a36Sopenharmony_ci return; 35862306a36Sopenharmony_ci 35962306a36Sopenharmony_ci for (i = 0; i < number; i++) { 36062306a36Sopenharmony_ci if (!pages[i]) { 36162306a36Sopenharmony_ci pr_alert("bm_free_pages tried to free a NULL pointer; i=%lu n=%lu\n", 36262306a36Sopenharmony_ci i, number); 36362306a36Sopenharmony_ci continue; 36462306a36Sopenharmony_ci } 36562306a36Sopenharmony_ci __free_page(pages[i]); 36662306a36Sopenharmony_ci pages[i] = NULL; 36762306a36Sopenharmony_ci } 36862306a36Sopenharmony_ci} 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_cistatic inline void bm_vk_free(void *ptr) 37162306a36Sopenharmony_ci{ 37262306a36Sopenharmony_ci kvfree(ptr); 37362306a36Sopenharmony_ci} 37462306a36Sopenharmony_ci 37562306a36Sopenharmony_ci/* 37662306a36Sopenharmony_ci * "have" and "want" are NUMBER OF PAGES. 37762306a36Sopenharmony_ci */ 37862306a36Sopenharmony_cistatic struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want) 37962306a36Sopenharmony_ci{ 38062306a36Sopenharmony_ci struct page **old_pages = b->bm_pages; 38162306a36Sopenharmony_ci struct page **new_pages, *page; 38262306a36Sopenharmony_ci unsigned int i, bytes; 38362306a36Sopenharmony_ci unsigned long have = b->bm_number_of_pages; 38462306a36Sopenharmony_ci 38562306a36Sopenharmony_ci BUG_ON(have == 0 && old_pages != NULL); 38662306a36Sopenharmony_ci BUG_ON(have != 0 && old_pages == NULL); 38762306a36Sopenharmony_ci 38862306a36Sopenharmony_ci if (have == want) 38962306a36Sopenharmony_ci return old_pages; 39062306a36Sopenharmony_ci 39162306a36Sopenharmony_ci /* Trying kmalloc first, falling back to vmalloc. 39262306a36Sopenharmony_ci * GFP_NOIO, as this is called while drbd IO is "suspended", 39362306a36Sopenharmony_ci * and during resize or attach on diskless Primary, 39462306a36Sopenharmony_ci * we must not block on IO to ourselves. 39562306a36Sopenharmony_ci * Context is receiver thread or dmsetup. */ 39662306a36Sopenharmony_ci bytes = sizeof(struct page *)*want; 39762306a36Sopenharmony_ci new_pages = kzalloc(bytes, GFP_NOIO | __GFP_NOWARN); 39862306a36Sopenharmony_ci if (!new_pages) { 39962306a36Sopenharmony_ci new_pages = __vmalloc(bytes, GFP_NOIO | __GFP_ZERO); 40062306a36Sopenharmony_ci if (!new_pages) 40162306a36Sopenharmony_ci return NULL; 40262306a36Sopenharmony_ci } 40362306a36Sopenharmony_ci 40462306a36Sopenharmony_ci if (want >= have) { 40562306a36Sopenharmony_ci for (i = 0; i < have; i++) 40662306a36Sopenharmony_ci new_pages[i] = old_pages[i]; 40762306a36Sopenharmony_ci for (; i < want; i++) { 40862306a36Sopenharmony_ci page = alloc_page(GFP_NOIO | __GFP_HIGHMEM); 40962306a36Sopenharmony_ci if (!page) { 41062306a36Sopenharmony_ci bm_free_pages(new_pages + have, i - have); 41162306a36Sopenharmony_ci bm_vk_free(new_pages); 41262306a36Sopenharmony_ci return NULL; 41362306a36Sopenharmony_ci } 41462306a36Sopenharmony_ci /* we want to know which page it is 41562306a36Sopenharmony_ci * from the endio handlers */ 41662306a36Sopenharmony_ci bm_store_page_idx(page, i); 41762306a36Sopenharmony_ci new_pages[i] = page; 41862306a36Sopenharmony_ci } 41962306a36Sopenharmony_ci } else { 42062306a36Sopenharmony_ci for (i = 0; i < want; i++) 42162306a36Sopenharmony_ci new_pages[i] = old_pages[i]; 42262306a36Sopenharmony_ci /* NOT HERE, we are outside the spinlock! 42362306a36Sopenharmony_ci bm_free_pages(old_pages + want, have - want); 42462306a36Sopenharmony_ci */ 42562306a36Sopenharmony_ci } 42662306a36Sopenharmony_ci 42762306a36Sopenharmony_ci return new_pages; 42862306a36Sopenharmony_ci} 42962306a36Sopenharmony_ci 43062306a36Sopenharmony_ci/* 43162306a36Sopenharmony_ci * allocates the drbd_bitmap and stores it in device->bitmap. 43262306a36Sopenharmony_ci */ 43362306a36Sopenharmony_ciint drbd_bm_init(struct drbd_device *device) 43462306a36Sopenharmony_ci{ 43562306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 43662306a36Sopenharmony_ci WARN_ON(b != NULL); 43762306a36Sopenharmony_ci b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL); 43862306a36Sopenharmony_ci if (!b) 43962306a36Sopenharmony_ci return -ENOMEM; 44062306a36Sopenharmony_ci spin_lock_init(&b->bm_lock); 44162306a36Sopenharmony_ci mutex_init(&b->bm_change); 44262306a36Sopenharmony_ci init_waitqueue_head(&b->bm_io_wait); 44362306a36Sopenharmony_ci 44462306a36Sopenharmony_ci device->bitmap = b; 44562306a36Sopenharmony_ci 44662306a36Sopenharmony_ci return 0; 44762306a36Sopenharmony_ci} 44862306a36Sopenharmony_ci 44962306a36Sopenharmony_cisector_t drbd_bm_capacity(struct drbd_device *device) 45062306a36Sopenharmony_ci{ 45162306a36Sopenharmony_ci if (!expect(device, device->bitmap)) 45262306a36Sopenharmony_ci return 0; 45362306a36Sopenharmony_ci return device->bitmap->bm_dev_capacity; 45462306a36Sopenharmony_ci} 45562306a36Sopenharmony_ci 45662306a36Sopenharmony_ci/* called on driver unload. TODO: call when a device is destroyed. 45762306a36Sopenharmony_ci */ 45862306a36Sopenharmony_civoid drbd_bm_cleanup(struct drbd_device *device) 45962306a36Sopenharmony_ci{ 46062306a36Sopenharmony_ci if (!expect(device, device->bitmap)) 46162306a36Sopenharmony_ci return; 46262306a36Sopenharmony_ci bm_free_pages(device->bitmap->bm_pages, device->bitmap->bm_number_of_pages); 46362306a36Sopenharmony_ci bm_vk_free(device->bitmap->bm_pages); 46462306a36Sopenharmony_ci kfree(device->bitmap); 46562306a36Sopenharmony_ci device->bitmap = NULL; 46662306a36Sopenharmony_ci} 46762306a36Sopenharmony_ci 46862306a36Sopenharmony_ci/* 46962306a36Sopenharmony_ci * since (b->bm_bits % BITS_PER_LONG) != 0, 47062306a36Sopenharmony_ci * this masks out the remaining bits. 47162306a36Sopenharmony_ci * Returns the number of bits cleared. 47262306a36Sopenharmony_ci */ 47362306a36Sopenharmony_ci#ifndef BITS_PER_PAGE 47462306a36Sopenharmony_ci#define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3)) 47562306a36Sopenharmony_ci#define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1) 47662306a36Sopenharmony_ci#else 47762306a36Sopenharmony_ci# if BITS_PER_PAGE != (1UL << (PAGE_SHIFT + 3)) 47862306a36Sopenharmony_ci# error "ambiguous BITS_PER_PAGE" 47962306a36Sopenharmony_ci# endif 48062306a36Sopenharmony_ci#endif 48162306a36Sopenharmony_ci#define BITS_PER_LONG_MASK (BITS_PER_LONG - 1) 48262306a36Sopenharmony_cistatic int bm_clear_surplus(struct drbd_bitmap *b) 48362306a36Sopenharmony_ci{ 48462306a36Sopenharmony_ci unsigned long mask; 48562306a36Sopenharmony_ci unsigned long *p_addr, *bm; 48662306a36Sopenharmony_ci int tmp; 48762306a36Sopenharmony_ci int cleared = 0; 48862306a36Sopenharmony_ci 48962306a36Sopenharmony_ci /* number of bits modulo bits per page */ 49062306a36Sopenharmony_ci tmp = (b->bm_bits & BITS_PER_PAGE_MASK); 49162306a36Sopenharmony_ci /* mask the used bits of the word containing the last bit */ 49262306a36Sopenharmony_ci mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1; 49362306a36Sopenharmony_ci /* bitmap is always stored little endian, 49462306a36Sopenharmony_ci * on disk and in core memory alike */ 49562306a36Sopenharmony_ci mask = cpu_to_lel(mask); 49662306a36Sopenharmony_ci 49762306a36Sopenharmony_ci p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1); 49862306a36Sopenharmony_ci bm = p_addr + (tmp/BITS_PER_LONG); 49962306a36Sopenharmony_ci if (mask) { 50062306a36Sopenharmony_ci /* If mask != 0, we are not exactly aligned, so bm now points 50162306a36Sopenharmony_ci * to the long containing the last bit. 50262306a36Sopenharmony_ci * If mask == 0, bm already points to the word immediately 50362306a36Sopenharmony_ci * after the last (long word aligned) bit. */ 50462306a36Sopenharmony_ci cleared = hweight_long(*bm & ~mask); 50562306a36Sopenharmony_ci *bm &= mask; 50662306a36Sopenharmony_ci bm++; 50762306a36Sopenharmony_ci } 50862306a36Sopenharmony_ci 50962306a36Sopenharmony_ci if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) { 51062306a36Sopenharmony_ci /* on a 32bit arch, we may need to zero out 51162306a36Sopenharmony_ci * a padding long to align with a 64bit remote */ 51262306a36Sopenharmony_ci cleared += hweight_long(*bm); 51362306a36Sopenharmony_ci *bm = 0; 51462306a36Sopenharmony_ci } 51562306a36Sopenharmony_ci bm_unmap(p_addr); 51662306a36Sopenharmony_ci return cleared; 51762306a36Sopenharmony_ci} 51862306a36Sopenharmony_ci 51962306a36Sopenharmony_cistatic void bm_set_surplus(struct drbd_bitmap *b) 52062306a36Sopenharmony_ci{ 52162306a36Sopenharmony_ci unsigned long mask; 52262306a36Sopenharmony_ci unsigned long *p_addr, *bm; 52362306a36Sopenharmony_ci int tmp; 52462306a36Sopenharmony_ci 52562306a36Sopenharmony_ci /* number of bits modulo bits per page */ 52662306a36Sopenharmony_ci tmp = (b->bm_bits & BITS_PER_PAGE_MASK); 52762306a36Sopenharmony_ci /* mask the used bits of the word containing the last bit */ 52862306a36Sopenharmony_ci mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1; 52962306a36Sopenharmony_ci /* bitmap is always stored little endian, 53062306a36Sopenharmony_ci * on disk and in core memory alike */ 53162306a36Sopenharmony_ci mask = cpu_to_lel(mask); 53262306a36Sopenharmony_ci 53362306a36Sopenharmony_ci p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1); 53462306a36Sopenharmony_ci bm = p_addr + (tmp/BITS_PER_LONG); 53562306a36Sopenharmony_ci if (mask) { 53662306a36Sopenharmony_ci /* If mask != 0, we are not exactly aligned, so bm now points 53762306a36Sopenharmony_ci * to the long containing the last bit. 53862306a36Sopenharmony_ci * If mask == 0, bm already points to the word immediately 53962306a36Sopenharmony_ci * after the last (long word aligned) bit. */ 54062306a36Sopenharmony_ci *bm |= ~mask; 54162306a36Sopenharmony_ci bm++; 54262306a36Sopenharmony_ci } 54362306a36Sopenharmony_ci 54462306a36Sopenharmony_ci if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) { 54562306a36Sopenharmony_ci /* on a 32bit arch, we may need to zero out 54662306a36Sopenharmony_ci * a padding long to align with a 64bit remote */ 54762306a36Sopenharmony_ci *bm = ~0UL; 54862306a36Sopenharmony_ci } 54962306a36Sopenharmony_ci bm_unmap(p_addr); 55062306a36Sopenharmony_ci} 55162306a36Sopenharmony_ci 55262306a36Sopenharmony_ci/* you better not modify the bitmap while this is running, 55362306a36Sopenharmony_ci * or its results will be stale */ 55462306a36Sopenharmony_cistatic unsigned long bm_count_bits(struct drbd_bitmap *b) 55562306a36Sopenharmony_ci{ 55662306a36Sopenharmony_ci unsigned long *p_addr; 55762306a36Sopenharmony_ci unsigned long bits = 0; 55862306a36Sopenharmony_ci unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1; 55962306a36Sopenharmony_ci int idx, last_word; 56062306a36Sopenharmony_ci 56162306a36Sopenharmony_ci /* all but last page */ 56262306a36Sopenharmony_ci for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) { 56362306a36Sopenharmony_ci p_addr = __bm_map_pidx(b, idx); 56462306a36Sopenharmony_ci bits += bitmap_weight(p_addr, BITS_PER_PAGE); 56562306a36Sopenharmony_ci __bm_unmap(p_addr); 56662306a36Sopenharmony_ci cond_resched(); 56762306a36Sopenharmony_ci } 56862306a36Sopenharmony_ci /* last (or only) page */ 56962306a36Sopenharmony_ci last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL; 57062306a36Sopenharmony_ci p_addr = __bm_map_pidx(b, idx); 57162306a36Sopenharmony_ci bits += bitmap_weight(p_addr, last_word * BITS_PER_LONG); 57262306a36Sopenharmony_ci p_addr[last_word] &= cpu_to_lel(mask); 57362306a36Sopenharmony_ci bits += hweight_long(p_addr[last_word]); 57462306a36Sopenharmony_ci /* 32bit arch, may have an unused padding long */ 57562306a36Sopenharmony_ci if (BITS_PER_LONG == 32 && (last_word & 1) == 0) 57662306a36Sopenharmony_ci p_addr[last_word+1] = 0; 57762306a36Sopenharmony_ci __bm_unmap(p_addr); 57862306a36Sopenharmony_ci return bits; 57962306a36Sopenharmony_ci} 58062306a36Sopenharmony_ci 58162306a36Sopenharmony_ci/* offset and len in long words.*/ 58262306a36Sopenharmony_cistatic void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len) 58362306a36Sopenharmony_ci{ 58462306a36Sopenharmony_ci unsigned long *p_addr, *bm; 58562306a36Sopenharmony_ci unsigned int idx; 58662306a36Sopenharmony_ci size_t do_now, end; 58762306a36Sopenharmony_ci 58862306a36Sopenharmony_ci end = offset + len; 58962306a36Sopenharmony_ci 59062306a36Sopenharmony_ci if (end > b->bm_words) { 59162306a36Sopenharmony_ci pr_alert("bm_memset end > bm_words\n"); 59262306a36Sopenharmony_ci return; 59362306a36Sopenharmony_ci } 59462306a36Sopenharmony_ci 59562306a36Sopenharmony_ci while (offset < end) { 59662306a36Sopenharmony_ci do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset; 59762306a36Sopenharmony_ci idx = bm_word_to_page_idx(b, offset); 59862306a36Sopenharmony_ci p_addr = bm_map_pidx(b, idx); 59962306a36Sopenharmony_ci bm = p_addr + MLPP(offset); 60062306a36Sopenharmony_ci if (bm+do_now > p_addr + LWPP) { 60162306a36Sopenharmony_ci pr_alert("BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n", 60262306a36Sopenharmony_ci p_addr, bm, (int)do_now); 60362306a36Sopenharmony_ci } else 60462306a36Sopenharmony_ci memset(bm, c, do_now * sizeof(long)); 60562306a36Sopenharmony_ci bm_unmap(p_addr); 60662306a36Sopenharmony_ci bm_set_page_need_writeout(b->bm_pages[idx]); 60762306a36Sopenharmony_ci offset += do_now; 60862306a36Sopenharmony_ci } 60962306a36Sopenharmony_ci} 61062306a36Sopenharmony_ci 61162306a36Sopenharmony_ci/* For the layout, see comment above drbd_md_set_sector_offsets(). */ 61262306a36Sopenharmony_cistatic u64 drbd_md_on_disk_bits(struct drbd_backing_dev *ldev) 61362306a36Sopenharmony_ci{ 61462306a36Sopenharmony_ci u64 bitmap_sectors; 61562306a36Sopenharmony_ci if (ldev->md.al_offset == 8) 61662306a36Sopenharmony_ci bitmap_sectors = ldev->md.md_size_sect - ldev->md.bm_offset; 61762306a36Sopenharmony_ci else 61862306a36Sopenharmony_ci bitmap_sectors = ldev->md.al_offset - ldev->md.bm_offset; 61962306a36Sopenharmony_ci return bitmap_sectors << (9 + 3); 62062306a36Sopenharmony_ci} 62162306a36Sopenharmony_ci 62262306a36Sopenharmony_ci/* 62362306a36Sopenharmony_ci * make sure the bitmap has enough room for the attached storage, 62462306a36Sopenharmony_ci * if necessary, resize. 62562306a36Sopenharmony_ci * called whenever we may have changed the device size. 62662306a36Sopenharmony_ci * returns -ENOMEM if we could not allocate enough memory, 0 on success. 62762306a36Sopenharmony_ci * In case this is actually a resize, we copy the old bitmap into the new one. 62862306a36Sopenharmony_ci * Otherwise, the bitmap is initialized to all bits set. 62962306a36Sopenharmony_ci */ 63062306a36Sopenharmony_ciint drbd_bm_resize(struct drbd_device *device, sector_t capacity, int set_new_bits) 63162306a36Sopenharmony_ci{ 63262306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 63362306a36Sopenharmony_ci unsigned long bits, words, owords, obits; 63462306a36Sopenharmony_ci unsigned long want, have, onpages; /* number of pages */ 63562306a36Sopenharmony_ci struct page **npages, **opages = NULL; 63662306a36Sopenharmony_ci int err = 0; 63762306a36Sopenharmony_ci bool growing; 63862306a36Sopenharmony_ci 63962306a36Sopenharmony_ci if (!expect(device, b)) 64062306a36Sopenharmony_ci return -ENOMEM; 64162306a36Sopenharmony_ci 64262306a36Sopenharmony_ci drbd_bm_lock(device, "resize", BM_LOCKED_MASK); 64362306a36Sopenharmony_ci 64462306a36Sopenharmony_ci drbd_info(device, "drbd_bm_resize called with capacity == %llu\n", 64562306a36Sopenharmony_ci (unsigned long long)capacity); 64662306a36Sopenharmony_ci 64762306a36Sopenharmony_ci if (capacity == b->bm_dev_capacity) 64862306a36Sopenharmony_ci goto out; 64962306a36Sopenharmony_ci 65062306a36Sopenharmony_ci if (capacity == 0) { 65162306a36Sopenharmony_ci spin_lock_irq(&b->bm_lock); 65262306a36Sopenharmony_ci opages = b->bm_pages; 65362306a36Sopenharmony_ci onpages = b->bm_number_of_pages; 65462306a36Sopenharmony_ci owords = b->bm_words; 65562306a36Sopenharmony_ci b->bm_pages = NULL; 65662306a36Sopenharmony_ci b->bm_number_of_pages = 65762306a36Sopenharmony_ci b->bm_set = 65862306a36Sopenharmony_ci b->bm_bits = 65962306a36Sopenharmony_ci b->bm_words = 66062306a36Sopenharmony_ci b->bm_dev_capacity = 0; 66162306a36Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 66262306a36Sopenharmony_ci bm_free_pages(opages, onpages); 66362306a36Sopenharmony_ci bm_vk_free(opages); 66462306a36Sopenharmony_ci goto out; 66562306a36Sopenharmony_ci } 66662306a36Sopenharmony_ci bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT)); 66762306a36Sopenharmony_ci 66862306a36Sopenharmony_ci /* if we would use 66962306a36Sopenharmony_ci words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL; 67062306a36Sopenharmony_ci a 32bit host could present the wrong number of words 67162306a36Sopenharmony_ci to a 64bit host. 67262306a36Sopenharmony_ci */ 67362306a36Sopenharmony_ci words = ALIGN(bits, 64) >> LN2_BPL; 67462306a36Sopenharmony_ci 67562306a36Sopenharmony_ci if (get_ldev(device)) { 67662306a36Sopenharmony_ci u64 bits_on_disk = drbd_md_on_disk_bits(device->ldev); 67762306a36Sopenharmony_ci put_ldev(device); 67862306a36Sopenharmony_ci if (bits > bits_on_disk) { 67962306a36Sopenharmony_ci drbd_info(device, "bits = %lu\n", bits); 68062306a36Sopenharmony_ci drbd_info(device, "bits_on_disk = %llu\n", bits_on_disk); 68162306a36Sopenharmony_ci err = -ENOSPC; 68262306a36Sopenharmony_ci goto out; 68362306a36Sopenharmony_ci } 68462306a36Sopenharmony_ci } 68562306a36Sopenharmony_ci 68662306a36Sopenharmony_ci want = PFN_UP(words*sizeof(long)); 68762306a36Sopenharmony_ci have = b->bm_number_of_pages; 68862306a36Sopenharmony_ci if (want == have) { 68962306a36Sopenharmony_ci D_ASSERT(device, b->bm_pages != NULL); 69062306a36Sopenharmony_ci npages = b->bm_pages; 69162306a36Sopenharmony_ci } else { 69262306a36Sopenharmony_ci if (drbd_insert_fault(device, DRBD_FAULT_BM_ALLOC)) 69362306a36Sopenharmony_ci npages = NULL; 69462306a36Sopenharmony_ci else 69562306a36Sopenharmony_ci npages = bm_realloc_pages(b, want); 69662306a36Sopenharmony_ci } 69762306a36Sopenharmony_ci 69862306a36Sopenharmony_ci if (!npages) { 69962306a36Sopenharmony_ci err = -ENOMEM; 70062306a36Sopenharmony_ci goto out; 70162306a36Sopenharmony_ci } 70262306a36Sopenharmony_ci 70362306a36Sopenharmony_ci spin_lock_irq(&b->bm_lock); 70462306a36Sopenharmony_ci opages = b->bm_pages; 70562306a36Sopenharmony_ci owords = b->bm_words; 70662306a36Sopenharmony_ci obits = b->bm_bits; 70762306a36Sopenharmony_ci 70862306a36Sopenharmony_ci growing = bits > obits; 70962306a36Sopenharmony_ci if (opages && growing && set_new_bits) 71062306a36Sopenharmony_ci bm_set_surplus(b); 71162306a36Sopenharmony_ci 71262306a36Sopenharmony_ci b->bm_pages = npages; 71362306a36Sopenharmony_ci b->bm_number_of_pages = want; 71462306a36Sopenharmony_ci b->bm_bits = bits; 71562306a36Sopenharmony_ci b->bm_words = words; 71662306a36Sopenharmony_ci b->bm_dev_capacity = capacity; 71762306a36Sopenharmony_ci 71862306a36Sopenharmony_ci if (growing) { 71962306a36Sopenharmony_ci if (set_new_bits) { 72062306a36Sopenharmony_ci bm_memset(b, owords, 0xff, words-owords); 72162306a36Sopenharmony_ci b->bm_set += bits - obits; 72262306a36Sopenharmony_ci } else 72362306a36Sopenharmony_ci bm_memset(b, owords, 0x00, words-owords); 72462306a36Sopenharmony_ci 72562306a36Sopenharmony_ci } 72662306a36Sopenharmony_ci 72762306a36Sopenharmony_ci if (want < have) { 72862306a36Sopenharmony_ci /* implicit: (opages != NULL) && (opages != npages) */ 72962306a36Sopenharmony_ci bm_free_pages(opages + want, have - want); 73062306a36Sopenharmony_ci } 73162306a36Sopenharmony_ci 73262306a36Sopenharmony_ci (void)bm_clear_surplus(b); 73362306a36Sopenharmony_ci 73462306a36Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 73562306a36Sopenharmony_ci if (opages != npages) 73662306a36Sopenharmony_ci bm_vk_free(opages); 73762306a36Sopenharmony_ci if (!growing) 73862306a36Sopenharmony_ci b->bm_set = bm_count_bits(b); 73962306a36Sopenharmony_ci drbd_info(device, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want); 74062306a36Sopenharmony_ci 74162306a36Sopenharmony_ci out: 74262306a36Sopenharmony_ci drbd_bm_unlock(device); 74362306a36Sopenharmony_ci return err; 74462306a36Sopenharmony_ci} 74562306a36Sopenharmony_ci 74662306a36Sopenharmony_ci/* inherently racy: 74762306a36Sopenharmony_ci * if not protected by other means, return value may be out of date when 74862306a36Sopenharmony_ci * leaving this function... 74962306a36Sopenharmony_ci * we still need to lock it, since it is important that this returns 75062306a36Sopenharmony_ci * bm_set == 0 precisely. 75162306a36Sopenharmony_ci * 75262306a36Sopenharmony_ci * maybe bm_set should be atomic_t ? 75362306a36Sopenharmony_ci */ 75462306a36Sopenharmony_ciunsigned long _drbd_bm_total_weight(struct drbd_device *device) 75562306a36Sopenharmony_ci{ 75662306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 75762306a36Sopenharmony_ci unsigned long s; 75862306a36Sopenharmony_ci unsigned long flags; 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_ci if (!expect(device, b)) 76162306a36Sopenharmony_ci return 0; 76262306a36Sopenharmony_ci if (!expect(device, b->bm_pages)) 76362306a36Sopenharmony_ci return 0; 76462306a36Sopenharmony_ci 76562306a36Sopenharmony_ci spin_lock_irqsave(&b->bm_lock, flags); 76662306a36Sopenharmony_ci s = b->bm_set; 76762306a36Sopenharmony_ci spin_unlock_irqrestore(&b->bm_lock, flags); 76862306a36Sopenharmony_ci 76962306a36Sopenharmony_ci return s; 77062306a36Sopenharmony_ci} 77162306a36Sopenharmony_ci 77262306a36Sopenharmony_ciunsigned long drbd_bm_total_weight(struct drbd_device *device) 77362306a36Sopenharmony_ci{ 77462306a36Sopenharmony_ci unsigned long s; 77562306a36Sopenharmony_ci /* if I don't have a disk, I don't know about out-of-sync status */ 77662306a36Sopenharmony_ci if (!get_ldev_if_state(device, D_NEGOTIATING)) 77762306a36Sopenharmony_ci return 0; 77862306a36Sopenharmony_ci s = _drbd_bm_total_weight(device); 77962306a36Sopenharmony_ci put_ldev(device); 78062306a36Sopenharmony_ci return s; 78162306a36Sopenharmony_ci} 78262306a36Sopenharmony_ci 78362306a36Sopenharmony_cisize_t drbd_bm_words(struct drbd_device *device) 78462306a36Sopenharmony_ci{ 78562306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 78662306a36Sopenharmony_ci if (!expect(device, b)) 78762306a36Sopenharmony_ci return 0; 78862306a36Sopenharmony_ci if (!expect(device, b->bm_pages)) 78962306a36Sopenharmony_ci return 0; 79062306a36Sopenharmony_ci 79162306a36Sopenharmony_ci return b->bm_words; 79262306a36Sopenharmony_ci} 79362306a36Sopenharmony_ci 79462306a36Sopenharmony_ciunsigned long drbd_bm_bits(struct drbd_device *device) 79562306a36Sopenharmony_ci{ 79662306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 79762306a36Sopenharmony_ci if (!expect(device, b)) 79862306a36Sopenharmony_ci return 0; 79962306a36Sopenharmony_ci 80062306a36Sopenharmony_ci return b->bm_bits; 80162306a36Sopenharmony_ci} 80262306a36Sopenharmony_ci 80362306a36Sopenharmony_ci/* merge number words from buffer into the bitmap starting at offset. 80462306a36Sopenharmony_ci * buffer[i] is expected to be little endian unsigned long. 80562306a36Sopenharmony_ci * bitmap must be locked by drbd_bm_lock. 80662306a36Sopenharmony_ci * currently only used from receive_bitmap. 80762306a36Sopenharmony_ci */ 80862306a36Sopenharmony_civoid drbd_bm_merge_lel(struct drbd_device *device, size_t offset, size_t number, 80962306a36Sopenharmony_ci unsigned long *buffer) 81062306a36Sopenharmony_ci{ 81162306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 81262306a36Sopenharmony_ci unsigned long *p_addr, *bm; 81362306a36Sopenharmony_ci unsigned long word, bits; 81462306a36Sopenharmony_ci unsigned int idx; 81562306a36Sopenharmony_ci size_t end, do_now; 81662306a36Sopenharmony_ci 81762306a36Sopenharmony_ci end = offset + number; 81862306a36Sopenharmony_ci 81962306a36Sopenharmony_ci if (!expect(device, b)) 82062306a36Sopenharmony_ci return; 82162306a36Sopenharmony_ci if (!expect(device, b->bm_pages)) 82262306a36Sopenharmony_ci return; 82362306a36Sopenharmony_ci if (number == 0) 82462306a36Sopenharmony_ci return; 82562306a36Sopenharmony_ci WARN_ON(offset >= b->bm_words); 82662306a36Sopenharmony_ci WARN_ON(end > b->bm_words); 82762306a36Sopenharmony_ci 82862306a36Sopenharmony_ci spin_lock_irq(&b->bm_lock); 82962306a36Sopenharmony_ci while (offset < end) { 83062306a36Sopenharmony_ci do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset; 83162306a36Sopenharmony_ci idx = bm_word_to_page_idx(b, offset); 83262306a36Sopenharmony_ci p_addr = bm_map_pidx(b, idx); 83362306a36Sopenharmony_ci bm = p_addr + MLPP(offset); 83462306a36Sopenharmony_ci offset += do_now; 83562306a36Sopenharmony_ci while (do_now--) { 83662306a36Sopenharmony_ci bits = hweight_long(*bm); 83762306a36Sopenharmony_ci word = *bm | *buffer++; 83862306a36Sopenharmony_ci *bm++ = word; 83962306a36Sopenharmony_ci b->bm_set += hweight_long(word) - bits; 84062306a36Sopenharmony_ci } 84162306a36Sopenharmony_ci bm_unmap(p_addr); 84262306a36Sopenharmony_ci bm_set_page_need_writeout(b->bm_pages[idx]); 84362306a36Sopenharmony_ci } 84462306a36Sopenharmony_ci /* with 32bit <-> 64bit cross-platform connect 84562306a36Sopenharmony_ci * this is only correct for current usage, 84662306a36Sopenharmony_ci * where we _know_ that we are 64 bit aligned, 84762306a36Sopenharmony_ci * and know that this function is used in this way, too... 84862306a36Sopenharmony_ci */ 84962306a36Sopenharmony_ci if (end == b->bm_words) 85062306a36Sopenharmony_ci b->bm_set -= bm_clear_surplus(b); 85162306a36Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 85262306a36Sopenharmony_ci} 85362306a36Sopenharmony_ci 85462306a36Sopenharmony_ci/* copy number words from the bitmap starting at offset into the buffer. 85562306a36Sopenharmony_ci * buffer[i] will be little endian unsigned long. 85662306a36Sopenharmony_ci */ 85762306a36Sopenharmony_civoid drbd_bm_get_lel(struct drbd_device *device, size_t offset, size_t number, 85862306a36Sopenharmony_ci unsigned long *buffer) 85962306a36Sopenharmony_ci{ 86062306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 86162306a36Sopenharmony_ci unsigned long *p_addr, *bm; 86262306a36Sopenharmony_ci size_t end, do_now; 86362306a36Sopenharmony_ci 86462306a36Sopenharmony_ci end = offset + number; 86562306a36Sopenharmony_ci 86662306a36Sopenharmony_ci if (!expect(device, b)) 86762306a36Sopenharmony_ci return; 86862306a36Sopenharmony_ci if (!expect(device, b->bm_pages)) 86962306a36Sopenharmony_ci return; 87062306a36Sopenharmony_ci 87162306a36Sopenharmony_ci spin_lock_irq(&b->bm_lock); 87262306a36Sopenharmony_ci if ((offset >= b->bm_words) || 87362306a36Sopenharmony_ci (end > b->bm_words) || 87462306a36Sopenharmony_ci (number <= 0)) 87562306a36Sopenharmony_ci drbd_err(device, "offset=%lu number=%lu bm_words=%lu\n", 87662306a36Sopenharmony_ci (unsigned long) offset, 87762306a36Sopenharmony_ci (unsigned long) number, 87862306a36Sopenharmony_ci (unsigned long) b->bm_words); 87962306a36Sopenharmony_ci else { 88062306a36Sopenharmony_ci while (offset < end) { 88162306a36Sopenharmony_ci do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset; 88262306a36Sopenharmony_ci p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset)); 88362306a36Sopenharmony_ci bm = p_addr + MLPP(offset); 88462306a36Sopenharmony_ci offset += do_now; 88562306a36Sopenharmony_ci while (do_now--) 88662306a36Sopenharmony_ci *buffer++ = *bm++; 88762306a36Sopenharmony_ci bm_unmap(p_addr); 88862306a36Sopenharmony_ci } 88962306a36Sopenharmony_ci } 89062306a36Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 89162306a36Sopenharmony_ci} 89262306a36Sopenharmony_ci 89362306a36Sopenharmony_ci/* set all bits in the bitmap */ 89462306a36Sopenharmony_civoid drbd_bm_set_all(struct drbd_device *device) 89562306a36Sopenharmony_ci{ 89662306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 89762306a36Sopenharmony_ci if (!expect(device, b)) 89862306a36Sopenharmony_ci return; 89962306a36Sopenharmony_ci if (!expect(device, b->bm_pages)) 90062306a36Sopenharmony_ci return; 90162306a36Sopenharmony_ci 90262306a36Sopenharmony_ci spin_lock_irq(&b->bm_lock); 90362306a36Sopenharmony_ci bm_memset(b, 0, 0xff, b->bm_words); 90462306a36Sopenharmony_ci (void)bm_clear_surplus(b); 90562306a36Sopenharmony_ci b->bm_set = b->bm_bits; 90662306a36Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 90762306a36Sopenharmony_ci} 90862306a36Sopenharmony_ci 90962306a36Sopenharmony_ci/* clear all bits in the bitmap */ 91062306a36Sopenharmony_civoid drbd_bm_clear_all(struct drbd_device *device) 91162306a36Sopenharmony_ci{ 91262306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 91362306a36Sopenharmony_ci if (!expect(device, b)) 91462306a36Sopenharmony_ci return; 91562306a36Sopenharmony_ci if (!expect(device, b->bm_pages)) 91662306a36Sopenharmony_ci return; 91762306a36Sopenharmony_ci 91862306a36Sopenharmony_ci spin_lock_irq(&b->bm_lock); 91962306a36Sopenharmony_ci bm_memset(b, 0, 0, b->bm_words); 92062306a36Sopenharmony_ci b->bm_set = 0; 92162306a36Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 92262306a36Sopenharmony_ci} 92362306a36Sopenharmony_ci 92462306a36Sopenharmony_cistatic void drbd_bm_aio_ctx_destroy(struct kref *kref) 92562306a36Sopenharmony_ci{ 92662306a36Sopenharmony_ci struct drbd_bm_aio_ctx *ctx = container_of(kref, struct drbd_bm_aio_ctx, kref); 92762306a36Sopenharmony_ci unsigned long flags; 92862306a36Sopenharmony_ci 92962306a36Sopenharmony_ci spin_lock_irqsave(&ctx->device->resource->req_lock, flags); 93062306a36Sopenharmony_ci list_del(&ctx->list); 93162306a36Sopenharmony_ci spin_unlock_irqrestore(&ctx->device->resource->req_lock, flags); 93262306a36Sopenharmony_ci put_ldev(ctx->device); 93362306a36Sopenharmony_ci kfree(ctx); 93462306a36Sopenharmony_ci} 93562306a36Sopenharmony_ci 93662306a36Sopenharmony_ci/* bv_page may be a copy, or may be the original */ 93762306a36Sopenharmony_cistatic void drbd_bm_endio(struct bio *bio) 93862306a36Sopenharmony_ci{ 93962306a36Sopenharmony_ci struct drbd_bm_aio_ctx *ctx = bio->bi_private; 94062306a36Sopenharmony_ci struct drbd_device *device = ctx->device; 94162306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 94262306a36Sopenharmony_ci unsigned int idx = bm_page_to_idx(bio_first_page_all(bio)); 94362306a36Sopenharmony_ci 94462306a36Sopenharmony_ci if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 && 94562306a36Sopenharmony_ci !bm_test_page_unchanged(b->bm_pages[idx])) 94662306a36Sopenharmony_ci drbd_warn(device, "bitmap page idx %u changed during IO!\n", idx); 94762306a36Sopenharmony_ci 94862306a36Sopenharmony_ci if (bio->bi_status) { 94962306a36Sopenharmony_ci /* ctx error will hold the completed-last non-zero error code, 95062306a36Sopenharmony_ci * in case error codes differ. */ 95162306a36Sopenharmony_ci ctx->error = blk_status_to_errno(bio->bi_status); 95262306a36Sopenharmony_ci bm_set_page_io_err(b->bm_pages[idx]); 95362306a36Sopenharmony_ci /* Not identical to on disk version of it. 95462306a36Sopenharmony_ci * Is BM_PAGE_IO_ERROR enough? */ 95562306a36Sopenharmony_ci if (drbd_ratelimit()) 95662306a36Sopenharmony_ci drbd_err(device, "IO ERROR %d on bitmap page idx %u\n", 95762306a36Sopenharmony_ci bio->bi_status, idx); 95862306a36Sopenharmony_ci } else { 95962306a36Sopenharmony_ci bm_clear_page_io_err(b->bm_pages[idx]); 96062306a36Sopenharmony_ci dynamic_drbd_dbg(device, "bitmap page idx %u completed\n", idx); 96162306a36Sopenharmony_ci } 96262306a36Sopenharmony_ci 96362306a36Sopenharmony_ci bm_page_unlock_io(device, idx); 96462306a36Sopenharmony_ci 96562306a36Sopenharmony_ci if (ctx->flags & BM_AIO_COPY_PAGES) 96662306a36Sopenharmony_ci mempool_free(bio->bi_io_vec[0].bv_page, &drbd_md_io_page_pool); 96762306a36Sopenharmony_ci 96862306a36Sopenharmony_ci bio_put(bio); 96962306a36Sopenharmony_ci 97062306a36Sopenharmony_ci if (atomic_dec_and_test(&ctx->in_flight)) { 97162306a36Sopenharmony_ci ctx->done = 1; 97262306a36Sopenharmony_ci wake_up(&device->misc_wait); 97362306a36Sopenharmony_ci kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy); 97462306a36Sopenharmony_ci } 97562306a36Sopenharmony_ci} 97662306a36Sopenharmony_ci 97762306a36Sopenharmony_ci/* For the layout, see comment above drbd_md_set_sector_offsets(). */ 97862306a36Sopenharmony_cistatic inline sector_t drbd_md_last_bitmap_sector(struct drbd_backing_dev *bdev) 97962306a36Sopenharmony_ci{ 98062306a36Sopenharmony_ci switch (bdev->md.meta_dev_idx) { 98162306a36Sopenharmony_ci case DRBD_MD_INDEX_INTERNAL: 98262306a36Sopenharmony_ci case DRBD_MD_INDEX_FLEX_INT: 98362306a36Sopenharmony_ci return bdev->md.md_offset + bdev->md.al_offset -1; 98462306a36Sopenharmony_ci case DRBD_MD_INDEX_FLEX_EXT: 98562306a36Sopenharmony_ci default: 98662306a36Sopenharmony_ci return bdev->md.md_offset + bdev->md.md_size_sect -1; 98762306a36Sopenharmony_ci } 98862306a36Sopenharmony_ci} 98962306a36Sopenharmony_ci 99062306a36Sopenharmony_cistatic void bm_page_io_async(struct drbd_bm_aio_ctx *ctx, int page_nr) __must_hold(local) 99162306a36Sopenharmony_ci{ 99262306a36Sopenharmony_ci struct drbd_device *device = ctx->device; 99362306a36Sopenharmony_ci enum req_op op = ctx->flags & BM_AIO_READ ? REQ_OP_READ : REQ_OP_WRITE; 99462306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 99562306a36Sopenharmony_ci struct bio *bio; 99662306a36Sopenharmony_ci struct page *page; 99762306a36Sopenharmony_ci sector_t last_bm_sect; 99862306a36Sopenharmony_ci sector_t first_bm_sect; 99962306a36Sopenharmony_ci sector_t on_disk_sector; 100062306a36Sopenharmony_ci unsigned int len; 100162306a36Sopenharmony_ci 100262306a36Sopenharmony_ci first_bm_sect = device->ldev->md.md_offset + device->ldev->md.bm_offset; 100362306a36Sopenharmony_ci on_disk_sector = first_bm_sect + (((sector_t)page_nr) << (PAGE_SHIFT-SECTOR_SHIFT)); 100462306a36Sopenharmony_ci 100562306a36Sopenharmony_ci /* this might happen with very small 100662306a36Sopenharmony_ci * flexible external meta data device, 100762306a36Sopenharmony_ci * or with PAGE_SIZE > 4k */ 100862306a36Sopenharmony_ci last_bm_sect = drbd_md_last_bitmap_sector(device->ldev); 100962306a36Sopenharmony_ci if (first_bm_sect <= on_disk_sector && last_bm_sect >= on_disk_sector) { 101062306a36Sopenharmony_ci sector_t len_sect = last_bm_sect - on_disk_sector + 1; 101162306a36Sopenharmony_ci if (len_sect < PAGE_SIZE/SECTOR_SIZE) 101262306a36Sopenharmony_ci len = (unsigned int)len_sect*SECTOR_SIZE; 101362306a36Sopenharmony_ci else 101462306a36Sopenharmony_ci len = PAGE_SIZE; 101562306a36Sopenharmony_ci } else { 101662306a36Sopenharmony_ci if (drbd_ratelimit()) { 101762306a36Sopenharmony_ci drbd_err(device, "Invalid offset during on-disk bitmap access: " 101862306a36Sopenharmony_ci "page idx %u, sector %llu\n", page_nr, on_disk_sector); 101962306a36Sopenharmony_ci } 102062306a36Sopenharmony_ci ctx->error = -EIO; 102162306a36Sopenharmony_ci bm_set_page_io_err(b->bm_pages[page_nr]); 102262306a36Sopenharmony_ci if (atomic_dec_and_test(&ctx->in_flight)) { 102362306a36Sopenharmony_ci ctx->done = 1; 102462306a36Sopenharmony_ci wake_up(&device->misc_wait); 102562306a36Sopenharmony_ci kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy); 102662306a36Sopenharmony_ci } 102762306a36Sopenharmony_ci return; 102862306a36Sopenharmony_ci } 102962306a36Sopenharmony_ci 103062306a36Sopenharmony_ci /* serialize IO on this page */ 103162306a36Sopenharmony_ci bm_page_lock_io(device, page_nr); 103262306a36Sopenharmony_ci /* before memcpy and submit, 103362306a36Sopenharmony_ci * so it can be redirtied any time */ 103462306a36Sopenharmony_ci bm_set_page_unchanged(b->bm_pages[page_nr]); 103562306a36Sopenharmony_ci 103662306a36Sopenharmony_ci if (ctx->flags & BM_AIO_COPY_PAGES) { 103762306a36Sopenharmony_ci page = mempool_alloc(&drbd_md_io_page_pool, 103862306a36Sopenharmony_ci GFP_NOIO | __GFP_HIGHMEM); 103962306a36Sopenharmony_ci copy_highpage(page, b->bm_pages[page_nr]); 104062306a36Sopenharmony_ci bm_store_page_idx(page, page_nr); 104162306a36Sopenharmony_ci } else 104262306a36Sopenharmony_ci page = b->bm_pages[page_nr]; 104362306a36Sopenharmony_ci bio = bio_alloc_bioset(device->ldev->md_bdev, 1, op, GFP_NOIO, 104462306a36Sopenharmony_ci &drbd_md_io_bio_set); 104562306a36Sopenharmony_ci bio->bi_iter.bi_sector = on_disk_sector; 104662306a36Sopenharmony_ci __bio_add_page(bio, page, len, 0); 104762306a36Sopenharmony_ci bio->bi_private = ctx; 104862306a36Sopenharmony_ci bio->bi_end_io = drbd_bm_endio; 104962306a36Sopenharmony_ci 105062306a36Sopenharmony_ci if (drbd_insert_fault(device, (op == REQ_OP_WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) { 105162306a36Sopenharmony_ci bio_io_error(bio); 105262306a36Sopenharmony_ci } else { 105362306a36Sopenharmony_ci submit_bio(bio); 105462306a36Sopenharmony_ci /* this should not count as user activity and cause the 105562306a36Sopenharmony_ci * resync to throttle -- see drbd_rs_should_slow_down(). */ 105662306a36Sopenharmony_ci atomic_add(len >> 9, &device->rs_sect_ev); 105762306a36Sopenharmony_ci } 105862306a36Sopenharmony_ci} 105962306a36Sopenharmony_ci 106062306a36Sopenharmony_ci/* 106162306a36Sopenharmony_ci * bm_rw: read/write the whole bitmap from/to its on disk location. 106262306a36Sopenharmony_ci */ 106362306a36Sopenharmony_cistatic int bm_rw(struct drbd_device *device, const unsigned int flags, unsigned lazy_writeout_upper_idx) __must_hold(local) 106462306a36Sopenharmony_ci{ 106562306a36Sopenharmony_ci struct drbd_bm_aio_ctx *ctx; 106662306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 106762306a36Sopenharmony_ci unsigned int num_pages, i, count = 0; 106862306a36Sopenharmony_ci unsigned long now; 106962306a36Sopenharmony_ci char ppb[10]; 107062306a36Sopenharmony_ci int err = 0; 107162306a36Sopenharmony_ci 107262306a36Sopenharmony_ci /* 107362306a36Sopenharmony_ci * We are protected against bitmap disappearing/resizing by holding an 107462306a36Sopenharmony_ci * ldev reference (caller must have called get_ldev()). 107562306a36Sopenharmony_ci * For read/write, we are protected against changes to the bitmap by 107662306a36Sopenharmony_ci * the bitmap lock (see drbd_bitmap_io). 107762306a36Sopenharmony_ci * For lazy writeout, we don't care for ongoing changes to the bitmap, 107862306a36Sopenharmony_ci * as we submit copies of pages anyways. 107962306a36Sopenharmony_ci */ 108062306a36Sopenharmony_ci 108162306a36Sopenharmony_ci ctx = kmalloc(sizeof(struct drbd_bm_aio_ctx), GFP_NOIO); 108262306a36Sopenharmony_ci if (!ctx) 108362306a36Sopenharmony_ci return -ENOMEM; 108462306a36Sopenharmony_ci 108562306a36Sopenharmony_ci *ctx = (struct drbd_bm_aio_ctx) { 108662306a36Sopenharmony_ci .device = device, 108762306a36Sopenharmony_ci .start_jif = jiffies, 108862306a36Sopenharmony_ci .in_flight = ATOMIC_INIT(1), 108962306a36Sopenharmony_ci .done = 0, 109062306a36Sopenharmony_ci .flags = flags, 109162306a36Sopenharmony_ci .error = 0, 109262306a36Sopenharmony_ci .kref = KREF_INIT(2), 109362306a36Sopenharmony_ci }; 109462306a36Sopenharmony_ci 109562306a36Sopenharmony_ci if (!get_ldev_if_state(device, D_ATTACHING)) { /* put is in drbd_bm_aio_ctx_destroy() */ 109662306a36Sopenharmony_ci drbd_err(device, "ASSERT FAILED: get_ldev_if_state() == 1 in bm_rw()\n"); 109762306a36Sopenharmony_ci kfree(ctx); 109862306a36Sopenharmony_ci return -ENODEV; 109962306a36Sopenharmony_ci } 110062306a36Sopenharmony_ci /* Here D_ATTACHING is sufficient since drbd_bm_read() is called only from 110162306a36Sopenharmony_ci drbd_adm_attach(), after device->ldev was assigned. */ 110262306a36Sopenharmony_ci 110362306a36Sopenharmony_ci if (0 == (ctx->flags & ~BM_AIO_READ)) 110462306a36Sopenharmony_ci WARN_ON(!(BM_LOCKED_MASK & b->bm_flags)); 110562306a36Sopenharmony_ci 110662306a36Sopenharmony_ci spin_lock_irq(&device->resource->req_lock); 110762306a36Sopenharmony_ci list_add_tail(&ctx->list, &device->pending_bitmap_io); 110862306a36Sopenharmony_ci spin_unlock_irq(&device->resource->req_lock); 110962306a36Sopenharmony_ci 111062306a36Sopenharmony_ci num_pages = b->bm_number_of_pages; 111162306a36Sopenharmony_ci 111262306a36Sopenharmony_ci now = jiffies; 111362306a36Sopenharmony_ci 111462306a36Sopenharmony_ci /* let the layers below us try to merge these bios... */ 111562306a36Sopenharmony_ci 111662306a36Sopenharmony_ci if (flags & BM_AIO_READ) { 111762306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) { 111862306a36Sopenharmony_ci atomic_inc(&ctx->in_flight); 111962306a36Sopenharmony_ci bm_page_io_async(ctx, i); 112062306a36Sopenharmony_ci ++count; 112162306a36Sopenharmony_ci cond_resched(); 112262306a36Sopenharmony_ci } 112362306a36Sopenharmony_ci } else if (flags & BM_AIO_WRITE_HINTED) { 112462306a36Sopenharmony_ci /* ASSERT: BM_AIO_WRITE_ALL_PAGES is not set. */ 112562306a36Sopenharmony_ci unsigned int hint; 112662306a36Sopenharmony_ci for (hint = 0; hint < b->n_bitmap_hints; hint++) { 112762306a36Sopenharmony_ci i = b->al_bitmap_hints[hint]; 112862306a36Sopenharmony_ci if (i >= num_pages) /* == -1U: no hint here. */ 112962306a36Sopenharmony_ci continue; 113062306a36Sopenharmony_ci /* Several AL-extents may point to the same page. */ 113162306a36Sopenharmony_ci if (!test_and_clear_bit(BM_PAGE_HINT_WRITEOUT, 113262306a36Sopenharmony_ci &page_private(b->bm_pages[i]))) 113362306a36Sopenharmony_ci continue; 113462306a36Sopenharmony_ci /* Has it even changed? */ 113562306a36Sopenharmony_ci if (bm_test_page_unchanged(b->bm_pages[i])) 113662306a36Sopenharmony_ci continue; 113762306a36Sopenharmony_ci atomic_inc(&ctx->in_flight); 113862306a36Sopenharmony_ci bm_page_io_async(ctx, i); 113962306a36Sopenharmony_ci ++count; 114062306a36Sopenharmony_ci } 114162306a36Sopenharmony_ci } else { 114262306a36Sopenharmony_ci for (i = 0; i < num_pages; i++) { 114362306a36Sopenharmony_ci /* ignore completely unchanged pages */ 114462306a36Sopenharmony_ci if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx) 114562306a36Sopenharmony_ci break; 114662306a36Sopenharmony_ci if (!(flags & BM_AIO_WRITE_ALL_PAGES) && 114762306a36Sopenharmony_ci bm_test_page_unchanged(b->bm_pages[i])) { 114862306a36Sopenharmony_ci dynamic_drbd_dbg(device, "skipped bm write for idx %u\n", i); 114962306a36Sopenharmony_ci continue; 115062306a36Sopenharmony_ci } 115162306a36Sopenharmony_ci /* during lazy writeout, 115262306a36Sopenharmony_ci * ignore those pages not marked for lazy writeout. */ 115362306a36Sopenharmony_ci if (lazy_writeout_upper_idx && 115462306a36Sopenharmony_ci !bm_test_page_lazy_writeout(b->bm_pages[i])) { 115562306a36Sopenharmony_ci dynamic_drbd_dbg(device, "skipped bm lazy write for idx %u\n", i); 115662306a36Sopenharmony_ci continue; 115762306a36Sopenharmony_ci } 115862306a36Sopenharmony_ci atomic_inc(&ctx->in_flight); 115962306a36Sopenharmony_ci bm_page_io_async(ctx, i); 116062306a36Sopenharmony_ci ++count; 116162306a36Sopenharmony_ci cond_resched(); 116262306a36Sopenharmony_ci } 116362306a36Sopenharmony_ci } 116462306a36Sopenharmony_ci 116562306a36Sopenharmony_ci /* 116662306a36Sopenharmony_ci * We initialize ctx->in_flight to one to make sure drbd_bm_endio 116762306a36Sopenharmony_ci * will not set ctx->done early, and decrement / test it here. If there 116862306a36Sopenharmony_ci * are still some bios in flight, we need to wait for them here. 116962306a36Sopenharmony_ci * If all IO is done already (or nothing had been submitted), there is 117062306a36Sopenharmony_ci * no need to wait. Still, we need to put the kref associated with the 117162306a36Sopenharmony_ci * "in_flight reached zero, all done" event. 117262306a36Sopenharmony_ci */ 117362306a36Sopenharmony_ci if (!atomic_dec_and_test(&ctx->in_flight)) 117462306a36Sopenharmony_ci wait_until_done_or_force_detached(device, device->ldev, &ctx->done); 117562306a36Sopenharmony_ci else 117662306a36Sopenharmony_ci kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy); 117762306a36Sopenharmony_ci 117862306a36Sopenharmony_ci /* summary for global bitmap IO */ 117962306a36Sopenharmony_ci if (flags == 0) { 118062306a36Sopenharmony_ci unsigned int ms = jiffies_to_msecs(jiffies - now); 118162306a36Sopenharmony_ci if (ms > 5) { 118262306a36Sopenharmony_ci drbd_info(device, "bitmap %s of %u pages took %u ms\n", 118362306a36Sopenharmony_ci (flags & BM_AIO_READ) ? "READ" : "WRITE", 118462306a36Sopenharmony_ci count, ms); 118562306a36Sopenharmony_ci } 118662306a36Sopenharmony_ci } 118762306a36Sopenharmony_ci 118862306a36Sopenharmony_ci if (ctx->error) { 118962306a36Sopenharmony_ci drbd_alert(device, "we had at least one MD IO ERROR during bitmap IO\n"); 119062306a36Sopenharmony_ci drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR); 119162306a36Sopenharmony_ci err = -EIO; /* ctx->error ? */ 119262306a36Sopenharmony_ci } 119362306a36Sopenharmony_ci 119462306a36Sopenharmony_ci if (atomic_read(&ctx->in_flight)) 119562306a36Sopenharmony_ci err = -EIO; /* Disk timeout/force-detach during IO... */ 119662306a36Sopenharmony_ci 119762306a36Sopenharmony_ci now = jiffies; 119862306a36Sopenharmony_ci if (flags & BM_AIO_READ) { 119962306a36Sopenharmony_ci b->bm_set = bm_count_bits(b); 120062306a36Sopenharmony_ci drbd_info(device, "recounting of set bits took additional %lu jiffies\n", 120162306a36Sopenharmony_ci jiffies - now); 120262306a36Sopenharmony_ci } 120362306a36Sopenharmony_ci now = b->bm_set; 120462306a36Sopenharmony_ci 120562306a36Sopenharmony_ci if ((flags & ~BM_AIO_READ) == 0) 120662306a36Sopenharmony_ci drbd_info(device, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n", 120762306a36Sopenharmony_ci ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now); 120862306a36Sopenharmony_ci 120962306a36Sopenharmony_ci kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy); 121062306a36Sopenharmony_ci return err; 121162306a36Sopenharmony_ci} 121262306a36Sopenharmony_ci 121362306a36Sopenharmony_ci/** 121462306a36Sopenharmony_ci * drbd_bm_read() - Read the whole bitmap from its on disk location. 121562306a36Sopenharmony_ci * @device: DRBD device. 121662306a36Sopenharmony_ci */ 121762306a36Sopenharmony_ciint drbd_bm_read(struct drbd_device *device, 121862306a36Sopenharmony_ci struct drbd_peer_device *peer_device) __must_hold(local) 121962306a36Sopenharmony_ci 122062306a36Sopenharmony_ci{ 122162306a36Sopenharmony_ci return bm_rw(device, BM_AIO_READ, 0); 122262306a36Sopenharmony_ci} 122362306a36Sopenharmony_ci 122462306a36Sopenharmony_ci/** 122562306a36Sopenharmony_ci * drbd_bm_write() - Write the whole bitmap to its on disk location. 122662306a36Sopenharmony_ci * @device: DRBD device. 122762306a36Sopenharmony_ci * 122862306a36Sopenharmony_ci * Will only write pages that have changed since last IO. 122962306a36Sopenharmony_ci */ 123062306a36Sopenharmony_ciint drbd_bm_write(struct drbd_device *device, 123162306a36Sopenharmony_ci struct drbd_peer_device *peer_device) __must_hold(local) 123262306a36Sopenharmony_ci{ 123362306a36Sopenharmony_ci return bm_rw(device, 0, 0); 123462306a36Sopenharmony_ci} 123562306a36Sopenharmony_ci 123662306a36Sopenharmony_ci/** 123762306a36Sopenharmony_ci * drbd_bm_write_all() - Write the whole bitmap to its on disk location. 123862306a36Sopenharmony_ci * @device: DRBD device. 123962306a36Sopenharmony_ci * 124062306a36Sopenharmony_ci * Will write all pages. 124162306a36Sopenharmony_ci */ 124262306a36Sopenharmony_ciint drbd_bm_write_all(struct drbd_device *device, 124362306a36Sopenharmony_ci struct drbd_peer_device *peer_device) __must_hold(local) 124462306a36Sopenharmony_ci{ 124562306a36Sopenharmony_ci return bm_rw(device, BM_AIO_WRITE_ALL_PAGES, 0); 124662306a36Sopenharmony_ci} 124762306a36Sopenharmony_ci 124862306a36Sopenharmony_ci/** 124962306a36Sopenharmony_ci * drbd_bm_write_lazy() - Write bitmap pages 0 to @upper_idx-1, if they have changed. 125062306a36Sopenharmony_ci * @device: DRBD device. 125162306a36Sopenharmony_ci * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages 125262306a36Sopenharmony_ci */ 125362306a36Sopenharmony_ciint drbd_bm_write_lazy(struct drbd_device *device, unsigned upper_idx) __must_hold(local) 125462306a36Sopenharmony_ci{ 125562306a36Sopenharmony_ci return bm_rw(device, BM_AIO_COPY_PAGES, upper_idx); 125662306a36Sopenharmony_ci} 125762306a36Sopenharmony_ci 125862306a36Sopenharmony_ci/** 125962306a36Sopenharmony_ci * drbd_bm_write_copy_pages() - Write the whole bitmap to its on disk location. 126062306a36Sopenharmony_ci * @device: DRBD device. 126162306a36Sopenharmony_ci * 126262306a36Sopenharmony_ci * Will only write pages that have changed since last IO. 126362306a36Sopenharmony_ci * In contrast to drbd_bm_write(), this will copy the bitmap pages 126462306a36Sopenharmony_ci * to temporary writeout pages. It is intended to trigger a full write-out 126562306a36Sopenharmony_ci * while still allowing the bitmap to change, for example if a resync or online 126662306a36Sopenharmony_ci * verify is aborted due to a failed peer disk, while local IO continues, or 126762306a36Sopenharmony_ci * pending resync acks are still being processed. 126862306a36Sopenharmony_ci */ 126962306a36Sopenharmony_ciint drbd_bm_write_copy_pages(struct drbd_device *device, 127062306a36Sopenharmony_ci struct drbd_peer_device *peer_device) __must_hold(local) 127162306a36Sopenharmony_ci{ 127262306a36Sopenharmony_ci return bm_rw(device, BM_AIO_COPY_PAGES, 0); 127362306a36Sopenharmony_ci} 127462306a36Sopenharmony_ci 127562306a36Sopenharmony_ci/** 127662306a36Sopenharmony_ci * drbd_bm_write_hinted() - Write bitmap pages with "hint" marks, if they have changed. 127762306a36Sopenharmony_ci * @device: DRBD device. 127862306a36Sopenharmony_ci */ 127962306a36Sopenharmony_ciint drbd_bm_write_hinted(struct drbd_device *device) __must_hold(local) 128062306a36Sopenharmony_ci{ 128162306a36Sopenharmony_ci return bm_rw(device, BM_AIO_WRITE_HINTED | BM_AIO_COPY_PAGES, 0); 128262306a36Sopenharmony_ci} 128362306a36Sopenharmony_ci 128462306a36Sopenharmony_ci/* NOTE 128562306a36Sopenharmony_ci * find_first_bit returns int, we return unsigned long. 128662306a36Sopenharmony_ci * For this to work on 32bit arch with bitnumbers > (1<<32), 128762306a36Sopenharmony_ci * we'd need to return u64, and get a whole lot of other places 128862306a36Sopenharmony_ci * fixed where we still use unsigned long. 128962306a36Sopenharmony_ci * 129062306a36Sopenharmony_ci * this returns a bit number, NOT a sector! 129162306a36Sopenharmony_ci */ 129262306a36Sopenharmony_cistatic unsigned long __bm_find_next(struct drbd_device *device, unsigned long bm_fo, 129362306a36Sopenharmony_ci const int find_zero_bit) 129462306a36Sopenharmony_ci{ 129562306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 129662306a36Sopenharmony_ci unsigned long *p_addr; 129762306a36Sopenharmony_ci unsigned long bit_offset; 129862306a36Sopenharmony_ci unsigned i; 129962306a36Sopenharmony_ci 130062306a36Sopenharmony_ci 130162306a36Sopenharmony_ci if (bm_fo > b->bm_bits) { 130262306a36Sopenharmony_ci drbd_err(device, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits); 130362306a36Sopenharmony_ci bm_fo = DRBD_END_OF_BITMAP; 130462306a36Sopenharmony_ci } else { 130562306a36Sopenharmony_ci while (bm_fo < b->bm_bits) { 130662306a36Sopenharmony_ci /* bit offset of the first bit in the page */ 130762306a36Sopenharmony_ci bit_offset = bm_fo & ~BITS_PER_PAGE_MASK; 130862306a36Sopenharmony_ci p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo)); 130962306a36Sopenharmony_ci 131062306a36Sopenharmony_ci if (find_zero_bit) 131162306a36Sopenharmony_ci i = find_next_zero_bit_le(p_addr, 131262306a36Sopenharmony_ci PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK); 131362306a36Sopenharmony_ci else 131462306a36Sopenharmony_ci i = find_next_bit_le(p_addr, 131562306a36Sopenharmony_ci PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK); 131662306a36Sopenharmony_ci 131762306a36Sopenharmony_ci __bm_unmap(p_addr); 131862306a36Sopenharmony_ci if (i < PAGE_SIZE*8) { 131962306a36Sopenharmony_ci bm_fo = bit_offset + i; 132062306a36Sopenharmony_ci if (bm_fo >= b->bm_bits) 132162306a36Sopenharmony_ci break; 132262306a36Sopenharmony_ci goto found; 132362306a36Sopenharmony_ci } 132462306a36Sopenharmony_ci bm_fo = bit_offset + PAGE_SIZE*8; 132562306a36Sopenharmony_ci } 132662306a36Sopenharmony_ci bm_fo = DRBD_END_OF_BITMAP; 132762306a36Sopenharmony_ci } 132862306a36Sopenharmony_ci found: 132962306a36Sopenharmony_ci return bm_fo; 133062306a36Sopenharmony_ci} 133162306a36Sopenharmony_ci 133262306a36Sopenharmony_cistatic unsigned long bm_find_next(struct drbd_device *device, 133362306a36Sopenharmony_ci unsigned long bm_fo, const int find_zero_bit) 133462306a36Sopenharmony_ci{ 133562306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 133662306a36Sopenharmony_ci unsigned long i = DRBD_END_OF_BITMAP; 133762306a36Sopenharmony_ci 133862306a36Sopenharmony_ci if (!expect(device, b)) 133962306a36Sopenharmony_ci return i; 134062306a36Sopenharmony_ci if (!expect(device, b->bm_pages)) 134162306a36Sopenharmony_ci return i; 134262306a36Sopenharmony_ci 134362306a36Sopenharmony_ci spin_lock_irq(&b->bm_lock); 134462306a36Sopenharmony_ci if (BM_DONT_TEST & b->bm_flags) 134562306a36Sopenharmony_ci bm_print_lock_info(device); 134662306a36Sopenharmony_ci 134762306a36Sopenharmony_ci i = __bm_find_next(device, bm_fo, find_zero_bit); 134862306a36Sopenharmony_ci 134962306a36Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 135062306a36Sopenharmony_ci return i; 135162306a36Sopenharmony_ci} 135262306a36Sopenharmony_ci 135362306a36Sopenharmony_ciunsigned long drbd_bm_find_next(struct drbd_device *device, unsigned long bm_fo) 135462306a36Sopenharmony_ci{ 135562306a36Sopenharmony_ci return bm_find_next(device, bm_fo, 0); 135662306a36Sopenharmony_ci} 135762306a36Sopenharmony_ci 135862306a36Sopenharmony_ci#if 0 135962306a36Sopenharmony_ci/* not yet needed for anything. */ 136062306a36Sopenharmony_ciunsigned long drbd_bm_find_next_zero(struct drbd_device *device, unsigned long bm_fo) 136162306a36Sopenharmony_ci{ 136262306a36Sopenharmony_ci return bm_find_next(device, bm_fo, 1); 136362306a36Sopenharmony_ci} 136462306a36Sopenharmony_ci#endif 136562306a36Sopenharmony_ci 136662306a36Sopenharmony_ci/* does not spin_lock_irqsave. 136762306a36Sopenharmony_ci * you must take drbd_bm_lock() first */ 136862306a36Sopenharmony_ciunsigned long _drbd_bm_find_next(struct drbd_device *device, unsigned long bm_fo) 136962306a36Sopenharmony_ci{ 137062306a36Sopenharmony_ci /* WARN_ON(!(BM_DONT_SET & device->b->bm_flags)); */ 137162306a36Sopenharmony_ci return __bm_find_next(device, bm_fo, 0); 137262306a36Sopenharmony_ci} 137362306a36Sopenharmony_ci 137462306a36Sopenharmony_ciunsigned long _drbd_bm_find_next_zero(struct drbd_device *device, unsigned long bm_fo) 137562306a36Sopenharmony_ci{ 137662306a36Sopenharmony_ci /* WARN_ON(!(BM_DONT_SET & device->b->bm_flags)); */ 137762306a36Sopenharmony_ci return __bm_find_next(device, bm_fo, 1); 137862306a36Sopenharmony_ci} 137962306a36Sopenharmony_ci 138062306a36Sopenharmony_ci/* returns number of bits actually changed. 138162306a36Sopenharmony_ci * for val != 0, we change 0 -> 1, return code positive 138262306a36Sopenharmony_ci * for val == 0, we change 1 -> 0, return code negative 138362306a36Sopenharmony_ci * wants bitnr, not sector. 138462306a36Sopenharmony_ci * expected to be called for only a few bits (e - s about BITS_PER_LONG). 138562306a36Sopenharmony_ci * Must hold bitmap lock already. */ 138662306a36Sopenharmony_cistatic int __bm_change_bits_to(struct drbd_device *device, const unsigned long s, 138762306a36Sopenharmony_ci unsigned long e, int val) 138862306a36Sopenharmony_ci{ 138962306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 139062306a36Sopenharmony_ci unsigned long *p_addr = NULL; 139162306a36Sopenharmony_ci unsigned long bitnr; 139262306a36Sopenharmony_ci unsigned int last_page_nr = -1U; 139362306a36Sopenharmony_ci int c = 0; 139462306a36Sopenharmony_ci int changed_total = 0; 139562306a36Sopenharmony_ci 139662306a36Sopenharmony_ci if (e >= b->bm_bits) { 139762306a36Sopenharmony_ci drbd_err(device, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n", 139862306a36Sopenharmony_ci s, e, b->bm_bits); 139962306a36Sopenharmony_ci e = b->bm_bits ? b->bm_bits -1 : 0; 140062306a36Sopenharmony_ci } 140162306a36Sopenharmony_ci for (bitnr = s; bitnr <= e; bitnr++) { 140262306a36Sopenharmony_ci unsigned int page_nr = bm_bit_to_page_idx(b, bitnr); 140362306a36Sopenharmony_ci if (page_nr != last_page_nr) { 140462306a36Sopenharmony_ci if (p_addr) 140562306a36Sopenharmony_ci __bm_unmap(p_addr); 140662306a36Sopenharmony_ci if (c < 0) 140762306a36Sopenharmony_ci bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]); 140862306a36Sopenharmony_ci else if (c > 0) 140962306a36Sopenharmony_ci bm_set_page_need_writeout(b->bm_pages[last_page_nr]); 141062306a36Sopenharmony_ci changed_total += c; 141162306a36Sopenharmony_ci c = 0; 141262306a36Sopenharmony_ci p_addr = __bm_map_pidx(b, page_nr); 141362306a36Sopenharmony_ci last_page_nr = page_nr; 141462306a36Sopenharmony_ci } 141562306a36Sopenharmony_ci if (val) 141662306a36Sopenharmony_ci c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr)); 141762306a36Sopenharmony_ci else 141862306a36Sopenharmony_ci c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr)); 141962306a36Sopenharmony_ci } 142062306a36Sopenharmony_ci if (p_addr) 142162306a36Sopenharmony_ci __bm_unmap(p_addr); 142262306a36Sopenharmony_ci if (c < 0) 142362306a36Sopenharmony_ci bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]); 142462306a36Sopenharmony_ci else if (c > 0) 142562306a36Sopenharmony_ci bm_set_page_need_writeout(b->bm_pages[last_page_nr]); 142662306a36Sopenharmony_ci changed_total += c; 142762306a36Sopenharmony_ci b->bm_set += changed_total; 142862306a36Sopenharmony_ci return changed_total; 142962306a36Sopenharmony_ci} 143062306a36Sopenharmony_ci 143162306a36Sopenharmony_ci/* returns number of bits actually changed. 143262306a36Sopenharmony_ci * for val != 0, we change 0 -> 1, return code positive 143362306a36Sopenharmony_ci * for val == 0, we change 1 -> 0, return code negative 143462306a36Sopenharmony_ci * wants bitnr, not sector */ 143562306a36Sopenharmony_cistatic int bm_change_bits_to(struct drbd_device *device, const unsigned long s, 143662306a36Sopenharmony_ci const unsigned long e, int val) 143762306a36Sopenharmony_ci{ 143862306a36Sopenharmony_ci unsigned long flags; 143962306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 144062306a36Sopenharmony_ci int c = 0; 144162306a36Sopenharmony_ci 144262306a36Sopenharmony_ci if (!expect(device, b)) 144362306a36Sopenharmony_ci return 1; 144462306a36Sopenharmony_ci if (!expect(device, b->bm_pages)) 144562306a36Sopenharmony_ci return 0; 144662306a36Sopenharmony_ci 144762306a36Sopenharmony_ci spin_lock_irqsave(&b->bm_lock, flags); 144862306a36Sopenharmony_ci if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags) 144962306a36Sopenharmony_ci bm_print_lock_info(device); 145062306a36Sopenharmony_ci 145162306a36Sopenharmony_ci c = __bm_change_bits_to(device, s, e, val); 145262306a36Sopenharmony_ci 145362306a36Sopenharmony_ci spin_unlock_irqrestore(&b->bm_lock, flags); 145462306a36Sopenharmony_ci return c; 145562306a36Sopenharmony_ci} 145662306a36Sopenharmony_ci 145762306a36Sopenharmony_ci/* returns number of bits changed 0 -> 1 */ 145862306a36Sopenharmony_ciint drbd_bm_set_bits(struct drbd_device *device, const unsigned long s, const unsigned long e) 145962306a36Sopenharmony_ci{ 146062306a36Sopenharmony_ci return bm_change_bits_to(device, s, e, 1); 146162306a36Sopenharmony_ci} 146262306a36Sopenharmony_ci 146362306a36Sopenharmony_ci/* returns number of bits changed 1 -> 0 */ 146462306a36Sopenharmony_ciint drbd_bm_clear_bits(struct drbd_device *device, const unsigned long s, const unsigned long e) 146562306a36Sopenharmony_ci{ 146662306a36Sopenharmony_ci return -bm_change_bits_to(device, s, e, 0); 146762306a36Sopenharmony_ci} 146862306a36Sopenharmony_ci 146962306a36Sopenharmony_ci/* sets all bits in full words, 147062306a36Sopenharmony_ci * from first_word up to, but not including, last_word */ 147162306a36Sopenharmony_cistatic inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b, 147262306a36Sopenharmony_ci int page_nr, int first_word, int last_word) 147362306a36Sopenharmony_ci{ 147462306a36Sopenharmony_ci int i; 147562306a36Sopenharmony_ci int bits; 147662306a36Sopenharmony_ci int changed = 0; 147762306a36Sopenharmony_ci unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr]); 147862306a36Sopenharmony_ci 147962306a36Sopenharmony_ci /* I think it is more cache line friendly to hweight_long then set to ~0UL, 148062306a36Sopenharmony_ci * than to first bitmap_weight() all words, then bitmap_fill() all words */ 148162306a36Sopenharmony_ci for (i = first_word; i < last_word; i++) { 148262306a36Sopenharmony_ci bits = hweight_long(paddr[i]); 148362306a36Sopenharmony_ci paddr[i] = ~0UL; 148462306a36Sopenharmony_ci changed += BITS_PER_LONG - bits; 148562306a36Sopenharmony_ci } 148662306a36Sopenharmony_ci kunmap_atomic(paddr); 148762306a36Sopenharmony_ci if (changed) { 148862306a36Sopenharmony_ci /* We only need lazy writeout, the information is still in the 148962306a36Sopenharmony_ci * remote bitmap as well, and is reconstructed during the next 149062306a36Sopenharmony_ci * bitmap exchange, if lost locally due to a crash. */ 149162306a36Sopenharmony_ci bm_set_page_lazy_writeout(b->bm_pages[page_nr]); 149262306a36Sopenharmony_ci b->bm_set += changed; 149362306a36Sopenharmony_ci } 149462306a36Sopenharmony_ci} 149562306a36Sopenharmony_ci 149662306a36Sopenharmony_ci/* Same thing as drbd_bm_set_bits, 149762306a36Sopenharmony_ci * but more efficient for a large bit range. 149862306a36Sopenharmony_ci * You must first drbd_bm_lock(). 149962306a36Sopenharmony_ci * Can be called to set the whole bitmap in one go. 150062306a36Sopenharmony_ci * Sets bits from s to e _inclusive_. */ 150162306a36Sopenharmony_civoid _drbd_bm_set_bits(struct drbd_device *device, const unsigned long s, const unsigned long e) 150262306a36Sopenharmony_ci{ 150362306a36Sopenharmony_ci /* First set_bit from the first bit (s) 150462306a36Sopenharmony_ci * up to the next long boundary (sl), 150562306a36Sopenharmony_ci * then assign full words up to the last long boundary (el), 150662306a36Sopenharmony_ci * then set_bit up to and including the last bit (e). 150762306a36Sopenharmony_ci * 150862306a36Sopenharmony_ci * Do not use memset, because we must account for changes, 150962306a36Sopenharmony_ci * so we need to loop over the words with hweight() anyways. 151062306a36Sopenharmony_ci */ 151162306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 151262306a36Sopenharmony_ci unsigned long sl = ALIGN(s,BITS_PER_LONG); 151362306a36Sopenharmony_ci unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1); 151462306a36Sopenharmony_ci int first_page; 151562306a36Sopenharmony_ci int last_page; 151662306a36Sopenharmony_ci int page_nr; 151762306a36Sopenharmony_ci int first_word; 151862306a36Sopenharmony_ci int last_word; 151962306a36Sopenharmony_ci 152062306a36Sopenharmony_ci if (e - s <= 3*BITS_PER_LONG) { 152162306a36Sopenharmony_ci /* don't bother; el and sl may even be wrong. */ 152262306a36Sopenharmony_ci spin_lock_irq(&b->bm_lock); 152362306a36Sopenharmony_ci __bm_change_bits_to(device, s, e, 1); 152462306a36Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 152562306a36Sopenharmony_ci return; 152662306a36Sopenharmony_ci } 152762306a36Sopenharmony_ci 152862306a36Sopenharmony_ci /* difference is large enough that we can trust sl and el */ 152962306a36Sopenharmony_ci 153062306a36Sopenharmony_ci spin_lock_irq(&b->bm_lock); 153162306a36Sopenharmony_ci 153262306a36Sopenharmony_ci /* bits filling the current long */ 153362306a36Sopenharmony_ci if (sl) 153462306a36Sopenharmony_ci __bm_change_bits_to(device, s, sl-1, 1); 153562306a36Sopenharmony_ci 153662306a36Sopenharmony_ci first_page = sl >> (3 + PAGE_SHIFT); 153762306a36Sopenharmony_ci last_page = el >> (3 + PAGE_SHIFT); 153862306a36Sopenharmony_ci 153962306a36Sopenharmony_ci /* MLPP: modulo longs per page */ 154062306a36Sopenharmony_ci /* LWPP: long words per page */ 154162306a36Sopenharmony_ci first_word = MLPP(sl >> LN2_BPL); 154262306a36Sopenharmony_ci last_word = LWPP; 154362306a36Sopenharmony_ci 154462306a36Sopenharmony_ci /* first and full pages, unless first page == last page */ 154562306a36Sopenharmony_ci for (page_nr = first_page; page_nr < last_page; page_nr++) { 154662306a36Sopenharmony_ci bm_set_full_words_within_one_page(device->bitmap, page_nr, first_word, last_word); 154762306a36Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 154862306a36Sopenharmony_ci cond_resched(); 154962306a36Sopenharmony_ci first_word = 0; 155062306a36Sopenharmony_ci spin_lock_irq(&b->bm_lock); 155162306a36Sopenharmony_ci } 155262306a36Sopenharmony_ci /* last page (respectively only page, for first page == last page) */ 155362306a36Sopenharmony_ci last_word = MLPP(el >> LN2_BPL); 155462306a36Sopenharmony_ci 155562306a36Sopenharmony_ci /* consider bitmap->bm_bits = 32768, bitmap->bm_number_of_pages = 1. (or multiples). 155662306a36Sopenharmony_ci * ==> e = 32767, el = 32768, last_page = 2, 155762306a36Sopenharmony_ci * and now last_word = 0. 155862306a36Sopenharmony_ci * We do not want to touch last_page in this case, 155962306a36Sopenharmony_ci * as we did not allocate it, it is not present in bitmap->bm_pages. 156062306a36Sopenharmony_ci */ 156162306a36Sopenharmony_ci if (last_word) 156262306a36Sopenharmony_ci bm_set_full_words_within_one_page(device->bitmap, last_page, first_word, last_word); 156362306a36Sopenharmony_ci 156462306a36Sopenharmony_ci /* possibly trailing bits. 156562306a36Sopenharmony_ci * example: (e & 63) == 63, el will be e+1. 156662306a36Sopenharmony_ci * if that even was the very last bit, 156762306a36Sopenharmony_ci * it would trigger an assert in __bm_change_bits_to() 156862306a36Sopenharmony_ci */ 156962306a36Sopenharmony_ci if (el <= e) 157062306a36Sopenharmony_ci __bm_change_bits_to(device, el, e, 1); 157162306a36Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 157262306a36Sopenharmony_ci} 157362306a36Sopenharmony_ci 157462306a36Sopenharmony_ci/* returns bit state 157562306a36Sopenharmony_ci * wants bitnr, NOT sector. 157662306a36Sopenharmony_ci * inherently racy... area needs to be locked by means of {al,rs}_lru 157762306a36Sopenharmony_ci * 1 ... bit set 157862306a36Sopenharmony_ci * 0 ... bit not set 157962306a36Sopenharmony_ci * -1 ... first out of bounds access, stop testing for bits! 158062306a36Sopenharmony_ci */ 158162306a36Sopenharmony_ciint drbd_bm_test_bit(struct drbd_device *device, const unsigned long bitnr) 158262306a36Sopenharmony_ci{ 158362306a36Sopenharmony_ci unsigned long flags; 158462306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 158562306a36Sopenharmony_ci unsigned long *p_addr; 158662306a36Sopenharmony_ci int i; 158762306a36Sopenharmony_ci 158862306a36Sopenharmony_ci if (!expect(device, b)) 158962306a36Sopenharmony_ci return 0; 159062306a36Sopenharmony_ci if (!expect(device, b->bm_pages)) 159162306a36Sopenharmony_ci return 0; 159262306a36Sopenharmony_ci 159362306a36Sopenharmony_ci spin_lock_irqsave(&b->bm_lock, flags); 159462306a36Sopenharmony_ci if (BM_DONT_TEST & b->bm_flags) 159562306a36Sopenharmony_ci bm_print_lock_info(device); 159662306a36Sopenharmony_ci if (bitnr < b->bm_bits) { 159762306a36Sopenharmony_ci p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr)); 159862306a36Sopenharmony_ci i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0; 159962306a36Sopenharmony_ci bm_unmap(p_addr); 160062306a36Sopenharmony_ci } else if (bitnr == b->bm_bits) { 160162306a36Sopenharmony_ci i = -1; 160262306a36Sopenharmony_ci } else { /* (bitnr > b->bm_bits) */ 160362306a36Sopenharmony_ci drbd_err(device, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits); 160462306a36Sopenharmony_ci i = 0; 160562306a36Sopenharmony_ci } 160662306a36Sopenharmony_ci 160762306a36Sopenharmony_ci spin_unlock_irqrestore(&b->bm_lock, flags); 160862306a36Sopenharmony_ci return i; 160962306a36Sopenharmony_ci} 161062306a36Sopenharmony_ci 161162306a36Sopenharmony_ci/* returns number of bits set in the range [s, e] */ 161262306a36Sopenharmony_ciint drbd_bm_count_bits(struct drbd_device *device, const unsigned long s, const unsigned long e) 161362306a36Sopenharmony_ci{ 161462306a36Sopenharmony_ci unsigned long flags; 161562306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 161662306a36Sopenharmony_ci unsigned long *p_addr = NULL; 161762306a36Sopenharmony_ci unsigned long bitnr; 161862306a36Sopenharmony_ci unsigned int page_nr = -1U; 161962306a36Sopenharmony_ci int c = 0; 162062306a36Sopenharmony_ci 162162306a36Sopenharmony_ci /* If this is called without a bitmap, that is a bug. But just to be 162262306a36Sopenharmony_ci * robust in case we screwed up elsewhere, in that case pretend there 162362306a36Sopenharmony_ci * was one dirty bit in the requested area, so we won't try to do a 162462306a36Sopenharmony_ci * local read there (no bitmap probably implies no disk) */ 162562306a36Sopenharmony_ci if (!expect(device, b)) 162662306a36Sopenharmony_ci return 1; 162762306a36Sopenharmony_ci if (!expect(device, b->bm_pages)) 162862306a36Sopenharmony_ci return 1; 162962306a36Sopenharmony_ci 163062306a36Sopenharmony_ci spin_lock_irqsave(&b->bm_lock, flags); 163162306a36Sopenharmony_ci if (BM_DONT_TEST & b->bm_flags) 163262306a36Sopenharmony_ci bm_print_lock_info(device); 163362306a36Sopenharmony_ci for (bitnr = s; bitnr <= e; bitnr++) { 163462306a36Sopenharmony_ci unsigned int idx = bm_bit_to_page_idx(b, bitnr); 163562306a36Sopenharmony_ci if (page_nr != idx) { 163662306a36Sopenharmony_ci page_nr = idx; 163762306a36Sopenharmony_ci if (p_addr) 163862306a36Sopenharmony_ci bm_unmap(p_addr); 163962306a36Sopenharmony_ci p_addr = bm_map_pidx(b, idx); 164062306a36Sopenharmony_ci } 164162306a36Sopenharmony_ci if (expect(device, bitnr < b->bm_bits)) 164262306a36Sopenharmony_ci c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr)); 164362306a36Sopenharmony_ci else 164462306a36Sopenharmony_ci drbd_err(device, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits); 164562306a36Sopenharmony_ci } 164662306a36Sopenharmony_ci if (p_addr) 164762306a36Sopenharmony_ci bm_unmap(p_addr); 164862306a36Sopenharmony_ci spin_unlock_irqrestore(&b->bm_lock, flags); 164962306a36Sopenharmony_ci return c; 165062306a36Sopenharmony_ci} 165162306a36Sopenharmony_ci 165262306a36Sopenharmony_ci 165362306a36Sopenharmony_ci/* inherently racy... 165462306a36Sopenharmony_ci * return value may be already out-of-date when this function returns. 165562306a36Sopenharmony_ci * but the general usage is that this is only use during a cstate when bits are 165662306a36Sopenharmony_ci * only cleared, not set, and typically only care for the case when the return 165762306a36Sopenharmony_ci * value is zero, or we already "locked" this "bitmap extent" by other means. 165862306a36Sopenharmony_ci * 165962306a36Sopenharmony_ci * enr is bm-extent number, since we chose to name one sector (512 bytes) 166062306a36Sopenharmony_ci * worth of the bitmap a "bitmap extent". 166162306a36Sopenharmony_ci * 166262306a36Sopenharmony_ci * TODO 166362306a36Sopenharmony_ci * I think since we use it like a reference count, we should use the real 166462306a36Sopenharmony_ci * reference count of some bitmap extent element from some lru instead... 166562306a36Sopenharmony_ci * 166662306a36Sopenharmony_ci */ 166762306a36Sopenharmony_ciint drbd_bm_e_weight(struct drbd_device *device, unsigned long enr) 166862306a36Sopenharmony_ci{ 166962306a36Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 167062306a36Sopenharmony_ci int count, s, e; 167162306a36Sopenharmony_ci unsigned long flags; 167262306a36Sopenharmony_ci unsigned long *p_addr, *bm; 167362306a36Sopenharmony_ci 167462306a36Sopenharmony_ci if (!expect(device, b)) 167562306a36Sopenharmony_ci return 0; 167662306a36Sopenharmony_ci if (!expect(device, b->bm_pages)) 167762306a36Sopenharmony_ci return 0; 167862306a36Sopenharmony_ci 167962306a36Sopenharmony_ci spin_lock_irqsave(&b->bm_lock, flags); 168062306a36Sopenharmony_ci if (BM_DONT_TEST & b->bm_flags) 168162306a36Sopenharmony_ci bm_print_lock_info(device); 168262306a36Sopenharmony_ci 168362306a36Sopenharmony_ci s = S2W(enr); 168462306a36Sopenharmony_ci e = min((size_t)S2W(enr+1), b->bm_words); 168562306a36Sopenharmony_ci count = 0; 168662306a36Sopenharmony_ci if (s < b->bm_words) { 168762306a36Sopenharmony_ci int n = e-s; 168862306a36Sopenharmony_ci p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s)); 168962306a36Sopenharmony_ci bm = p_addr + MLPP(s); 169062306a36Sopenharmony_ci count += bitmap_weight(bm, n * BITS_PER_LONG); 169162306a36Sopenharmony_ci bm_unmap(p_addr); 169262306a36Sopenharmony_ci } else { 169362306a36Sopenharmony_ci drbd_err(device, "start offset (%d) too large in drbd_bm_e_weight\n", s); 169462306a36Sopenharmony_ci } 169562306a36Sopenharmony_ci spin_unlock_irqrestore(&b->bm_lock, flags); 169662306a36Sopenharmony_ci return count; 169762306a36Sopenharmony_ci} 1698