1 /* 2 drbd_bitmap.c 3 4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg. 5 6 Copyright (C) 2004-2008, LINBIT Information Technologies GmbH. 7 Copyright (C) 2004-2008, Philipp Reisner <philipp.reisner@linbit.com>. 8 Copyright (C) 2004-2008, Lars Ellenberg <lars.ellenberg@linbit.com>. 9 10 drbd is free software; you can redistribute it and/or modify 11 it under the terms of the GNU General Public License as published by 12 the Free Software Foundation; either version 2, or (at your option) 13 any later version. 14 15 drbd is distributed in the hope that it will be useful, 16 but WITHOUT ANY WARRANTY; without even the implied warranty of 17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 GNU General Public License for more details. 19 20 You should have received a copy of the GNU General Public License 21 along with drbd; see the file COPYING. If not, write to 22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 23 */ 24 25 #include <linux/bitops.h> 26 #include <linux/vmalloc.h> 27 #include <linux/string.h> 28 #include <linux/drbd.h> 29 #include <linux/slab.h> 30 #include <asm/kmap_types.h> 31 32 #include "drbd_int.h" 33 34 35 /* OPAQUE outside this file! 36 * interface defined in drbd_int.h 37 38 * convention: 39 * function name drbd_bm_... => used elsewhere, "public". 40 * function name bm_... => internal to implementation, "private". 41 */ 42 43 44 /* 45 * LIMITATIONS: 46 * We want to support >= peta byte of backend storage, while for now still using 47 * a granularity of one bit per 4KiB of storage. 48 * 1 << 50 bytes backend storage (1 PiB) 49 * 1 << (50 - 12) bits needed 50 * 38 --> we need u64 to index and count bits 51 * 1 << (38 - 3) bitmap bytes needed 52 * 35 --> we still need u64 to index and count bytes 53 * (that's 32 GiB of bitmap for 1 PiB storage) 54 * 1 << (35 - 2) 32bit longs needed 55 * 33 --> we'd even need u64 to index and count 32bit long words. 56 * 1 << (35 - 3) 64bit longs needed 57 * 32 --> we could get away with a 32bit unsigned int to index and count 58 * 64bit long words, but I rather stay with unsigned long for now. 59 * We probably should neither count nor point to bytes or long words 60 * directly, but either by bitnumber, or by page index and offset. 61 * 1 << (35 - 12) 62 * 22 --> we need that much 4KiB pages of bitmap. 63 * 1 << (22 + 3) --> on a 64bit arch, 64 * we need 32 MiB to store the array of page pointers. 65 * 66 * Because I'm lazy, and because the resulting patch was too large, too ugly 67 * and still incomplete, on 32bit we still "only" support 16 TiB (minus some), 68 * (1 << 32) bits * 4k storage. 69 * 70 71 * bitmap storage and IO: 72 * Bitmap is stored little endian on disk, and is kept little endian in 73 * core memory. Currently we still hold the full bitmap in core as long 74 * as we are "attached" to a local disk, which at 32 GiB for 1PiB storage 75 * seems excessive. 76 * 77 * We plan to reduce the amount of in-core bitmap pages by paging them in 78 * and out against their on-disk location as necessary, but need to make 79 * sure we don't cause too much meta data IO, and must not deadlock in 80 * tight memory situations. This needs some more work. 81 */ 82 83 /* 84 * NOTE 85 * Access to the *bm_pages is protected by bm_lock. 86 * It is safe to read the other members within the lock. 87 * 88 * drbd_bm_set_bits is called from bio_endio callbacks, 89 * We may be called with irq already disabled, 90 * so we need spin_lock_irqsave(). 91 * And we need the kmap_atomic. 92 */ 93 struct drbd_bitmap { 94 struct page **bm_pages; 95 spinlock_t bm_lock; 96 97 /* see LIMITATIONS: above */ 98 99 unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */ 100 unsigned long bm_bits; 101 size_t bm_words; 102 size_t bm_number_of_pages; 103 sector_t bm_dev_capacity; 104 struct mutex bm_change; /* serializes resize operations */ 105 106 wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */ 107 108 enum bm_flag bm_flags; 109 110 /* debugging aid, in case we are still racy somewhere */ 111 char *bm_why; 112 struct task_struct *bm_task; 113 }; 114 115 static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s, 116 unsigned long e, int val, const enum km_type km); 117 118 #define bm_print_lock_info(m) __bm_print_lock_info(m, __func__) 119 static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func) 120 { 121 struct drbd_bitmap *b = mdev->bitmap; 122 if (!__ratelimit(&drbd_ratelimit_state)) 123 return; 124 dev_err(DEV, "FIXME %s in %s, bitmap locked for '%s' by %s\n", 125 current == mdev->receiver.task ? "receiver" : 126 current == mdev->asender.task ? "asender" : 127 current == mdev->worker.task ? "worker" : current->comm, 128 func, b->bm_why ?: "?", 129 b->bm_task == mdev->receiver.task ? "receiver" : 130 b->bm_task == mdev->asender.task ? "asender" : 131 b->bm_task == mdev->worker.task ? "worker" : "?"); 132 } 133 134 void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags) 135 { 136 struct drbd_bitmap *b = mdev->bitmap; 137 int trylock_failed; 138 139 if (!b) { 140 dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n"); 141 return; 142 } 143 144 trylock_failed = !mutex_trylock(&b->bm_change); 145 146 if (trylock_failed) { 147 dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n", 148 current == mdev->receiver.task ? "receiver" : 149 current == mdev->asender.task ? "asender" : 150 current == mdev->worker.task ? "worker" : current->comm, 151 why, b->bm_why ?: "?", 152 b->bm_task == mdev->receiver.task ? "receiver" : 153 b->bm_task == mdev->asender.task ? "asender" : 154 b->bm_task == mdev->worker.task ? "worker" : "?"); 155 mutex_lock(&b->bm_change); 156 } 157 if (BM_LOCKED_MASK & b->bm_flags) 158 dev_err(DEV, "FIXME bitmap already locked in bm_lock\n"); 159 b->bm_flags |= flags & BM_LOCKED_MASK; 160 161 b->bm_why = why; 162 b->bm_task = current; 163 } 164 165 void drbd_bm_unlock(struct drbd_conf *mdev) 166 { 167 struct drbd_bitmap *b = mdev->bitmap; 168 if (!b) { 169 dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n"); 170 return; 171 } 172 173 if (!(BM_LOCKED_MASK & mdev->bitmap->bm_flags)) 174 dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n"); 175 176 b->bm_flags &= ~BM_LOCKED_MASK; 177 b->bm_why = NULL; 178 b->bm_task = NULL; 179 mutex_unlock(&b->bm_change); 180 } 181 182 /* we store some "meta" info about our pages in page->private */ 183 /* at a granularity of 4k storage per bitmap bit: 184 * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks 185 * 1<<38 bits, 186 * 1<<23 4k bitmap pages. 187 * Use 24 bits as page index, covers 2 peta byte storage 188 * at a granularity of 4k per bit. 189 * Used to report the failed page idx on io error from the endio handlers. 190 */ 191 #define BM_PAGE_IDX_MASK ((1UL<<24)-1) 192 /* this page is currently read in, or written back */ 193 #define BM_PAGE_IO_LOCK 31 194 /* if there has been an IO error for this page */ 195 #define BM_PAGE_IO_ERROR 30 196 /* this is to be able to intelligently skip disk IO, 197 * set if bits have been set since last IO. */ 198 #define BM_PAGE_NEED_WRITEOUT 29 199 /* to mark for lazy writeout once syncer cleared all clearable bits, 200 * we if bits have been cleared since last IO. */ 201 #define BM_PAGE_LAZY_WRITEOUT 28 202 203 /* store_page_idx uses non-atomic assignment. It is only used directly after 204 * allocating the page. All other bm_set_page_* and bm_clear_page_* need to 205 * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap 206 * changes) may happen from various contexts, and wait_on_bit/wake_up_bit 207 * requires it all to be atomic as well. */ 208 static void bm_store_page_idx(struct page *page, unsigned long idx) 209 { 210 BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK)); 211 page_private(page) |= idx; 212 } 213 214 static unsigned long bm_page_to_idx(struct page *page) 215 { 216 return page_private(page) & BM_PAGE_IDX_MASK; 217 } 218 219 /* As is very unlikely that the same page is under IO from more than one 220 * context, we can get away with a bit per page and one wait queue per bitmap. 221 */ 222 static void bm_page_lock_io(struct drbd_conf *mdev, int page_nr) 223 { 224 struct drbd_bitmap *b = mdev->bitmap; 225 void *addr = &page_private(b->bm_pages[page_nr]); 226 wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr)); 227 } 228 229 static void bm_page_unlock_io(struct drbd_conf *mdev, int page_nr) 230 { 231 struct drbd_bitmap *b = mdev->bitmap; 232 void *addr = &page_private(b->bm_pages[page_nr]); 233 clear_bit(BM_PAGE_IO_LOCK, addr); 234 smp_mb__after_clear_bit(); 235 wake_up(&mdev->bitmap->bm_io_wait); 236 } 237 238 /* set _before_ submit_io, so it may be reset due to being changed 239 * while this page is in flight... will get submitted later again */ 240 static void bm_set_page_unchanged(struct page *page) 241 { 242 /* use cmpxchg? */ 243 clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page)); 244 clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page)); 245 } 246 247 static void bm_set_page_need_writeout(struct page *page) 248 { 249 set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page)); 250 } 251 252 static int bm_test_page_unchanged(struct page *page) 253 { 254 volatile const unsigned long *addr = &page_private(page); 255 return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0; 256 } 257 258 static void bm_set_page_io_err(struct page *page) 259 { 260 set_bit(BM_PAGE_IO_ERROR, &page_private(page)); 261 } 262 263 static void bm_clear_page_io_err(struct page *page) 264 { 265 clear_bit(BM_PAGE_IO_ERROR, &page_private(page)); 266 } 267 268 static void bm_set_page_lazy_writeout(struct page *page) 269 { 270 set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page)); 271 } 272 273 static int bm_test_page_lazy_writeout(struct page *page) 274 { 275 return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page)); 276 } 277 278 /* on a 32bit box, this would allow for exactly (2<<38) bits. */ 279 static unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr) 280 { 281 /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */ 282 unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3); 283 BUG_ON(page_nr >= b->bm_number_of_pages); 284 return page_nr; 285 } 286 287 static unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr) 288 { 289 /* page_nr = (bitnr/8) >> PAGE_SHIFT; */ 290 unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3); 291 BUG_ON(page_nr >= b->bm_number_of_pages); 292 return page_nr; 293 } 294 295 static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx, const enum km_type km) 296 { 297 struct page *page = b->bm_pages[idx]; 298 return (unsigned long *) kmap_atomic(page, km); 299 } 300 301 static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx) 302 { 303 return __bm_map_pidx(b, idx, KM_IRQ1); 304 } 305 306 static void __bm_unmap(unsigned long *p_addr, const enum km_type km) 307 { 308 kunmap_atomic(p_addr, km); 309 }; 310 311 static void bm_unmap(unsigned long *p_addr) 312 { 313 return __bm_unmap(p_addr, KM_IRQ1); 314 } 315 316 /* long word offset of _bitmap_ sector */ 317 #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL)) 318 /* word offset from start of bitmap to word number _in_page_ 319 * modulo longs per page 320 #define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long)) 321 hm, well, Philipp thinks gcc might not optimize the % into & (... - 1) 322 so do it explicitly: 323 */ 324 #define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1)) 325 326 /* Long words per page */ 327 #define LWPP (PAGE_SIZE/sizeof(long)) 328 329 /* 330 * actually most functions herein should take a struct drbd_bitmap*, not a 331 * struct drbd_conf*, but for the debug macros I like to have the mdev around 332 * to be able to report device specific. 333 */ 334 335 336 static void bm_free_pages(struct page **pages, unsigned long number) 337 { 338 unsigned long i; 339 if (!pages) 340 return; 341 342 for (i = 0; i < number; i++) { 343 if (!pages[i]) { 344 printk(KERN_ALERT "drbd: bm_free_pages tried to free " 345 "a NULL pointer; i=%lu n=%lu\n", 346 i, number); 347 continue; 348 } 349 __free_page(pages[i]); 350 pages[i] = NULL; 351 } 352 } 353 354 static void bm_vk_free(void *ptr, int v) 355 { 356 if (v) 357 vfree(ptr); 358 else 359 kfree(ptr); 360 } 361 362 /* 363 * "have" and "want" are NUMBER OF PAGES. 364 */ 365 static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want) 366 { 367 struct page **old_pages = b->bm_pages; 368 struct page **new_pages, *page; 369 unsigned int i, bytes, vmalloced = 0; 370 unsigned long have = b->bm_number_of_pages; 371 372 BUG_ON(have == 0 && old_pages != NULL); 373 BUG_ON(have != 0 && old_pages == NULL); 374 375 if (have == want) 376 return old_pages; 377 378 /* Trying kmalloc first, falling back to vmalloc. 379 * GFP_KERNEL is ok, as this is done when a lower level disk is 380 * "attached" to the drbd. Context is receiver thread or cqueue 381 * thread. As we have no disk yet, we are not in the IO path, 382 * not even the IO path of the peer. */ 383 bytes = sizeof(struct page *)*want; 384 new_pages = kmalloc(bytes, GFP_KERNEL); 385 if (!new_pages) { 386 new_pages = vmalloc(bytes); 387 if (!new_pages) 388 return NULL; 389 vmalloced = 1; 390 } 391 392 memset(new_pages, 0, bytes); 393 if (want >= have) { 394 for (i = 0; i < have; i++) 395 new_pages[i] = old_pages[i]; 396 for (; i < want; i++) { 397 page = alloc_page(GFP_HIGHUSER); 398 if (!page) { 399 bm_free_pages(new_pages + have, i - have); 400 bm_vk_free(new_pages, vmalloced); 401 return NULL; 402 } 403 /* we want to know which page it is 404 * from the endio handlers */ 405 bm_store_page_idx(page, i); 406 new_pages[i] = page; 407 } 408 } else { 409 for (i = 0; i < want; i++) 410 new_pages[i] = old_pages[i]; 411 /* NOT HERE, we are outside the spinlock! 412 bm_free_pages(old_pages + want, have - want); 413 */ 414 } 415 416 if (vmalloced) 417 b->bm_flags |= BM_P_VMALLOCED; 418 else 419 b->bm_flags &= ~BM_P_VMALLOCED; 420 421 return new_pages; 422 } 423 424 /* 425 * called on driver init only. TODO call when a device is created. 426 * allocates the drbd_bitmap, and stores it in mdev->bitmap. 427 */ 428 int drbd_bm_init(struct drbd_conf *mdev) 429 { 430 struct drbd_bitmap *b = mdev->bitmap; 431 WARN_ON(b != NULL); 432 b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL); 433 if (!b) 434 return -ENOMEM; 435 spin_lock_init(&b->bm_lock); 436 mutex_init(&b->bm_change); 437 init_waitqueue_head(&b->bm_io_wait); 438 439 mdev->bitmap = b; 440 441 return 0; 442 } 443 444 sector_t drbd_bm_capacity(struct drbd_conf *mdev) 445 { 446 ERR_IF(!mdev->bitmap) return 0; 447 return mdev->bitmap->bm_dev_capacity; 448 } 449 450 /* called on driver unload. TODO: call when a device is destroyed. 451 */ 452 void drbd_bm_cleanup(struct drbd_conf *mdev) 453 { 454 ERR_IF (!mdev->bitmap) return; 455 bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages); 456 bm_vk_free(mdev->bitmap->bm_pages, (BM_P_VMALLOCED & mdev->bitmap->bm_flags)); 457 kfree(mdev->bitmap); 458 mdev->bitmap = NULL; 459 } 460 461 /* 462 * since (b->bm_bits % BITS_PER_LONG) != 0, 463 * this masks out the remaining bits. 464 * Returns the number of bits cleared. 465 */ 466 #define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3)) 467 #define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1) 468 #define BITS_PER_LONG_MASK (BITS_PER_LONG - 1) 469 static int bm_clear_surplus(struct drbd_bitmap *b) 470 { 471 unsigned long mask; 472 unsigned long *p_addr, *bm; 473 int tmp; 474 int cleared = 0; 475 476 /* number of bits modulo bits per page */ 477 tmp = (b->bm_bits & BITS_PER_PAGE_MASK); 478 /* mask the used bits of the word containing the last bit */ 479 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1; 480 /* bitmap is always stored little endian, 481 * on disk and in core memory alike */ 482 mask = cpu_to_lel(mask); 483 484 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1); 485 bm = p_addr + (tmp/BITS_PER_LONG); 486 if (mask) { 487 /* If mask != 0, we are not exactly aligned, so bm now points 488 * to the long containing the last bit. 489 * If mask == 0, bm already points to the word immediately 490 * after the last (long word aligned) bit. */ 491 cleared = hweight_long(*bm & ~mask); 492 *bm &= mask; 493 bm++; 494 } 495 496 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) { 497 /* on a 32bit arch, we may need to zero out 498 * a padding long to align with a 64bit remote */ 499 cleared += hweight_long(*bm); 500 *bm = 0; 501 } 502 bm_unmap(p_addr); 503 return cleared; 504 } 505 506 static void bm_set_surplus(struct drbd_bitmap *b) 507 { 508 unsigned long mask; 509 unsigned long *p_addr, *bm; 510 int tmp; 511 512 /* number of bits modulo bits per page */ 513 tmp = (b->bm_bits & BITS_PER_PAGE_MASK); 514 /* mask the used bits of the word containing the last bit */ 515 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1; 516 /* bitmap is always stored little endian, 517 * on disk and in core memory alike */ 518 mask = cpu_to_lel(mask); 519 520 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1); 521 bm = p_addr + (tmp/BITS_PER_LONG); 522 if (mask) { 523 /* If mask != 0, we are not exactly aligned, so bm now points 524 * to the long containing the last bit. 525 * If mask == 0, bm already points to the word immediately 526 * after the last (long word aligned) bit. */ 527 *bm |= ~mask; 528 bm++; 529 } 530 531 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) { 532 /* on a 32bit arch, we may need to zero out 533 * a padding long to align with a 64bit remote */ 534 *bm = ~0UL; 535 } 536 bm_unmap(p_addr); 537 } 538 539 /* you better not modify the bitmap while this is running, 540 * or its results will be stale */ 541 static unsigned long bm_count_bits(struct drbd_bitmap *b) 542 { 543 unsigned long *p_addr; 544 unsigned long bits = 0; 545 unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1; 546 int idx, i, last_word; 547 548 /* all but last page */ 549 for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) { 550 p_addr = __bm_map_pidx(b, idx, KM_USER0); 551 for (i = 0; i < LWPP; i++) 552 bits += hweight_long(p_addr[i]); 553 __bm_unmap(p_addr, KM_USER0); 554 cond_resched(); 555 } 556 /* last (or only) page */ 557 last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL; 558 p_addr = __bm_map_pidx(b, idx, KM_USER0); 559 for (i = 0; i < last_word; i++) 560 bits += hweight_long(p_addr[i]); 561 p_addr[last_word] &= cpu_to_lel(mask); 562 bits += hweight_long(p_addr[last_word]); 563 /* 32bit arch, may have an unused padding long */ 564 if (BITS_PER_LONG == 32 && (last_word & 1) == 0) 565 p_addr[last_word+1] = 0; 566 __bm_unmap(p_addr, KM_USER0); 567 return bits; 568 } 569 570 /* offset and len in long words.*/ 571 static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len) 572 { 573 unsigned long *p_addr, *bm; 574 unsigned int idx; 575 size_t do_now, end; 576 577 end = offset + len; 578 579 if (end > b->bm_words) { 580 printk(KERN_ALERT "drbd: bm_memset end > bm_words\n"); 581 return; 582 } 583 584 while (offset < end) { 585 do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset; 586 idx = bm_word_to_page_idx(b, offset); 587 p_addr = bm_map_pidx(b, idx); 588 bm = p_addr + MLPP(offset); 589 if (bm+do_now > p_addr + LWPP) { 590 printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n", 591 p_addr, bm, (int)do_now); 592 } else 593 memset(bm, c, do_now * sizeof(long)); 594 bm_unmap(p_addr); 595 bm_set_page_need_writeout(b->bm_pages[idx]); 596 offset += do_now; 597 } 598 } 599 600 /* 601 * make sure the bitmap has enough room for the attached storage, 602 * if necessary, resize. 603 * called whenever we may have changed the device size. 604 * returns -ENOMEM if we could not allocate enough memory, 0 on success. 605 * In case this is actually a resize, we copy the old bitmap into the new one. 606 * Otherwise, the bitmap is initialized to all bits set. 607 */ 608 int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits) 609 { 610 struct drbd_bitmap *b = mdev->bitmap; 611 unsigned long bits, words, owords, obits; 612 unsigned long want, have, onpages; /* number of pages */ 613 struct page **npages, **opages = NULL; 614 int err = 0, growing; 615 int opages_vmalloced; 616 617 ERR_IF(!b) return -ENOMEM; 618 619 drbd_bm_lock(mdev, "resize", BM_LOCKED_MASK); 620 621 dev_info(DEV, "drbd_bm_resize called with capacity == %llu\n", 622 (unsigned long long)capacity); 623 624 if (capacity == b->bm_dev_capacity) 625 goto out; 626 627 opages_vmalloced = (BM_P_VMALLOCED & b->bm_flags); 628 629 if (capacity == 0) { 630 spin_lock_irq(&b->bm_lock); 631 opages = b->bm_pages; 632 onpages = b->bm_number_of_pages; 633 owords = b->bm_words; 634 b->bm_pages = NULL; 635 b->bm_number_of_pages = 636 b->bm_set = 637 b->bm_bits = 638 b->bm_words = 639 b->bm_dev_capacity = 0; 640 spin_unlock_irq(&b->bm_lock); 641 bm_free_pages(opages, onpages); 642 bm_vk_free(opages, opages_vmalloced); 643 goto out; 644 } 645 bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT)); 646 647 /* if we would use 648 words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL; 649 a 32bit host could present the wrong number of words 650 to a 64bit host. 651 */ 652 words = ALIGN(bits, 64) >> LN2_BPL; 653 654 if (get_ldev(mdev)) { 655 u64 bits_on_disk = ((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12; 656 put_ldev(mdev); 657 if (bits > bits_on_disk) { 658 dev_info(DEV, "bits = %lu\n", bits); 659 dev_info(DEV, "bits_on_disk = %llu\n", bits_on_disk); 660 err = -ENOSPC; 661 goto out; 662 } 663 } 664 665 want = ALIGN(words*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT; 666 have = b->bm_number_of_pages; 667 if (want == have) { 668 D_ASSERT(b->bm_pages != NULL); 669 npages = b->bm_pages; 670 } else { 671 if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC)) 672 npages = NULL; 673 else 674 npages = bm_realloc_pages(b, want); 675 } 676 677 if (!npages) { 678 err = -ENOMEM; 679 goto out; 680 } 681 682 spin_lock_irq(&b->bm_lock); 683 opages = b->bm_pages; 684 owords = b->bm_words; 685 obits = b->bm_bits; 686 687 growing = bits > obits; 688 if (opages && growing && set_new_bits) 689 bm_set_surplus(b); 690 691 b->bm_pages = npages; 692 b->bm_number_of_pages = want; 693 b->bm_bits = bits; 694 b->bm_words = words; 695 b->bm_dev_capacity = capacity; 696 697 if (growing) { 698 if (set_new_bits) { 699 bm_memset(b, owords, 0xff, words-owords); 700 b->bm_set += bits - obits; 701 } else 702 bm_memset(b, owords, 0x00, words-owords); 703 704 } 705 706 if (want < have) { 707 /* implicit: (opages != NULL) && (opages != npages) */ 708 bm_free_pages(opages + want, have - want); 709 } 710 711 (void)bm_clear_surplus(b); 712 713 spin_unlock_irq(&b->bm_lock); 714 if (opages != npages) 715 bm_vk_free(opages, opages_vmalloced); 716 if (!growing) 717 b->bm_set = bm_count_bits(b); 718 dev_info(DEV, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want); 719 720 out: 721 drbd_bm_unlock(mdev); 722 return err; 723 } 724 725 /* inherently racy: 726 * if not protected by other means, return value may be out of date when 727 * leaving this function... 728 * we still need to lock it, since it is important that this returns 729 * bm_set == 0 precisely. 730 * 731 * maybe bm_set should be atomic_t ? 732 */ 733 unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev) 734 { 735 struct drbd_bitmap *b = mdev->bitmap; 736 unsigned long s; 737 unsigned long flags; 738 739 ERR_IF(!b) return 0; 740 ERR_IF(!b->bm_pages) return 0; 741 742 spin_lock_irqsave(&b->bm_lock, flags); 743 s = b->bm_set; 744 spin_unlock_irqrestore(&b->bm_lock, flags); 745 746 return s; 747 } 748 749 unsigned long drbd_bm_total_weight(struct drbd_conf *mdev) 750 { 751 unsigned long s; 752 /* if I don't have a disk, I don't know about out-of-sync status */ 753 if (!get_ldev_if_state(mdev, D_NEGOTIATING)) 754 return 0; 755 s = _drbd_bm_total_weight(mdev); 756 put_ldev(mdev); 757 return s; 758 } 759 760 size_t drbd_bm_words(struct drbd_conf *mdev) 761 { 762 struct drbd_bitmap *b = mdev->bitmap; 763 ERR_IF(!b) return 0; 764 ERR_IF(!b->bm_pages) return 0; 765 766 return b->bm_words; 767 } 768 769 unsigned long drbd_bm_bits(struct drbd_conf *mdev) 770 { 771 struct drbd_bitmap *b = mdev->bitmap; 772 ERR_IF(!b) return 0; 773 774 return b->bm_bits; 775 } 776 777 /* merge number words from buffer into the bitmap starting at offset. 778 * buffer[i] is expected to be little endian unsigned long. 779 * bitmap must be locked by drbd_bm_lock. 780 * currently only used from receive_bitmap. 781 */ 782 void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number, 783 unsigned long *buffer) 784 { 785 struct drbd_bitmap *b = mdev->bitmap; 786 unsigned long *p_addr, *bm; 787 unsigned long word, bits; 788 unsigned int idx; 789 size_t end, do_now; 790 791 end = offset + number; 792 793 ERR_IF(!b) return; 794 ERR_IF(!b->bm_pages) return; 795 if (number == 0) 796 return; 797 WARN_ON(offset >= b->bm_words); 798 WARN_ON(end > b->bm_words); 799 800 spin_lock_irq(&b->bm_lock); 801 while (offset < end) { 802 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset; 803 idx = bm_word_to_page_idx(b, offset); 804 p_addr = bm_map_pidx(b, idx); 805 bm = p_addr + MLPP(offset); 806 offset += do_now; 807 while (do_now--) { 808 bits = hweight_long(*bm); 809 word = *bm | *buffer++; 810 *bm++ = word; 811 b->bm_set += hweight_long(word) - bits; 812 } 813 bm_unmap(p_addr); 814 bm_set_page_need_writeout(b->bm_pages[idx]); 815 } 816 /* with 32bit <-> 64bit cross-platform connect 817 * this is only correct for current usage, 818 * where we _know_ that we are 64 bit aligned, 819 * and know that this function is used in this way, too... 820 */ 821 if (end == b->bm_words) 822 b->bm_set -= bm_clear_surplus(b); 823 spin_unlock_irq(&b->bm_lock); 824 } 825 826 /* copy number words from the bitmap starting at offset into the buffer. 827 * buffer[i] will be little endian unsigned long. 828 */ 829 void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number, 830 unsigned long *buffer) 831 { 832 struct drbd_bitmap *b = mdev->bitmap; 833 unsigned long *p_addr, *bm; 834 size_t end, do_now; 835 836 end = offset + number; 837 838 ERR_IF(!b) return; 839 ERR_IF(!b->bm_pages) return; 840 841 spin_lock_irq(&b->bm_lock); 842 if ((offset >= b->bm_words) || 843 (end > b->bm_words) || 844 (number <= 0)) 845 dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n", 846 (unsigned long) offset, 847 (unsigned long) number, 848 (unsigned long) b->bm_words); 849 else { 850 while (offset < end) { 851 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset; 852 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset)); 853 bm = p_addr + MLPP(offset); 854 offset += do_now; 855 while (do_now--) 856 *buffer++ = *bm++; 857 bm_unmap(p_addr); 858 } 859 } 860 spin_unlock_irq(&b->bm_lock); 861 } 862 863 /* set all bits in the bitmap */ 864 void drbd_bm_set_all(struct drbd_conf *mdev) 865 { 866 struct drbd_bitmap *b = mdev->bitmap; 867 ERR_IF(!b) return; 868 ERR_IF(!b->bm_pages) return; 869 870 spin_lock_irq(&b->bm_lock); 871 bm_memset(b, 0, 0xff, b->bm_words); 872 (void)bm_clear_surplus(b); 873 b->bm_set = b->bm_bits; 874 spin_unlock_irq(&b->bm_lock); 875 } 876 877 /* clear all bits in the bitmap */ 878 void drbd_bm_clear_all(struct drbd_conf *mdev) 879 { 880 struct drbd_bitmap *b = mdev->bitmap; 881 ERR_IF(!b) return; 882 ERR_IF(!b->bm_pages) return; 883 884 spin_lock_irq(&b->bm_lock); 885 bm_memset(b, 0, 0, b->bm_words); 886 b->bm_set = 0; 887 spin_unlock_irq(&b->bm_lock); 888 } 889 890 struct bm_aio_ctx { 891 struct drbd_conf *mdev; 892 atomic_t in_flight; 893 struct completion done; 894 unsigned flags; 895 #define BM_AIO_COPY_PAGES 1 896 int error; 897 }; 898 899 /* bv_page may be a copy, or may be the original */ 900 static void bm_async_io_complete(struct bio *bio, int error) 901 { 902 struct bm_aio_ctx *ctx = bio->bi_private; 903 struct drbd_conf *mdev = ctx->mdev; 904 struct drbd_bitmap *b = mdev->bitmap; 905 unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page); 906 int uptodate = bio_flagged(bio, BIO_UPTODATE); 907 908 909 /* strange behavior of some lower level drivers... 910 * fail the request by clearing the uptodate flag, 911 * but do not return any error?! 912 * do we want to WARN() on this? */ 913 if (!error && !uptodate) 914 error = -EIO; 915 916 if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 && 917 !bm_test_page_unchanged(b->bm_pages[idx])) 918 dev_warn(DEV, "bitmap page idx %u changed during IO!\n", idx); 919 920 if (error) { 921 /* ctx error will hold the completed-last non-zero error code, 922 * in case error codes differ. */ 923 ctx->error = error; 924 bm_set_page_io_err(b->bm_pages[idx]); 925 /* Not identical to on disk version of it. 926 * Is BM_PAGE_IO_ERROR enough? */ 927 if (__ratelimit(&drbd_ratelimit_state)) 928 dev_err(DEV, "IO ERROR %d on bitmap page idx %u\n", 929 error, idx); 930 } else { 931 bm_clear_page_io_err(b->bm_pages[idx]); 932 dynamic_dev_dbg(DEV, "bitmap page idx %u completed\n", idx); 933 } 934 935 bm_page_unlock_io(mdev, idx); 936 937 /* FIXME give back to page pool */ 938 if (ctx->flags & BM_AIO_COPY_PAGES) 939 put_page(bio->bi_io_vec[0].bv_page); 940 941 bio_put(bio); 942 943 if (atomic_dec_and_test(&ctx->in_flight)) 944 complete(&ctx->done); 945 } 946 947 static void bm_page_io_async(struct bm_aio_ctx *ctx, int page_nr, int rw) __must_hold(local) 948 { 949 /* we are process context. we always get a bio */ 950 struct bio *bio = bio_alloc(GFP_KERNEL, 1); 951 struct drbd_conf *mdev = ctx->mdev; 952 struct drbd_bitmap *b = mdev->bitmap; 953 struct page *page; 954 unsigned int len; 955 956 sector_t on_disk_sector = 957 mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset; 958 on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9); 959 960 /* this might happen with very small 961 * flexible external meta data device, 962 * or with PAGE_SIZE > 4k */ 963 len = min_t(unsigned int, PAGE_SIZE, 964 (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9); 965 966 /* serialize IO on this page */ 967 bm_page_lock_io(mdev, page_nr); 968 /* before memcpy and submit, 969 * so it can be redirtied any time */ 970 bm_set_page_unchanged(b->bm_pages[page_nr]); 971 972 if (ctx->flags & BM_AIO_COPY_PAGES) { 973 /* FIXME alloc_page is good enough for now, but actually needs 974 * to use pre-allocated page pool */ 975 void *src, *dest; 976 page = alloc_page(__GFP_HIGHMEM|__GFP_WAIT); 977 dest = kmap_atomic(page, KM_USER0); 978 src = kmap_atomic(b->bm_pages[page_nr], KM_USER1); 979 memcpy(dest, src, PAGE_SIZE); 980 kunmap_atomic(src, KM_USER1); 981 kunmap_atomic(dest, KM_USER0); 982 bm_store_page_idx(page, page_nr); 983 } else 984 page = b->bm_pages[page_nr]; 985 986 bio->bi_bdev = mdev->ldev->md_bdev; 987 bio->bi_sector = on_disk_sector; 988 bio_add_page(bio, page, len, 0); 989 bio->bi_private = ctx; 990 bio->bi_end_io = bm_async_io_complete; 991 992 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) { 993 bio->bi_rw |= rw; 994 bio_endio(bio, -EIO); 995 } else { 996 submit_bio(rw, bio); 997 } 998 } 999 1000 /* 1001 * bm_rw: read/write the whole bitmap from/to its on disk location. 1002 */ 1003 static int bm_rw(struct drbd_conf *mdev, int rw, unsigned lazy_writeout_upper_idx) __must_hold(local) 1004 { 1005 struct bm_aio_ctx ctx = { 1006 .mdev = mdev, 1007 .in_flight = ATOMIC_INIT(1), 1008 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done), 1009 .flags = lazy_writeout_upper_idx ? BM_AIO_COPY_PAGES : 0, 1010 }; 1011 struct drbd_bitmap *b = mdev->bitmap; 1012 int num_pages, i, count = 0; 1013 unsigned long now; 1014 char ppb[10]; 1015 int err = 0; 1016 1017 /* 1018 * We are protected against bitmap disappearing/resizing by holding an 1019 * ldev reference (caller must have called get_ldev()). 1020 * For read/write, we are protected against changes to the bitmap by 1021 * the bitmap lock (see drbd_bitmap_io). 1022 * For lazy writeout, we don't care for ongoing changes to the bitmap, 1023 * as we submit copies of pages anyways. 1024 */ 1025 if (!ctx.flags) 1026 WARN_ON(!(BM_LOCKED_MASK & b->bm_flags)); 1027 1028 num_pages = b->bm_number_of_pages; 1029 1030 now = jiffies; 1031 1032 /* let the layers below us try to merge these bios... */ 1033 for (i = 0; i < num_pages; i++) { 1034 /* ignore completely unchanged pages */ 1035 if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx) 1036 break; 1037 if (rw & WRITE) { 1038 if (bm_test_page_unchanged(b->bm_pages[i])) { 1039 dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i); 1040 continue; 1041 } 1042 /* during lazy writeout, 1043 * ignore those pages not marked for lazy writeout. */ 1044 if (lazy_writeout_upper_idx && 1045 !bm_test_page_lazy_writeout(b->bm_pages[i])) { 1046 dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i); 1047 continue; 1048 } 1049 } 1050 atomic_inc(&ctx.in_flight); 1051 bm_page_io_async(&ctx, i, rw); 1052 ++count; 1053 cond_resched(); 1054 } 1055 1056 /* 1057 * We initialize ctx.in_flight to one to make sure bm_async_io_complete 1058 * will not complete() early, and decrement / test it here. If there 1059 * are still some bios in flight, we need to wait for them here. 1060 */ 1061 if (!atomic_dec_and_test(&ctx.in_flight)) 1062 wait_for_completion(&ctx.done); 1063 dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n", 1064 rw == WRITE ? "WRITE" : "READ", 1065 count, jiffies - now); 1066 1067 if (ctx.error) { 1068 dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n"); 1069 drbd_chk_io_error(mdev, 1, true); 1070 err = -EIO; /* ctx.error ? */ 1071 } 1072 1073 now = jiffies; 1074 if (rw == WRITE) { 1075 drbd_md_flush(mdev); 1076 } else /* rw == READ */ { 1077 b->bm_set = bm_count_bits(b); 1078 dev_info(DEV, "recounting of set bits took additional %lu jiffies\n", 1079 jiffies - now); 1080 } 1081 now = b->bm_set; 1082 1083 dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n", 1084 ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now); 1085 1086 return err; 1087 } 1088 1089 /** 1090 * drbd_bm_read() - Read the whole bitmap from its on disk location. 1091 * @mdev: DRBD device. 1092 */ 1093 int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local) 1094 { 1095 return bm_rw(mdev, READ, 0); 1096 } 1097 1098 /** 1099 * drbd_bm_write() - Write the whole bitmap to its on disk location. 1100 * @mdev: DRBD device. 1101 * 1102 * Will only write pages that have changed since last IO. 1103 */ 1104 int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local) 1105 { 1106 return bm_rw(mdev, WRITE, 0); 1107 } 1108 1109 /** 1110 * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed. 1111 * @mdev: DRBD device. 1112 * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages 1113 */ 1114 int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local) 1115 { 1116 return bm_rw(mdev, WRITE, upper_idx); 1117 } 1118 1119 1120 /** 1121 * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap 1122 * @mdev: DRBD device. 1123 * @idx: bitmap page index 1124 * 1125 * We don't want to special case on logical_block_size of the backend device, 1126 * so we submit PAGE_SIZE aligned pieces. 1127 * Note that on "most" systems, PAGE_SIZE is 4k. 1128 * 1129 * In case this becomes an issue on systems with larger PAGE_SIZE, 1130 * we may want to change this again to write 4k aligned 4k pieces. 1131 */ 1132 int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local) 1133 { 1134 struct bm_aio_ctx ctx = { 1135 .mdev = mdev, 1136 .in_flight = ATOMIC_INIT(1), 1137 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done), 1138 .flags = BM_AIO_COPY_PAGES, 1139 }; 1140 1141 if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) { 1142 dynamic_dev_dbg(DEV, "skipped bm page write for idx %u\n", idx); 1143 return 0; 1144 } 1145 1146 bm_page_io_async(&ctx, idx, WRITE_SYNC); 1147 wait_for_completion(&ctx.done); 1148 1149 if (ctx.error) 1150 drbd_chk_io_error(mdev, 1, true); 1151 /* that should force detach, so the in memory bitmap will be 1152 * gone in a moment as well. */ 1153 1154 mdev->bm_writ_cnt++; 1155 return ctx.error; 1156 } 1157 1158 /* NOTE 1159 * find_first_bit returns int, we return unsigned long. 1160 * For this to work on 32bit arch with bitnumbers > (1<<32), 1161 * we'd need to return u64, and get a whole lot of other places 1162 * fixed where we still use unsigned long. 1163 * 1164 * this returns a bit number, NOT a sector! 1165 */ 1166 static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo, 1167 const int find_zero_bit, const enum km_type km) 1168 { 1169 struct drbd_bitmap *b = mdev->bitmap; 1170 unsigned long *p_addr; 1171 unsigned long bit_offset; 1172 unsigned i; 1173 1174 1175 if (bm_fo > b->bm_bits) { 1176 dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits); 1177 bm_fo = DRBD_END_OF_BITMAP; 1178 } else { 1179 while (bm_fo < b->bm_bits) { 1180 /* bit offset of the first bit in the page */ 1181 bit_offset = bm_fo & ~BITS_PER_PAGE_MASK; 1182 p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km); 1183 1184 if (find_zero_bit) 1185 i = find_next_zero_bit_le(p_addr, 1186 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK); 1187 else 1188 i = find_next_bit_le(p_addr, 1189 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK); 1190 1191 __bm_unmap(p_addr, km); 1192 if (i < PAGE_SIZE*8) { 1193 bm_fo = bit_offset + i; 1194 if (bm_fo >= b->bm_bits) 1195 break; 1196 goto found; 1197 } 1198 bm_fo = bit_offset + PAGE_SIZE*8; 1199 } 1200 bm_fo = DRBD_END_OF_BITMAP; 1201 } 1202 found: 1203 return bm_fo; 1204 } 1205 1206 static unsigned long bm_find_next(struct drbd_conf *mdev, 1207 unsigned long bm_fo, const int find_zero_bit) 1208 { 1209 struct drbd_bitmap *b = mdev->bitmap; 1210 unsigned long i = DRBD_END_OF_BITMAP; 1211 1212 ERR_IF(!b) return i; 1213 ERR_IF(!b->bm_pages) return i; 1214 1215 spin_lock_irq(&b->bm_lock); 1216 if (BM_DONT_TEST & b->bm_flags) 1217 bm_print_lock_info(mdev); 1218 1219 i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1); 1220 1221 spin_unlock_irq(&b->bm_lock); 1222 return i; 1223 } 1224 1225 unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo) 1226 { 1227 return bm_find_next(mdev, bm_fo, 0); 1228 } 1229 1230 #if 0 1231 /* not yet needed for anything. */ 1232 unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo) 1233 { 1234 return bm_find_next(mdev, bm_fo, 1); 1235 } 1236 #endif 1237 1238 /* does not spin_lock_irqsave. 1239 * you must take drbd_bm_lock() first */ 1240 unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo) 1241 { 1242 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */ 1243 return __bm_find_next(mdev, bm_fo, 0, KM_USER1); 1244 } 1245 1246 unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo) 1247 { 1248 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */ 1249 return __bm_find_next(mdev, bm_fo, 1, KM_USER1); 1250 } 1251 1252 /* returns number of bits actually changed. 1253 * for val != 0, we change 0 -> 1, return code positive 1254 * for val == 0, we change 1 -> 0, return code negative 1255 * wants bitnr, not sector. 1256 * expected to be called for only a few bits (e - s about BITS_PER_LONG). 1257 * Must hold bitmap lock already. */ 1258 static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s, 1259 unsigned long e, int val, const enum km_type km) 1260 { 1261 struct drbd_bitmap *b = mdev->bitmap; 1262 unsigned long *p_addr = NULL; 1263 unsigned long bitnr; 1264 unsigned int last_page_nr = -1U; 1265 int c = 0; 1266 int changed_total = 0; 1267 1268 if (e >= b->bm_bits) { 1269 dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n", 1270 s, e, b->bm_bits); 1271 e = b->bm_bits ? b->bm_bits -1 : 0; 1272 } 1273 for (bitnr = s; bitnr <= e; bitnr++) { 1274 unsigned int page_nr = bm_bit_to_page_idx(b, bitnr); 1275 if (page_nr != last_page_nr) { 1276 if (p_addr) 1277 __bm_unmap(p_addr, km); 1278 if (c < 0) 1279 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]); 1280 else if (c > 0) 1281 bm_set_page_need_writeout(b->bm_pages[last_page_nr]); 1282 changed_total += c; 1283 c = 0; 1284 p_addr = __bm_map_pidx(b, page_nr, km); 1285 last_page_nr = page_nr; 1286 } 1287 if (val) 1288 c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr)); 1289 else 1290 c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr)); 1291 } 1292 if (p_addr) 1293 __bm_unmap(p_addr, km); 1294 if (c < 0) 1295 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]); 1296 else if (c > 0) 1297 bm_set_page_need_writeout(b->bm_pages[last_page_nr]); 1298 changed_total += c; 1299 b->bm_set += changed_total; 1300 return changed_total; 1301 } 1302 1303 /* returns number of bits actually changed. 1304 * for val != 0, we change 0 -> 1, return code positive 1305 * for val == 0, we change 1 -> 0, return code negative 1306 * wants bitnr, not sector */ 1307 static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s, 1308 const unsigned long e, int val) 1309 { 1310 unsigned long flags; 1311 struct drbd_bitmap *b = mdev->bitmap; 1312 int c = 0; 1313 1314 ERR_IF(!b) return 1; 1315 ERR_IF(!b->bm_pages) return 0; 1316 1317 spin_lock_irqsave(&b->bm_lock, flags); 1318 if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags) 1319 bm_print_lock_info(mdev); 1320 1321 c = __bm_change_bits_to(mdev, s, e, val, KM_IRQ1); 1322 1323 spin_unlock_irqrestore(&b->bm_lock, flags); 1324 return c; 1325 } 1326 1327 /* returns number of bits changed 0 -> 1 */ 1328 int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e) 1329 { 1330 return bm_change_bits_to(mdev, s, e, 1); 1331 } 1332 1333 /* returns number of bits changed 1 -> 0 */ 1334 int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e) 1335 { 1336 return -bm_change_bits_to(mdev, s, e, 0); 1337 } 1338 1339 /* sets all bits in full words, 1340 * from first_word up to, but not including, last_word */ 1341 static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b, 1342 int page_nr, int first_word, int last_word) 1343 { 1344 int i; 1345 int bits; 1346 unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_USER0); 1347 for (i = first_word; i < last_word; i++) { 1348 bits = hweight_long(paddr[i]); 1349 paddr[i] = ~0UL; 1350 b->bm_set += BITS_PER_LONG - bits; 1351 } 1352 kunmap_atomic(paddr, KM_USER0); 1353 } 1354 1355 /* Same thing as drbd_bm_set_bits, but without taking the spin_lock_irqsave. 1356 * You must first drbd_bm_lock(). 1357 * Can be called to set the whole bitmap in one go. 1358 * Sets bits from s to e _inclusive_. */ 1359 void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e) 1360 { 1361 /* First set_bit from the first bit (s) 1362 * up to the next long boundary (sl), 1363 * then assign full words up to the last long boundary (el), 1364 * then set_bit up to and including the last bit (e). 1365 * 1366 * Do not use memset, because we must account for changes, 1367 * so we need to loop over the words with hweight() anyways. 1368 */ 1369 unsigned long sl = ALIGN(s,BITS_PER_LONG); 1370 unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1); 1371 int first_page; 1372 int last_page; 1373 int page_nr; 1374 int first_word; 1375 int last_word; 1376 1377 if (e - s <= 3*BITS_PER_LONG) { 1378 /* don't bother; el and sl may even be wrong. */ 1379 __bm_change_bits_to(mdev, s, e, 1, KM_USER0); 1380 return; 1381 } 1382 1383 /* difference is large enough that we can trust sl and el */ 1384 1385 /* bits filling the current long */ 1386 if (sl) 1387 __bm_change_bits_to(mdev, s, sl-1, 1, KM_USER0); 1388 1389 first_page = sl >> (3 + PAGE_SHIFT); 1390 last_page = el >> (3 + PAGE_SHIFT); 1391 1392 /* MLPP: modulo longs per page */ 1393 /* LWPP: long words per page */ 1394 first_word = MLPP(sl >> LN2_BPL); 1395 last_word = LWPP; 1396 1397 /* first and full pages, unless first page == last page */ 1398 for (page_nr = first_page; page_nr < last_page; page_nr++) { 1399 bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word); 1400 cond_resched(); 1401 first_word = 0; 1402 } 1403 1404 /* last page (respectively only page, for first page == last page) */ 1405 last_word = MLPP(el >> LN2_BPL); 1406 bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word); 1407 1408 /* possibly trailing bits. 1409 * example: (e & 63) == 63, el will be e+1. 1410 * if that even was the very last bit, 1411 * it would trigger an assert in __bm_change_bits_to() 1412 */ 1413 if (el <= e) 1414 __bm_change_bits_to(mdev, el, e, 1, KM_USER0); 1415 } 1416 1417 /* returns bit state 1418 * wants bitnr, NOT sector. 1419 * inherently racy... area needs to be locked by means of {al,rs}_lru 1420 * 1 ... bit set 1421 * 0 ... bit not set 1422 * -1 ... first out of bounds access, stop testing for bits! 1423 */ 1424 int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr) 1425 { 1426 unsigned long flags; 1427 struct drbd_bitmap *b = mdev->bitmap; 1428 unsigned long *p_addr; 1429 int i; 1430 1431 ERR_IF(!b) return 0; 1432 ERR_IF(!b->bm_pages) return 0; 1433 1434 spin_lock_irqsave(&b->bm_lock, flags); 1435 if (BM_DONT_TEST & b->bm_flags) 1436 bm_print_lock_info(mdev); 1437 if (bitnr < b->bm_bits) { 1438 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr)); 1439 i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0; 1440 bm_unmap(p_addr); 1441 } else if (bitnr == b->bm_bits) { 1442 i = -1; 1443 } else { /* (bitnr > b->bm_bits) */ 1444 dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits); 1445 i = 0; 1446 } 1447 1448 spin_unlock_irqrestore(&b->bm_lock, flags); 1449 return i; 1450 } 1451 1452 /* returns number of bits set in the range [s, e] */ 1453 int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e) 1454 { 1455 unsigned long flags; 1456 struct drbd_bitmap *b = mdev->bitmap; 1457 unsigned long *p_addr = NULL; 1458 unsigned long bitnr; 1459 unsigned int page_nr = -1U; 1460 int c = 0; 1461 1462 /* If this is called without a bitmap, that is a bug. But just to be 1463 * robust in case we screwed up elsewhere, in that case pretend there 1464 * was one dirty bit in the requested area, so we won't try to do a 1465 * local read there (no bitmap probably implies no disk) */ 1466 ERR_IF(!b) return 1; 1467 ERR_IF(!b->bm_pages) return 1; 1468 1469 spin_lock_irqsave(&b->bm_lock, flags); 1470 if (BM_DONT_TEST & b->bm_flags) 1471 bm_print_lock_info(mdev); 1472 for (bitnr = s; bitnr <= e; bitnr++) { 1473 unsigned int idx = bm_bit_to_page_idx(b, bitnr); 1474 if (page_nr != idx) { 1475 page_nr = idx; 1476 if (p_addr) 1477 bm_unmap(p_addr); 1478 p_addr = bm_map_pidx(b, idx); 1479 } 1480 ERR_IF (bitnr >= b->bm_bits) { 1481 dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits); 1482 } else { 1483 c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr)); 1484 } 1485 } 1486 if (p_addr) 1487 bm_unmap(p_addr); 1488 spin_unlock_irqrestore(&b->bm_lock, flags); 1489 return c; 1490 } 1491 1492 1493 /* inherently racy... 1494 * return value may be already out-of-date when this function returns. 1495 * but the general usage is that this is only use during a cstate when bits are 1496 * only cleared, not set, and typically only care for the case when the return 1497 * value is zero, or we already "locked" this "bitmap extent" by other means. 1498 * 1499 * enr is bm-extent number, since we chose to name one sector (512 bytes) 1500 * worth of the bitmap a "bitmap extent". 1501 * 1502 * TODO 1503 * I think since we use it like a reference count, we should use the real 1504 * reference count of some bitmap extent element from some lru instead... 1505 * 1506 */ 1507 int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr) 1508 { 1509 struct drbd_bitmap *b = mdev->bitmap; 1510 int count, s, e; 1511 unsigned long flags; 1512 unsigned long *p_addr, *bm; 1513 1514 ERR_IF(!b) return 0; 1515 ERR_IF(!b->bm_pages) return 0; 1516 1517 spin_lock_irqsave(&b->bm_lock, flags); 1518 if (BM_DONT_TEST & b->bm_flags) 1519 bm_print_lock_info(mdev); 1520 1521 s = S2W(enr); 1522 e = min((size_t)S2W(enr+1), b->bm_words); 1523 count = 0; 1524 if (s < b->bm_words) { 1525 int n = e-s; 1526 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s)); 1527 bm = p_addr + MLPP(s); 1528 while (n--) 1529 count += hweight_long(*bm++); 1530 bm_unmap(p_addr); 1531 } else { 1532 dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s); 1533 } 1534 spin_unlock_irqrestore(&b->bm_lock, flags); 1535 return count; 1536 } 1537 1538 /* Set all bits covered by the AL-extent al_enr. 1539 * Returns number of bits changed. */ 1540 unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr) 1541 { 1542 struct drbd_bitmap *b = mdev->bitmap; 1543 unsigned long *p_addr, *bm; 1544 unsigned long weight; 1545 unsigned long s, e; 1546 int count, i, do_now; 1547 ERR_IF(!b) return 0; 1548 ERR_IF(!b->bm_pages) return 0; 1549 1550 spin_lock_irq(&b->bm_lock); 1551 if (BM_DONT_SET & b->bm_flags) 1552 bm_print_lock_info(mdev); 1553 weight = b->bm_set; 1554 1555 s = al_enr * BM_WORDS_PER_AL_EXT; 1556 e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words); 1557 /* assert that s and e are on the same page */ 1558 D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3) 1559 == s >> (PAGE_SHIFT - LN2_BPL + 3)); 1560 count = 0; 1561 if (s < b->bm_words) { 1562 i = do_now = e-s; 1563 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s)); 1564 bm = p_addr + MLPP(s); 1565 while (i--) { 1566 count += hweight_long(*bm); 1567 *bm = -1UL; 1568 bm++; 1569 } 1570 bm_unmap(p_addr); 1571 b->bm_set += do_now*BITS_PER_LONG - count; 1572 if (e == b->bm_words) 1573 b->bm_set -= bm_clear_surplus(b); 1574 } else { 1575 dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s); 1576 } 1577 weight = b->bm_set - weight; 1578 spin_unlock_irq(&b->bm_lock); 1579 return weight; 1580 } 1581