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