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