1b411b363SPhilipp Reisner /* 2b411b363SPhilipp Reisner drbd_bitmap.c 3b411b363SPhilipp Reisner 4b411b363SPhilipp Reisner This file is part of DRBD by Philipp Reisner and Lars Ellenberg. 5b411b363SPhilipp Reisner 6b411b363SPhilipp Reisner Copyright (C) 2004-2008, LINBIT Information Technologies GmbH. 7b411b363SPhilipp Reisner Copyright (C) 2004-2008, Philipp Reisner <philipp.reisner@linbit.com>. 8b411b363SPhilipp Reisner Copyright (C) 2004-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. 9b411b363SPhilipp Reisner 10b411b363SPhilipp Reisner drbd is free software; you can redistribute it and/or modify 11b411b363SPhilipp Reisner it under the terms of the GNU General Public License as published by 12b411b363SPhilipp Reisner the Free Software Foundation; either version 2, or (at your option) 13b411b363SPhilipp Reisner any later version. 14b411b363SPhilipp Reisner 15b411b363SPhilipp Reisner drbd is distributed in the hope that it will be useful, 16b411b363SPhilipp Reisner but WITHOUT ANY WARRANTY; without even the implied warranty of 17b411b363SPhilipp Reisner MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18b411b363SPhilipp Reisner GNU General Public License for more details. 19b411b363SPhilipp Reisner 20b411b363SPhilipp Reisner You should have received a copy of the GNU General Public License 21b411b363SPhilipp Reisner along with drbd; see the file COPYING. If not, write to 22b411b363SPhilipp Reisner the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 23b411b363SPhilipp Reisner */ 24b411b363SPhilipp Reisner 25b411b363SPhilipp Reisner #include <linux/bitops.h> 26b411b363SPhilipp Reisner #include <linux/vmalloc.h> 27b411b363SPhilipp Reisner #include <linux/string.h> 28b411b363SPhilipp Reisner #include <linux/drbd.h> 295a0e3ad6STejun Heo #include <linux/slab.h> 30b411b363SPhilipp Reisner #include <asm/kmap_types.h> 31b411b363SPhilipp Reisner #include "drbd_int.h" 32b411b363SPhilipp Reisner 33b411b363SPhilipp Reisner /* OPAQUE outside this file! 34b411b363SPhilipp Reisner * interface defined in drbd_int.h 35b411b363SPhilipp Reisner 36b411b363SPhilipp Reisner * convention: 37b411b363SPhilipp Reisner * function name drbd_bm_... => used elsewhere, "public". 38b411b363SPhilipp Reisner * function name bm_... => internal to implementation, "private". 39b411b363SPhilipp Reisner 40b411b363SPhilipp Reisner * Note that since find_first_bit returns int, at the current granularity of 41b411b363SPhilipp Reisner * the bitmap (4KB per byte), this implementation "only" supports up to 42b411b363SPhilipp Reisner * 1<<(32+12) == 16 TB... 43b411b363SPhilipp Reisner */ 44b411b363SPhilipp Reisner 45b411b363SPhilipp Reisner /* 46b411b363SPhilipp Reisner * NOTE 47b411b363SPhilipp Reisner * Access to the *bm_pages is protected by bm_lock. 48b411b363SPhilipp Reisner * It is safe to read the other members within the lock. 49b411b363SPhilipp Reisner * 50b411b363SPhilipp Reisner * drbd_bm_set_bits is called from bio_endio callbacks, 51b411b363SPhilipp Reisner * We may be called with irq already disabled, 52b411b363SPhilipp Reisner * so we need spin_lock_irqsave(). 53b411b363SPhilipp Reisner * And we need the kmap_atomic. 54b411b363SPhilipp Reisner */ 55b411b363SPhilipp Reisner struct drbd_bitmap { 56b411b363SPhilipp Reisner struct page **bm_pages; 57b411b363SPhilipp Reisner spinlock_t bm_lock; 58b411b363SPhilipp Reisner /* WARNING unsigned long bm_*: 59b411b363SPhilipp Reisner * 32bit number of bit offset is just enough for 512 MB bitmap. 60b411b363SPhilipp Reisner * it will blow up if we make the bitmap bigger... 61b411b363SPhilipp Reisner * not that it makes much sense to have a bitmap that large, 62b411b363SPhilipp Reisner * rather change the granularity to 16k or 64k or something. 63b411b363SPhilipp Reisner * (that implies other problems, however...) 64b411b363SPhilipp Reisner */ 65b411b363SPhilipp Reisner unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */ 66b411b363SPhilipp Reisner unsigned long bm_bits; 67b411b363SPhilipp Reisner size_t bm_words; 68b411b363SPhilipp Reisner size_t bm_number_of_pages; 69b411b363SPhilipp Reisner sector_t bm_dev_capacity; 708a03ae2aSThomas Gleixner struct mutex bm_change; /* serializes resize operations */ 71b411b363SPhilipp Reisner 72b411b363SPhilipp Reisner atomic_t bm_async_io; 73b411b363SPhilipp Reisner wait_queue_head_t bm_io_wait; 74b411b363SPhilipp Reisner 75b411b363SPhilipp Reisner unsigned long bm_flags; 76b411b363SPhilipp Reisner 77b411b363SPhilipp Reisner /* debugging aid, in case we are still racy somewhere */ 78b411b363SPhilipp Reisner char *bm_why; 79b411b363SPhilipp Reisner struct task_struct *bm_task; 80b411b363SPhilipp Reisner }; 81b411b363SPhilipp Reisner 82b411b363SPhilipp Reisner /* definition of bits in bm_flags */ 83b411b363SPhilipp Reisner #define BM_LOCKED 0 84b411b363SPhilipp Reisner #define BM_MD_IO_ERROR 1 85b411b363SPhilipp Reisner #define BM_P_VMALLOCED 2 86b411b363SPhilipp Reisner 87b4ee79daSPhilipp Reisner static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s, 88fd76438cSPhilipp Reisner unsigned long e, int val, const enum km_type km); 89fd76438cSPhilipp Reisner 90b411b363SPhilipp Reisner static int bm_is_locked(struct drbd_bitmap *b) 91b411b363SPhilipp Reisner { 92b411b363SPhilipp Reisner return test_bit(BM_LOCKED, &b->bm_flags); 93b411b363SPhilipp Reisner } 94b411b363SPhilipp Reisner 95b411b363SPhilipp Reisner #define bm_print_lock_info(m) __bm_print_lock_info(m, __func__) 96b411b363SPhilipp Reisner static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func) 97b411b363SPhilipp Reisner { 98b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 99b411b363SPhilipp Reisner if (!__ratelimit(&drbd_ratelimit_state)) 100b411b363SPhilipp Reisner return; 101b411b363SPhilipp Reisner dev_err(DEV, "FIXME %s in %s, bitmap locked for '%s' by %s\n", 102b411b363SPhilipp Reisner current == mdev->receiver.task ? "receiver" : 103b411b363SPhilipp Reisner current == mdev->asender.task ? "asender" : 104b411b363SPhilipp Reisner current == mdev->worker.task ? "worker" : current->comm, 105b411b363SPhilipp Reisner func, b->bm_why ?: "?", 106b411b363SPhilipp Reisner b->bm_task == mdev->receiver.task ? "receiver" : 107b411b363SPhilipp Reisner b->bm_task == mdev->asender.task ? "asender" : 108b411b363SPhilipp Reisner b->bm_task == mdev->worker.task ? "worker" : "?"); 109b411b363SPhilipp Reisner } 110b411b363SPhilipp Reisner 111b411b363SPhilipp Reisner void drbd_bm_lock(struct drbd_conf *mdev, char *why) 112b411b363SPhilipp Reisner { 113b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 114b411b363SPhilipp Reisner int trylock_failed; 115b411b363SPhilipp Reisner 116b411b363SPhilipp Reisner if (!b) { 117b411b363SPhilipp Reisner dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n"); 118b411b363SPhilipp Reisner return; 119b411b363SPhilipp Reisner } 120b411b363SPhilipp Reisner 1218a03ae2aSThomas Gleixner trylock_failed = !mutex_trylock(&b->bm_change); 122b411b363SPhilipp Reisner 123b411b363SPhilipp Reisner if (trylock_failed) { 124b411b363SPhilipp Reisner dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n", 125b411b363SPhilipp Reisner current == mdev->receiver.task ? "receiver" : 126b411b363SPhilipp Reisner current == mdev->asender.task ? "asender" : 127b411b363SPhilipp Reisner current == mdev->worker.task ? "worker" : current->comm, 128b411b363SPhilipp Reisner why, b->bm_why ?: "?", 129b411b363SPhilipp Reisner b->bm_task == mdev->receiver.task ? "receiver" : 130b411b363SPhilipp Reisner b->bm_task == mdev->asender.task ? "asender" : 131b411b363SPhilipp Reisner b->bm_task == mdev->worker.task ? "worker" : "?"); 1328a03ae2aSThomas Gleixner mutex_lock(&b->bm_change); 133b411b363SPhilipp Reisner } 134b411b363SPhilipp Reisner if (__test_and_set_bit(BM_LOCKED, &b->bm_flags)) 135b411b363SPhilipp Reisner dev_err(DEV, "FIXME bitmap already locked in bm_lock\n"); 136b411b363SPhilipp Reisner 137b411b363SPhilipp Reisner b->bm_why = why; 138b411b363SPhilipp Reisner b->bm_task = current; 139b411b363SPhilipp Reisner } 140b411b363SPhilipp Reisner 141b411b363SPhilipp Reisner void drbd_bm_unlock(struct drbd_conf *mdev) 142b411b363SPhilipp Reisner { 143b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 144b411b363SPhilipp Reisner if (!b) { 145b411b363SPhilipp Reisner dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n"); 146b411b363SPhilipp Reisner return; 147b411b363SPhilipp Reisner } 148b411b363SPhilipp Reisner 149b411b363SPhilipp Reisner if (!__test_and_clear_bit(BM_LOCKED, &mdev->bitmap->bm_flags)) 150b411b363SPhilipp Reisner dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n"); 151b411b363SPhilipp Reisner 152b411b363SPhilipp Reisner b->bm_why = NULL; 153b411b363SPhilipp Reisner b->bm_task = NULL; 1548a03ae2aSThomas Gleixner mutex_unlock(&b->bm_change); 155b411b363SPhilipp Reisner } 156b411b363SPhilipp Reisner 157b411b363SPhilipp Reisner /* word offset to long pointer */ 158b411b363SPhilipp Reisner static unsigned long *__bm_map_paddr(struct drbd_bitmap *b, unsigned long offset, const enum km_type km) 159b411b363SPhilipp Reisner { 160b411b363SPhilipp Reisner struct page *page; 161b411b363SPhilipp Reisner unsigned long page_nr; 162b411b363SPhilipp Reisner 163b411b363SPhilipp Reisner /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */ 164b411b363SPhilipp Reisner page_nr = offset >> (PAGE_SHIFT - LN2_BPL + 3); 165b411b363SPhilipp Reisner BUG_ON(page_nr >= b->bm_number_of_pages); 166b411b363SPhilipp Reisner page = b->bm_pages[page_nr]; 167b411b363SPhilipp Reisner 168b411b363SPhilipp Reisner return (unsigned long *) kmap_atomic(page, km); 169b411b363SPhilipp Reisner } 170b411b363SPhilipp Reisner 171b411b363SPhilipp Reisner static unsigned long * bm_map_paddr(struct drbd_bitmap *b, unsigned long offset) 172b411b363SPhilipp Reisner { 173b411b363SPhilipp Reisner return __bm_map_paddr(b, offset, KM_IRQ1); 174b411b363SPhilipp Reisner } 175b411b363SPhilipp Reisner 176b411b363SPhilipp Reisner static void __bm_unmap(unsigned long *p_addr, const enum km_type km) 177b411b363SPhilipp Reisner { 178b411b363SPhilipp Reisner kunmap_atomic(p_addr, km); 179b411b363SPhilipp Reisner }; 180b411b363SPhilipp Reisner 181b411b363SPhilipp Reisner static void bm_unmap(unsigned long *p_addr) 182b411b363SPhilipp Reisner { 183b411b363SPhilipp Reisner return __bm_unmap(p_addr, KM_IRQ1); 184b411b363SPhilipp Reisner } 185b411b363SPhilipp Reisner 186b411b363SPhilipp Reisner /* long word offset of _bitmap_ sector */ 187b411b363SPhilipp Reisner #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL)) 188b411b363SPhilipp Reisner /* word offset from start of bitmap to word number _in_page_ 189b411b363SPhilipp Reisner * modulo longs per page 190b411b363SPhilipp Reisner #define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long)) 191b411b363SPhilipp Reisner hm, well, Philipp thinks gcc might not optimze the % into & (... - 1) 192b411b363SPhilipp Reisner so do it explicitly: 193b411b363SPhilipp Reisner */ 194b411b363SPhilipp Reisner #define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1)) 195b411b363SPhilipp Reisner 196b411b363SPhilipp Reisner /* Long words per page */ 197b411b363SPhilipp Reisner #define LWPP (PAGE_SIZE/sizeof(long)) 198b411b363SPhilipp Reisner 199b411b363SPhilipp Reisner /* 200b411b363SPhilipp Reisner * actually most functions herein should take a struct drbd_bitmap*, not a 201b411b363SPhilipp Reisner * struct drbd_conf*, but for the debug macros I like to have the mdev around 202b411b363SPhilipp Reisner * to be able to report device specific. 203b411b363SPhilipp Reisner */ 204b411b363SPhilipp Reisner 205b411b363SPhilipp Reisner static void bm_free_pages(struct page **pages, unsigned long number) 206b411b363SPhilipp Reisner { 207b411b363SPhilipp Reisner unsigned long i; 208b411b363SPhilipp Reisner if (!pages) 209b411b363SPhilipp Reisner return; 210b411b363SPhilipp Reisner 211b411b363SPhilipp Reisner for (i = 0; i < number; i++) { 212b411b363SPhilipp Reisner if (!pages[i]) { 213b411b363SPhilipp Reisner printk(KERN_ALERT "drbd: bm_free_pages tried to free " 214b411b363SPhilipp Reisner "a NULL pointer; i=%lu n=%lu\n", 215b411b363SPhilipp Reisner i, number); 216b411b363SPhilipp Reisner continue; 217b411b363SPhilipp Reisner } 218b411b363SPhilipp Reisner __free_page(pages[i]); 219b411b363SPhilipp Reisner pages[i] = NULL; 220b411b363SPhilipp Reisner } 221b411b363SPhilipp Reisner } 222b411b363SPhilipp Reisner 223b411b363SPhilipp Reisner static void bm_vk_free(void *ptr, int v) 224b411b363SPhilipp Reisner { 225b411b363SPhilipp Reisner if (v) 226b411b363SPhilipp Reisner vfree(ptr); 227b411b363SPhilipp Reisner else 228b411b363SPhilipp Reisner kfree(ptr); 229b411b363SPhilipp Reisner } 230b411b363SPhilipp Reisner 231b411b363SPhilipp Reisner /* 232b411b363SPhilipp Reisner * "have" and "want" are NUMBER OF PAGES. 233b411b363SPhilipp Reisner */ 234b411b363SPhilipp Reisner static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want) 235b411b363SPhilipp Reisner { 236b411b363SPhilipp Reisner struct page **old_pages = b->bm_pages; 237b411b363SPhilipp Reisner struct page **new_pages, *page; 238b411b363SPhilipp Reisner unsigned int i, bytes, vmalloced = 0; 239b411b363SPhilipp Reisner unsigned long have = b->bm_number_of_pages; 240b411b363SPhilipp Reisner 241b411b363SPhilipp Reisner BUG_ON(have == 0 && old_pages != NULL); 242b411b363SPhilipp Reisner BUG_ON(have != 0 && old_pages == NULL); 243b411b363SPhilipp Reisner 244b411b363SPhilipp Reisner if (have == want) 245b411b363SPhilipp Reisner return old_pages; 246b411b363SPhilipp Reisner 247b411b363SPhilipp Reisner /* Trying kmalloc first, falling back to vmalloc. 248b411b363SPhilipp Reisner * GFP_KERNEL is ok, as this is done when a lower level disk is 249b411b363SPhilipp Reisner * "attached" to the drbd. Context is receiver thread or cqueue 250b411b363SPhilipp Reisner * thread. As we have no disk yet, we are not in the IO path, 251b411b363SPhilipp Reisner * not even the IO path of the peer. */ 252b411b363SPhilipp Reisner bytes = sizeof(struct page *)*want; 253b411b363SPhilipp Reisner new_pages = kmalloc(bytes, GFP_KERNEL); 254b411b363SPhilipp Reisner if (!new_pages) { 255b411b363SPhilipp Reisner new_pages = vmalloc(bytes); 256b411b363SPhilipp Reisner if (!new_pages) 257b411b363SPhilipp Reisner return NULL; 258b411b363SPhilipp Reisner vmalloced = 1; 259b411b363SPhilipp Reisner } 260b411b363SPhilipp Reisner 261b411b363SPhilipp Reisner memset(new_pages, 0, bytes); 262b411b363SPhilipp Reisner if (want >= have) { 263b411b363SPhilipp Reisner for (i = 0; i < have; i++) 264b411b363SPhilipp Reisner new_pages[i] = old_pages[i]; 265b411b363SPhilipp Reisner for (; i < want; i++) { 266b411b363SPhilipp Reisner page = alloc_page(GFP_HIGHUSER); 267b411b363SPhilipp Reisner if (!page) { 268b411b363SPhilipp Reisner bm_free_pages(new_pages + have, i - have); 269b411b363SPhilipp Reisner bm_vk_free(new_pages, vmalloced); 270b411b363SPhilipp Reisner return NULL; 271b411b363SPhilipp Reisner } 272b411b363SPhilipp Reisner new_pages[i] = page; 273b411b363SPhilipp Reisner } 274b411b363SPhilipp Reisner } else { 275b411b363SPhilipp Reisner for (i = 0; i < want; i++) 276b411b363SPhilipp Reisner new_pages[i] = old_pages[i]; 277b411b363SPhilipp Reisner /* NOT HERE, we are outside the spinlock! 278b411b363SPhilipp Reisner bm_free_pages(old_pages + want, have - want); 279b411b363SPhilipp Reisner */ 280b411b363SPhilipp Reisner } 281b411b363SPhilipp Reisner 282b411b363SPhilipp Reisner if (vmalloced) 283b411b363SPhilipp Reisner set_bit(BM_P_VMALLOCED, &b->bm_flags); 284b411b363SPhilipp Reisner else 285b411b363SPhilipp Reisner clear_bit(BM_P_VMALLOCED, &b->bm_flags); 286b411b363SPhilipp Reisner 287b411b363SPhilipp Reisner return new_pages; 288b411b363SPhilipp Reisner } 289b411b363SPhilipp Reisner 290b411b363SPhilipp Reisner /* 291b411b363SPhilipp Reisner * called on driver init only. TODO call when a device is created. 292b411b363SPhilipp Reisner * allocates the drbd_bitmap, and stores it in mdev->bitmap. 293b411b363SPhilipp Reisner */ 294b411b363SPhilipp Reisner int drbd_bm_init(struct drbd_conf *mdev) 295b411b363SPhilipp Reisner { 296b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 297b411b363SPhilipp Reisner WARN_ON(b != NULL); 298b411b363SPhilipp Reisner b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL); 299b411b363SPhilipp Reisner if (!b) 300b411b363SPhilipp Reisner return -ENOMEM; 301b411b363SPhilipp Reisner spin_lock_init(&b->bm_lock); 3028a03ae2aSThomas Gleixner mutex_init(&b->bm_change); 303b411b363SPhilipp Reisner init_waitqueue_head(&b->bm_io_wait); 304b411b363SPhilipp Reisner 305b411b363SPhilipp Reisner mdev->bitmap = b; 306b411b363SPhilipp Reisner 307b411b363SPhilipp Reisner return 0; 308b411b363SPhilipp Reisner } 309b411b363SPhilipp Reisner 310b411b363SPhilipp Reisner sector_t drbd_bm_capacity(struct drbd_conf *mdev) 311b411b363SPhilipp Reisner { 312b411b363SPhilipp Reisner ERR_IF(!mdev->bitmap) return 0; 313b411b363SPhilipp Reisner return mdev->bitmap->bm_dev_capacity; 314b411b363SPhilipp Reisner } 315b411b363SPhilipp Reisner 316b411b363SPhilipp Reisner /* called on driver unload. TODO: call when a device is destroyed. 317b411b363SPhilipp Reisner */ 318b411b363SPhilipp Reisner void drbd_bm_cleanup(struct drbd_conf *mdev) 319b411b363SPhilipp Reisner { 320b411b363SPhilipp Reisner ERR_IF (!mdev->bitmap) return; 321b411b363SPhilipp Reisner bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages); 322b411b363SPhilipp Reisner bm_vk_free(mdev->bitmap->bm_pages, test_bit(BM_P_VMALLOCED, &mdev->bitmap->bm_flags)); 323b411b363SPhilipp Reisner kfree(mdev->bitmap); 324b411b363SPhilipp Reisner mdev->bitmap = NULL; 325b411b363SPhilipp Reisner } 326b411b363SPhilipp Reisner 327b411b363SPhilipp Reisner /* 328b411b363SPhilipp Reisner * since (b->bm_bits % BITS_PER_LONG) != 0, 329b411b363SPhilipp Reisner * this masks out the remaining bits. 330b411b363SPhilipp Reisner * Returns the number of bits cleared. 331b411b363SPhilipp Reisner */ 332b411b363SPhilipp Reisner static int bm_clear_surplus(struct drbd_bitmap *b) 333b411b363SPhilipp Reisner { 334b411b363SPhilipp Reisner const unsigned long mask = (1UL << (b->bm_bits & (BITS_PER_LONG-1))) - 1; 335b411b363SPhilipp Reisner size_t w = b->bm_bits >> LN2_BPL; 336b411b363SPhilipp Reisner int cleared = 0; 337b411b363SPhilipp Reisner unsigned long *p_addr, *bm; 338b411b363SPhilipp Reisner 339b411b363SPhilipp Reisner p_addr = bm_map_paddr(b, w); 340b411b363SPhilipp Reisner bm = p_addr + MLPP(w); 341b411b363SPhilipp Reisner if (w < b->bm_words) { 342b411b363SPhilipp Reisner cleared = hweight_long(*bm & ~mask); 343b411b363SPhilipp Reisner *bm &= mask; 344b411b363SPhilipp Reisner w++; bm++; 345b411b363SPhilipp Reisner } 346b411b363SPhilipp Reisner 347b411b363SPhilipp Reisner if (w < b->bm_words) { 348b411b363SPhilipp Reisner cleared += hweight_long(*bm); 349b411b363SPhilipp Reisner *bm = 0; 350b411b363SPhilipp Reisner } 351b411b363SPhilipp Reisner bm_unmap(p_addr); 352b411b363SPhilipp Reisner return cleared; 353b411b363SPhilipp Reisner } 354b411b363SPhilipp Reisner 355b411b363SPhilipp Reisner static void bm_set_surplus(struct drbd_bitmap *b) 356b411b363SPhilipp Reisner { 357b411b363SPhilipp Reisner const unsigned long mask = (1UL << (b->bm_bits & (BITS_PER_LONG-1))) - 1; 358b411b363SPhilipp Reisner size_t w = b->bm_bits >> LN2_BPL; 359b411b363SPhilipp Reisner unsigned long *p_addr, *bm; 360b411b363SPhilipp Reisner 361b411b363SPhilipp Reisner p_addr = bm_map_paddr(b, w); 362b411b363SPhilipp Reisner bm = p_addr + MLPP(w); 363b411b363SPhilipp Reisner if (w < b->bm_words) { 364b411b363SPhilipp Reisner *bm |= ~mask; 365b411b363SPhilipp Reisner bm++; w++; 366b411b363SPhilipp Reisner } 367b411b363SPhilipp Reisner 368b411b363SPhilipp Reisner if (w < b->bm_words) { 369b411b363SPhilipp Reisner *bm = ~(0UL); 370b411b363SPhilipp Reisner } 371b411b363SPhilipp Reisner bm_unmap(p_addr); 372b411b363SPhilipp Reisner } 373b411b363SPhilipp Reisner 374b411b363SPhilipp Reisner static unsigned long __bm_count_bits(struct drbd_bitmap *b, const int swap_endian) 375b411b363SPhilipp Reisner { 376b411b363SPhilipp Reisner unsigned long *p_addr, *bm, offset = 0; 377b411b363SPhilipp Reisner unsigned long bits = 0; 378b411b363SPhilipp Reisner unsigned long i, do_now; 379b411b363SPhilipp Reisner 380b411b363SPhilipp Reisner while (offset < b->bm_words) { 381b411b363SPhilipp Reisner i = do_now = min_t(size_t, b->bm_words-offset, LWPP); 382b411b363SPhilipp Reisner p_addr = __bm_map_paddr(b, offset, KM_USER0); 383b411b363SPhilipp Reisner bm = p_addr + MLPP(offset); 384b411b363SPhilipp Reisner while (i--) { 385b411b363SPhilipp Reisner #ifndef __LITTLE_ENDIAN 386b411b363SPhilipp Reisner if (swap_endian) 387b411b363SPhilipp Reisner *bm = lel_to_cpu(*bm); 388b411b363SPhilipp Reisner #endif 389b411b363SPhilipp Reisner bits += hweight_long(*bm++); 390b411b363SPhilipp Reisner } 391b411b363SPhilipp Reisner __bm_unmap(p_addr, KM_USER0); 392b411b363SPhilipp Reisner offset += do_now; 393b411b363SPhilipp Reisner cond_resched(); 394b411b363SPhilipp Reisner } 395b411b363SPhilipp Reisner 396b411b363SPhilipp Reisner return bits; 397b411b363SPhilipp Reisner } 398b411b363SPhilipp Reisner 399b411b363SPhilipp Reisner static unsigned long bm_count_bits(struct drbd_bitmap *b) 400b411b363SPhilipp Reisner { 401b411b363SPhilipp Reisner return __bm_count_bits(b, 0); 402b411b363SPhilipp Reisner } 403b411b363SPhilipp Reisner 404b411b363SPhilipp Reisner static unsigned long bm_count_bits_swap_endian(struct drbd_bitmap *b) 405b411b363SPhilipp Reisner { 406b411b363SPhilipp Reisner return __bm_count_bits(b, 1); 407b411b363SPhilipp Reisner } 408b411b363SPhilipp Reisner 409b411b363SPhilipp Reisner /* offset and len in long words.*/ 410b411b363SPhilipp Reisner static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len) 411b411b363SPhilipp Reisner { 412b411b363SPhilipp Reisner unsigned long *p_addr, *bm; 413b411b363SPhilipp Reisner size_t do_now, end; 414b411b363SPhilipp Reisner 415b411b363SPhilipp Reisner #define BM_SECTORS_PER_BIT (BM_BLOCK_SIZE/512) 416b411b363SPhilipp Reisner 417b411b363SPhilipp Reisner end = offset + len; 418b411b363SPhilipp Reisner 419b411b363SPhilipp Reisner if (end > b->bm_words) { 420b411b363SPhilipp Reisner printk(KERN_ALERT "drbd: bm_memset end > bm_words\n"); 421b411b363SPhilipp Reisner return; 422b411b363SPhilipp Reisner } 423b411b363SPhilipp Reisner 424b411b363SPhilipp Reisner while (offset < end) { 425b411b363SPhilipp Reisner do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset; 426b411b363SPhilipp Reisner p_addr = bm_map_paddr(b, offset); 427b411b363SPhilipp Reisner bm = p_addr + MLPP(offset); 428b411b363SPhilipp Reisner if (bm+do_now > p_addr + LWPP) { 429b411b363SPhilipp Reisner printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n", 430b411b363SPhilipp Reisner p_addr, bm, (int)do_now); 431b411b363SPhilipp Reisner break; /* breaks to after catch_oob_access_end() only! */ 432b411b363SPhilipp Reisner } 433b411b363SPhilipp Reisner memset(bm, c, do_now * sizeof(long)); 434b411b363SPhilipp Reisner bm_unmap(p_addr); 435b411b363SPhilipp Reisner offset += do_now; 436b411b363SPhilipp Reisner } 437b411b363SPhilipp Reisner } 438b411b363SPhilipp Reisner 439b411b363SPhilipp Reisner /* 440b411b363SPhilipp Reisner * make sure the bitmap has enough room for the attached storage, 441b411b363SPhilipp Reisner * if necessary, resize. 442b411b363SPhilipp Reisner * called whenever we may have changed the device size. 443b411b363SPhilipp Reisner * returns -ENOMEM if we could not allocate enough memory, 0 on success. 444b411b363SPhilipp Reisner * In case this is actually a resize, we copy the old bitmap into the new one. 445b411b363SPhilipp Reisner * Otherwise, the bitmap is initialized to all bits set. 446b411b363SPhilipp Reisner */ 44702d9a94bSPhilipp Reisner int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits) 448b411b363SPhilipp Reisner { 449b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 450b411b363SPhilipp Reisner unsigned long bits, words, owords, obits, *p_addr, *bm; 451b411b363SPhilipp Reisner unsigned long want, have, onpages; /* number of pages */ 452b411b363SPhilipp Reisner struct page **npages, **opages = NULL; 453b411b363SPhilipp Reisner int err = 0, growing; 454b411b363SPhilipp Reisner int opages_vmalloced; 455b411b363SPhilipp Reisner 456b411b363SPhilipp Reisner ERR_IF(!b) return -ENOMEM; 457b411b363SPhilipp Reisner 458b411b363SPhilipp Reisner drbd_bm_lock(mdev, "resize"); 459b411b363SPhilipp Reisner 460b411b363SPhilipp Reisner dev_info(DEV, "drbd_bm_resize called with capacity == %llu\n", 461b411b363SPhilipp Reisner (unsigned long long)capacity); 462b411b363SPhilipp Reisner 463b411b363SPhilipp Reisner if (capacity == b->bm_dev_capacity) 464b411b363SPhilipp Reisner goto out; 465b411b363SPhilipp Reisner 466b411b363SPhilipp Reisner opages_vmalloced = test_bit(BM_P_VMALLOCED, &b->bm_flags); 467b411b363SPhilipp Reisner 468b411b363SPhilipp Reisner if (capacity == 0) { 469b411b363SPhilipp Reisner spin_lock_irq(&b->bm_lock); 470b411b363SPhilipp Reisner opages = b->bm_pages; 471b411b363SPhilipp Reisner onpages = b->bm_number_of_pages; 472b411b363SPhilipp Reisner owords = b->bm_words; 473b411b363SPhilipp Reisner b->bm_pages = NULL; 474b411b363SPhilipp Reisner b->bm_number_of_pages = 475b411b363SPhilipp Reisner b->bm_set = 476b411b363SPhilipp Reisner b->bm_bits = 477b411b363SPhilipp Reisner b->bm_words = 478b411b363SPhilipp Reisner b->bm_dev_capacity = 0; 479b411b363SPhilipp Reisner spin_unlock_irq(&b->bm_lock); 480b411b363SPhilipp Reisner bm_free_pages(opages, onpages); 481b411b363SPhilipp Reisner bm_vk_free(opages, opages_vmalloced); 482b411b363SPhilipp Reisner goto out; 483b411b363SPhilipp Reisner } 484b411b363SPhilipp Reisner bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT)); 485b411b363SPhilipp Reisner 486b411b363SPhilipp Reisner /* if we would use 487b411b363SPhilipp Reisner words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL; 488b411b363SPhilipp Reisner a 32bit host could present the wrong number of words 489b411b363SPhilipp Reisner to a 64bit host. 490b411b363SPhilipp Reisner */ 491b411b363SPhilipp Reisner words = ALIGN(bits, 64) >> LN2_BPL; 492b411b363SPhilipp Reisner 493b411b363SPhilipp Reisner if (get_ldev(mdev)) { 494b411b363SPhilipp Reisner D_ASSERT((u64)bits <= (((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12)); 495b411b363SPhilipp Reisner put_ldev(mdev); 496b411b363SPhilipp Reisner } 497b411b363SPhilipp Reisner 498b411b363SPhilipp Reisner /* one extra long to catch off by one errors */ 499b411b363SPhilipp Reisner want = ALIGN((words+1)*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT; 500b411b363SPhilipp Reisner have = b->bm_number_of_pages; 501b411b363SPhilipp Reisner if (want == have) { 502b411b363SPhilipp Reisner D_ASSERT(b->bm_pages != NULL); 503b411b363SPhilipp Reisner npages = b->bm_pages; 504b411b363SPhilipp Reisner } else { 5050cf9d27eSAndreas Gruenbacher if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC)) 506b411b363SPhilipp Reisner npages = NULL; 507b411b363SPhilipp Reisner else 508b411b363SPhilipp Reisner npages = bm_realloc_pages(b, want); 509b411b363SPhilipp Reisner } 510b411b363SPhilipp Reisner 511b411b363SPhilipp Reisner if (!npages) { 512b411b363SPhilipp Reisner err = -ENOMEM; 513b411b363SPhilipp Reisner goto out; 514b411b363SPhilipp Reisner } 515b411b363SPhilipp Reisner 516b411b363SPhilipp Reisner spin_lock_irq(&b->bm_lock); 517b411b363SPhilipp Reisner opages = b->bm_pages; 518b411b363SPhilipp Reisner owords = b->bm_words; 519b411b363SPhilipp Reisner obits = b->bm_bits; 520b411b363SPhilipp Reisner 521b411b363SPhilipp Reisner growing = bits > obits; 5225223671bSPhilipp Reisner if (opages && growing && set_new_bits) 523b411b363SPhilipp Reisner bm_set_surplus(b); 524b411b363SPhilipp Reisner 525b411b363SPhilipp Reisner b->bm_pages = npages; 526b411b363SPhilipp Reisner b->bm_number_of_pages = want; 527b411b363SPhilipp Reisner b->bm_bits = bits; 528b411b363SPhilipp Reisner b->bm_words = words; 529b411b363SPhilipp Reisner b->bm_dev_capacity = capacity; 530b411b363SPhilipp Reisner 531b411b363SPhilipp Reisner if (growing) { 53202d9a94bSPhilipp Reisner if (set_new_bits) { 533b411b363SPhilipp Reisner bm_memset(b, owords, 0xff, words-owords); 534b411b363SPhilipp Reisner b->bm_set += bits - obits; 53502d9a94bSPhilipp Reisner } else 53602d9a94bSPhilipp Reisner bm_memset(b, owords, 0x00, words-owords); 53702d9a94bSPhilipp Reisner 538b411b363SPhilipp Reisner } 539b411b363SPhilipp Reisner 540b411b363SPhilipp Reisner if (want < have) { 541b411b363SPhilipp Reisner /* implicit: (opages != NULL) && (opages != npages) */ 542b411b363SPhilipp Reisner bm_free_pages(opages + want, have - want); 543b411b363SPhilipp Reisner } 544b411b363SPhilipp Reisner 545b411b363SPhilipp Reisner p_addr = bm_map_paddr(b, words); 546b411b363SPhilipp Reisner bm = p_addr + MLPP(words); 547b411b363SPhilipp Reisner *bm = DRBD_MAGIC; 548b411b363SPhilipp Reisner bm_unmap(p_addr); 549b411b363SPhilipp Reisner 550b411b363SPhilipp Reisner (void)bm_clear_surplus(b); 551b411b363SPhilipp Reisner 552b411b363SPhilipp Reisner spin_unlock_irq(&b->bm_lock); 553b411b363SPhilipp Reisner if (opages != npages) 554b411b363SPhilipp Reisner bm_vk_free(opages, opages_vmalloced); 555b411b363SPhilipp Reisner if (!growing) 556b411b363SPhilipp Reisner b->bm_set = bm_count_bits(b); 557b411b363SPhilipp Reisner dev_info(DEV, "resync bitmap: bits=%lu words=%lu\n", bits, words); 558b411b363SPhilipp Reisner 559b411b363SPhilipp Reisner out: 560b411b363SPhilipp Reisner drbd_bm_unlock(mdev); 561b411b363SPhilipp Reisner return err; 562b411b363SPhilipp Reisner } 563b411b363SPhilipp Reisner 564b411b363SPhilipp Reisner /* inherently racy: 565b411b363SPhilipp Reisner * if not protected by other means, return value may be out of date when 566b411b363SPhilipp Reisner * leaving this function... 567b411b363SPhilipp Reisner * we still need to lock it, since it is important that this returns 568b411b363SPhilipp Reisner * bm_set == 0 precisely. 569b411b363SPhilipp Reisner * 570b411b363SPhilipp Reisner * maybe bm_set should be atomic_t ? 571b411b363SPhilipp Reisner */ 5720778286aSPhilipp Reisner unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev) 573b411b363SPhilipp Reisner { 574b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 575b411b363SPhilipp Reisner unsigned long s; 576b411b363SPhilipp Reisner unsigned long flags; 577b411b363SPhilipp Reisner 578b411b363SPhilipp Reisner ERR_IF(!b) return 0; 579b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return 0; 580b411b363SPhilipp Reisner 581b411b363SPhilipp Reisner spin_lock_irqsave(&b->bm_lock, flags); 582b411b363SPhilipp Reisner s = b->bm_set; 583b411b363SPhilipp Reisner spin_unlock_irqrestore(&b->bm_lock, flags); 584b411b363SPhilipp Reisner 585b411b363SPhilipp Reisner return s; 586b411b363SPhilipp Reisner } 587b411b363SPhilipp Reisner 588b411b363SPhilipp Reisner unsigned long drbd_bm_total_weight(struct drbd_conf *mdev) 589b411b363SPhilipp Reisner { 590b411b363SPhilipp Reisner unsigned long s; 591b411b363SPhilipp Reisner /* if I don't have a disk, I don't know about out-of-sync status */ 592b411b363SPhilipp Reisner if (!get_ldev_if_state(mdev, D_NEGOTIATING)) 593b411b363SPhilipp Reisner return 0; 594b411b363SPhilipp Reisner s = _drbd_bm_total_weight(mdev); 595b411b363SPhilipp Reisner put_ldev(mdev); 596b411b363SPhilipp Reisner return s; 597b411b363SPhilipp Reisner } 598b411b363SPhilipp Reisner 599b411b363SPhilipp Reisner size_t drbd_bm_words(struct drbd_conf *mdev) 600b411b363SPhilipp Reisner { 601b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 602b411b363SPhilipp Reisner ERR_IF(!b) return 0; 603b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return 0; 604b411b363SPhilipp Reisner 605b411b363SPhilipp Reisner return b->bm_words; 606b411b363SPhilipp Reisner } 607b411b363SPhilipp Reisner 608b411b363SPhilipp Reisner unsigned long drbd_bm_bits(struct drbd_conf *mdev) 609b411b363SPhilipp Reisner { 610b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 611b411b363SPhilipp Reisner ERR_IF(!b) return 0; 612b411b363SPhilipp Reisner 613b411b363SPhilipp Reisner return b->bm_bits; 614b411b363SPhilipp Reisner } 615b411b363SPhilipp Reisner 616b411b363SPhilipp Reisner /* merge number words from buffer into the bitmap starting at offset. 617b411b363SPhilipp Reisner * buffer[i] is expected to be little endian unsigned long. 618b411b363SPhilipp Reisner * bitmap must be locked by drbd_bm_lock. 619b411b363SPhilipp Reisner * currently only used from receive_bitmap. 620b411b363SPhilipp Reisner */ 621b411b363SPhilipp Reisner void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number, 622b411b363SPhilipp Reisner unsigned long *buffer) 623b411b363SPhilipp Reisner { 624b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 625b411b363SPhilipp Reisner unsigned long *p_addr, *bm; 626b411b363SPhilipp Reisner unsigned long word, bits; 627b411b363SPhilipp Reisner size_t end, do_now; 628b411b363SPhilipp Reisner 629b411b363SPhilipp Reisner end = offset + number; 630b411b363SPhilipp Reisner 631b411b363SPhilipp Reisner ERR_IF(!b) return; 632b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return; 633b411b363SPhilipp Reisner if (number == 0) 634b411b363SPhilipp Reisner return; 635b411b363SPhilipp Reisner WARN_ON(offset >= b->bm_words); 636b411b363SPhilipp Reisner WARN_ON(end > b->bm_words); 637b411b363SPhilipp Reisner 638b411b363SPhilipp Reisner spin_lock_irq(&b->bm_lock); 639b411b363SPhilipp Reisner while (offset < end) { 640b411b363SPhilipp Reisner do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset; 641b411b363SPhilipp Reisner p_addr = bm_map_paddr(b, offset); 642b411b363SPhilipp Reisner bm = p_addr + MLPP(offset); 643b411b363SPhilipp Reisner offset += do_now; 644b411b363SPhilipp Reisner while (do_now--) { 645b411b363SPhilipp Reisner bits = hweight_long(*bm); 646b411b363SPhilipp Reisner word = *bm | lel_to_cpu(*buffer++); 647b411b363SPhilipp Reisner *bm++ = word; 648b411b363SPhilipp Reisner b->bm_set += hweight_long(word) - bits; 649b411b363SPhilipp Reisner } 650b411b363SPhilipp Reisner bm_unmap(p_addr); 651b411b363SPhilipp Reisner } 652b411b363SPhilipp Reisner /* with 32bit <-> 64bit cross-platform connect 653b411b363SPhilipp Reisner * this is only correct for current usage, 654b411b363SPhilipp Reisner * where we _know_ that we are 64 bit aligned, 655b411b363SPhilipp Reisner * and know that this function is used in this way, too... 656b411b363SPhilipp Reisner */ 657b411b363SPhilipp Reisner if (end == b->bm_words) 658b411b363SPhilipp Reisner b->bm_set -= bm_clear_surplus(b); 659b411b363SPhilipp Reisner 660b411b363SPhilipp Reisner spin_unlock_irq(&b->bm_lock); 661b411b363SPhilipp Reisner } 662b411b363SPhilipp Reisner 663b411b363SPhilipp Reisner /* copy number words from the bitmap starting at offset into the buffer. 664b411b363SPhilipp Reisner * buffer[i] will be little endian unsigned long. 665b411b363SPhilipp Reisner */ 666b411b363SPhilipp Reisner void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number, 667b411b363SPhilipp Reisner unsigned long *buffer) 668b411b363SPhilipp Reisner { 669b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 670b411b363SPhilipp Reisner unsigned long *p_addr, *bm; 671b411b363SPhilipp Reisner size_t end, do_now; 672b411b363SPhilipp Reisner 673b411b363SPhilipp Reisner end = offset + number; 674b411b363SPhilipp Reisner 675b411b363SPhilipp Reisner ERR_IF(!b) return; 676b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return; 677b411b363SPhilipp Reisner 678b411b363SPhilipp Reisner spin_lock_irq(&b->bm_lock); 679b411b363SPhilipp Reisner if ((offset >= b->bm_words) || 680b411b363SPhilipp Reisner (end > b->bm_words) || 681b411b363SPhilipp Reisner (number <= 0)) 682b411b363SPhilipp Reisner dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n", 683b411b363SPhilipp Reisner (unsigned long) offset, 684b411b363SPhilipp Reisner (unsigned long) number, 685b411b363SPhilipp Reisner (unsigned long) b->bm_words); 686b411b363SPhilipp Reisner else { 687b411b363SPhilipp Reisner while (offset < end) { 688b411b363SPhilipp Reisner do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset; 689b411b363SPhilipp Reisner p_addr = bm_map_paddr(b, offset); 690b411b363SPhilipp Reisner bm = p_addr + MLPP(offset); 691b411b363SPhilipp Reisner offset += do_now; 692b411b363SPhilipp Reisner while (do_now--) 693b411b363SPhilipp Reisner *buffer++ = cpu_to_lel(*bm++); 694b411b363SPhilipp Reisner bm_unmap(p_addr); 695b411b363SPhilipp Reisner } 696b411b363SPhilipp Reisner } 697b411b363SPhilipp Reisner spin_unlock_irq(&b->bm_lock); 698b411b363SPhilipp Reisner } 699b411b363SPhilipp Reisner 700b411b363SPhilipp Reisner /* set all bits in the bitmap */ 701b411b363SPhilipp Reisner void drbd_bm_set_all(struct drbd_conf *mdev) 702b411b363SPhilipp Reisner { 703b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 704b411b363SPhilipp Reisner ERR_IF(!b) return; 705b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return; 706b411b363SPhilipp Reisner 707b411b363SPhilipp Reisner spin_lock_irq(&b->bm_lock); 708b411b363SPhilipp Reisner bm_memset(b, 0, 0xff, b->bm_words); 709b411b363SPhilipp Reisner (void)bm_clear_surplus(b); 710b411b363SPhilipp Reisner b->bm_set = b->bm_bits; 711b411b363SPhilipp Reisner spin_unlock_irq(&b->bm_lock); 712b411b363SPhilipp Reisner } 713b411b363SPhilipp Reisner 714b411b363SPhilipp Reisner /* clear all bits in the bitmap */ 715b411b363SPhilipp Reisner void drbd_bm_clear_all(struct drbd_conf *mdev) 716b411b363SPhilipp Reisner { 717b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 718b411b363SPhilipp Reisner ERR_IF(!b) return; 719b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return; 720b411b363SPhilipp Reisner 721b411b363SPhilipp Reisner spin_lock_irq(&b->bm_lock); 722b411b363SPhilipp Reisner bm_memset(b, 0, 0, b->bm_words); 723b411b363SPhilipp Reisner b->bm_set = 0; 724b411b363SPhilipp Reisner spin_unlock_irq(&b->bm_lock); 725b411b363SPhilipp Reisner } 726b411b363SPhilipp Reisner 727b411b363SPhilipp Reisner static void bm_async_io_complete(struct bio *bio, int error) 728b411b363SPhilipp Reisner { 729b411b363SPhilipp Reisner struct drbd_bitmap *b = bio->bi_private; 730b411b363SPhilipp Reisner int uptodate = bio_flagged(bio, BIO_UPTODATE); 731b411b363SPhilipp Reisner 732b411b363SPhilipp Reisner 733b411b363SPhilipp Reisner /* strange behavior of some lower level drivers... 734b411b363SPhilipp Reisner * fail the request by clearing the uptodate flag, 735b411b363SPhilipp Reisner * but do not return any error?! 736b411b363SPhilipp Reisner * do we want to WARN() on this? */ 737b411b363SPhilipp Reisner if (!error && !uptodate) 738b411b363SPhilipp Reisner error = -EIO; 739b411b363SPhilipp Reisner 740b411b363SPhilipp Reisner if (error) { 741b411b363SPhilipp Reisner /* doh. what now? 742b411b363SPhilipp Reisner * for now, set all bits, and flag MD_IO_ERROR */ 743b411b363SPhilipp Reisner __set_bit(BM_MD_IO_ERROR, &b->bm_flags); 744b411b363SPhilipp Reisner } 745b411b363SPhilipp Reisner if (atomic_dec_and_test(&b->bm_async_io)) 746b411b363SPhilipp Reisner wake_up(&b->bm_io_wait); 747b411b363SPhilipp Reisner 748b411b363SPhilipp Reisner bio_put(bio); 749b411b363SPhilipp Reisner } 750b411b363SPhilipp Reisner 751b411b363SPhilipp Reisner static void bm_page_io_async(struct drbd_conf *mdev, struct drbd_bitmap *b, int page_nr, int rw) __must_hold(local) 752b411b363SPhilipp Reisner { 753b411b363SPhilipp Reisner /* we are process context. we always get a bio */ 754b411b363SPhilipp Reisner struct bio *bio = bio_alloc(GFP_KERNEL, 1); 755b411b363SPhilipp Reisner unsigned int len; 756b411b363SPhilipp Reisner sector_t on_disk_sector = 757b411b363SPhilipp Reisner mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset; 758b411b363SPhilipp Reisner on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9); 759b411b363SPhilipp Reisner 760b411b363SPhilipp Reisner /* this might happen with very small 761b411b363SPhilipp Reisner * flexible external meta data device */ 762b411b363SPhilipp Reisner len = min_t(unsigned int, PAGE_SIZE, 763b411b363SPhilipp Reisner (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9); 764b411b363SPhilipp Reisner 765b411b363SPhilipp Reisner bio->bi_bdev = mdev->ldev->md_bdev; 766b411b363SPhilipp Reisner bio->bi_sector = on_disk_sector; 767b411b363SPhilipp Reisner bio_add_page(bio, b->bm_pages[page_nr], len, 0); 768b411b363SPhilipp Reisner bio->bi_private = b; 769b411b363SPhilipp Reisner bio->bi_end_io = bm_async_io_complete; 770b411b363SPhilipp Reisner 7710cf9d27eSAndreas Gruenbacher if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) { 772b411b363SPhilipp Reisner bio->bi_rw |= rw; 773b411b363SPhilipp Reisner bio_endio(bio, -EIO); 774b411b363SPhilipp Reisner } else { 775b411b363SPhilipp Reisner submit_bio(rw, bio); 776b411b363SPhilipp Reisner } 777b411b363SPhilipp Reisner } 778b411b363SPhilipp Reisner 779b411b363SPhilipp Reisner # if defined(__LITTLE_ENDIAN) 780b411b363SPhilipp Reisner /* nothing to do, on disk == in memory */ 781b411b363SPhilipp Reisner # define bm_cpu_to_lel(x) ((void)0) 782b411b363SPhilipp Reisner # else 783b4ee79daSPhilipp Reisner static void bm_cpu_to_lel(struct drbd_bitmap *b) 784b411b363SPhilipp Reisner { 785b411b363SPhilipp Reisner /* need to cpu_to_lel all the pages ... 786b411b363SPhilipp Reisner * this may be optimized by using 787b411b363SPhilipp Reisner * cpu_to_lel(-1) == -1 and cpu_to_lel(0) == 0; 788b411b363SPhilipp Reisner * the following is still not optimal, but better than nothing */ 789b411b363SPhilipp Reisner unsigned int i; 790b411b363SPhilipp Reisner unsigned long *p_addr, *bm; 791b411b363SPhilipp Reisner if (b->bm_set == 0) { 792b411b363SPhilipp Reisner /* no page at all; avoid swap if all is 0 */ 793b411b363SPhilipp Reisner i = b->bm_number_of_pages; 794b411b363SPhilipp Reisner } else if (b->bm_set == b->bm_bits) { 795b411b363SPhilipp Reisner /* only the last page */ 796b411b363SPhilipp Reisner i = b->bm_number_of_pages - 1; 797b411b363SPhilipp Reisner } else { 798b411b363SPhilipp Reisner /* all pages */ 799b411b363SPhilipp Reisner i = 0; 800b411b363SPhilipp Reisner } 801b411b363SPhilipp Reisner for (; i < b->bm_number_of_pages; i++) { 802b411b363SPhilipp Reisner p_addr = kmap_atomic(b->bm_pages[i], KM_USER0); 803b411b363SPhilipp Reisner for (bm = p_addr; bm < p_addr + PAGE_SIZE/sizeof(long); bm++) 804b411b363SPhilipp Reisner *bm = cpu_to_lel(*bm); 805b411b363SPhilipp Reisner kunmap_atomic(p_addr, KM_USER0); 806b411b363SPhilipp Reisner } 807b411b363SPhilipp Reisner } 808b411b363SPhilipp Reisner # endif 809b411b363SPhilipp Reisner /* lel_to_cpu == cpu_to_lel */ 810b411b363SPhilipp Reisner # define bm_lel_to_cpu(x) bm_cpu_to_lel(x) 811b411b363SPhilipp Reisner 812b411b363SPhilipp Reisner /* 813b411b363SPhilipp Reisner * bm_rw: read/write the whole bitmap from/to its on disk location. 814b411b363SPhilipp Reisner */ 815b411b363SPhilipp Reisner static int bm_rw(struct drbd_conf *mdev, int rw) __must_hold(local) 816b411b363SPhilipp Reisner { 817b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 818b411b363SPhilipp Reisner /* sector_t sector; */ 819b411b363SPhilipp Reisner int bm_words, num_pages, i; 820b411b363SPhilipp Reisner unsigned long now; 821b411b363SPhilipp Reisner char ppb[10]; 822b411b363SPhilipp Reisner int err = 0; 823b411b363SPhilipp Reisner 824b411b363SPhilipp Reisner WARN_ON(!bm_is_locked(b)); 825b411b363SPhilipp Reisner 826b411b363SPhilipp Reisner /* no spinlock here, the drbd_bm_lock should be enough! */ 827b411b363SPhilipp Reisner 828b411b363SPhilipp Reisner bm_words = drbd_bm_words(mdev); 829b411b363SPhilipp Reisner num_pages = (bm_words*sizeof(long) + PAGE_SIZE-1) >> PAGE_SHIFT; 830b411b363SPhilipp Reisner 831b411b363SPhilipp Reisner /* on disk bitmap is little endian */ 832b411b363SPhilipp Reisner if (rw == WRITE) 833b411b363SPhilipp Reisner bm_cpu_to_lel(b); 834b411b363SPhilipp Reisner 835b411b363SPhilipp Reisner now = jiffies; 836b411b363SPhilipp Reisner atomic_set(&b->bm_async_io, num_pages); 837b411b363SPhilipp Reisner __clear_bit(BM_MD_IO_ERROR, &b->bm_flags); 838b411b363SPhilipp Reisner 839b411b363SPhilipp Reisner /* let the layers below us try to merge these bios... */ 840b411b363SPhilipp Reisner for (i = 0; i < num_pages; i++) 841b411b363SPhilipp Reisner bm_page_io_async(mdev, b, i, rw); 842b411b363SPhilipp Reisner 843b411b363SPhilipp Reisner wait_event(b->bm_io_wait, atomic_read(&b->bm_async_io) == 0); 844b411b363SPhilipp Reisner 845b411b363SPhilipp Reisner if (test_bit(BM_MD_IO_ERROR, &b->bm_flags)) { 846b411b363SPhilipp Reisner dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n"); 847*81e84650SAndreas Gruenbacher drbd_chk_io_error(mdev, 1, true); 848b411b363SPhilipp Reisner err = -EIO; 849b411b363SPhilipp Reisner } 850b411b363SPhilipp Reisner 851b411b363SPhilipp Reisner now = jiffies; 852b411b363SPhilipp Reisner if (rw == WRITE) { 853b411b363SPhilipp Reisner /* swap back endianness */ 854b411b363SPhilipp Reisner bm_lel_to_cpu(b); 855b411b363SPhilipp Reisner /* flush bitmap to stable storage */ 856b411b363SPhilipp Reisner drbd_md_flush(mdev); 857b411b363SPhilipp Reisner } else /* rw == READ */ { 858b411b363SPhilipp Reisner /* just read, if necessary adjust endianness */ 859b411b363SPhilipp Reisner b->bm_set = bm_count_bits_swap_endian(b); 860b411b363SPhilipp Reisner dev_info(DEV, "recounting of set bits took additional %lu jiffies\n", 861b411b363SPhilipp Reisner jiffies - now); 862b411b363SPhilipp Reisner } 863b411b363SPhilipp Reisner now = b->bm_set; 864b411b363SPhilipp Reisner 865b411b363SPhilipp Reisner dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n", 866b411b363SPhilipp Reisner ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now); 867b411b363SPhilipp Reisner 868b411b363SPhilipp Reisner return err; 869b411b363SPhilipp Reisner } 870b411b363SPhilipp Reisner 871b411b363SPhilipp Reisner /** 872b411b363SPhilipp Reisner * drbd_bm_read() - Read the whole bitmap from its on disk location. 873b411b363SPhilipp Reisner * @mdev: DRBD device. 874b411b363SPhilipp Reisner */ 875b411b363SPhilipp Reisner int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local) 876b411b363SPhilipp Reisner { 877b411b363SPhilipp Reisner return bm_rw(mdev, READ); 878b411b363SPhilipp Reisner } 879b411b363SPhilipp Reisner 880b411b363SPhilipp Reisner /** 881b411b363SPhilipp Reisner * drbd_bm_write() - Write the whole bitmap to its on disk location. 882b411b363SPhilipp Reisner * @mdev: DRBD device. 883b411b363SPhilipp Reisner */ 884b411b363SPhilipp Reisner int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local) 885b411b363SPhilipp Reisner { 886b411b363SPhilipp Reisner return bm_rw(mdev, WRITE); 887b411b363SPhilipp Reisner } 888b411b363SPhilipp Reisner 889b411b363SPhilipp Reisner /** 890b411b363SPhilipp Reisner * drbd_bm_write_sect: Writes a 512 (MD_SECTOR_SIZE) byte piece of the bitmap 891b411b363SPhilipp Reisner * @mdev: DRBD device. 892b411b363SPhilipp Reisner * @enr: Extent number in the resync lru (happens to be sector offset) 893b411b363SPhilipp Reisner * 894b411b363SPhilipp Reisner * The BM_EXT_SIZE is on purpose exactly the amount of the bitmap covered 895b411b363SPhilipp Reisner * by a single sector write. Therefore enr == sector offset from the 896b411b363SPhilipp Reisner * start of the bitmap. 897b411b363SPhilipp Reisner */ 898b411b363SPhilipp Reisner int drbd_bm_write_sect(struct drbd_conf *mdev, unsigned long enr) __must_hold(local) 899b411b363SPhilipp Reisner { 900b411b363SPhilipp Reisner sector_t on_disk_sector = enr + mdev->ldev->md.md_offset 901b411b363SPhilipp Reisner + mdev->ldev->md.bm_offset; 902b411b363SPhilipp Reisner int bm_words, num_words, offset; 903b411b363SPhilipp Reisner int err = 0; 904b411b363SPhilipp Reisner 905b411b363SPhilipp Reisner mutex_lock(&mdev->md_io_mutex); 906b411b363SPhilipp Reisner bm_words = drbd_bm_words(mdev); 907b411b363SPhilipp Reisner offset = S2W(enr); /* word offset into bitmap */ 908b411b363SPhilipp Reisner num_words = min(S2W(1), bm_words - offset); 909b411b363SPhilipp Reisner if (num_words < S2W(1)) 910b411b363SPhilipp Reisner memset(page_address(mdev->md_io_page), 0, MD_SECTOR_SIZE); 911b411b363SPhilipp Reisner drbd_bm_get_lel(mdev, offset, num_words, 912b411b363SPhilipp Reisner page_address(mdev->md_io_page)); 913b411b363SPhilipp Reisner if (!drbd_md_sync_page_io(mdev, mdev->ldev, on_disk_sector, WRITE)) { 914b411b363SPhilipp Reisner int i; 915b411b363SPhilipp Reisner err = -EIO; 916b411b363SPhilipp Reisner dev_err(DEV, "IO ERROR writing bitmap sector %lu " 917b411b363SPhilipp Reisner "(meta-disk sector %llus)\n", 918b411b363SPhilipp Reisner enr, (unsigned long long)on_disk_sector); 919*81e84650SAndreas Gruenbacher drbd_chk_io_error(mdev, 1, true); 920b411b363SPhilipp Reisner for (i = 0; i < AL_EXT_PER_BM_SECT; i++) 921b411b363SPhilipp Reisner drbd_bm_ALe_set_all(mdev, enr*AL_EXT_PER_BM_SECT+i); 922b411b363SPhilipp Reisner } 923b411b363SPhilipp Reisner mdev->bm_writ_cnt++; 924b411b363SPhilipp Reisner mutex_unlock(&mdev->md_io_mutex); 925b411b363SPhilipp Reisner return err; 926b411b363SPhilipp Reisner } 927b411b363SPhilipp Reisner 928b411b363SPhilipp Reisner /* NOTE 929b411b363SPhilipp Reisner * find_first_bit returns int, we return unsigned long. 930b411b363SPhilipp Reisner * should not make much difference anyways, but ... 931b411b363SPhilipp Reisner * 932b411b363SPhilipp Reisner * this returns a bit number, NOT a sector! 933b411b363SPhilipp Reisner */ 934b411b363SPhilipp Reisner #define BPP_MASK ((1UL << (PAGE_SHIFT+3)) - 1) 935b411b363SPhilipp Reisner static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo, 936b411b363SPhilipp Reisner const int find_zero_bit, const enum km_type km) 937b411b363SPhilipp Reisner { 938b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 939b411b363SPhilipp Reisner unsigned long i = -1UL; 940b411b363SPhilipp Reisner unsigned long *p_addr; 941b411b363SPhilipp Reisner unsigned long bit_offset; /* bit offset of the mapped page. */ 942b411b363SPhilipp Reisner 943b411b363SPhilipp Reisner if (bm_fo > b->bm_bits) { 944b411b363SPhilipp Reisner dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits); 945b411b363SPhilipp Reisner } else { 946b411b363SPhilipp Reisner while (bm_fo < b->bm_bits) { 947b411b363SPhilipp Reisner unsigned long offset; 948b411b363SPhilipp Reisner bit_offset = bm_fo & ~BPP_MASK; /* bit offset of the page */ 949b411b363SPhilipp Reisner offset = bit_offset >> LN2_BPL; /* word offset of the page */ 950b411b363SPhilipp Reisner p_addr = __bm_map_paddr(b, offset, km); 951b411b363SPhilipp Reisner 952b411b363SPhilipp Reisner if (find_zero_bit) 953b411b363SPhilipp Reisner i = find_next_zero_bit(p_addr, PAGE_SIZE*8, bm_fo & BPP_MASK); 954b411b363SPhilipp Reisner else 955b411b363SPhilipp Reisner i = find_next_bit(p_addr, PAGE_SIZE*8, bm_fo & BPP_MASK); 956b411b363SPhilipp Reisner 957b411b363SPhilipp Reisner __bm_unmap(p_addr, km); 958b411b363SPhilipp Reisner if (i < PAGE_SIZE*8) { 959b411b363SPhilipp Reisner i = bit_offset + i; 960b411b363SPhilipp Reisner if (i >= b->bm_bits) 961b411b363SPhilipp Reisner break; 962b411b363SPhilipp Reisner goto found; 963b411b363SPhilipp Reisner } 964b411b363SPhilipp Reisner bm_fo = bit_offset + PAGE_SIZE*8; 965b411b363SPhilipp Reisner } 966b411b363SPhilipp Reisner i = -1UL; 967b411b363SPhilipp Reisner } 968b411b363SPhilipp Reisner found: 969b411b363SPhilipp Reisner return i; 970b411b363SPhilipp Reisner } 971b411b363SPhilipp Reisner 972b411b363SPhilipp Reisner static unsigned long bm_find_next(struct drbd_conf *mdev, 973b411b363SPhilipp Reisner unsigned long bm_fo, const int find_zero_bit) 974b411b363SPhilipp Reisner { 975b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 976b411b363SPhilipp Reisner unsigned long i = -1UL; 977b411b363SPhilipp Reisner 978b411b363SPhilipp Reisner ERR_IF(!b) return i; 979b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return i; 980b411b363SPhilipp Reisner 981b411b363SPhilipp Reisner spin_lock_irq(&b->bm_lock); 982b411b363SPhilipp Reisner if (bm_is_locked(b)) 983b411b363SPhilipp Reisner bm_print_lock_info(mdev); 984b411b363SPhilipp Reisner 985b411b363SPhilipp Reisner i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1); 986b411b363SPhilipp Reisner 987b411b363SPhilipp Reisner spin_unlock_irq(&b->bm_lock); 988b411b363SPhilipp Reisner return i; 989b411b363SPhilipp Reisner } 990b411b363SPhilipp Reisner 991b411b363SPhilipp Reisner unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo) 992b411b363SPhilipp Reisner { 993b411b363SPhilipp Reisner return bm_find_next(mdev, bm_fo, 0); 994b411b363SPhilipp Reisner } 995b411b363SPhilipp Reisner 996b411b363SPhilipp Reisner #if 0 997b411b363SPhilipp Reisner /* not yet needed for anything. */ 998b411b363SPhilipp Reisner unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo) 999b411b363SPhilipp Reisner { 1000b411b363SPhilipp Reisner return bm_find_next(mdev, bm_fo, 1); 1001b411b363SPhilipp Reisner } 1002b411b363SPhilipp Reisner #endif 1003b411b363SPhilipp Reisner 1004b411b363SPhilipp Reisner /* does not spin_lock_irqsave. 1005b411b363SPhilipp Reisner * you must take drbd_bm_lock() first */ 1006b411b363SPhilipp Reisner unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo) 1007b411b363SPhilipp Reisner { 1008b411b363SPhilipp Reisner /* WARN_ON(!bm_is_locked(mdev)); */ 1009b411b363SPhilipp Reisner return __bm_find_next(mdev, bm_fo, 0, KM_USER1); 1010b411b363SPhilipp Reisner } 1011b411b363SPhilipp Reisner 1012b411b363SPhilipp Reisner unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo) 1013b411b363SPhilipp Reisner { 1014b411b363SPhilipp Reisner /* WARN_ON(!bm_is_locked(mdev)); */ 1015b411b363SPhilipp Reisner return __bm_find_next(mdev, bm_fo, 1, KM_USER1); 1016b411b363SPhilipp Reisner } 1017b411b363SPhilipp Reisner 1018b411b363SPhilipp Reisner /* returns number of bits actually changed. 1019b411b363SPhilipp Reisner * for val != 0, we change 0 -> 1, return code positive 1020b411b363SPhilipp Reisner * for val == 0, we change 1 -> 0, return code negative 1021b411b363SPhilipp Reisner * wants bitnr, not sector. 1022b411b363SPhilipp Reisner * expected to be called for only a few bits (e - s about BITS_PER_LONG). 1023b411b363SPhilipp Reisner * Must hold bitmap lock already. */ 1024b4ee79daSPhilipp Reisner static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s, 1025b411b363SPhilipp Reisner unsigned long e, int val, const enum km_type km) 1026b411b363SPhilipp Reisner { 1027b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 1028b411b363SPhilipp Reisner unsigned long *p_addr = NULL; 1029b411b363SPhilipp Reisner unsigned long bitnr; 1030b411b363SPhilipp Reisner unsigned long last_page_nr = -1UL; 1031b411b363SPhilipp Reisner int c = 0; 1032b411b363SPhilipp Reisner 1033b411b363SPhilipp Reisner if (e >= b->bm_bits) { 1034b411b363SPhilipp Reisner dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n", 1035b411b363SPhilipp Reisner s, e, b->bm_bits); 1036b411b363SPhilipp Reisner e = b->bm_bits ? b->bm_bits -1 : 0; 1037b411b363SPhilipp Reisner } 1038b411b363SPhilipp Reisner for (bitnr = s; bitnr <= e; bitnr++) { 1039b411b363SPhilipp Reisner unsigned long offset = bitnr>>LN2_BPL; 1040b411b363SPhilipp Reisner unsigned long page_nr = offset >> (PAGE_SHIFT - LN2_BPL + 3); 1041b411b363SPhilipp Reisner if (page_nr != last_page_nr) { 1042b411b363SPhilipp Reisner if (p_addr) 1043b411b363SPhilipp Reisner __bm_unmap(p_addr, km); 1044b411b363SPhilipp Reisner p_addr = __bm_map_paddr(b, offset, km); 1045b411b363SPhilipp Reisner last_page_nr = page_nr; 1046b411b363SPhilipp Reisner } 1047b411b363SPhilipp Reisner if (val) 1048b411b363SPhilipp Reisner c += (0 == __test_and_set_bit(bitnr & BPP_MASK, p_addr)); 1049b411b363SPhilipp Reisner else 1050b411b363SPhilipp Reisner c -= (0 != __test_and_clear_bit(bitnr & BPP_MASK, p_addr)); 1051b411b363SPhilipp Reisner } 1052b411b363SPhilipp Reisner if (p_addr) 1053b411b363SPhilipp Reisner __bm_unmap(p_addr, km); 1054b411b363SPhilipp Reisner b->bm_set += c; 1055b411b363SPhilipp Reisner return c; 1056b411b363SPhilipp Reisner } 1057b411b363SPhilipp Reisner 1058b411b363SPhilipp Reisner /* returns number of bits actually changed. 1059b411b363SPhilipp Reisner * for val != 0, we change 0 -> 1, return code positive 1060b411b363SPhilipp Reisner * for val == 0, we change 1 -> 0, return code negative 1061b411b363SPhilipp Reisner * wants bitnr, not sector */ 1062b4ee79daSPhilipp Reisner static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s, 1063b411b363SPhilipp Reisner const unsigned long e, int val) 1064b411b363SPhilipp Reisner { 1065b411b363SPhilipp Reisner unsigned long flags; 1066b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 1067b411b363SPhilipp Reisner int c = 0; 1068b411b363SPhilipp Reisner 1069b411b363SPhilipp Reisner ERR_IF(!b) return 1; 1070b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return 0; 1071b411b363SPhilipp Reisner 1072b411b363SPhilipp Reisner spin_lock_irqsave(&b->bm_lock, flags); 1073b411b363SPhilipp Reisner if (bm_is_locked(b)) 1074b411b363SPhilipp Reisner bm_print_lock_info(mdev); 1075b411b363SPhilipp Reisner 1076b411b363SPhilipp Reisner c = __bm_change_bits_to(mdev, s, e, val, KM_IRQ1); 1077b411b363SPhilipp Reisner 1078b411b363SPhilipp Reisner spin_unlock_irqrestore(&b->bm_lock, flags); 1079b411b363SPhilipp Reisner return c; 1080b411b363SPhilipp Reisner } 1081b411b363SPhilipp Reisner 1082b411b363SPhilipp Reisner /* returns number of bits changed 0 -> 1 */ 1083b411b363SPhilipp Reisner int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e) 1084b411b363SPhilipp Reisner { 1085b411b363SPhilipp Reisner return bm_change_bits_to(mdev, s, e, 1); 1086b411b363SPhilipp Reisner } 1087b411b363SPhilipp Reisner 1088b411b363SPhilipp Reisner /* returns number of bits changed 1 -> 0 */ 1089b411b363SPhilipp Reisner int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e) 1090b411b363SPhilipp Reisner { 1091b411b363SPhilipp Reisner return -bm_change_bits_to(mdev, s, e, 0); 1092b411b363SPhilipp Reisner } 1093b411b363SPhilipp Reisner 1094b411b363SPhilipp Reisner /* sets all bits in full words, 1095b411b363SPhilipp Reisner * from first_word up to, but not including, last_word */ 1096b411b363SPhilipp Reisner static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b, 1097b411b363SPhilipp Reisner int page_nr, int first_word, int last_word) 1098b411b363SPhilipp Reisner { 1099b411b363SPhilipp Reisner int i; 1100b411b363SPhilipp Reisner int bits; 1101b411b363SPhilipp Reisner unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_USER0); 1102b411b363SPhilipp Reisner for (i = first_word; i < last_word; i++) { 1103b411b363SPhilipp Reisner bits = hweight_long(paddr[i]); 1104b411b363SPhilipp Reisner paddr[i] = ~0UL; 1105b411b363SPhilipp Reisner b->bm_set += BITS_PER_LONG - bits; 1106b411b363SPhilipp Reisner } 1107b411b363SPhilipp Reisner kunmap_atomic(paddr, KM_USER0); 1108b411b363SPhilipp Reisner } 1109b411b363SPhilipp Reisner 1110b411b363SPhilipp Reisner /* Same thing as drbd_bm_set_bits, but without taking the spin_lock_irqsave. 1111b411b363SPhilipp Reisner * You must first drbd_bm_lock(). 1112b411b363SPhilipp Reisner * Can be called to set the whole bitmap in one go. 1113b411b363SPhilipp Reisner * Sets bits from s to e _inclusive_. */ 1114b411b363SPhilipp Reisner void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e) 1115b411b363SPhilipp Reisner { 1116b411b363SPhilipp Reisner /* First set_bit from the first bit (s) 1117b411b363SPhilipp Reisner * up to the next long boundary (sl), 1118b411b363SPhilipp Reisner * then assign full words up to the last long boundary (el), 1119b411b363SPhilipp Reisner * then set_bit up to and including the last bit (e). 1120b411b363SPhilipp Reisner * 1121b411b363SPhilipp Reisner * Do not use memset, because we must account for changes, 1122b411b363SPhilipp Reisner * so we need to loop over the words with hweight() anyways. 1123b411b363SPhilipp Reisner */ 1124b411b363SPhilipp Reisner unsigned long sl = ALIGN(s,BITS_PER_LONG); 1125b411b363SPhilipp Reisner unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1); 1126b411b363SPhilipp Reisner int first_page; 1127b411b363SPhilipp Reisner int last_page; 1128b411b363SPhilipp Reisner int page_nr; 1129b411b363SPhilipp Reisner int first_word; 1130b411b363SPhilipp Reisner int last_word; 1131b411b363SPhilipp Reisner 1132b411b363SPhilipp Reisner if (e - s <= 3*BITS_PER_LONG) { 1133b411b363SPhilipp Reisner /* don't bother; el and sl may even be wrong. */ 1134b411b363SPhilipp Reisner __bm_change_bits_to(mdev, s, e, 1, KM_USER0); 1135b411b363SPhilipp Reisner return; 1136b411b363SPhilipp Reisner } 1137b411b363SPhilipp Reisner 1138b411b363SPhilipp Reisner /* difference is large enough that we can trust sl and el */ 1139b411b363SPhilipp Reisner 1140b411b363SPhilipp Reisner /* bits filling the current long */ 1141b411b363SPhilipp Reisner if (sl) 1142b411b363SPhilipp Reisner __bm_change_bits_to(mdev, s, sl-1, 1, KM_USER0); 1143b411b363SPhilipp Reisner 1144b411b363SPhilipp Reisner first_page = sl >> (3 + PAGE_SHIFT); 1145b411b363SPhilipp Reisner last_page = el >> (3 + PAGE_SHIFT); 1146b411b363SPhilipp Reisner 1147b411b363SPhilipp Reisner /* MLPP: modulo longs per page */ 1148b411b363SPhilipp Reisner /* LWPP: long words per page */ 1149b411b363SPhilipp Reisner first_word = MLPP(sl >> LN2_BPL); 1150b411b363SPhilipp Reisner last_word = LWPP; 1151b411b363SPhilipp Reisner 1152b411b363SPhilipp Reisner /* first and full pages, unless first page == last page */ 1153b411b363SPhilipp Reisner for (page_nr = first_page; page_nr < last_page; page_nr++) { 1154b411b363SPhilipp Reisner bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word); 1155b411b363SPhilipp Reisner cond_resched(); 1156b411b363SPhilipp Reisner first_word = 0; 1157b411b363SPhilipp Reisner } 1158b411b363SPhilipp Reisner 1159b411b363SPhilipp Reisner /* last page (respectively only page, for first page == last page) */ 1160b411b363SPhilipp Reisner last_word = MLPP(el >> LN2_BPL); 1161b411b363SPhilipp Reisner bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word); 1162b411b363SPhilipp Reisner 1163b411b363SPhilipp Reisner /* possibly trailing bits. 1164b411b363SPhilipp Reisner * example: (e & 63) == 63, el will be e+1. 1165b411b363SPhilipp Reisner * if that even was the very last bit, 1166b411b363SPhilipp Reisner * it would trigger an assert in __bm_change_bits_to() 1167b411b363SPhilipp Reisner */ 1168b411b363SPhilipp Reisner if (el <= e) 1169b411b363SPhilipp Reisner __bm_change_bits_to(mdev, el, e, 1, KM_USER0); 1170b411b363SPhilipp Reisner } 1171b411b363SPhilipp Reisner 1172b411b363SPhilipp Reisner /* returns bit state 1173b411b363SPhilipp Reisner * wants bitnr, NOT sector. 1174b411b363SPhilipp Reisner * inherently racy... area needs to be locked by means of {al,rs}_lru 1175b411b363SPhilipp Reisner * 1 ... bit set 1176b411b363SPhilipp Reisner * 0 ... bit not set 1177b411b363SPhilipp Reisner * -1 ... first out of bounds access, stop testing for bits! 1178b411b363SPhilipp Reisner */ 1179b411b363SPhilipp Reisner int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr) 1180b411b363SPhilipp Reisner { 1181b411b363SPhilipp Reisner unsigned long flags; 1182b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 1183b411b363SPhilipp Reisner unsigned long *p_addr; 1184b411b363SPhilipp Reisner int i; 1185b411b363SPhilipp Reisner 1186b411b363SPhilipp Reisner ERR_IF(!b) return 0; 1187b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return 0; 1188b411b363SPhilipp Reisner 1189b411b363SPhilipp Reisner spin_lock_irqsave(&b->bm_lock, flags); 1190b411b363SPhilipp Reisner if (bm_is_locked(b)) 1191b411b363SPhilipp Reisner bm_print_lock_info(mdev); 1192b411b363SPhilipp Reisner if (bitnr < b->bm_bits) { 1193b411b363SPhilipp Reisner unsigned long offset = bitnr>>LN2_BPL; 1194b411b363SPhilipp Reisner p_addr = bm_map_paddr(b, offset); 1195b411b363SPhilipp Reisner i = test_bit(bitnr & BPP_MASK, p_addr) ? 1 : 0; 1196b411b363SPhilipp Reisner bm_unmap(p_addr); 1197b411b363SPhilipp Reisner } else if (bitnr == b->bm_bits) { 1198b411b363SPhilipp Reisner i = -1; 1199b411b363SPhilipp Reisner } else { /* (bitnr > b->bm_bits) */ 1200b411b363SPhilipp Reisner dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits); 1201b411b363SPhilipp Reisner i = 0; 1202b411b363SPhilipp Reisner } 1203b411b363SPhilipp Reisner 1204b411b363SPhilipp Reisner spin_unlock_irqrestore(&b->bm_lock, flags); 1205b411b363SPhilipp Reisner return i; 1206b411b363SPhilipp Reisner } 1207b411b363SPhilipp Reisner 1208b411b363SPhilipp Reisner /* returns number of bits set in the range [s, e] */ 1209b411b363SPhilipp Reisner int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e) 1210b411b363SPhilipp Reisner { 1211b411b363SPhilipp Reisner unsigned long flags; 1212b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 1213b411b363SPhilipp Reisner unsigned long *p_addr = NULL, page_nr = -1; 1214b411b363SPhilipp Reisner unsigned long bitnr; 1215b411b363SPhilipp Reisner int c = 0; 1216b411b363SPhilipp Reisner size_t w; 1217b411b363SPhilipp Reisner 1218b411b363SPhilipp Reisner /* If this is called without a bitmap, that is a bug. But just to be 1219b411b363SPhilipp Reisner * robust in case we screwed up elsewhere, in that case pretend there 1220b411b363SPhilipp Reisner * was one dirty bit in the requested area, so we won't try to do a 1221b411b363SPhilipp Reisner * local read there (no bitmap probably implies no disk) */ 1222b411b363SPhilipp Reisner ERR_IF(!b) return 1; 1223b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return 1; 1224b411b363SPhilipp Reisner 1225b411b363SPhilipp Reisner spin_lock_irqsave(&b->bm_lock, flags); 1226b411b363SPhilipp Reisner if (bm_is_locked(b)) 1227b411b363SPhilipp Reisner bm_print_lock_info(mdev); 1228b411b363SPhilipp Reisner for (bitnr = s; bitnr <= e; bitnr++) { 1229b411b363SPhilipp Reisner w = bitnr >> LN2_BPL; 1230b411b363SPhilipp Reisner if (page_nr != w >> (PAGE_SHIFT - LN2_BPL + 3)) { 1231b411b363SPhilipp Reisner page_nr = w >> (PAGE_SHIFT - LN2_BPL + 3); 1232b411b363SPhilipp Reisner if (p_addr) 1233b411b363SPhilipp Reisner bm_unmap(p_addr); 1234b411b363SPhilipp Reisner p_addr = bm_map_paddr(b, w); 1235b411b363SPhilipp Reisner } 1236b411b363SPhilipp Reisner ERR_IF (bitnr >= b->bm_bits) { 1237b411b363SPhilipp Reisner dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits); 1238b411b363SPhilipp Reisner } else { 1239b411b363SPhilipp Reisner c += (0 != test_bit(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr)); 1240b411b363SPhilipp Reisner } 1241b411b363SPhilipp Reisner } 1242b411b363SPhilipp Reisner if (p_addr) 1243b411b363SPhilipp Reisner bm_unmap(p_addr); 1244b411b363SPhilipp Reisner spin_unlock_irqrestore(&b->bm_lock, flags); 1245b411b363SPhilipp Reisner return c; 1246b411b363SPhilipp Reisner } 1247b411b363SPhilipp Reisner 1248b411b363SPhilipp Reisner 1249b411b363SPhilipp Reisner /* inherently racy... 1250b411b363SPhilipp Reisner * return value may be already out-of-date when this function returns. 1251b411b363SPhilipp Reisner * but the general usage is that this is only use during a cstate when bits are 1252b411b363SPhilipp Reisner * only cleared, not set, and typically only care for the case when the return 1253b411b363SPhilipp Reisner * value is zero, or we already "locked" this "bitmap extent" by other means. 1254b411b363SPhilipp Reisner * 1255b411b363SPhilipp Reisner * enr is bm-extent number, since we chose to name one sector (512 bytes) 1256b411b363SPhilipp Reisner * worth of the bitmap a "bitmap extent". 1257b411b363SPhilipp Reisner * 1258b411b363SPhilipp Reisner * TODO 1259b411b363SPhilipp Reisner * I think since we use it like a reference count, we should use the real 1260b411b363SPhilipp Reisner * reference count of some bitmap extent element from some lru instead... 1261b411b363SPhilipp Reisner * 1262b411b363SPhilipp Reisner */ 1263b411b363SPhilipp Reisner int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr) 1264b411b363SPhilipp Reisner { 1265b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 1266b411b363SPhilipp Reisner int count, s, e; 1267b411b363SPhilipp Reisner unsigned long flags; 1268b411b363SPhilipp Reisner unsigned long *p_addr, *bm; 1269b411b363SPhilipp Reisner 1270b411b363SPhilipp Reisner ERR_IF(!b) return 0; 1271b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return 0; 1272b411b363SPhilipp Reisner 1273b411b363SPhilipp Reisner spin_lock_irqsave(&b->bm_lock, flags); 1274b411b363SPhilipp Reisner if (bm_is_locked(b)) 1275b411b363SPhilipp Reisner bm_print_lock_info(mdev); 1276b411b363SPhilipp Reisner 1277b411b363SPhilipp Reisner s = S2W(enr); 1278b411b363SPhilipp Reisner e = min((size_t)S2W(enr+1), b->bm_words); 1279b411b363SPhilipp Reisner count = 0; 1280b411b363SPhilipp Reisner if (s < b->bm_words) { 1281b411b363SPhilipp Reisner int n = e-s; 1282b411b363SPhilipp Reisner p_addr = bm_map_paddr(b, s); 1283b411b363SPhilipp Reisner bm = p_addr + MLPP(s); 1284b411b363SPhilipp Reisner while (n--) 1285b411b363SPhilipp Reisner count += hweight_long(*bm++); 1286b411b363SPhilipp Reisner bm_unmap(p_addr); 1287b411b363SPhilipp Reisner } else { 1288b411b363SPhilipp Reisner dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s); 1289b411b363SPhilipp Reisner } 1290b411b363SPhilipp Reisner spin_unlock_irqrestore(&b->bm_lock, flags); 1291b411b363SPhilipp Reisner return count; 1292b411b363SPhilipp Reisner } 1293b411b363SPhilipp Reisner 1294b411b363SPhilipp Reisner /* set all bits covered by the AL-extent al_enr */ 1295b411b363SPhilipp Reisner unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr) 1296b411b363SPhilipp Reisner { 1297b411b363SPhilipp Reisner struct drbd_bitmap *b = mdev->bitmap; 1298b411b363SPhilipp Reisner unsigned long *p_addr, *bm; 1299b411b363SPhilipp Reisner unsigned long weight; 1300b411b363SPhilipp Reisner int count, s, e, i, do_now; 1301b411b363SPhilipp Reisner ERR_IF(!b) return 0; 1302b411b363SPhilipp Reisner ERR_IF(!b->bm_pages) return 0; 1303b411b363SPhilipp Reisner 1304b411b363SPhilipp Reisner spin_lock_irq(&b->bm_lock); 1305b411b363SPhilipp Reisner if (bm_is_locked(b)) 1306b411b363SPhilipp Reisner bm_print_lock_info(mdev); 1307b411b363SPhilipp Reisner weight = b->bm_set; 1308b411b363SPhilipp Reisner 1309b411b363SPhilipp Reisner s = al_enr * BM_WORDS_PER_AL_EXT; 1310b411b363SPhilipp Reisner e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words); 1311b411b363SPhilipp Reisner /* assert that s and e are on the same page */ 1312b411b363SPhilipp Reisner D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3) 1313b411b363SPhilipp Reisner == s >> (PAGE_SHIFT - LN2_BPL + 3)); 1314b411b363SPhilipp Reisner count = 0; 1315b411b363SPhilipp Reisner if (s < b->bm_words) { 1316b411b363SPhilipp Reisner i = do_now = e-s; 1317b411b363SPhilipp Reisner p_addr = bm_map_paddr(b, s); 1318b411b363SPhilipp Reisner bm = p_addr + MLPP(s); 1319b411b363SPhilipp Reisner while (i--) { 1320b411b363SPhilipp Reisner count += hweight_long(*bm); 1321b411b363SPhilipp Reisner *bm = -1UL; 1322b411b363SPhilipp Reisner bm++; 1323b411b363SPhilipp Reisner } 1324b411b363SPhilipp Reisner bm_unmap(p_addr); 1325b411b363SPhilipp Reisner b->bm_set += do_now*BITS_PER_LONG - count; 1326b411b363SPhilipp Reisner if (e == b->bm_words) 1327b411b363SPhilipp Reisner b->bm_set -= bm_clear_surplus(b); 1328b411b363SPhilipp Reisner } else { 1329b411b363SPhilipp Reisner dev_err(DEV, "start offset (%d) too large in drbd_bm_ALe_set_all\n", s); 1330b411b363SPhilipp Reisner } 1331b411b363SPhilipp Reisner weight = b->bm_set - weight; 1332b411b363SPhilipp Reisner spin_unlock_irq(&b->bm_lock); 1333b411b363SPhilipp Reisner return weight; 1334b411b363SPhilipp Reisner } 1335