18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci drbd_bitmap.c 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci This file is part of DRBD by Philipp Reisner and Lars Ellenberg. 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci Copyright (C) 2004-2008, LINBIT Information Technologies GmbH. 88c2ecf20Sopenharmony_ci Copyright (C) 2004-2008, Philipp Reisner <philipp.reisner@linbit.com>. 98c2ecf20Sopenharmony_ci Copyright (C) 2004-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <linux/bitmap.h> 168c2ecf20Sopenharmony_ci#include <linux/vmalloc.h> 178c2ecf20Sopenharmony_ci#include <linux/string.h> 188c2ecf20Sopenharmony_ci#include <linux/drbd.h> 198c2ecf20Sopenharmony_ci#include <linux/slab.h> 208c2ecf20Sopenharmony_ci#include <linux/highmem.h> 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#include "drbd_int.h" 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci/* OPAQUE outside this file! 268c2ecf20Sopenharmony_ci * interface defined in drbd_int.h 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci * convention: 298c2ecf20Sopenharmony_ci * function name drbd_bm_... => used elsewhere, "public". 308c2ecf20Sopenharmony_ci * function name bm_... => internal to implementation, "private". 318c2ecf20Sopenharmony_ci */ 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci/* 358c2ecf20Sopenharmony_ci * LIMITATIONS: 368c2ecf20Sopenharmony_ci * We want to support >= peta byte of backend storage, while for now still using 378c2ecf20Sopenharmony_ci * a granularity of one bit per 4KiB of storage. 388c2ecf20Sopenharmony_ci * 1 << 50 bytes backend storage (1 PiB) 398c2ecf20Sopenharmony_ci * 1 << (50 - 12) bits needed 408c2ecf20Sopenharmony_ci * 38 --> we need u64 to index and count bits 418c2ecf20Sopenharmony_ci * 1 << (38 - 3) bitmap bytes needed 428c2ecf20Sopenharmony_ci * 35 --> we still need u64 to index and count bytes 438c2ecf20Sopenharmony_ci * (that's 32 GiB of bitmap for 1 PiB storage) 448c2ecf20Sopenharmony_ci * 1 << (35 - 2) 32bit longs needed 458c2ecf20Sopenharmony_ci * 33 --> we'd even need u64 to index and count 32bit long words. 468c2ecf20Sopenharmony_ci * 1 << (35 - 3) 64bit longs needed 478c2ecf20Sopenharmony_ci * 32 --> we could get away with a 32bit unsigned int to index and count 488c2ecf20Sopenharmony_ci * 64bit long words, but I rather stay with unsigned long for now. 498c2ecf20Sopenharmony_ci * We probably should neither count nor point to bytes or long words 508c2ecf20Sopenharmony_ci * directly, but either by bitnumber, or by page index and offset. 518c2ecf20Sopenharmony_ci * 1 << (35 - 12) 528c2ecf20Sopenharmony_ci * 22 --> we need that much 4KiB pages of bitmap. 538c2ecf20Sopenharmony_ci * 1 << (22 + 3) --> on a 64bit arch, 548c2ecf20Sopenharmony_ci * we need 32 MiB to store the array of page pointers. 558c2ecf20Sopenharmony_ci * 568c2ecf20Sopenharmony_ci * Because I'm lazy, and because the resulting patch was too large, too ugly 578c2ecf20Sopenharmony_ci * and still incomplete, on 32bit we still "only" support 16 TiB (minus some), 588c2ecf20Sopenharmony_ci * (1 << 32) bits * 4k storage. 598c2ecf20Sopenharmony_ci * 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci * bitmap storage and IO: 628c2ecf20Sopenharmony_ci * Bitmap is stored little endian on disk, and is kept little endian in 638c2ecf20Sopenharmony_ci * core memory. Currently we still hold the full bitmap in core as long 648c2ecf20Sopenharmony_ci * as we are "attached" to a local disk, which at 32 GiB for 1PiB storage 658c2ecf20Sopenharmony_ci * seems excessive. 668c2ecf20Sopenharmony_ci * 678c2ecf20Sopenharmony_ci * We plan to reduce the amount of in-core bitmap pages by paging them in 688c2ecf20Sopenharmony_ci * and out against their on-disk location as necessary, but need to make 698c2ecf20Sopenharmony_ci * sure we don't cause too much meta data IO, and must not deadlock in 708c2ecf20Sopenharmony_ci * tight memory situations. This needs some more work. 718c2ecf20Sopenharmony_ci */ 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_ci/* 748c2ecf20Sopenharmony_ci * NOTE 758c2ecf20Sopenharmony_ci * Access to the *bm_pages is protected by bm_lock. 768c2ecf20Sopenharmony_ci * It is safe to read the other members within the lock. 778c2ecf20Sopenharmony_ci * 788c2ecf20Sopenharmony_ci * drbd_bm_set_bits is called from bio_endio callbacks, 798c2ecf20Sopenharmony_ci * We may be called with irq already disabled, 808c2ecf20Sopenharmony_ci * so we need spin_lock_irqsave(). 818c2ecf20Sopenharmony_ci * And we need the kmap_atomic. 828c2ecf20Sopenharmony_ci */ 838c2ecf20Sopenharmony_cistruct drbd_bitmap { 848c2ecf20Sopenharmony_ci struct page **bm_pages; 858c2ecf20Sopenharmony_ci spinlock_t bm_lock; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci /* exclusively to be used by __al_write_transaction(), 888c2ecf20Sopenharmony_ci * drbd_bm_mark_for_writeout() and 898c2ecf20Sopenharmony_ci * and drbd_bm_write_hinted() -> bm_rw() called from there. 908c2ecf20Sopenharmony_ci */ 918c2ecf20Sopenharmony_ci unsigned int n_bitmap_hints; 928c2ecf20Sopenharmony_ci unsigned int al_bitmap_hints[AL_UPDATES_PER_TRANSACTION]; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci /* see LIMITATIONS: above */ 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */ 978c2ecf20Sopenharmony_ci unsigned long bm_bits; 988c2ecf20Sopenharmony_ci size_t bm_words; 998c2ecf20Sopenharmony_ci size_t bm_number_of_pages; 1008c2ecf20Sopenharmony_ci sector_t bm_dev_capacity; 1018c2ecf20Sopenharmony_ci struct mutex bm_change; /* serializes resize operations */ 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */ 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci enum bm_flag bm_flags; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci /* debugging aid, in case we are still racy somewhere */ 1088c2ecf20Sopenharmony_ci char *bm_why; 1098c2ecf20Sopenharmony_ci struct task_struct *bm_task; 1108c2ecf20Sopenharmony_ci}; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci#define bm_print_lock_info(m) __bm_print_lock_info(m, __func__) 1138c2ecf20Sopenharmony_cistatic void __bm_print_lock_info(struct drbd_device *device, const char *func) 1148c2ecf20Sopenharmony_ci{ 1158c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 1168c2ecf20Sopenharmony_ci if (!__ratelimit(&drbd_ratelimit_state)) 1178c2ecf20Sopenharmony_ci return; 1188c2ecf20Sopenharmony_ci drbd_err(device, "FIXME %s[%d] in %s, bitmap locked for '%s' by %s[%d]\n", 1198c2ecf20Sopenharmony_ci current->comm, task_pid_nr(current), 1208c2ecf20Sopenharmony_ci func, b->bm_why ?: "?", 1218c2ecf20Sopenharmony_ci b->bm_task->comm, task_pid_nr(b->bm_task)); 1228c2ecf20Sopenharmony_ci} 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_civoid drbd_bm_lock(struct drbd_device *device, char *why, enum bm_flag flags) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 1278c2ecf20Sopenharmony_ci int trylock_failed; 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci if (!b) { 1308c2ecf20Sopenharmony_ci drbd_err(device, "FIXME no bitmap in drbd_bm_lock!?\n"); 1318c2ecf20Sopenharmony_ci return; 1328c2ecf20Sopenharmony_ci } 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci trylock_failed = !mutex_trylock(&b->bm_change); 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci if (trylock_failed) { 1378c2ecf20Sopenharmony_ci drbd_warn(device, "%s[%d] going to '%s' but bitmap already locked for '%s' by %s[%d]\n", 1388c2ecf20Sopenharmony_ci current->comm, task_pid_nr(current), 1398c2ecf20Sopenharmony_ci why, b->bm_why ?: "?", 1408c2ecf20Sopenharmony_ci b->bm_task->comm, task_pid_nr(b->bm_task)); 1418c2ecf20Sopenharmony_ci mutex_lock(&b->bm_change); 1428c2ecf20Sopenharmony_ci } 1438c2ecf20Sopenharmony_ci if (BM_LOCKED_MASK & b->bm_flags) 1448c2ecf20Sopenharmony_ci drbd_err(device, "FIXME bitmap already locked in bm_lock\n"); 1458c2ecf20Sopenharmony_ci b->bm_flags |= flags & BM_LOCKED_MASK; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci b->bm_why = why; 1488c2ecf20Sopenharmony_ci b->bm_task = current; 1498c2ecf20Sopenharmony_ci} 1508c2ecf20Sopenharmony_ci 1518c2ecf20Sopenharmony_civoid drbd_bm_unlock(struct drbd_device *device) 1528c2ecf20Sopenharmony_ci{ 1538c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 1548c2ecf20Sopenharmony_ci if (!b) { 1558c2ecf20Sopenharmony_ci drbd_err(device, "FIXME no bitmap in drbd_bm_unlock!?\n"); 1568c2ecf20Sopenharmony_ci return; 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci if (!(BM_LOCKED_MASK & device->bitmap->bm_flags)) 1608c2ecf20Sopenharmony_ci drbd_err(device, "FIXME bitmap not locked in bm_unlock\n"); 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci b->bm_flags &= ~BM_LOCKED_MASK; 1638c2ecf20Sopenharmony_ci b->bm_why = NULL; 1648c2ecf20Sopenharmony_ci b->bm_task = NULL; 1658c2ecf20Sopenharmony_ci mutex_unlock(&b->bm_change); 1668c2ecf20Sopenharmony_ci} 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci/* we store some "meta" info about our pages in page->private */ 1698c2ecf20Sopenharmony_ci/* at a granularity of 4k storage per bitmap bit: 1708c2ecf20Sopenharmony_ci * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks 1718c2ecf20Sopenharmony_ci * 1<<38 bits, 1728c2ecf20Sopenharmony_ci * 1<<23 4k bitmap pages. 1738c2ecf20Sopenharmony_ci * Use 24 bits as page index, covers 2 peta byte storage 1748c2ecf20Sopenharmony_ci * at a granularity of 4k per bit. 1758c2ecf20Sopenharmony_ci * Used to report the failed page idx on io error from the endio handlers. 1768c2ecf20Sopenharmony_ci */ 1778c2ecf20Sopenharmony_ci#define BM_PAGE_IDX_MASK ((1UL<<24)-1) 1788c2ecf20Sopenharmony_ci/* this page is currently read in, or written back */ 1798c2ecf20Sopenharmony_ci#define BM_PAGE_IO_LOCK 31 1808c2ecf20Sopenharmony_ci/* if there has been an IO error for this page */ 1818c2ecf20Sopenharmony_ci#define BM_PAGE_IO_ERROR 30 1828c2ecf20Sopenharmony_ci/* this is to be able to intelligently skip disk IO, 1838c2ecf20Sopenharmony_ci * set if bits have been set since last IO. */ 1848c2ecf20Sopenharmony_ci#define BM_PAGE_NEED_WRITEOUT 29 1858c2ecf20Sopenharmony_ci/* to mark for lazy writeout once syncer cleared all clearable bits, 1868c2ecf20Sopenharmony_ci * we if bits have been cleared since last IO. */ 1878c2ecf20Sopenharmony_ci#define BM_PAGE_LAZY_WRITEOUT 28 1888c2ecf20Sopenharmony_ci/* pages marked with this "HINT" will be considered for writeout 1898c2ecf20Sopenharmony_ci * on activity log transactions */ 1908c2ecf20Sopenharmony_ci#define BM_PAGE_HINT_WRITEOUT 27 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci/* store_page_idx uses non-atomic assignment. It is only used directly after 1938c2ecf20Sopenharmony_ci * allocating the page. All other bm_set_page_* and bm_clear_page_* need to 1948c2ecf20Sopenharmony_ci * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap 1958c2ecf20Sopenharmony_ci * changes) may happen from various contexts, and wait_on_bit/wake_up_bit 1968c2ecf20Sopenharmony_ci * requires it all to be atomic as well. */ 1978c2ecf20Sopenharmony_cistatic void bm_store_page_idx(struct page *page, unsigned long idx) 1988c2ecf20Sopenharmony_ci{ 1998c2ecf20Sopenharmony_ci BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK)); 2008c2ecf20Sopenharmony_ci set_page_private(page, idx); 2018c2ecf20Sopenharmony_ci} 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_cistatic unsigned long bm_page_to_idx(struct page *page) 2048c2ecf20Sopenharmony_ci{ 2058c2ecf20Sopenharmony_ci return page_private(page) & BM_PAGE_IDX_MASK; 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_ci/* As is very unlikely that the same page is under IO from more than one 2098c2ecf20Sopenharmony_ci * context, we can get away with a bit per page and one wait queue per bitmap. 2108c2ecf20Sopenharmony_ci */ 2118c2ecf20Sopenharmony_cistatic void bm_page_lock_io(struct drbd_device *device, int page_nr) 2128c2ecf20Sopenharmony_ci{ 2138c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 2148c2ecf20Sopenharmony_ci void *addr = &page_private(b->bm_pages[page_nr]); 2158c2ecf20Sopenharmony_ci wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr)); 2168c2ecf20Sopenharmony_ci} 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_cistatic void bm_page_unlock_io(struct drbd_device *device, int page_nr) 2198c2ecf20Sopenharmony_ci{ 2208c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 2218c2ecf20Sopenharmony_ci void *addr = &page_private(b->bm_pages[page_nr]); 2228c2ecf20Sopenharmony_ci clear_bit_unlock(BM_PAGE_IO_LOCK, addr); 2238c2ecf20Sopenharmony_ci wake_up(&device->bitmap->bm_io_wait); 2248c2ecf20Sopenharmony_ci} 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci/* set _before_ submit_io, so it may be reset due to being changed 2278c2ecf20Sopenharmony_ci * while this page is in flight... will get submitted later again */ 2288c2ecf20Sopenharmony_cistatic void bm_set_page_unchanged(struct page *page) 2298c2ecf20Sopenharmony_ci{ 2308c2ecf20Sopenharmony_ci /* use cmpxchg? */ 2318c2ecf20Sopenharmony_ci clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page)); 2328c2ecf20Sopenharmony_ci clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page)); 2338c2ecf20Sopenharmony_ci} 2348c2ecf20Sopenharmony_ci 2358c2ecf20Sopenharmony_cistatic void bm_set_page_need_writeout(struct page *page) 2368c2ecf20Sopenharmony_ci{ 2378c2ecf20Sopenharmony_ci set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page)); 2388c2ecf20Sopenharmony_ci} 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_civoid drbd_bm_reset_al_hints(struct drbd_device *device) 2418c2ecf20Sopenharmony_ci{ 2428c2ecf20Sopenharmony_ci device->bitmap->n_bitmap_hints = 0; 2438c2ecf20Sopenharmony_ci} 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci/** 2468c2ecf20Sopenharmony_ci * drbd_bm_mark_for_writeout() - mark a page with a "hint" to be considered for writeout 2478c2ecf20Sopenharmony_ci * @device: DRBD device. 2488c2ecf20Sopenharmony_ci * @page_nr: the bitmap page to mark with the "hint" flag 2498c2ecf20Sopenharmony_ci * 2508c2ecf20Sopenharmony_ci * From within an activity log transaction, we mark a few pages with these 2518c2ecf20Sopenharmony_ci * hints, then call drbd_bm_write_hinted(), which will only write out changed 2528c2ecf20Sopenharmony_ci * pages which are flagged with this mark. 2538c2ecf20Sopenharmony_ci */ 2548c2ecf20Sopenharmony_civoid drbd_bm_mark_for_writeout(struct drbd_device *device, int page_nr) 2558c2ecf20Sopenharmony_ci{ 2568c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 2578c2ecf20Sopenharmony_ci struct page *page; 2588c2ecf20Sopenharmony_ci if (page_nr >= device->bitmap->bm_number_of_pages) { 2598c2ecf20Sopenharmony_ci drbd_warn(device, "BAD: page_nr: %u, number_of_pages: %u\n", 2608c2ecf20Sopenharmony_ci page_nr, (int)device->bitmap->bm_number_of_pages); 2618c2ecf20Sopenharmony_ci return; 2628c2ecf20Sopenharmony_ci } 2638c2ecf20Sopenharmony_ci page = device->bitmap->bm_pages[page_nr]; 2648c2ecf20Sopenharmony_ci BUG_ON(b->n_bitmap_hints >= ARRAY_SIZE(b->al_bitmap_hints)); 2658c2ecf20Sopenharmony_ci if (!test_and_set_bit(BM_PAGE_HINT_WRITEOUT, &page_private(page))) 2668c2ecf20Sopenharmony_ci b->al_bitmap_hints[b->n_bitmap_hints++] = page_nr; 2678c2ecf20Sopenharmony_ci} 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistatic int bm_test_page_unchanged(struct page *page) 2708c2ecf20Sopenharmony_ci{ 2718c2ecf20Sopenharmony_ci volatile const unsigned long *addr = &page_private(page); 2728c2ecf20Sopenharmony_ci return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0; 2738c2ecf20Sopenharmony_ci} 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_cistatic void bm_set_page_io_err(struct page *page) 2768c2ecf20Sopenharmony_ci{ 2778c2ecf20Sopenharmony_ci set_bit(BM_PAGE_IO_ERROR, &page_private(page)); 2788c2ecf20Sopenharmony_ci} 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_cistatic void bm_clear_page_io_err(struct page *page) 2818c2ecf20Sopenharmony_ci{ 2828c2ecf20Sopenharmony_ci clear_bit(BM_PAGE_IO_ERROR, &page_private(page)); 2838c2ecf20Sopenharmony_ci} 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_cistatic void bm_set_page_lazy_writeout(struct page *page) 2868c2ecf20Sopenharmony_ci{ 2878c2ecf20Sopenharmony_ci set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page)); 2888c2ecf20Sopenharmony_ci} 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_cistatic int bm_test_page_lazy_writeout(struct page *page) 2918c2ecf20Sopenharmony_ci{ 2928c2ecf20Sopenharmony_ci return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page)); 2938c2ecf20Sopenharmony_ci} 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci/* on a 32bit box, this would allow for exactly (2<<38) bits. */ 2968c2ecf20Sopenharmony_cistatic unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr) 2978c2ecf20Sopenharmony_ci{ 2988c2ecf20Sopenharmony_ci /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */ 2998c2ecf20Sopenharmony_ci unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3); 3008c2ecf20Sopenharmony_ci BUG_ON(page_nr >= b->bm_number_of_pages); 3018c2ecf20Sopenharmony_ci return page_nr; 3028c2ecf20Sopenharmony_ci} 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_cistatic unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr) 3058c2ecf20Sopenharmony_ci{ 3068c2ecf20Sopenharmony_ci /* page_nr = (bitnr/8) >> PAGE_SHIFT; */ 3078c2ecf20Sopenharmony_ci unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3); 3088c2ecf20Sopenharmony_ci BUG_ON(page_nr >= b->bm_number_of_pages); 3098c2ecf20Sopenharmony_ci return page_nr; 3108c2ecf20Sopenharmony_ci} 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_cistatic unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx) 3138c2ecf20Sopenharmony_ci{ 3148c2ecf20Sopenharmony_ci struct page *page = b->bm_pages[idx]; 3158c2ecf20Sopenharmony_ci return (unsigned long *) kmap_atomic(page); 3168c2ecf20Sopenharmony_ci} 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_cistatic unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx) 3198c2ecf20Sopenharmony_ci{ 3208c2ecf20Sopenharmony_ci return __bm_map_pidx(b, idx); 3218c2ecf20Sopenharmony_ci} 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_cistatic void __bm_unmap(unsigned long *p_addr) 3248c2ecf20Sopenharmony_ci{ 3258c2ecf20Sopenharmony_ci kunmap_atomic(p_addr); 3268c2ecf20Sopenharmony_ci}; 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_cistatic void bm_unmap(unsigned long *p_addr) 3298c2ecf20Sopenharmony_ci{ 3308c2ecf20Sopenharmony_ci return __bm_unmap(p_addr); 3318c2ecf20Sopenharmony_ci} 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci/* long word offset of _bitmap_ sector */ 3348c2ecf20Sopenharmony_ci#define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL)) 3358c2ecf20Sopenharmony_ci/* word offset from start of bitmap to word number _in_page_ 3368c2ecf20Sopenharmony_ci * modulo longs per page 3378c2ecf20Sopenharmony_ci#define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long)) 3388c2ecf20Sopenharmony_ci hm, well, Philipp thinks gcc might not optimize the % into & (... - 1) 3398c2ecf20Sopenharmony_ci so do it explicitly: 3408c2ecf20Sopenharmony_ci */ 3418c2ecf20Sopenharmony_ci#define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1)) 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci/* Long words per page */ 3448c2ecf20Sopenharmony_ci#define LWPP (PAGE_SIZE/sizeof(long)) 3458c2ecf20Sopenharmony_ci 3468c2ecf20Sopenharmony_ci/* 3478c2ecf20Sopenharmony_ci * actually most functions herein should take a struct drbd_bitmap*, not a 3488c2ecf20Sopenharmony_ci * struct drbd_device*, but for the debug macros I like to have the device around 3498c2ecf20Sopenharmony_ci * to be able to report device specific. 3508c2ecf20Sopenharmony_ci */ 3518c2ecf20Sopenharmony_ci 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_cistatic void bm_free_pages(struct page **pages, unsigned long number) 3548c2ecf20Sopenharmony_ci{ 3558c2ecf20Sopenharmony_ci unsigned long i; 3568c2ecf20Sopenharmony_ci if (!pages) 3578c2ecf20Sopenharmony_ci return; 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci for (i = 0; i < number; i++) { 3608c2ecf20Sopenharmony_ci if (!pages[i]) { 3618c2ecf20Sopenharmony_ci pr_alert("bm_free_pages tried to free a NULL pointer; i=%lu n=%lu\n", 3628c2ecf20Sopenharmony_ci i, number); 3638c2ecf20Sopenharmony_ci continue; 3648c2ecf20Sopenharmony_ci } 3658c2ecf20Sopenharmony_ci __free_page(pages[i]); 3668c2ecf20Sopenharmony_ci pages[i] = NULL; 3678c2ecf20Sopenharmony_ci } 3688c2ecf20Sopenharmony_ci} 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_cistatic inline void bm_vk_free(void *ptr) 3718c2ecf20Sopenharmony_ci{ 3728c2ecf20Sopenharmony_ci kvfree(ptr); 3738c2ecf20Sopenharmony_ci} 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci/* 3768c2ecf20Sopenharmony_ci * "have" and "want" are NUMBER OF PAGES. 3778c2ecf20Sopenharmony_ci */ 3788c2ecf20Sopenharmony_cistatic struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want) 3798c2ecf20Sopenharmony_ci{ 3808c2ecf20Sopenharmony_ci struct page **old_pages = b->bm_pages; 3818c2ecf20Sopenharmony_ci struct page **new_pages, *page; 3828c2ecf20Sopenharmony_ci unsigned int i, bytes; 3838c2ecf20Sopenharmony_ci unsigned long have = b->bm_number_of_pages; 3848c2ecf20Sopenharmony_ci 3858c2ecf20Sopenharmony_ci BUG_ON(have == 0 && old_pages != NULL); 3868c2ecf20Sopenharmony_ci BUG_ON(have != 0 && old_pages == NULL); 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_ci if (have == want) 3898c2ecf20Sopenharmony_ci return old_pages; 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci /* Trying kmalloc first, falling back to vmalloc. 3928c2ecf20Sopenharmony_ci * GFP_NOIO, as this is called while drbd IO is "suspended", 3938c2ecf20Sopenharmony_ci * and during resize or attach on diskless Primary, 3948c2ecf20Sopenharmony_ci * we must not block on IO to ourselves. 3958c2ecf20Sopenharmony_ci * Context is receiver thread or dmsetup. */ 3968c2ecf20Sopenharmony_ci bytes = sizeof(struct page *)*want; 3978c2ecf20Sopenharmony_ci new_pages = kzalloc(bytes, GFP_NOIO | __GFP_NOWARN); 3988c2ecf20Sopenharmony_ci if (!new_pages) { 3998c2ecf20Sopenharmony_ci new_pages = __vmalloc(bytes, GFP_NOIO | __GFP_ZERO); 4008c2ecf20Sopenharmony_ci if (!new_pages) 4018c2ecf20Sopenharmony_ci return NULL; 4028c2ecf20Sopenharmony_ci } 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci if (want >= have) { 4058c2ecf20Sopenharmony_ci for (i = 0; i < have; i++) 4068c2ecf20Sopenharmony_ci new_pages[i] = old_pages[i]; 4078c2ecf20Sopenharmony_ci for (; i < want; i++) { 4088c2ecf20Sopenharmony_ci page = alloc_page(GFP_NOIO | __GFP_HIGHMEM); 4098c2ecf20Sopenharmony_ci if (!page) { 4108c2ecf20Sopenharmony_ci bm_free_pages(new_pages + have, i - have); 4118c2ecf20Sopenharmony_ci bm_vk_free(new_pages); 4128c2ecf20Sopenharmony_ci return NULL; 4138c2ecf20Sopenharmony_ci } 4148c2ecf20Sopenharmony_ci /* we want to know which page it is 4158c2ecf20Sopenharmony_ci * from the endio handlers */ 4168c2ecf20Sopenharmony_ci bm_store_page_idx(page, i); 4178c2ecf20Sopenharmony_ci new_pages[i] = page; 4188c2ecf20Sopenharmony_ci } 4198c2ecf20Sopenharmony_ci } else { 4208c2ecf20Sopenharmony_ci for (i = 0; i < want; i++) 4218c2ecf20Sopenharmony_ci new_pages[i] = old_pages[i]; 4228c2ecf20Sopenharmony_ci /* NOT HERE, we are outside the spinlock! 4238c2ecf20Sopenharmony_ci bm_free_pages(old_pages + want, have - want); 4248c2ecf20Sopenharmony_ci */ 4258c2ecf20Sopenharmony_ci } 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ci return new_pages; 4288c2ecf20Sopenharmony_ci} 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci/* 4318c2ecf20Sopenharmony_ci * allocates the drbd_bitmap and stores it in device->bitmap. 4328c2ecf20Sopenharmony_ci */ 4338c2ecf20Sopenharmony_ciint drbd_bm_init(struct drbd_device *device) 4348c2ecf20Sopenharmony_ci{ 4358c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 4368c2ecf20Sopenharmony_ci WARN_ON(b != NULL); 4378c2ecf20Sopenharmony_ci b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL); 4388c2ecf20Sopenharmony_ci if (!b) 4398c2ecf20Sopenharmony_ci return -ENOMEM; 4408c2ecf20Sopenharmony_ci spin_lock_init(&b->bm_lock); 4418c2ecf20Sopenharmony_ci mutex_init(&b->bm_change); 4428c2ecf20Sopenharmony_ci init_waitqueue_head(&b->bm_io_wait); 4438c2ecf20Sopenharmony_ci 4448c2ecf20Sopenharmony_ci device->bitmap = b; 4458c2ecf20Sopenharmony_ci 4468c2ecf20Sopenharmony_ci return 0; 4478c2ecf20Sopenharmony_ci} 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_cisector_t drbd_bm_capacity(struct drbd_device *device) 4508c2ecf20Sopenharmony_ci{ 4518c2ecf20Sopenharmony_ci if (!expect(device->bitmap)) 4528c2ecf20Sopenharmony_ci return 0; 4538c2ecf20Sopenharmony_ci return device->bitmap->bm_dev_capacity; 4548c2ecf20Sopenharmony_ci} 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci/* called on driver unload. TODO: call when a device is destroyed. 4578c2ecf20Sopenharmony_ci */ 4588c2ecf20Sopenharmony_civoid drbd_bm_cleanup(struct drbd_device *device) 4598c2ecf20Sopenharmony_ci{ 4608c2ecf20Sopenharmony_ci if (!expect(device->bitmap)) 4618c2ecf20Sopenharmony_ci return; 4628c2ecf20Sopenharmony_ci bm_free_pages(device->bitmap->bm_pages, device->bitmap->bm_number_of_pages); 4638c2ecf20Sopenharmony_ci bm_vk_free(device->bitmap->bm_pages); 4648c2ecf20Sopenharmony_ci kfree(device->bitmap); 4658c2ecf20Sopenharmony_ci device->bitmap = NULL; 4668c2ecf20Sopenharmony_ci} 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci/* 4698c2ecf20Sopenharmony_ci * since (b->bm_bits % BITS_PER_LONG) != 0, 4708c2ecf20Sopenharmony_ci * this masks out the remaining bits. 4718c2ecf20Sopenharmony_ci * Returns the number of bits cleared. 4728c2ecf20Sopenharmony_ci */ 4738c2ecf20Sopenharmony_ci#ifndef BITS_PER_PAGE 4748c2ecf20Sopenharmony_ci#define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3)) 4758c2ecf20Sopenharmony_ci#define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1) 4768c2ecf20Sopenharmony_ci#else 4778c2ecf20Sopenharmony_ci# if BITS_PER_PAGE != (1UL << (PAGE_SHIFT + 3)) 4788c2ecf20Sopenharmony_ci# error "ambiguous BITS_PER_PAGE" 4798c2ecf20Sopenharmony_ci# endif 4808c2ecf20Sopenharmony_ci#endif 4818c2ecf20Sopenharmony_ci#define BITS_PER_LONG_MASK (BITS_PER_LONG - 1) 4828c2ecf20Sopenharmony_cistatic int bm_clear_surplus(struct drbd_bitmap *b) 4838c2ecf20Sopenharmony_ci{ 4848c2ecf20Sopenharmony_ci unsigned long mask; 4858c2ecf20Sopenharmony_ci unsigned long *p_addr, *bm; 4868c2ecf20Sopenharmony_ci int tmp; 4878c2ecf20Sopenharmony_ci int cleared = 0; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci /* number of bits modulo bits per page */ 4908c2ecf20Sopenharmony_ci tmp = (b->bm_bits & BITS_PER_PAGE_MASK); 4918c2ecf20Sopenharmony_ci /* mask the used bits of the word containing the last bit */ 4928c2ecf20Sopenharmony_ci mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1; 4938c2ecf20Sopenharmony_ci /* bitmap is always stored little endian, 4948c2ecf20Sopenharmony_ci * on disk and in core memory alike */ 4958c2ecf20Sopenharmony_ci mask = cpu_to_lel(mask); 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_ci p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1); 4988c2ecf20Sopenharmony_ci bm = p_addr + (tmp/BITS_PER_LONG); 4998c2ecf20Sopenharmony_ci if (mask) { 5008c2ecf20Sopenharmony_ci /* If mask != 0, we are not exactly aligned, so bm now points 5018c2ecf20Sopenharmony_ci * to the long containing the last bit. 5028c2ecf20Sopenharmony_ci * If mask == 0, bm already points to the word immediately 5038c2ecf20Sopenharmony_ci * after the last (long word aligned) bit. */ 5048c2ecf20Sopenharmony_ci cleared = hweight_long(*bm & ~mask); 5058c2ecf20Sopenharmony_ci *bm &= mask; 5068c2ecf20Sopenharmony_ci bm++; 5078c2ecf20Sopenharmony_ci } 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) { 5108c2ecf20Sopenharmony_ci /* on a 32bit arch, we may need to zero out 5118c2ecf20Sopenharmony_ci * a padding long to align with a 64bit remote */ 5128c2ecf20Sopenharmony_ci cleared += hweight_long(*bm); 5138c2ecf20Sopenharmony_ci *bm = 0; 5148c2ecf20Sopenharmony_ci } 5158c2ecf20Sopenharmony_ci bm_unmap(p_addr); 5168c2ecf20Sopenharmony_ci return cleared; 5178c2ecf20Sopenharmony_ci} 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_cistatic void bm_set_surplus(struct drbd_bitmap *b) 5208c2ecf20Sopenharmony_ci{ 5218c2ecf20Sopenharmony_ci unsigned long mask; 5228c2ecf20Sopenharmony_ci unsigned long *p_addr, *bm; 5238c2ecf20Sopenharmony_ci int tmp; 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci /* number of bits modulo bits per page */ 5268c2ecf20Sopenharmony_ci tmp = (b->bm_bits & BITS_PER_PAGE_MASK); 5278c2ecf20Sopenharmony_ci /* mask the used bits of the word containing the last bit */ 5288c2ecf20Sopenharmony_ci mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1; 5298c2ecf20Sopenharmony_ci /* bitmap is always stored little endian, 5308c2ecf20Sopenharmony_ci * on disk and in core memory alike */ 5318c2ecf20Sopenharmony_ci mask = cpu_to_lel(mask); 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1); 5348c2ecf20Sopenharmony_ci bm = p_addr + (tmp/BITS_PER_LONG); 5358c2ecf20Sopenharmony_ci if (mask) { 5368c2ecf20Sopenharmony_ci /* If mask != 0, we are not exactly aligned, so bm now points 5378c2ecf20Sopenharmony_ci * to the long containing the last bit. 5388c2ecf20Sopenharmony_ci * If mask == 0, bm already points to the word immediately 5398c2ecf20Sopenharmony_ci * after the last (long word aligned) bit. */ 5408c2ecf20Sopenharmony_ci *bm |= ~mask; 5418c2ecf20Sopenharmony_ci bm++; 5428c2ecf20Sopenharmony_ci } 5438c2ecf20Sopenharmony_ci 5448c2ecf20Sopenharmony_ci if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) { 5458c2ecf20Sopenharmony_ci /* on a 32bit arch, we may need to zero out 5468c2ecf20Sopenharmony_ci * a padding long to align with a 64bit remote */ 5478c2ecf20Sopenharmony_ci *bm = ~0UL; 5488c2ecf20Sopenharmony_ci } 5498c2ecf20Sopenharmony_ci bm_unmap(p_addr); 5508c2ecf20Sopenharmony_ci} 5518c2ecf20Sopenharmony_ci 5528c2ecf20Sopenharmony_ci/* you better not modify the bitmap while this is running, 5538c2ecf20Sopenharmony_ci * or its results will be stale */ 5548c2ecf20Sopenharmony_cistatic unsigned long bm_count_bits(struct drbd_bitmap *b) 5558c2ecf20Sopenharmony_ci{ 5568c2ecf20Sopenharmony_ci unsigned long *p_addr; 5578c2ecf20Sopenharmony_ci unsigned long bits = 0; 5588c2ecf20Sopenharmony_ci unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1; 5598c2ecf20Sopenharmony_ci int idx, last_word; 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci /* all but last page */ 5628c2ecf20Sopenharmony_ci for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) { 5638c2ecf20Sopenharmony_ci p_addr = __bm_map_pidx(b, idx); 5648c2ecf20Sopenharmony_ci bits += bitmap_weight(p_addr, BITS_PER_PAGE); 5658c2ecf20Sopenharmony_ci __bm_unmap(p_addr); 5668c2ecf20Sopenharmony_ci cond_resched(); 5678c2ecf20Sopenharmony_ci } 5688c2ecf20Sopenharmony_ci /* last (or only) page */ 5698c2ecf20Sopenharmony_ci last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL; 5708c2ecf20Sopenharmony_ci p_addr = __bm_map_pidx(b, idx); 5718c2ecf20Sopenharmony_ci bits += bitmap_weight(p_addr, last_word * BITS_PER_LONG); 5728c2ecf20Sopenharmony_ci p_addr[last_word] &= cpu_to_lel(mask); 5738c2ecf20Sopenharmony_ci bits += hweight_long(p_addr[last_word]); 5748c2ecf20Sopenharmony_ci /* 32bit arch, may have an unused padding long */ 5758c2ecf20Sopenharmony_ci if (BITS_PER_LONG == 32 && (last_word & 1) == 0) 5768c2ecf20Sopenharmony_ci p_addr[last_word+1] = 0; 5778c2ecf20Sopenharmony_ci __bm_unmap(p_addr); 5788c2ecf20Sopenharmony_ci return bits; 5798c2ecf20Sopenharmony_ci} 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci/* offset and len in long words.*/ 5828c2ecf20Sopenharmony_cistatic void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len) 5838c2ecf20Sopenharmony_ci{ 5848c2ecf20Sopenharmony_ci unsigned long *p_addr, *bm; 5858c2ecf20Sopenharmony_ci unsigned int idx; 5868c2ecf20Sopenharmony_ci size_t do_now, end; 5878c2ecf20Sopenharmony_ci 5888c2ecf20Sopenharmony_ci end = offset + len; 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ci if (end > b->bm_words) { 5918c2ecf20Sopenharmony_ci pr_alert("bm_memset end > bm_words\n"); 5928c2ecf20Sopenharmony_ci return; 5938c2ecf20Sopenharmony_ci } 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci while (offset < end) { 5968c2ecf20Sopenharmony_ci do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset; 5978c2ecf20Sopenharmony_ci idx = bm_word_to_page_idx(b, offset); 5988c2ecf20Sopenharmony_ci p_addr = bm_map_pidx(b, idx); 5998c2ecf20Sopenharmony_ci bm = p_addr + MLPP(offset); 6008c2ecf20Sopenharmony_ci if (bm+do_now > p_addr + LWPP) { 6018c2ecf20Sopenharmony_ci pr_alert("BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n", 6028c2ecf20Sopenharmony_ci p_addr, bm, (int)do_now); 6038c2ecf20Sopenharmony_ci } else 6048c2ecf20Sopenharmony_ci memset(bm, c, do_now * sizeof(long)); 6058c2ecf20Sopenharmony_ci bm_unmap(p_addr); 6068c2ecf20Sopenharmony_ci bm_set_page_need_writeout(b->bm_pages[idx]); 6078c2ecf20Sopenharmony_ci offset += do_now; 6088c2ecf20Sopenharmony_ci } 6098c2ecf20Sopenharmony_ci} 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci/* For the layout, see comment above drbd_md_set_sector_offsets(). */ 6128c2ecf20Sopenharmony_cistatic u64 drbd_md_on_disk_bits(struct drbd_backing_dev *ldev) 6138c2ecf20Sopenharmony_ci{ 6148c2ecf20Sopenharmony_ci u64 bitmap_sectors; 6158c2ecf20Sopenharmony_ci if (ldev->md.al_offset == 8) 6168c2ecf20Sopenharmony_ci bitmap_sectors = ldev->md.md_size_sect - ldev->md.bm_offset; 6178c2ecf20Sopenharmony_ci else 6188c2ecf20Sopenharmony_ci bitmap_sectors = ldev->md.al_offset - ldev->md.bm_offset; 6198c2ecf20Sopenharmony_ci return bitmap_sectors << (9 + 3); 6208c2ecf20Sopenharmony_ci} 6218c2ecf20Sopenharmony_ci 6228c2ecf20Sopenharmony_ci/* 6238c2ecf20Sopenharmony_ci * make sure the bitmap has enough room for the attached storage, 6248c2ecf20Sopenharmony_ci * if necessary, resize. 6258c2ecf20Sopenharmony_ci * called whenever we may have changed the device size. 6268c2ecf20Sopenharmony_ci * returns -ENOMEM if we could not allocate enough memory, 0 on success. 6278c2ecf20Sopenharmony_ci * In case this is actually a resize, we copy the old bitmap into the new one. 6288c2ecf20Sopenharmony_ci * Otherwise, the bitmap is initialized to all bits set. 6298c2ecf20Sopenharmony_ci */ 6308c2ecf20Sopenharmony_ciint drbd_bm_resize(struct drbd_device *device, sector_t capacity, int set_new_bits) 6318c2ecf20Sopenharmony_ci{ 6328c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 6338c2ecf20Sopenharmony_ci unsigned long bits, words, owords, obits; 6348c2ecf20Sopenharmony_ci unsigned long want, have, onpages; /* number of pages */ 6358c2ecf20Sopenharmony_ci struct page **npages, **opages = NULL; 6368c2ecf20Sopenharmony_ci int err = 0; 6378c2ecf20Sopenharmony_ci bool growing; 6388c2ecf20Sopenharmony_ci 6398c2ecf20Sopenharmony_ci if (!expect(b)) 6408c2ecf20Sopenharmony_ci return -ENOMEM; 6418c2ecf20Sopenharmony_ci 6428c2ecf20Sopenharmony_ci drbd_bm_lock(device, "resize", BM_LOCKED_MASK); 6438c2ecf20Sopenharmony_ci 6448c2ecf20Sopenharmony_ci drbd_info(device, "drbd_bm_resize called with capacity == %llu\n", 6458c2ecf20Sopenharmony_ci (unsigned long long)capacity); 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci if (capacity == b->bm_dev_capacity) 6488c2ecf20Sopenharmony_ci goto out; 6498c2ecf20Sopenharmony_ci 6508c2ecf20Sopenharmony_ci if (capacity == 0) { 6518c2ecf20Sopenharmony_ci spin_lock_irq(&b->bm_lock); 6528c2ecf20Sopenharmony_ci opages = b->bm_pages; 6538c2ecf20Sopenharmony_ci onpages = b->bm_number_of_pages; 6548c2ecf20Sopenharmony_ci owords = b->bm_words; 6558c2ecf20Sopenharmony_ci b->bm_pages = NULL; 6568c2ecf20Sopenharmony_ci b->bm_number_of_pages = 6578c2ecf20Sopenharmony_ci b->bm_set = 6588c2ecf20Sopenharmony_ci b->bm_bits = 6598c2ecf20Sopenharmony_ci b->bm_words = 6608c2ecf20Sopenharmony_ci b->bm_dev_capacity = 0; 6618c2ecf20Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 6628c2ecf20Sopenharmony_ci bm_free_pages(opages, onpages); 6638c2ecf20Sopenharmony_ci bm_vk_free(opages); 6648c2ecf20Sopenharmony_ci goto out; 6658c2ecf20Sopenharmony_ci } 6668c2ecf20Sopenharmony_ci bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT)); 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ci /* if we would use 6698c2ecf20Sopenharmony_ci words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL; 6708c2ecf20Sopenharmony_ci a 32bit host could present the wrong number of words 6718c2ecf20Sopenharmony_ci to a 64bit host. 6728c2ecf20Sopenharmony_ci */ 6738c2ecf20Sopenharmony_ci words = ALIGN(bits, 64) >> LN2_BPL; 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci if (get_ldev(device)) { 6768c2ecf20Sopenharmony_ci u64 bits_on_disk = drbd_md_on_disk_bits(device->ldev); 6778c2ecf20Sopenharmony_ci put_ldev(device); 6788c2ecf20Sopenharmony_ci if (bits > bits_on_disk) { 6798c2ecf20Sopenharmony_ci drbd_info(device, "bits = %lu\n", bits); 6808c2ecf20Sopenharmony_ci drbd_info(device, "bits_on_disk = %llu\n", bits_on_disk); 6818c2ecf20Sopenharmony_ci err = -ENOSPC; 6828c2ecf20Sopenharmony_ci goto out; 6838c2ecf20Sopenharmony_ci } 6848c2ecf20Sopenharmony_ci } 6858c2ecf20Sopenharmony_ci 6868c2ecf20Sopenharmony_ci want = ALIGN(words*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT; 6878c2ecf20Sopenharmony_ci have = b->bm_number_of_pages; 6888c2ecf20Sopenharmony_ci if (want == have) { 6898c2ecf20Sopenharmony_ci D_ASSERT(device, b->bm_pages != NULL); 6908c2ecf20Sopenharmony_ci npages = b->bm_pages; 6918c2ecf20Sopenharmony_ci } else { 6928c2ecf20Sopenharmony_ci if (drbd_insert_fault(device, DRBD_FAULT_BM_ALLOC)) 6938c2ecf20Sopenharmony_ci npages = NULL; 6948c2ecf20Sopenharmony_ci else 6958c2ecf20Sopenharmony_ci npages = bm_realloc_pages(b, want); 6968c2ecf20Sopenharmony_ci } 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci if (!npages) { 6998c2ecf20Sopenharmony_ci err = -ENOMEM; 7008c2ecf20Sopenharmony_ci goto out; 7018c2ecf20Sopenharmony_ci } 7028c2ecf20Sopenharmony_ci 7038c2ecf20Sopenharmony_ci spin_lock_irq(&b->bm_lock); 7048c2ecf20Sopenharmony_ci opages = b->bm_pages; 7058c2ecf20Sopenharmony_ci owords = b->bm_words; 7068c2ecf20Sopenharmony_ci obits = b->bm_bits; 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci growing = bits > obits; 7098c2ecf20Sopenharmony_ci if (opages && growing && set_new_bits) 7108c2ecf20Sopenharmony_ci bm_set_surplus(b); 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_ci b->bm_pages = npages; 7138c2ecf20Sopenharmony_ci b->bm_number_of_pages = want; 7148c2ecf20Sopenharmony_ci b->bm_bits = bits; 7158c2ecf20Sopenharmony_ci b->bm_words = words; 7168c2ecf20Sopenharmony_ci b->bm_dev_capacity = capacity; 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci if (growing) { 7198c2ecf20Sopenharmony_ci if (set_new_bits) { 7208c2ecf20Sopenharmony_ci bm_memset(b, owords, 0xff, words-owords); 7218c2ecf20Sopenharmony_ci b->bm_set += bits - obits; 7228c2ecf20Sopenharmony_ci } else 7238c2ecf20Sopenharmony_ci bm_memset(b, owords, 0x00, words-owords); 7248c2ecf20Sopenharmony_ci 7258c2ecf20Sopenharmony_ci } 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ci if (want < have) { 7288c2ecf20Sopenharmony_ci /* implicit: (opages != NULL) && (opages != npages) */ 7298c2ecf20Sopenharmony_ci bm_free_pages(opages + want, have - want); 7308c2ecf20Sopenharmony_ci } 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci (void)bm_clear_surplus(b); 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 7358c2ecf20Sopenharmony_ci if (opages != npages) 7368c2ecf20Sopenharmony_ci bm_vk_free(opages); 7378c2ecf20Sopenharmony_ci if (!growing) 7388c2ecf20Sopenharmony_ci b->bm_set = bm_count_bits(b); 7398c2ecf20Sopenharmony_ci drbd_info(device, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want); 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_ci out: 7428c2ecf20Sopenharmony_ci drbd_bm_unlock(device); 7438c2ecf20Sopenharmony_ci return err; 7448c2ecf20Sopenharmony_ci} 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_ci/* inherently racy: 7478c2ecf20Sopenharmony_ci * if not protected by other means, return value may be out of date when 7488c2ecf20Sopenharmony_ci * leaving this function... 7498c2ecf20Sopenharmony_ci * we still need to lock it, since it is important that this returns 7508c2ecf20Sopenharmony_ci * bm_set == 0 precisely. 7518c2ecf20Sopenharmony_ci * 7528c2ecf20Sopenharmony_ci * maybe bm_set should be atomic_t ? 7538c2ecf20Sopenharmony_ci */ 7548c2ecf20Sopenharmony_ciunsigned long _drbd_bm_total_weight(struct drbd_device *device) 7558c2ecf20Sopenharmony_ci{ 7568c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 7578c2ecf20Sopenharmony_ci unsigned long s; 7588c2ecf20Sopenharmony_ci unsigned long flags; 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_ci if (!expect(b)) 7618c2ecf20Sopenharmony_ci return 0; 7628c2ecf20Sopenharmony_ci if (!expect(b->bm_pages)) 7638c2ecf20Sopenharmony_ci return 0; 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_ci spin_lock_irqsave(&b->bm_lock, flags); 7668c2ecf20Sopenharmony_ci s = b->bm_set; 7678c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&b->bm_lock, flags); 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_ci return s; 7708c2ecf20Sopenharmony_ci} 7718c2ecf20Sopenharmony_ci 7728c2ecf20Sopenharmony_ciunsigned long drbd_bm_total_weight(struct drbd_device *device) 7738c2ecf20Sopenharmony_ci{ 7748c2ecf20Sopenharmony_ci unsigned long s; 7758c2ecf20Sopenharmony_ci /* if I don't have a disk, I don't know about out-of-sync status */ 7768c2ecf20Sopenharmony_ci if (!get_ldev_if_state(device, D_NEGOTIATING)) 7778c2ecf20Sopenharmony_ci return 0; 7788c2ecf20Sopenharmony_ci s = _drbd_bm_total_weight(device); 7798c2ecf20Sopenharmony_ci put_ldev(device); 7808c2ecf20Sopenharmony_ci return s; 7818c2ecf20Sopenharmony_ci} 7828c2ecf20Sopenharmony_ci 7838c2ecf20Sopenharmony_cisize_t drbd_bm_words(struct drbd_device *device) 7848c2ecf20Sopenharmony_ci{ 7858c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 7868c2ecf20Sopenharmony_ci if (!expect(b)) 7878c2ecf20Sopenharmony_ci return 0; 7888c2ecf20Sopenharmony_ci if (!expect(b->bm_pages)) 7898c2ecf20Sopenharmony_ci return 0; 7908c2ecf20Sopenharmony_ci 7918c2ecf20Sopenharmony_ci return b->bm_words; 7928c2ecf20Sopenharmony_ci} 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_ciunsigned long drbd_bm_bits(struct drbd_device *device) 7958c2ecf20Sopenharmony_ci{ 7968c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 7978c2ecf20Sopenharmony_ci if (!expect(b)) 7988c2ecf20Sopenharmony_ci return 0; 7998c2ecf20Sopenharmony_ci 8008c2ecf20Sopenharmony_ci return b->bm_bits; 8018c2ecf20Sopenharmony_ci} 8028c2ecf20Sopenharmony_ci 8038c2ecf20Sopenharmony_ci/* merge number words from buffer into the bitmap starting at offset. 8048c2ecf20Sopenharmony_ci * buffer[i] is expected to be little endian unsigned long. 8058c2ecf20Sopenharmony_ci * bitmap must be locked by drbd_bm_lock. 8068c2ecf20Sopenharmony_ci * currently only used from receive_bitmap. 8078c2ecf20Sopenharmony_ci */ 8088c2ecf20Sopenharmony_civoid drbd_bm_merge_lel(struct drbd_device *device, size_t offset, size_t number, 8098c2ecf20Sopenharmony_ci unsigned long *buffer) 8108c2ecf20Sopenharmony_ci{ 8118c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 8128c2ecf20Sopenharmony_ci unsigned long *p_addr, *bm; 8138c2ecf20Sopenharmony_ci unsigned long word, bits; 8148c2ecf20Sopenharmony_ci unsigned int idx; 8158c2ecf20Sopenharmony_ci size_t end, do_now; 8168c2ecf20Sopenharmony_ci 8178c2ecf20Sopenharmony_ci end = offset + number; 8188c2ecf20Sopenharmony_ci 8198c2ecf20Sopenharmony_ci if (!expect(b)) 8208c2ecf20Sopenharmony_ci return; 8218c2ecf20Sopenharmony_ci if (!expect(b->bm_pages)) 8228c2ecf20Sopenharmony_ci return; 8238c2ecf20Sopenharmony_ci if (number == 0) 8248c2ecf20Sopenharmony_ci return; 8258c2ecf20Sopenharmony_ci WARN_ON(offset >= b->bm_words); 8268c2ecf20Sopenharmony_ci WARN_ON(end > b->bm_words); 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci spin_lock_irq(&b->bm_lock); 8298c2ecf20Sopenharmony_ci while (offset < end) { 8308c2ecf20Sopenharmony_ci do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset; 8318c2ecf20Sopenharmony_ci idx = bm_word_to_page_idx(b, offset); 8328c2ecf20Sopenharmony_ci p_addr = bm_map_pidx(b, idx); 8338c2ecf20Sopenharmony_ci bm = p_addr + MLPP(offset); 8348c2ecf20Sopenharmony_ci offset += do_now; 8358c2ecf20Sopenharmony_ci while (do_now--) { 8368c2ecf20Sopenharmony_ci bits = hweight_long(*bm); 8378c2ecf20Sopenharmony_ci word = *bm | *buffer++; 8388c2ecf20Sopenharmony_ci *bm++ = word; 8398c2ecf20Sopenharmony_ci b->bm_set += hweight_long(word) - bits; 8408c2ecf20Sopenharmony_ci } 8418c2ecf20Sopenharmony_ci bm_unmap(p_addr); 8428c2ecf20Sopenharmony_ci bm_set_page_need_writeout(b->bm_pages[idx]); 8438c2ecf20Sopenharmony_ci } 8448c2ecf20Sopenharmony_ci /* with 32bit <-> 64bit cross-platform connect 8458c2ecf20Sopenharmony_ci * this is only correct for current usage, 8468c2ecf20Sopenharmony_ci * where we _know_ that we are 64 bit aligned, 8478c2ecf20Sopenharmony_ci * and know that this function is used in this way, too... 8488c2ecf20Sopenharmony_ci */ 8498c2ecf20Sopenharmony_ci if (end == b->bm_words) 8508c2ecf20Sopenharmony_ci b->bm_set -= bm_clear_surplus(b); 8518c2ecf20Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 8528c2ecf20Sopenharmony_ci} 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ci/* copy number words from the bitmap starting at offset into the buffer. 8558c2ecf20Sopenharmony_ci * buffer[i] will be little endian unsigned long. 8568c2ecf20Sopenharmony_ci */ 8578c2ecf20Sopenharmony_civoid drbd_bm_get_lel(struct drbd_device *device, size_t offset, size_t number, 8588c2ecf20Sopenharmony_ci unsigned long *buffer) 8598c2ecf20Sopenharmony_ci{ 8608c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 8618c2ecf20Sopenharmony_ci unsigned long *p_addr, *bm; 8628c2ecf20Sopenharmony_ci size_t end, do_now; 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_ci end = offset + number; 8658c2ecf20Sopenharmony_ci 8668c2ecf20Sopenharmony_ci if (!expect(b)) 8678c2ecf20Sopenharmony_ci return; 8688c2ecf20Sopenharmony_ci if (!expect(b->bm_pages)) 8698c2ecf20Sopenharmony_ci return; 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_ci spin_lock_irq(&b->bm_lock); 8728c2ecf20Sopenharmony_ci if ((offset >= b->bm_words) || 8738c2ecf20Sopenharmony_ci (end > b->bm_words) || 8748c2ecf20Sopenharmony_ci (number <= 0)) 8758c2ecf20Sopenharmony_ci drbd_err(device, "offset=%lu number=%lu bm_words=%lu\n", 8768c2ecf20Sopenharmony_ci (unsigned long) offset, 8778c2ecf20Sopenharmony_ci (unsigned long) number, 8788c2ecf20Sopenharmony_ci (unsigned long) b->bm_words); 8798c2ecf20Sopenharmony_ci else { 8808c2ecf20Sopenharmony_ci while (offset < end) { 8818c2ecf20Sopenharmony_ci do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset; 8828c2ecf20Sopenharmony_ci p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset)); 8838c2ecf20Sopenharmony_ci bm = p_addr + MLPP(offset); 8848c2ecf20Sopenharmony_ci offset += do_now; 8858c2ecf20Sopenharmony_ci while (do_now--) 8868c2ecf20Sopenharmony_ci *buffer++ = *bm++; 8878c2ecf20Sopenharmony_ci bm_unmap(p_addr); 8888c2ecf20Sopenharmony_ci } 8898c2ecf20Sopenharmony_ci } 8908c2ecf20Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 8918c2ecf20Sopenharmony_ci} 8928c2ecf20Sopenharmony_ci 8938c2ecf20Sopenharmony_ci/* set all bits in the bitmap */ 8948c2ecf20Sopenharmony_civoid drbd_bm_set_all(struct drbd_device *device) 8958c2ecf20Sopenharmony_ci{ 8968c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 8978c2ecf20Sopenharmony_ci if (!expect(b)) 8988c2ecf20Sopenharmony_ci return; 8998c2ecf20Sopenharmony_ci if (!expect(b->bm_pages)) 9008c2ecf20Sopenharmony_ci return; 9018c2ecf20Sopenharmony_ci 9028c2ecf20Sopenharmony_ci spin_lock_irq(&b->bm_lock); 9038c2ecf20Sopenharmony_ci bm_memset(b, 0, 0xff, b->bm_words); 9048c2ecf20Sopenharmony_ci (void)bm_clear_surplus(b); 9058c2ecf20Sopenharmony_ci b->bm_set = b->bm_bits; 9068c2ecf20Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 9078c2ecf20Sopenharmony_ci} 9088c2ecf20Sopenharmony_ci 9098c2ecf20Sopenharmony_ci/* clear all bits in the bitmap */ 9108c2ecf20Sopenharmony_civoid drbd_bm_clear_all(struct drbd_device *device) 9118c2ecf20Sopenharmony_ci{ 9128c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 9138c2ecf20Sopenharmony_ci if (!expect(b)) 9148c2ecf20Sopenharmony_ci return; 9158c2ecf20Sopenharmony_ci if (!expect(b->bm_pages)) 9168c2ecf20Sopenharmony_ci return; 9178c2ecf20Sopenharmony_ci 9188c2ecf20Sopenharmony_ci spin_lock_irq(&b->bm_lock); 9198c2ecf20Sopenharmony_ci bm_memset(b, 0, 0, b->bm_words); 9208c2ecf20Sopenharmony_ci b->bm_set = 0; 9218c2ecf20Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 9228c2ecf20Sopenharmony_ci} 9238c2ecf20Sopenharmony_ci 9248c2ecf20Sopenharmony_cistatic void drbd_bm_aio_ctx_destroy(struct kref *kref) 9258c2ecf20Sopenharmony_ci{ 9268c2ecf20Sopenharmony_ci struct drbd_bm_aio_ctx *ctx = container_of(kref, struct drbd_bm_aio_ctx, kref); 9278c2ecf20Sopenharmony_ci unsigned long flags; 9288c2ecf20Sopenharmony_ci 9298c2ecf20Sopenharmony_ci spin_lock_irqsave(&ctx->device->resource->req_lock, flags); 9308c2ecf20Sopenharmony_ci list_del(&ctx->list); 9318c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&ctx->device->resource->req_lock, flags); 9328c2ecf20Sopenharmony_ci put_ldev(ctx->device); 9338c2ecf20Sopenharmony_ci kfree(ctx); 9348c2ecf20Sopenharmony_ci} 9358c2ecf20Sopenharmony_ci 9368c2ecf20Sopenharmony_ci/* bv_page may be a copy, or may be the original */ 9378c2ecf20Sopenharmony_cistatic void drbd_bm_endio(struct bio *bio) 9388c2ecf20Sopenharmony_ci{ 9398c2ecf20Sopenharmony_ci struct drbd_bm_aio_ctx *ctx = bio->bi_private; 9408c2ecf20Sopenharmony_ci struct drbd_device *device = ctx->device; 9418c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 9428c2ecf20Sopenharmony_ci unsigned int idx = bm_page_to_idx(bio_first_page_all(bio)); 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_ci if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 && 9458c2ecf20Sopenharmony_ci !bm_test_page_unchanged(b->bm_pages[idx])) 9468c2ecf20Sopenharmony_ci drbd_warn(device, "bitmap page idx %u changed during IO!\n", idx); 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci if (bio->bi_status) { 9498c2ecf20Sopenharmony_ci /* ctx error will hold the completed-last non-zero error code, 9508c2ecf20Sopenharmony_ci * in case error codes differ. */ 9518c2ecf20Sopenharmony_ci ctx->error = blk_status_to_errno(bio->bi_status); 9528c2ecf20Sopenharmony_ci bm_set_page_io_err(b->bm_pages[idx]); 9538c2ecf20Sopenharmony_ci /* Not identical to on disk version of it. 9548c2ecf20Sopenharmony_ci * Is BM_PAGE_IO_ERROR enough? */ 9558c2ecf20Sopenharmony_ci if (__ratelimit(&drbd_ratelimit_state)) 9568c2ecf20Sopenharmony_ci drbd_err(device, "IO ERROR %d on bitmap page idx %u\n", 9578c2ecf20Sopenharmony_ci bio->bi_status, idx); 9588c2ecf20Sopenharmony_ci } else { 9598c2ecf20Sopenharmony_ci bm_clear_page_io_err(b->bm_pages[idx]); 9608c2ecf20Sopenharmony_ci dynamic_drbd_dbg(device, "bitmap page idx %u completed\n", idx); 9618c2ecf20Sopenharmony_ci } 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci bm_page_unlock_io(device, idx); 9648c2ecf20Sopenharmony_ci 9658c2ecf20Sopenharmony_ci if (ctx->flags & BM_AIO_COPY_PAGES) 9668c2ecf20Sopenharmony_ci mempool_free(bio->bi_io_vec[0].bv_page, &drbd_md_io_page_pool); 9678c2ecf20Sopenharmony_ci 9688c2ecf20Sopenharmony_ci bio_put(bio); 9698c2ecf20Sopenharmony_ci 9708c2ecf20Sopenharmony_ci if (atomic_dec_and_test(&ctx->in_flight)) { 9718c2ecf20Sopenharmony_ci ctx->done = 1; 9728c2ecf20Sopenharmony_ci wake_up(&device->misc_wait); 9738c2ecf20Sopenharmony_ci kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy); 9748c2ecf20Sopenharmony_ci } 9758c2ecf20Sopenharmony_ci} 9768c2ecf20Sopenharmony_ci 9778c2ecf20Sopenharmony_cistatic void bm_page_io_async(struct drbd_bm_aio_ctx *ctx, int page_nr) __must_hold(local) 9788c2ecf20Sopenharmony_ci{ 9798c2ecf20Sopenharmony_ci struct bio *bio = bio_alloc_drbd(GFP_NOIO); 9808c2ecf20Sopenharmony_ci struct drbd_device *device = ctx->device; 9818c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 9828c2ecf20Sopenharmony_ci struct page *page; 9838c2ecf20Sopenharmony_ci unsigned int len; 9848c2ecf20Sopenharmony_ci unsigned int op = (ctx->flags & BM_AIO_READ) ? REQ_OP_READ : REQ_OP_WRITE; 9858c2ecf20Sopenharmony_ci 9868c2ecf20Sopenharmony_ci sector_t on_disk_sector = 9878c2ecf20Sopenharmony_ci device->ldev->md.md_offset + device->ldev->md.bm_offset; 9888c2ecf20Sopenharmony_ci on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9); 9898c2ecf20Sopenharmony_ci 9908c2ecf20Sopenharmony_ci /* this might happen with very small 9918c2ecf20Sopenharmony_ci * flexible external meta data device, 9928c2ecf20Sopenharmony_ci * or with PAGE_SIZE > 4k */ 9938c2ecf20Sopenharmony_ci len = min_t(unsigned int, PAGE_SIZE, 9948c2ecf20Sopenharmony_ci (drbd_md_last_sector(device->ldev) - on_disk_sector + 1)<<9); 9958c2ecf20Sopenharmony_ci 9968c2ecf20Sopenharmony_ci /* serialize IO on this page */ 9978c2ecf20Sopenharmony_ci bm_page_lock_io(device, page_nr); 9988c2ecf20Sopenharmony_ci /* before memcpy and submit, 9998c2ecf20Sopenharmony_ci * so it can be redirtied any time */ 10008c2ecf20Sopenharmony_ci bm_set_page_unchanged(b->bm_pages[page_nr]); 10018c2ecf20Sopenharmony_ci 10028c2ecf20Sopenharmony_ci if (ctx->flags & BM_AIO_COPY_PAGES) { 10038c2ecf20Sopenharmony_ci page = mempool_alloc(&drbd_md_io_page_pool, 10048c2ecf20Sopenharmony_ci GFP_NOIO | __GFP_HIGHMEM); 10058c2ecf20Sopenharmony_ci copy_highpage(page, b->bm_pages[page_nr]); 10068c2ecf20Sopenharmony_ci bm_store_page_idx(page, page_nr); 10078c2ecf20Sopenharmony_ci } else 10088c2ecf20Sopenharmony_ci page = b->bm_pages[page_nr]; 10098c2ecf20Sopenharmony_ci bio_set_dev(bio, device->ldev->md_bdev); 10108c2ecf20Sopenharmony_ci bio->bi_iter.bi_sector = on_disk_sector; 10118c2ecf20Sopenharmony_ci /* bio_add_page of a single page to an empty bio will always succeed, 10128c2ecf20Sopenharmony_ci * according to api. Do we want to assert that? */ 10138c2ecf20Sopenharmony_ci bio_add_page(bio, page, len, 0); 10148c2ecf20Sopenharmony_ci bio->bi_private = ctx; 10158c2ecf20Sopenharmony_ci bio->bi_end_io = drbd_bm_endio; 10168c2ecf20Sopenharmony_ci bio_set_op_attrs(bio, op, 0); 10178c2ecf20Sopenharmony_ci 10188c2ecf20Sopenharmony_ci if (drbd_insert_fault(device, (op == REQ_OP_WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) { 10198c2ecf20Sopenharmony_ci bio_io_error(bio); 10208c2ecf20Sopenharmony_ci } else { 10218c2ecf20Sopenharmony_ci submit_bio(bio); 10228c2ecf20Sopenharmony_ci /* this should not count as user activity and cause the 10238c2ecf20Sopenharmony_ci * resync to throttle -- see drbd_rs_should_slow_down(). */ 10248c2ecf20Sopenharmony_ci atomic_add(len >> 9, &device->rs_sect_ev); 10258c2ecf20Sopenharmony_ci } 10268c2ecf20Sopenharmony_ci} 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_ci/* 10298c2ecf20Sopenharmony_ci * bm_rw: read/write the whole bitmap from/to its on disk location. 10308c2ecf20Sopenharmony_ci */ 10318c2ecf20Sopenharmony_cistatic int bm_rw(struct drbd_device *device, const unsigned int flags, unsigned lazy_writeout_upper_idx) __must_hold(local) 10328c2ecf20Sopenharmony_ci{ 10338c2ecf20Sopenharmony_ci struct drbd_bm_aio_ctx *ctx; 10348c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 10358c2ecf20Sopenharmony_ci unsigned int num_pages, i, count = 0; 10368c2ecf20Sopenharmony_ci unsigned long now; 10378c2ecf20Sopenharmony_ci char ppb[10]; 10388c2ecf20Sopenharmony_ci int err = 0; 10398c2ecf20Sopenharmony_ci 10408c2ecf20Sopenharmony_ci /* 10418c2ecf20Sopenharmony_ci * We are protected against bitmap disappearing/resizing by holding an 10428c2ecf20Sopenharmony_ci * ldev reference (caller must have called get_ldev()). 10438c2ecf20Sopenharmony_ci * For read/write, we are protected against changes to the bitmap by 10448c2ecf20Sopenharmony_ci * the bitmap lock (see drbd_bitmap_io). 10458c2ecf20Sopenharmony_ci * For lazy writeout, we don't care for ongoing changes to the bitmap, 10468c2ecf20Sopenharmony_ci * as we submit copies of pages anyways. 10478c2ecf20Sopenharmony_ci */ 10488c2ecf20Sopenharmony_ci 10498c2ecf20Sopenharmony_ci ctx = kmalloc(sizeof(struct drbd_bm_aio_ctx), GFP_NOIO); 10508c2ecf20Sopenharmony_ci if (!ctx) 10518c2ecf20Sopenharmony_ci return -ENOMEM; 10528c2ecf20Sopenharmony_ci 10538c2ecf20Sopenharmony_ci *ctx = (struct drbd_bm_aio_ctx) { 10548c2ecf20Sopenharmony_ci .device = device, 10558c2ecf20Sopenharmony_ci .start_jif = jiffies, 10568c2ecf20Sopenharmony_ci .in_flight = ATOMIC_INIT(1), 10578c2ecf20Sopenharmony_ci .done = 0, 10588c2ecf20Sopenharmony_ci .flags = flags, 10598c2ecf20Sopenharmony_ci .error = 0, 10608c2ecf20Sopenharmony_ci .kref = KREF_INIT(2), 10618c2ecf20Sopenharmony_ci }; 10628c2ecf20Sopenharmony_ci 10638c2ecf20Sopenharmony_ci if (!get_ldev_if_state(device, D_ATTACHING)) { /* put is in drbd_bm_aio_ctx_destroy() */ 10648c2ecf20Sopenharmony_ci drbd_err(device, "ASSERT FAILED: get_ldev_if_state() == 1 in bm_rw()\n"); 10658c2ecf20Sopenharmony_ci kfree(ctx); 10668c2ecf20Sopenharmony_ci return -ENODEV; 10678c2ecf20Sopenharmony_ci } 10688c2ecf20Sopenharmony_ci /* Here D_ATTACHING is sufficient since drbd_bm_read() is called only from 10698c2ecf20Sopenharmony_ci drbd_adm_attach(), after device->ldev was assigned. */ 10708c2ecf20Sopenharmony_ci 10718c2ecf20Sopenharmony_ci if (0 == (ctx->flags & ~BM_AIO_READ)) 10728c2ecf20Sopenharmony_ci WARN_ON(!(BM_LOCKED_MASK & b->bm_flags)); 10738c2ecf20Sopenharmony_ci 10748c2ecf20Sopenharmony_ci spin_lock_irq(&device->resource->req_lock); 10758c2ecf20Sopenharmony_ci list_add_tail(&ctx->list, &device->pending_bitmap_io); 10768c2ecf20Sopenharmony_ci spin_unlock_irq(&device->resource->req_lock); 10778c2ecf20Sopenharmony_ci 10788c2ecf20Sopenharmony_ci num_pages = b->bm_number_of_pages; 10798c2ecf20Sopenharmony_ci 10808c2ecf20Sopenharmony_ci now = jiffies; 10818c2ecf20Sopenharmony_ci 10828c2ecf20Sopenharmony_ci /* let the layers below us try to merge these bios... */ 10838c2ecf20Sopenharmony_ci 10848c2ecf20Sopenharmony_ci if (flags & BM_AIO_READ) { 10858c2ecf20Sopenharmony_ci for (i = 0; i < num_pages; i++) { 10868c2ecf20Sopenharmony_ci atomic_inc(&ctx->in_flight); 10878c2ecf20Sopenharmony_ci bm_page_io_async(ctx, i); 10888c2ecf20Sopenharmony_ci ++count; 10898c2ecf20Sopenharmony_ci cond_resched(); 10908c2ecf20Sopenharmony_ci } 10918c2ecf20Sopenharmony_ci } else if (flags & BM_AIO_WRITE_HINTED) { 10928c2ecf20Sopenharmony_ci /* ASSERT: BM_AIO_WRITE_ALL_PAGES is not set. */ 10938c2ecf20Sopenharmony_ci unsigned int hint; 10948c2ecf20Sopenharmony_ci for (hint = 0; hint < b->n_bitmap_hints; hint++) { 10958c2ecf20Sopenharmony_ci i = b->al_bitmap_hints[hint]; 10968c2ecf20Sopenharmony_ci if (i >= num_pages) /* == -1U: no hint here. */ 10978c2ecf20Sopenharmony_ci continue; 10988c2ecf20Sopenharmony_ci /* Several AL-extents may point to the same page. */ 10998c2ecf20Sopenharmony_ci if (!test_and_clear_bit(BM_PAGE_HINT_WRITEOUT, 11008c2ecf20Sopenharmony_ci &page_private(b->bm_pages[i]))) 11018c2ecf20Sopenharmony_ci continue; 11028c2ecf20Sopenharmony_ci /* Has it even changed? */ 11038c2ecf20Sopenharmony_ci if (bm_test_page_unchanged(b->bm_pages[i])) 11048c2ecf20Sopenharmony_ci continue; 11058c2ecf20Sopenharmony_ci atomic_inc(&ctx->in_flight); 11068c2ecf20Sopenharmony_ci bm_page_io_async(ctx, i); 11078c2ecf20Sopenharmony_ci ++count; 11088c2ecf20Sopenharmony_ci } 11098c2ecf20Sopenharmony_ci } else { 11108c2ecf20Sopenharmony_ci for (i = 0; i < num_pages; i++) { 11118c2ecf20Sopenharmony_ci /* ignore completely unchanged pages */ 11128c2ecf20Sopenharmony_ci if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx) 11138c2ecf20Sopenharmony_ci break; 11148c2ecf20Sopenharmony_ci if (!(flags & BM_AIO_WRITE_ALL_PAGES) && 11158c2ecf20Sopenharmony_ci bm_test_page_unchanged(b->bm_pages[i])) { 11168c2ecf20Sopenharmony_ci dynamic_drbd_dbg(device, "skipped bm write for idx %u\n", i); 11178c2ecf20Sopenharmony_ci continue; 11188c2ecf20Sopenharmony_ci } 11198c2ecf20Sopenharmony_ci /* during lazy writeout, 11208c2ecf20Sopenharmony_ci * ignore those pages not marked for lazy writeout. */ 11218c2ecf20Sopenharmony_ci if (lazy_writeout_upper_idx && 11228c2ecf20Sopenharmony_ci !bm_test_page_lazy_writeout(b->bm_pages[i])) { 11238c2ecf20Sopenharmony_ci dynamic_drbd_dbg(device, "skipped bm lazy write for idx %u\n", i); 11248c2ecf20Sopenharmony_ci continue; 11258c2ecf20Sopenharmony_ci } 11268c2ecf20Sopenharmony_ci atomic_inc(&ctx->in_flight); 11278c2ecf20Sopenharmony_ci bm_page_io_async(ctx, i); 11288c2ecf20Sopenharmony_ci ++count; 11298c2ecf20Sopenharmony_ci cond_resched(); 11308c2ecf20Sopenharmony_ci } 11318c2ecf20Sopenharmony_ci } 11328c2ecf20Sopenharmony_ci 11338c2ecf20Sopenharmony_ci /* 11348c2ecf20Sopenharmony_ci * We initialize ctx->in_flight to one to make sure drbd_bm_endio 11358c2ecf20Sopenharmony_ci * will not set ctx->done early, and decrement / test it here. If there 11368c2ecf20Sopenharmony_ci * are still some bios in flight, we need to wait for them here. 11378c2ecf20Sopenharmony_ci * If all IO is done already (or nothing had been submitted), there is 11388c2ecf20Sopenharmony_ci * no need to wait. Still, we need to put the kref associated with the 11398c2ecf20Sopenharmony_ci * "in_flight reached zero, all done" event. 11408c2ecf20Sopenharmony_ci */ 11418c2ecf20Sopenharmony_ci if (!atomic_dec_and_test(&ctx->in_flight)) 11428c2ecf20Sopenharmony_ci wait_until_done_or_force_detached(device, device->ldev, &ctx->done); 11438c2ecf20Sopenharmony_ci else 11448c2ecf20Sopenharmony_ci kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy); 11458c2ecf20Sopenharmony_ci 11468c2ecf20Sopenharmony_ci /* summary for global bitmap IO */ 11478c2ecf20Sopenharmony_ci if (flags == 0) { 11488c2ecf20Sopenharmony_ci unsigned int ms = jiffies_to_msecs(jiffies - now); 11498c2ecf20Sopenharmony_ci if (ms > 5) { 11508c2ecf20Sopenharmony_ci drbd_info(device, "bitmap %s of %u pages took %u ms\n", 11518c2ecf20Sopenharmony_ci (flags & BM_AIO_READ) ? "READ" : "WRITE", 11528c2ecf20Sopenharmony_ci count, ms); 11538c2ecf20Sopenharmony_ci } 11548c2ecf20Sopenharmony_ci } 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_ci if (ctx->error) { 11578c2ecf20Sopenharmony_ci drbd_alert(device, "we had at least one MD IO ERROR during bitmap IO\n"); 11588c2ecf20Sopenharmony_ci drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR); 11598c2ecf20Sopenharmony_ci err = -EIO; /* ctx->error ? */ 11608c2ecf20Sopenharmony_ci } 11618c2ecf20Sopenharmony_ci 11628c2ecf20Sopenharmony_ci if (atomic_read(&ctx->in_flight)) 11638c2ecf20Sopenharmony_ci err = -EIO; /* Disk timeout/force-detach during IO... */ 11648c2ecf20Sopenharmony_ci 11658c2ecf20Sopenharmony_ci now = jiffies; 11668c2ecf20Sopenharmony_ci if (flags & BM_AIO_READ) { 11678c2ecf20Sopenharmony_ci b->bm_set = bm_count_bits(b); 11688c2ecf20Sopenharmony_ci drbd_info(device, "recounting of set bits took additional %lu jiffies\n", 11698c2ecf20Sopenharmony_ci jiffies - now); 11708c2ecf20Sopenharmony_ci } 11718c2ecf20Sopenharmony_ci now = b->bm_set; 11728c2ecf20Sopenharmony_ci 11738c2ecf20Sopenharmony_ci if ((flags & ~BM_AIO_READ) == 0) 11748c2ecf20Sopenharmony_ci drbd_info(device, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n", 11758c2ecf20Sopenharmony_ci ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now); 11768c2ecf20Sopenharmony_ci 11778c2ecf20Sopenharmony_ci kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy); 11788c2ecf20Sopenharmony_ci return err; 11798c2ecf20Sopenharmony_ci} 11808c2ecf20Sopenharmony_ci 11818c2ecf20Sopenharmony_ci/** 11828c2ecf20Sopenharmony_ci * drbd_bm_read() - Read the whole bitmap from its on disk location. 11838c2ecf20Sopenharmony_ci * @device: DRBD device. 11848c2ecf20Sopenharmony_ci */ 11858c2ecf20Sopenharmony_ciint drbd_bm_read(struct drbd_device *device) __must_hold(local) 11868c2ecf20Sopenharmony_ci{ 11878c2ecf20Sopenharmony_ci return bm_rw(device, BM_AIO_READ, 0); 11888c2ecf20Sopenharmony_ci} 11898c2ecf20Sopenharmony_ci 11908c2ecf20Sopenharmony_ci/** 11918c2ecf20Sopenharmony_ci * drbd_bm_write() - Write the whole bitmap to its on disk location. 11928c2ecf20Sopenharmony_ci * @device: DRBD device. 11938c2ecf20Sopenharmony_ci * 11948c2ecf20Sopenharmony_ci * Will only write pages that have changed since last IO. 11958c2ecf20Sopenharmony_ci */ 11968c2ecf20Sopenharmony_ciint drbd_bm_write(struct drbd_device *device) __must_hold(local) 11978c2ecf20Sopenharmony_ci{ 11988c2ecf20Sopenharmony_ci return bm_rw(device, 0, 0); 11998c2ecf20Sopenharmony_ci} 12008c2ecf20Sopenharmony_ci 12018c2ecf20Sopenharmony_ci/** 12028c2ecf20Sopenharmony_ci * drbd_bm_write_all() - Write the whole bitmap to its on disk location. 12038c2ecf20Sopenharmony_ci * @device: DRBD device. 12048c2ecf20Sopenharmony_ci * 12058c2ecf20Sopenharmony_ci * Will write all pages. 12068c2ecf20Sopenharmony_ci */ 12078c2ecf20Sopenharmony_ciint drbd_bm_write_all(struct drbd_device *device) __must_hold(local) 12088c2ecf20Sopenharmony_ci{ 12098c2ecf20Sopenharmony_ci return bm_rw(device, BM_AIO_WRITE_ALL_PAGES, 0); 12108c2ecf20Sopenharmony_ci} 12118c2ecf20Sopenharmony_ci 12128c2ecf20Sopenharmony_ci/** 12138c2ecf20Sopenharmony_ci * drbd_bm_write_lazy() - Write bitmap pages 0 to @upper_idx-1, if they have changed. 12148c2ecf20Sopenharmony_ci * @device: DRBD device. 12158c2ecf20Sopenharmony_ci * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages 12168c2ecf20Sopenharmony_ci */ 12178c2ecf20Sopenharmony_ciint drbd_bm_write_lazy(struct drbd_device *device, unsigned upper_idx) __must_hold(local) 12188c2ecf20Sopenharmony_ci{ 12198c2ecf20Sopenharmony_ci return bm_rw(device, BM_AIO_COPY_PAGES, upper_idx); 12208c2ecf20Sopenharmony_ci} 12218c2ecf20Sopenharmony_ci 12228c2ecf20Sopenharmony_ci/** 12238c2ecf20Sopenharmony_ci * drbd_bm_write_copy_pages() - Write the whole bitmap to its on disk location. 12248c2ecf20Sopenharmony_ci * @device: DRBD device. 12258c2ecf20Sopenharmony_ci * 12268c2ecf20Sopenharmony_ci * Will only write pages that have changed since last IO. 12278c2ecf20Sopenharmony_ci * In contrast to drbd_bm_write(), this will copy the bitmap pages 12288c2ecf20Sopenharmony_ci * to temporary writeout pages. It is intended to trigger a full write-out 12298c2ecf20Sopenharmony_ci * while still allowing the bitmap to change, for example if a resync or online 12308c2ecf20Sopenharmony_ci * verify is aborted due to a failed peer disk, while local IO continues, or 12318c2ecf20Sopenharmony_ci * pending resync acks are still being processed. 12328c2ecf20Sopenharmony_ci */ 12338c2ecf20Sopenharmony_ciint drbd_bm_write_copy_pages(struct drbd_device *device) __must_hold(local) 12348c2ecf20Sopenharmony_ci{ 12358c2ecf20Sopenharmony_ci return bm_rw(device, BM_AIO_COPY_PAGES, 0); 12368c2ecf20Sopenharmony_ci} 12378c2ecf20Sopenharmony_ci 12388c2ecf20Sopenharmony_ci/** 12398c2ecf20Sopenharmony_ci * drbd_bm_write_hinted() - Write bitmap pages with "hint" marks, if they have changed. 12408c2ecf20Sopenharmony_ci * @device: DRBD device. 12418c2ecf20Sopenharmony_ci */ 12428c2ecf20Sopenharmony_ciint drbd_bm_write_hinted(struct drbd_device *device) __must_hold(local) 12438c2ecf20Sopenharmony_ci{ 12448c2ecf20Sopenharmony_ci return bm_rw(device, BM_AIO_WRITE_HINTED | BM_AIO_COPY_PAGES, 0); 12458c2ecf20Sopenharmony_ci} 12468c2ecf20Sopenharmony_ci 12478c2ecf20Sopenharmony_ci/* NOTE 12488c2ecf20Sopenharmony_ci * find_first_bit returns int, we return unsigned long. 12498c2ecf20Sopenharmony_ci * For this to work on 32bit arch with bitnumbers > (1<<32), 12508c2ecf20Sopenharmony_ci * we'd need to return u64, and get a whole lot of other places 12518c2ecf20Sopenharmony_ci * fixed where we still use unsigned long. 12528c2ecf20Sopenharmony_ci * 12538c2ecf20Sopenharmony_ci * this returns a bit number, NOT a sector! 12548c2ecf20Sopenharmony_ci */ 12558c2ecf20Sopenharmony_cistatic unsigned long __bm_find_next(struct drbd_device *device, unsigned long bm_fo, 12568c2ecf20Sopenharmony_ci const int find_zero_bit) 12578c2ecf20Sopenharmony_ci{ 12588c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 12598c2ecf20Sopenharmony_ci unsigned long *p_addr; 12608c2ecf20Sopenharmony_ci unsigned long bit_offset; 12618c2ecf20Sopenharmony_ci unsigned i; 12628c2ecf20Sopenharmony_ci 12638c2ecf20Sopenharmony_ci 12648c2ecf20Sopenharmony_ci if (bm_fo > b->bm_bits) { 12658c2ecf20Sopenharmony_ci drbd_err(device, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits); 12668c2ecf20Sopenharmony_ci bm_fo = DRBD_END_OF_BITMAP; 12678c2ecf20Sopenharmony_ci } else { 12688c2ecf20Sopenharmony_ci while (bm_fo < b->bm_bits) { 12698c2ecf20Sopenharmony_ci /* bit offset of the first bit in the page */ 12708c2ecf20Sopenharmony_ci bit_offset = bm_fo & ~BITS_PER_PAGE_MASK; 12718c2ecf20Sopenharmony_ci p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo)); 12728c2ecf20Sopenharmony_ci 12738c2ecf20Sopenharmony_ci if (find_zero_bit) 12748c2ecf20Sopenharmony_ci i = find_next_zero_bit_le(p_addr, 12758c2ecf20Sopenharmony_ci PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK); 12768c2ecf20Sopenharmony_ci else 12778c2ecf20Sopenharmony_ci i = find_next_bit_le(p_addr, 12788c2ecf20Sopenharmony_ci PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK); 12798c2ecf20Sopenharmony_ci 12808c2ecf20Sopenharmony_ci __bm_unmap(p_addr); 12818c2ecf20Sopenharmony_ci if (i < PAGE_SIZE*8) { 12828c2ecf20Sopenharmony_ci bm_fo = bit_offset + i; 12838c2ecf20Sopenharmony_ci if (bm_fo >= b->bm_bits) 12848c2ecf20Sopenharmony_ci break; 12858c2ecf20Sopenharmony_ci goto found; 12868c2ecf20Sopenharmony_ci } 12878c2ecf20Sopenharmony_ci bm_fo = bit_offset + PAGE_SIZE*8; 12888c2ecf20Sopenharmony_ci } 12898c2ecf20Sopenharmony_ci bm_fo = DRBD_END_OF_BITMAP; 12908c2ecf20Sopenharmony_ci } 12918c2ecf20Sopenharmony_ci found: 12928c2ecf20Sopenharmony_ci return bm_fo; 12938c2ecf20Sopenharmony_ci} 12948c2ecf20Sopenharmony_ci 12958c2ecf20Sopenharmony_cistatic unsigned long bm_find_next(struct drbd_device *device, 12968c2ecf20Sopenharmony_ci unsigned long bm_fo, const int find_zero_bit) 12978c2ecf20Sopenharmony_ci{ 12988c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 12998c2ecf20Sopenharmony_ci unsigned long i = DRBD_END_OF_BITMAP; 13008c2ecf20Sopenharmony_ci 13018c2ecf20Sopenharmony_ci if (!expect(b)) 13028c2ecf20Sopenharmony_ci return i; 13038c2ecf20Sopenharmony_ci if (!expect(b->bm_pages)) 13048c2ecf20Sopenharmony_ci return i; 13058c2ecf20Sopenharmony_ci 13068c2ecf20Sopenharmony_ci spin_lock_irq(&b->bm_lock); 13078c2ecf20Sopenharmony_ci if (BM_DONT_TEST & b->bm_flags) 13088c2ecf20Sopenharmony_ci bm_print_lock_info(device); 13098c2ecf20Sopenharmony_ci 13108c2ecf20Sopenharmony_ci i = __bm_find_next(device, bm_fo, find_zero_bit); 13118c2ecf20Sopenharmony_ci 13128c2ecf20Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 13138c2ecf20Sopenharmony_ci return i; 13148c2ecf20Sopenharmony_ci} 13158c2ecf20Sopenharmony_ci 13168c2ecf20Sopenharmony_ciunsigned long drbd_bm_find_next(struct drbd_device *device, unsigned long bm_fo) 13178c2ecf20Sopenharmony_ci{ 13188c2ecf20Sopenharmony_ci return bm_find_next(device, bm_fo, 0); 13198c2ecf20Sopenharmony_ci} 13208c2ecf20Sopenharmony_ci 13218c2ecf20Sopenharmony_ci#if 0 13228c2ecf20Sopenharmony_ci/* not yet needed for anything. */ 13238c2ecf20Sopenharmony_ciunsigned long drbd_bm_find_next_zero(struct drbd_device *device, unsigned long bm_fo) 13248c2ecf20Sopenharmony_ci{ 13258c2ecf20Sopenharmony_ci return bm_find_next(device, bm_fo, 1); 13268c2ecf20Sopenharmony_ci} 13278c2ecf20Sopenharmony_ci#endif 13288c2ecf20Sopenharmony_ci 13298c2ecf20Sopenharmony_ci/* does not spin_lock_irqsave. 13308c2ecf20Sopenharmony_ci * you must take drbd_bm_lock() first */ 13318c2ecf20Sopenharmony_ciunsigned long _drbd_bm_find_next(struct drbd_device *device, unsigned long bm_fo) 13328c2ecf20Sopenharmony_ci{ 13338c2ecf20Sopenharmony_ci /* WARN_ON(!(BM_DONT_SET & device->b->bm_flags)); */ 13348c2ecf20Sopenharmony_ci return __bm_find_next(device, bm_fo, 0); 13358c2ecf20Sopenharmony_ci} 13368c2ecf20Sopenharmony_ci 13378c2ecf20Sopenharmony_ciunsigned long _drbd_bm_find_next_zero(struct drbd_device *device, unsigned long bm_fo) 13388c2ecf20Sopenharmony_ci{ 13398c2ecf20Sopenharmony_ci /* WARN_ON(!(BM_DONT_SET & device->b->bm_flags)); */ 13408c2ecf20Sopenharmony_ci return __bm_find_next(device, bm_fo, 1); 13418c2ecf20Sopenharmony_ci} 13428c2ecf20Sopenharmony_ci 13438c2ecf20Sopenharmony_ci/* returns number of bits actually changed. 13448c2ecf20Sopenharmony_ci * for val != 0, we change 0 -> 1, return code positive 13458c2ecf20Sopenharmony_ci * for val == 0, we change 1 -> 0, return code negative 13468c2ecf20Sopenharmony_ci * wants bitnr, not sector. 13478c2ecf20Sopenharmony_ci * expected to be called for only a few bits (e - s about BITS_PER_LONG). 13488c2ecf20Sopenharmony_ci * Must hold bitmap lock already. */ 13498c2ecf20Sopenharmony_cistatic int __bm_change_bits_to(struct drbd_device *device, const unsigned long s, 13508c2ecf20Sopenharmony_ci unsigned long e, int val) 13518c2ecf20Sopenharmony_ci{ 13528c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 13538c2ecf20Sopenharmony_ci unsigned long *p_addr = NULL; 13548c2ecf20Sopenharmony_ci unsigned long bitnr; 13558c2ecf20Sopenharmony_ci unsigned int last_page_nr = -1U; 13568c2ecf20Sopenharmony_ci int c = 0; 13578c2ecf20Sopenharmony_ci int changed_total = 0; 13588c2ecf20Sopenharmony_ci 13598c2ecf20Sopenharmony_ci if (e >= b->bm_bits) { 13608c2ecf20Sopenharmony_ci drbd_err(device, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n", 13618c2ecf20Sopenharmony_ci s, e, b->bm_bits); 13628c2ecf20Sopenharmony_ci e = b->bm_bits ? b->bm_bits -1 : 0; 13638c2ecf20Sopenharmony_ci } 13648c2ecf20Sopenharmony_ci for (bitnr = s; bitnr <= e; bitnr++) { 13658c2ecf20Sopenharmony_ci unsigned int page_nr = bm_bit_to_page_idx(b, bitnr); 13668c2ecf20Sopenharmony_ci if (page_nr != last_page_nr) { 13678c2ecf20Sopenharmony_ci if (p_addr) 13688c2ecf20Sopenharmony_ci __bm_unmap(p_addr); 13698c2ecf20Sopenharmony_ci if (c < 0) 13708c2ecf20Sopenharmony_ci bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]); 13718c2ecf20Sopenharmony_ci else if (c > 0) 13728c2ecf20Sopenharmony_ci bm_set_page_need_writeout(b->bm_pages[last_page_nr]); 13738c2ecf20Sopenharmony_ci changed_total += c; 13748c2ecf20Sopenharmony_ci c = 0; 13758c2ecf20Sopenharmony_ci p_addr = __bm_map_pidx(b, page_nr); 13768c2ecf20Sopenharmony_ci last_page_nr = page_nr; 13778c2ecf20Sopenharmony_ci } 13788c2ecf20Sopenharmony_ci if (val) 13798c2ecf20Sopenharmony_ci c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr)); 13808c2ecf20Sopenharmony_ci else 13818c2ecf20Sopenharmony_ci c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr)); 13828c2ecf20Sopenharmony_ci } 13838c2ecf20Sopenharmony_ci if (p_addr) 13848c2ecf20Sopenharmony_ci __bm_unmap(p_addr); 13858c2ecf20Sopenharmony_ci if (c < 0) 13868c2ecf20Sopenharmony_ci bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]); 13878c2ecf20Sopenharmony_ci else if (c > 0) 13888c2ecf20Sopenharmony_ci bm_set_page_need_writeout(b->bm_pages[last_page_nr]); 13898c2ecf20Sopenharmony_ci changed_total += c; 13908c2ecf20Sopenharmony_ci b->bm_set += changed_total; 13918c2ecf20Sopenharmony_ci return changed_total; 13928c2ecf20Sopenharmony_ci} 13938c2ecf20Sopenharmony_ci 13948c2ecf20Sopenharmony_ci/* returns number of bits actually changed. 13958c2ecf20Sopenharmony_ci * for val != 0, we change 0 -> 1, return code positive 13968c2ecf20Sopenharmony_ci * for val == 0, we change 1 -> 0, return code negative 13978c2ecf20Sopenharmony_ci * wants bitnr, not sector */ 13988c2ecf20Sopenharmony_cistatic int bm_change_bits_to(struct drbd_device *device, const unsigned long s, 13998c2ecf20Sopenharmony_ci const unsigned long e, int val) 14008c2ecf20Sopenharmony_ci{ 14018c2ecf20Sopenharmony_ci unsigned long flags; 14028c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 14038c2ecf20Sopenharmony_ci int c = 0; 14048c2ecf20Sopenharmony_ci 14058c2ecf20Sopenharmony_ci if (!expect(b)) 14068c2ecf20Sopenharmony_ci return 1; 14078c2ecf20Sopenharmony_ci if (!expect(b->bm_pages)) 14088c2ecf20Sopenharmony_ci return 0; 14098c2ecf20Sopenharmony_ci 14108c2ecf20Sopenharmony_ci spin_lock_irqsave(&b->bm_lock, flags); 14118c2ecf20Sopenharmony_ci if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags) 14128c2ecf20Sopenharmony_ci bm_print_lock_info(device); 14138c2ecf20Sopenharmony_ci 14148c2ecf20Sopenharmony_ci c = __bm_change_bits_to(device, s, e, val); 14158c2ecf20Sopenharmony_ci 14168c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&b->bm_lock, flags); 14178c2ecf20Sopenharmony_ci return c; 14188c2ecf20Sopenharmony_ci} 14198c2ecf20Sopenharmony_ci 14208c2ecf20Sopenharmony_ci/* returns number of bits changed 0 -> 1 */ 14218c2ecf20Sopenharmony_ciint drbd_bm_set_bits(struct drbd_device *device, const unsigned long s, const unsigned long e) 14228c2ecf20Sopenharmony_ci{ 14238c2ecf20Sopenharmony_ci return bm_change_bits_to(device, s, e, 1); 14248c2ecf20Sopenharmony_ci} 14258c2ecf20Sopenharmony_ci 14268c2ecf20Sopenharmony_ci/* returns number of bits changed 1 -> 0 */ 14278c2ecf20Sopenharmony_ciint drbd_bm_clear_bits(struct drbd_device *device, const unsigned long s, const unsigned long e) 14288c2ecf20Sopenharmony_ci{ 14298c2ecf20Sopenharmony_ci return -bm_change_bits_to(device, s, e, 0); 14308c2ecf20Sopenharmony_ci} 14318c2ecf20Sopenharmony_ci 14328c2ecf20Sopenharmony_ci/* sets all bits in full words, 14338c2ecf20Sopenharmony_ci * from first_word up to, but not including, last_word */ 14348c2ecf20Sopenharmony_cistatic inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b, 14358c2ecf20Sopenharmony_ci int page_nr, int first_word, int last_word) 14368c2ecf20Sopenharmony_ci{ 14378c2ecf20Sopenharmony_ci int i; 14388c2ecf20Sopenharmony_ci int bits; 14398c2ecf20Sopenharmony_ci int changed = 0; 14408c2ecf20Sopenharmony_ci unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr]); 14418c2ecf20Sopenharmony_ci 14428c2ecf20Sopenharmony_ci /* I think it is more cache line friendly to hweight_long then set to ~0UL, 14438c2ecf20Sopenharmony_ci * than to first bitmap_weight() all words, then bitmap_fill() all words */ 14448c2ecf20Sopenharmony_ci for (i = first_word; i < last_word; i++) { 14458c2ecf20Sopenharmony_ci bits = hweight_long(paddr[i]); 14468c2ecf20Sopenharmony_ci paddr[i] = ~0UL; 14478c2ecf20Sopenharmony_ci changed += BITS_PER_LONG - bits; 14488c2ecf20Sopenharmony_ci } 14498c2ecf20Sopenharmony_ci kunmap_atomic(paddr); 14508c2ecf20Sopenharmony_ci if (changed) { 14518c2ecf20Sopenharmony_ci /* We only need lazy writeout, the information is still in the 14528c2ecf20Sopenharmony_ci * remote bitmap as well, and is reconstructed during the next 14538c2ecf20Sopenharmony_ci * bitmap exchange, if lost locally due to a crash. */ 14548c2ecf20Sopenharmony_ci bm_set_page_lazy_writeout(b->bm_pages[page_nr]); 14558c2ecf20Sopenharmony_ci b->bm_set += changed; 14568c2ecf20Sopenharmony_ci } 14578c2ecf20Sopenharmony_ci} 14588c2ecf20Sopenharmony_ci 14598c2ecf20Sopenharmony_ci/* Same thing as drbd_bm_set_bits, 14608c2ecf20Sopenharmony_ci * but more efficient for a large bit range. 14618c2ecf20Sopenharmony_ci * You must first drbd_bm_lock(). 14628c2ecf20Sopenharmony_ci * Can be called to set the whole bitmap in one go. 14638c2ecf20Sopenharmony_ci * Sets bits from s to e _inclusive_. */ 14648c2ecf20Sopenharmony_civoid _drbd_bm_set_bits(struct drbd_device *device, const unsigned long s, const unsigned long e) 14658c2ecf20Sopenharmony_ci{ 14668c2ecf20Sopenharmony_ci /* First set_bit from the first bit (s) 14678c2ecf20Sopenharmony_ci * up to the next long boundary (sl), 14688c2ecf20Sopenharmony_ci * then assign full words up to the last long boundary (el), 14698c2ecf20Sopenharmony_ci * then set_bit up to and including the last bit (e). 14708c2ecf20Sopenharmony_ci * 14718c2ecf20Sopenharmony_ci * Do not use memset, because we must account for changes, 14728c2ecf20Sopenharmony_ci * so we need to loop over the words with hweight() anyways. 14738c2ecf20Sopenharmony_ci */ 14748c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 14758c2ecf20Sopenharmony_ci unsigned long sl = ALIGN(s,BITS_PER_LONG); 14768c2ecf20Sopenharmony_ci unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1); 14778c2ecf20Sopenharmony_ci int first_page; 14788c2ecf20Sopenharmony_ci int last_page; 14798c2ecf20Sopenharmony_ci int page_nr; 14808c2ecf20Sopenharmony_ci int first_word; 14818c2ecf20Sopenharmony_ci int last_word; 14828c2ecf20Sopenharmony_ci 14838c2ecf20Sopenharmony_ci if (e - s <= 3*BITS_PER_LONG) { 14848c2ecf20Sopenharmony_ci /* don't bother; el and sl may even be wrong. */ 14858c2ecf20Sopenharmony_ci spin_lock_irq(&b->bm_lock); 14868c2ecf20Sopenharmony_ci __bm_change_bits_to(device, s, e, 1); 14878c2ecf20Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 14888c2ecf20Sopenharmony_ci return; 14898c2ecf20Sopenharmony_ci } 14908c2ecf20Sopenharmony_ci 14918c2ecf20Sopenharmony_ci /* difference is large enough that we can trust sl and el */ 14928c2ecf20Sopenharmony_ci 14938c2ecf20Sopenharmony_ci spin_lock_irq(&b->bm_lock); 14948c2ecf20Sopenharmony_ci 14958c2ecf20Sopenharmony_ci /* bits filling the current long */ 14968c2ecf20Sopenharmony_ci if (sl) 14978c2ecf20Sopenharmony_ci __bm_change_bits_to(device, s, sl-1, 1); 14988c2ecf20Sopenharmony_ci 14998c2ecf20Sopenharmony_ci first_page = sl >> (3 + PAGE_SHIFT); 15008c2ecf20Sopenharmony_ci last_page = el >> (3 + PAGE_SHIFT); 15018c2ecf20Sopenharmony_ci 15028c2ecf20Sopenharmony_ci /* MLPP: modulo longs per page */ 15038c2ecf20Sopenharmony_ci /* LWPP: long words per page */ 15048c2ecf20Sopenharmony_ci first_word = MLPP(sl >> LN2_BPL); 15058c2ecf20Sopenharmony_ci last_word = LWPP; 15068c2ecf20Sopenharmony_ci 15078c2ecf20Sopenharmony_ci /* first and full pages, unless first page == last page */ 15088c2ecf20Sopenharmony_ci for (page_nr = first_page; page_nr < last_page; page_nr++) { 15098c2ecf20Sopenharmony_ci bm_set_full_words_within_one_page(device->bitmap, page_nr, first_word, last_word); 15108c2ecf20Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 15118c2ecf20Sopenharmony_ci cond_resched(); 15128c2ecf20Sopenharmony_ci first_word = 0; 15138c2ecf20Sopenharmony_ci spin_lock_irq(&b->bm_lock); 15148c2ecf20Sopenharmony_ci } 15158c2ecf20Sopenharmony_ci /* last page (respectively only page, for first page == last page) */ 15168c2ecf20Sopenharmony_ci last_word = MLPP(el >> LN2_BPL); 15178c2ecf20Sopenharmony_ci 15188c2ecf20Sopenharmony_ci /* consider bitmap->bm_bits = 32768, bitmap->bm_number_of_pages = 1. (or multiples). 15198c2ecf20Sopenharmony_ci * ==> e = 32767, el = 32768, last_page = 2, 15208c2ecf20Sopenharmony_ci * and now last_word = 0. 15218c2ecf20Sopenharmony_ci * We do not want to touch last_page in this case, 15228c2ecf20Sopenharmony_ci * as we did not allocate it, it is not present in bitmap->bm_pages. 15238c2ecf20Sopenharmony_ci */ 15248c2ecf20Sopenharmony_ci if (last_word) 15258c2ecf20Sopenharmony_ci bm_set_full_words_within_one_page(device->bitmap, last_page, first_word, last_word); 15268c2ecf20Sopenharmony_ci 15278c2ecf20Sopenharmony_ci /* possibly trailing bits. 15288c2ecf20Sopenharmony_ci * example: (e & 63) == 63, el will be e+1. 15298c2ecf20Sopenharmony_ci * if that even was the very last bit, 15308c2ecf20Sopenharmony_ci * it would trigger an assert in __bm_change_bits_to() 15318c2ecf20Sopenharmony_ci */ 15328c2ecf20Sopenharmony_ci if (el <= e) 15338c2ecf20Sopenharmony_ci __bm_change_bits_to(device, el, e, 1); 15348c2ecf20Sopenharmony_ci spin_unlock_irq(&b->bm_lock); 15358c2ecf20Sopenharmony_ci} 15368c2ecf20Sopenharmony_ci 15378c2ecf20Sopenharmony_ci/* returns bit state 15388c2ecf20Sopenharmony_ci * wants bitnr, NOT sector. 15398c2ecf20Sopenharmony_ci * inherently racy... area needs to be locked by means of {al,rs}_lru 15408c2ecf20Sopenharmony_ci * 1 ... bit set 15418c2ecf20Sopenharmony_ci * 0 ... bit not set 15428c2ecf20Sopenharmony_ci * -1 ... first out of bounds access, stop testing for bits! 15438c2ecf20Sopenharmony_ci */ 15448c2ecf20Sopenharmony_ciint drbd_bm_test_bit(struct drbd_device *device, const unsigned long bitnr) 15458c2ecf20Sopenharmony_ci{ 15468c2ecf20Sopenharmony_ci unsigned long flags; 15478c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 15488c2ecf20Sopenharmony_ci unsigned long *p_addr; 15498c2ecf20Sopenharmony_ci int i; 15508c2ecf20Sopenharmony_ci 15518c2ecf20Sopenharmony_ci if (!expect(b)) 15528c2ecf20Sopenharmony_ci return 0; 15538c2ecf20Sopenharmony_ci if (!expect(b->bm_pages)) 15548c2ecf20Sopenharmony_ci return 0; 15558c2ecf20Sopenharmony_ci 15568c2ecf20Sopenharmony_ci spin_lock_irqsave(&b->bm_lock, flags); 15578c2ecf20Sopenharmony_ci if (BM_DONT_TEST & b->bm_flags) 15588c2ecf20Sopenharmony_ci bm_print_lock_info(device); 15598c2ecf20Sopenharmony_ci if (bitnr < b->bm_bits) { 15608c2ecf20Sopenharmony_ci p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr)); 15618c2ecf20Sopenharmony_ci i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0; 15628c2ecf20Sopenharmony_ci bm_unmap(p_addr); 15638c2ecf20Sopenharmony_ci } else if (bitnr == b->bm_bits) { 15648c2ecf20Sopenharmony_ci i = -1; 15658c2ecf20Sopenharmony_ci } else { /* (bitnr > b->bm_bits) */ 15668c2ecf20Sopenharmony_ci drbd_err(device, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits); 15678c2ecf20Sopenharmony_ci i = 0; 15688c2ecf20Sopenharmony_ci } 15698c2ecf20Sopenharmony_ci 15708c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&b->bm_lock, flags); 15718c2ecf20Sopenharmony_ci return i; 15728c2ecf20Sopenharmony_ci} 15738c2ecf20Sopenharmony_ci 15748c2ecf20Sopenharmony_ci/* returns number of bits set in the range [s, e] */ 15758c2ecf20Sopenharmony_ciint drbd_bm_count_bits(struct drbd_device *device, const unsigned long s, const unsigned long e) 15768c2ecf20Sopenharmony_ci{ 15778c2ecf20Sopenharmony_ci unsigned long flags; 15788c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 15798c2ecf20Sopenharmony_ci unsigned long *p_addr = NULL; 15808c2ecf20Sopenharmony_ci unsigned long bitnr; 15818c2ecf20Sopenharmony_ci unsigned int page_nr = -1U; 15828c2ecf20Sopenharmony_ci int c = 0; 15838c2ecf20Sopenharmony_ci 15848c2ecf20Sopenharmony_ci /* If this is called without a bitmap, that is a bug. But just to be 15858c2ecf20Sopenharmony_ci * robust in case we screwed up elsewhere, in that case pretend there 15868c2ecf20Sopenharmony_ci * was one dirty bit in the requested area, so we won't try to do a 15878c2ecf20Sopenharmony_ci * local read there (no bitmap probably implies no disk) */ 15888c2ecf20Sopenharmony_ci if (!expect(b)) 15898c2ecf20Sopenharmony_ci return 1; 15908c2ecf20Sopenharmony_ci if (!expect(b->bm_pages)) 15918c2ecf20Sopenharmony_ci return 1; 15928c2ecf20Sopenharmony_ci 15938c2ecf20Sopenharmony_ci spin_lock_irqsave(&b->bm_lock, flags); 15948c2ecf20Sopenharmony_ci if (BM_DONT_TEST & b->bm_flags) 15958c2ecf20Sopenharmony_ci bm_print_lock_info(device); 15968c2ecf20Sopenharmony_ci for (bitnr = s; bitnr <= e; bitnr++) { 15978c2ecf20Sopenharmony_ci unsigned int idx = bm_bit_to_page_idx(b, bitnr); 15988c2ecf20Sopenharmony_ci if (page_nr != idx) { 15998c2ecf20Sopenharmony_ci page_nr = idx; 16008c2ecf20Sopenharmony_ci if (p_addr) 16018c2ecf20Sopenharmony_ci bm_unmap(p_addr); 16028c2ecf20Sopenharmony_ci p_addr = bm_map_pidx(b, idx); 16038c2ecf20Sopenharmony_ci } 16048c2ecf20Sopenharmony_ci if (expect(bitnr < b->bm_bits)) 16058c2ecf20Sopenharmony_ci c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr)); 16068c2ecf20Sopenharmony_ci else 16078c2ecf20Sopenharmony_ci drbd_err(device, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits); 16088c2ecf20Sopenharmony_ci } 16098c2ecf20Sopenharmony_ci if (p_addr) 16108c2ecf20Sopenharmony_ci bm_unmap(p_addr); 16118c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&b->bm_lock, flags); 16128c2ecf20Sopenharmony_ci return c; 16138c2ecf20Sopenharmony_ci} 16148c2ecf20Sopenharmony_ci 16158c2ecf20Sopenharmony_ci 16168c2ecf20Sopenharmony_ci/* inherently racy... 16178c2ecf20Sopenharmony_ci * return value may be already out-of-date when this function returns. 16188c2ecf20Sopenharmony_ci * but the general usage is that this is only use during a cstate when bits are 16198c2ecf20Sopenharmony_ci * only cleared, not set, and typically only care for the case when the return 16208c2ecf20Sopenharmony_ci * value is zero, or we already "locked" this "bitmap extent" by other means. 16218c2ecf20Sopenharmony_ci * 16228c2ecf20Sopenharmony_ci * enr is bm-extent number, since we chose to name one sector (512 bytes) 16238c2ecf20Sopenharmony_ci * worth of the bitmap a "bitmap extent". 16248c2ecf20Sopenharmony_ci * 16258c2ecf20Sopenharmony_ci * TODO 16268c2ecf20Sopenharmony_ci * I think since we use it like a reference count, we should use the real 16278c2ecf20Sopenharmony_ci * reference count of some bitmap extent element from some lru instead... 16288c2ecf20Sopenharmony_ci * 16298c2ecf20Sopenharmony_ci */ 16308c2ecf20Sopenharmony_ciint drbd_bm_e_weight(struct drbd_device *device, unsigned long enr) 16318c2ecf20Sopenharmony_ci{ 16328c2ecf20Sopenharmony_ci struct drbd_bitmap *b = device->bitmap; 16338c2ecf20Sopenharmony_ci int count, s, e; 16348c2ecf20Sopenharmony_ci unsigned long flags; 16358c2ecf20Sopenharmony_ci unsigned long *p_addr, *bm; 16368c2ecf20Sopenharmony_ci 16378c2ecf20Sopenharmony_ci if (!expect(b)) 16388c2ecf20Sopenharmony_ci return 0; 16398c2ecf20Sopenharmony_ci if (!expect(b->bm_pages)) 16408c2ecf20Sopenharmony_ci return 0; 16418c2ecf20Sopenharmony_ci 16428c2ecf20Sopenharmony_ci spin_lock_irqsave(&b->bm_lock, flags); 16438c2ecf20Sopenharmony_ci if (BM_DONT_TEST & b->bm_flags) 16448c2ecf20Sopenharmony_ci bm_print_lock_info(device); 16458c2ecf20Sopenharmony_ci 16468c2ecf20Sopenharmony_ci s = S2W(enr); 16478c2ecf20Sopenharmony_ci e = min((size_t)S2W(enr+1), b->bm_words); 16488c2ecf20Sopenharmony_ci count = 0; 16498c2ecf20Sopenharmony_ci if (s < b->bm_words) { 16508c2ecf20Sopenharmony_ci int n = e-s; 16518c2ecf20Sopenharmony_ci p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s)); 16528c2ecf20Sopenharmony_ci bm = p_addr + MLPP(s); 16538c2ecf20Sopenharmony_ci count += bitmap_weight(bm, n * BITS_PER_LONG); 16548c2ecf20Sopenharmony_ci bm_unmap(p_addr); 16558c2ecf20Sopenharmony_ci } else { 16568c2ecf20Sopenharmony_ci drbd_err(device, "start offset (%d) too large in drbd_bm_e_weight\n", s); 16578c2ecf20Sopenharmony_ci } 16588c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&b->bm_lock, flags); 16598c2ecf20Sopenharmony_ci return count; 16608c2ecf20Sopenharmony_ci} 1661