4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
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>.
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)
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.
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.
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>
32 #include <asm-generic/bitops/le.h>
37 /* OPAQUE outside this file!
38 * interface defined in drbd_int.h
41 * function name drbd_bm_... => used elsewhere, "public".
42 * function name bm_... => internal to implementation, "private".
48 * We want to support >= peta byte of backend storage, while for now still using
49 * a granularity of one bit per 4KiB of storage.
50 * 1 << 50 bytes backend storage (1 PiB)
51 * 1 << (50 - 12) bits needed
52 * 38 --> we need u64 to index and count bits
53 * 1 << (38 - 3) bitmap bytes needed
54 * 35 --> we still need u64 to index and count bytes
55 * (that's 32 GiB of bitmap for 1 PiB storage)
56 * 1 << (35 - 2) 32bit longs needed
57 * 33 --> we'd even need u64 to index and count 32bit long words.
58 * 1 << (35 - 3) 64bit longs needed
59 * 32 --> we could get away with a 32bit unsigned int to index and count
60 * 64bit long words, but I rather stay with unsigned long for now.
61 * We probably should neither count nor point to bytes or long words
62 * directly, but either by bitnumber, or by page index and offset.
64 * 22 --> we need that much 4KiB pages of bitmap.
65 * 1 << (22 + 3) --> on a 64bit arch,
66 * we need 32 MiB to store the array of page pointers.
68 * Because I'm lazy, and because the resulting patch was too large, too ugly
69 * and still incomplete, on 32bit we still "only" support 16 TiB (minus some),
70 * (1 << 32) bits * 4k storage.
73 * bitmap storage and IO:
74 * Bitmap is stored little endian on disk, and is kept little endian in
75 * core memory. Currently we still hold the full bitmap in core as long
76 * as we are "attached" to a local disk, which at 32 GiB for 1PiB storage
79 * We plan to reduce the amount of in-core bitmap pages by pageing them in
80 * and out against their on-disk location as necessary, but need to make
81 * sure we don't cause too much meta data IO, and must not deadlock in
82 * tight memory situations. This needs some more work.
87 * Access to the *bm_pages is protected by bm_lock.
88 * It is safe to read the other members within the lock.
90 * drbd_bm_set_bits is called from bio_endio callbacks,
91 * We may be called with irq already disabled,
92 * so we need spin_lock_irqsave().
93 * And we need the kmap_atomic.
96 struct page **bm_pages;
99 /* see LIMITATIONS: above */
101 unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */
102 unsigned long bm_bits;
104 size_t bm_number_of_pages;
105 sector_t bm_dev_capacity;
106 struct mutex bm_change; /* serializes resize operations */
108 wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */
110 enum bm_flag bm_flags;
112 /* debugging aid, in case we are still racy somewhere */
114 struct task_struct *bm_task;
117 static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
118 unsigned long e, int val, const enum km_type km);
120 #define bm_print_lock_info(m) __bm_print_lock_info(m, __func__)
121 static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func)
123 struct drbd_bitmap *b = mdev->bitmap;
124 if (!__ratelimit(&drbd_ratelimit_state))
126 dev_err(DEV, "FIXME %s in %s, bitmap locked for '%s' by %s\n",
127 current == mdev->receiver.task ? "receiver" :
128 current == mdev->asender.task ? "asender" :
129 current == mdev->worker.task ? "worker" : current->comm,
130 func, b->bm_why ?: "?",
131 b->bm_task == mdev->receiver.task ? "receiver" :
132 b->bm_task == mdev->asender.task ? "asender" :
133 b->bm_task == mdev->worker.task ? "worker" : "?");
136 void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags)
138 struct drbd_bitmap *b = mdev->bitmap;
142 dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n");
146 trylock_failed = !mutex_trylock(&b->bm_change);
148 if (trylock_failed) {
149 dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n",
150 current == mdev->receiver.task ? "receiver" :
151 current == mdev->asender.task ? "asender" :
152 current == mdev->worker.task ? "worker" : current->comm,
153 why, b->bm_why ?: "?",
154 b->bm_task == mdev->receiver.task ? "receiver" :
155 b->bm_task == mdev->asender.task ? "asender" :
156 b->bm_task == mdev->worker.task ? "worker" : "?");
157 mutex_lock(&b->bm_change);
159 if (BM_LOCKED_MASK & b->bm_flags)
160 dev_err(DEV, "FIXME bitmap already locked in bm_lock\n");
161 b->bm_flags |= flags & BM_LOCKED_MASK;
164 b->bm_task = current;
167 void drbd_bm_unlock(struct drbd_conf *mdev)
169 struct drbd_bitmap *b = mdev->bitmap;
171 dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n");
175 if (!(BM_LOCKED_MASK & mdev->bitmap->bm_flags))
176 dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");
178 b->bm_flags &= ~BM_LOCKED_MASK;
181 mutex_unlock(&b->bm_change);
184 /* we store some "meta" info about our pages in page->private */
185 /* at a granularity of 4k storage per bitmap bit:
186 * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks
188 * 1<<23 4k bitmap pages.
189 * Use 24 bits as page index, covers 2 peta byte storage
190 * at a granularity of 4k per bit.
191 * Used to report the failed page idx on io error from the endio handlers.
193 #define BM_PAGE_IDX_MASK ((1UL<<24)-1)
194 /* this page is currently read in, or written back */
195 #define BM_PAGE_IO_LOCK 31
196 /* if there has been an IO error for this page */
197 #define BM_PAGE_IO_ERROR 30
198 /* this is to be able to intelligently skip disk IO,
199 * set if bits have been set since last IO. */
200 #define BM_PAGE_NEED_WRITEOUT 29
201 /* to mark for lazy writeout once syncer cleared all clearable bits,
202 * we if bits have been cleared since last IO. */
203 #define BM_PAGE_LAZY_WRITEOUT 28
205 /* store_page_idx uses non-atomic assingment. It is only used directly after
206 * allocating the page. All other bm_set_page_* and bm_clear_page_* need to
207 * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap
208 * changes) may happen from various contexts, and wait_on_bit/wake_up_bit
209 * requires it all to be atomic as well. */
210 static void bm_store_page_idx(struct page *page, unsigned long idx)
212 BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
213 page_private(page) |= idx;
216 static unsigned long bm_page_to_idx(struct page *page)
218 return page_private(page) & BM_PAGE_IDX_MASK;
221 /* As is very unlikely that the same page is under IO from more than one
222 * context, we can get away with a bit per page and one wait queue per bitmap.
224 static void bm_page_lock_io(struct drbd_conf *mdev, int page_nr)
226 struct drbd_bitmap *b = mdev->bitmap;
227 void *addr = &page_private(b->bm_pages[page_nr]);
228 wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
231 static void bm_page_unlock_io(struct drbd_conf *mdev, int page_nr)
233 struct drbd_bitmap *b = mdev->bitmap;
234 void *addr = &page_private(b->bm_pages[page_nr]);
235 clear_bit(BM_PAGE_IO_LOCK, addr);
236 smp_mb__after_clear_bit();
237 wake_up(&mdev->bitmap->bm_io_wait);
240 /* set _before_ submit_io, so it may be reset due to being changed
241 * while this page is in flight... will get submitted later again */
242 static void bm_set_page_unchanged(struct page *page)
245 clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
246 clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
249 static void bm_set_page_need_writeout(struct page *page)
251 set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
254 static int bm_test_page_unchanged(struct page *page)
256 volatile const unsigned long *addr = &page_private(page);
257 return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0;
260 static void bm_set_page_io_err(struct page *page)
262 set_bit(BM_PAGE_IO_ERROR, &page_private(page));
265 static void bm_clear_page_io_err(struct page *page)
267 clear_bit(BM_PAGE_IO_ERROR, &page_private(page));
270 static void bm_set_page_lazy_writeout(struct page *page)
272 set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
275 static int bm_test_page_lazy_writeout(struct page *page)
277 return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
280 /* on a 32bit box, this would allow for exactly (2<<38) bits. */
281 static unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr)
283 /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
284 unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
285 BUG_ON(page_nr >= b->bm_number_of_pages);
289 static unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr)
291 /* page_nr = (bitnr/8) >> PAGE_SHIFT; */
292 unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3);
293 BUG_ON(page_nr >= b->bm_number_of_pages);
297 static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx, const enum km_type km)
299 struct page *page = b->bm_pages[idx];
300 return (unsigned long *) kmap_atomic(page, km);
303 static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
305 return __bm_map_pidx(b, idx, KM_IRQ1);
308 static void __bm_unmap(unsigned long *p_addr, const enum km_type km)
310 kunmap_atomic(p_addr, km);
313 static void bm_unmap(unsigned long *p_addr)
315 return __bm_unmap(p_addr, KM_IRQ1);
318 /* long word offset of _bitmap_ sector */
319 #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
320 /* word offset from start of bitmap to word number _in_page_
321 * modulo longs per page
322 #define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
323 hm, well, Philipp thinks gcc might not optimze the % into & (... - 1)
326 #define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
328 /* Long words per page */
329 #define LWPP (PAGE_SIZE/sizeof(long))
332 * actually most functions herein should take a struct drbd_bitmap*, not a
333 * struct drbd_conf*, but for the debug macros I like to have the mdev around
334 * to be able to report device specific.
338 static void bm_free_pages(struct page **pages, unsigned long number)
344 for (i = 0; i < number; i++) {
346 printk(KERN_ALERT "drbd: bm_free_pages tried to free "
347 "a NULL pointer; i=%lu n=%lu\n",
351 __free_page(pages[i]);
356 static void bm_vk_free(void *ptr, int v)
365 * "have" and "want" are NUMBER OF PAGES.
367 static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
369 struct page **old_pages = b->bm_pages;
370 struct page **new_pages, *page;
371 unsigned int i, bytes, vmalloced = 0;
372 unsigned long have = b->bm_number_of_pages;
374 BUG_ON(have == 0 && old_pages != NULL);
375 BUG_ON(have != 0 && old_pages == NULL);
380 /* Trying kmalloc first, falling back to vmalloc.
381 * GFP_KERNEL is ok, as this is done when a lower level disk is
382 * "attached" to the drbd. Context is receiver thread or cqueue
383 * thread. As we have no disk yet, we are not in the IO path,
384 * not even the IO path of the peer. */
385 bytes = sizeof(struct page *)*want;
386 new_pages = kmalloc(bytes, GFP_KERNEL);
388 new_pages = vmalloc(bytes);
394 memset(new_pages, 0, bytes);
396 for (i = 0; i < have; i++)
397 new_pages[i] = old_pages[i];
398 for (; i < want; i++) {
399 page = alloc_page(GFP_HIGHUSER);
401 bm_free_pages(new_pages + have, i - have);
402 bm_vk_free(new_pages, vmalloced);
405 /* we want to know which page it is
406 * from the endio handlers */
407 bm_store_page_idx(page, i);
411 for (i = 0; i < want; i++)
412 new_pages[i] = old_pages[i];
413 /* NOT HERE, we are outside the spinlock!
414 bm_free_pages(old_pages + want, have - want);
419 b->bm_flags |= BM_P_VMALLOCED;
421 b->bm_flags &= ~BM_P_VMALLOCED;
427 * called on driver init only. TODO call when a device is created.
428 * allocates the drbd_bitmap, and stores it in mdev->bitmap.
430 int drbd_bm_init(struct drbd_conf *mdev)
432 struct drbd_bitmap *b = mdev->bitmap;
434 b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
437 spin_lock_init(&b->bm_lock);
438 mutex_init(&b->bm_change);
439 init_waitqueue_head(&b->bm_io_wait);
446 sector_t drbd_bm_capacity(struct drbd_conf *mdev)
448 ERR_IF(!mdev->bitmap) return 0;
449 return mdev->bitmap->bm_dev_capacity;
452 /* called on driver unload. TODO: call when a device is destroyed.
454 void drbd_bm_cleanup(struct drbd_conf *mdev)
456 ERR_IF (!mdev->bitmap) return;
457 bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
458 bm_vk_free(mdev->bitmap->bm_pages, (BM_P_VMALLOCED & mdev->bitmap->bm_flags));
464 * since (b->bm_bits % BITS_PER_LONG) != 0,
465 * this masks out the remaining bits.
466 * Returns the number of bits cleared.
468 #define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3))
469 #define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1)
470 #define BITS_PER_LONG_MASK (BITS_PER_LONG - 1)
471 static int bm_clear_surplus(struct drbd_bitmap *b)
474 unsigned long *p_addr, *bm;
478 /* number of bits modulo bits per page */
479 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
480 /* mask the used bits of the word containing the last bit */
481 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
482 /* bitmap is always stored little endian,
483 * on disk and in core memory alike */
484 mask = cpu_to_lel(mask);
486 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
487 bm = p_addr + (tmp/BITS_PER_LONG);
489 /* If mask != 0, we are not exactly aligned, so bm now points
490 * to the long containing the last bit.
491 * If mask == 0, bm already points to the word immediately
492 * after the last (long word aligned) bit. */
493 cleared = hweight_long(*bm & ~mask);
498 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
499 /* on a 32bit arch, we may need to zero out
500 * a padding long to align with a 64bit remote */
501 cleared += hweight_long(*bm);
508 static void bm_set_surplus(struct drbd_bitmap *b)
511 unsigned long *p_addr, *bm;
514 /* number of bits modulo bits per page */
515 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
516 /* mask the used bits of the word containing the last bit */
517 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
518 /* bitmap is always stored little endian,
519 * on disk and in core memory alike */
520 mask = cpu_to_lel(mask);
522 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
523 bm = p_addr + (tmp/BITS_PER_LONG);
525 /* If mask != 0, we are not exactly aligned, so bm now points
526 * to the long containing the last bit.
527 * If mask == 0, bm already points to the word immediately
528 * after the last (long word aligned) bit. */
533 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
534 /* on a 32bit arch, we may need to zero out
535 * a padding long to align with a 64bit remote */
541 /* you better not modify the bitmap while this is running,
542 * or its results will be stale */
543 static unsigned long bm_count_bits(struct drbd_bitmap *b)
545 unsigned long *p_addr;
546 unsigned long bits = 0;
547 unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1;
548 int idx, i, last_word;
550 /* all but last page */
551 for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) {
552 p_addr = __bm_map_pidx(b, idx, KM_USER0);
553 for (i = 0; i < LWPP; i++)
554 bits += hweight_long(p_addr[i]);
555 __bm_unmap(p_addr, KM_USER0);
558 /* last (or only) page */
559 last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL;
560 p_addr = __bm_map_pidx(b, idx, KM_USER0);
561 for (i = 0; i < last_word; i++)
562 bits += hweight_long(p_addr[i]);
563 p_addr[last_word] &= cpu_to_lel(mask);
564 bits += hweight_long(p_addr[last_word]);
565 /* 32bit arch, may have an unused padding long */
566 if (BITS_PER_LONG == 32 && (last_word & 1) == 0)
567 p_addr[last_word+1] = 0;
568 __bm_unmap(p_addr, KM_USER0);
572 /* offset and len in long words.*/
573 static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
575 unsigned long *p_addr, *bm;
581 if (end > b->bm_words) {
582 printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
586 while (offset < end) {
587 do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
588 idx = bm_word_to_page_idx(b, offset);
589 p_addr = bm_map_pidx(b, idx);
590 bm = p_addr + MLPP(offset);
591 if (bm+do_now > p_addr + LWPP) {
592 printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
593 p_addr, bm, (int)do_now);
595 memset(bm, c, do_now * sizeof(long));
597 bm_set_page_need_writeout(b->bm_pages[idx]);
603 * make sure the bitmap has enough room for the attached storage,
604 * if necessary, resize.
605 * called whenever we may have changed the device size.
606 * returns -ENOMEM if we could not allocate enough memory, 0 on success.
607 * In case this is actually a resize, we copy the old bitmap into the new one.
608 * Otherwise, the bitmap is initialized to all bits set.
610 int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
612 struct drbd_bitmap *b = mdev->bitmap;
613 unsigned long bits, words, owords, obits;
614 unsigned long want, have, onpages; /* number of pages */
615 struct page **npages, **opages = NULL;
616 int err = 0, growing;
617 int opages_vmalloced;
619 ERR_IF(!b) return -ENOMEM;
621 drbd_bm_lock(mdev, "resize", BM_LOCKED_MASK);
623 dev_info(DEV, "drbd_bm_resize called with capacity == %llu\n",
624 (unsigned long long)capacity);
626 if (capacity == b->bm_dev_capacity)
629 opages_vmalloced = (BM_P_VMALLOCED & b->bm_flags);
632 spin_lock_irq(&b->bm_lock);
633 opages = b->bm_pages;
634 onpages = b->bm_number_of_pages;
635 owords = b->bm_words;
637 b->bm_number_of_pages =
641 b->bm_dev_capacity = 0;
642 spin_unlock_irq(&b->bm_lock);
643 bm_free_pages(opages, onpages);
644 bm_vk_free(opages, opages_vmalloced);
647 bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));
650 words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
651 a 32bit host could present the wrong number of words
654 words = ALIGN(bits, 64) >> LN2_BPL;
656 if (get_ldev(mdev)) {
657 u64 bits_on_disk = ((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12;
659 if (bits > bits_on_disk) {
660 dev_info(DEV, "bits = %lu\n", bits);
661 dev_info(DEV, "bits_on_disk = %llu\n", bits_on_disk);
667 want = ALIGN(words*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT;
668 have = b->bm_number_of_pages;
670 D_ASSERT(b->bm_pages != NULL);
671 npages = b->bm_pages;
673 if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC))
676 npages = bm_realloc_pages(b, want);
684 spin_lock_irq(&b->bm_lock);
685 opages = b->bm_pages;
686 owords = b->bm_words;
689 growing = bits > obits;
690 if (opages && growing && set_new_bits)
693 b->bm_pages = npages;
694 b->bm_number_of_pages = want;
697 b->bm_dev_capacity = capacity;
701 bm_memset(b, owords, 0xff, words-owords);
702 b->bm_set += bits - obits;
704 bm_memset(b, owords, 0x00, words-owords);
709 /* implicit: (opages != NULL) && (opages != npages) */
710 bm_free_pages(opages + want, have - want);
713 (void)bm_clear_surplus(b);
715 spin_unlock_irq(&b->bm_lock);
716 if (opages != npages)
717 bm_vk_free(opages, opages_vmalloced);
719 b->bm_set = bm_count_bits(b);
720 dev_info(DEV, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);
723 drbd_bm_unlock(mdev);
728 * if not protected by other means, return value may be out of date when
729 * leaving this function...
730 * we still need to lock it, since it is important that this returns
731 * bm_set == 0 precisely.
733 * maybe bm_set should be atomic_t ?
735 unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev)
737 struct drbd_bitmap *b = mdev->bitmap;
742 ERR_IF(!b->bm_pages) return 0;
744 spin_lock_irqsave(&b->bm_lock, flags);
746 spin_unlock_irqrestore(&b->bm_lock, flags);
751 unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
754 /* if I don't have a disk, I don't know about out-of-sync status */
755 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
757 s = _drbd_bm_total_weight(mdev);
762 size_t drbd_bm_words(struct drbd_conf *mdev)
764 struct drbd_bitmap *b = mdev->bitmap;
766 ERR_IF(!b->bm_pages) return 0;
771 unsigned long drbd_bm_bits(struct drbd_conf *mdev)
773 struct drbd_bitmap *b = mdev->bitmap;
779 /* merge number words from buffer into the bitmap starting at offset.
780 * buffer[i] is expected to be little endian unsigned long.
781 * bitmap must be locked by drbd_bm_lock.
782 * currently only used from receive_bitmap.
784 void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
785 unsigned long *buffer)
787 struct drbd_bitmap *b = mdev->bitmap;
788 unsigned long *p_addr, *bm;
789 unsigned long word, bits;
793 end = offset + number;
796 ERR_IF(!b->bm_pages) return;
799 WARN_ON(offset >= b->bm_words);
800 WARN_ON(end > b->bm_words);
802 spin_lock_irq(&b->bm_lock);
803 while (offset < end) {
804 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
805 idx = bm_word_to_page_idx(b, offset);
806 p_addr = bm_map_pidx(b, idx);
807 bm = p_addr + MLPP(offset);
810 bits = hweight_long(*bm);
811 word = *bm | *buffer++;
813 b->bm_set += hweight_long(word) - bits;
816 bm_set_page_need_writeout(b->bm_pages[idx]);
818 /* with 32bit <-> 64bit cross-platform connect
819 * this is only correct for current usage,
820 * where we _know_ that we are 64 bit aligned,
821 * and know that this function is used in this way, too...
823 if (end == b->bm_words)
824 b->bm_set -= bm_clear_surplus(b);
825 spin_unlock_irq(&b->bm_lock);
828 /* copy number words from the bitmap starting at offset into the buffer.
829 * buffer[i] will be little endian unsigned long.
831 void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
832 unsigned long *buffer)
834 struct drbd_bitmap *b = mdev->bitmap;
835 unsigned long *p_addr, *bm;
838 end = offset + number;
841 ERR_IF(!b->bm_pages) return;
843 spin_lock_irq(&b->bm_lock);
844 if ((offset >= b->bm_words) ||
845 (end > b->bm_words) ||
847 dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n",
848 (unsigned long) offset,
849 (unsigned long) number,
850 (unsigned long) b->bm_words);
852 while (offset < end) {
853 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
854 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
855 bm = p_addr + MLPP(offset);
862 spin_unlock_irq(&b->bm_lock);
865 /* set all bits in the bitmap */
866 void drbd_bm_set_all(struct drbd_conf *mdev)
868 struct drbd_bitmap *b = mdev->bitmap;
870 ERR_IF(!b->bm_pages) return;
872 spin_lock_irq(&b->bm_lock);
873 bm_memset(b, 0, 0xff, b->bm_words);
874 (void)bm_clear_surplus(b);
875 b->bm_set = b->bm_bits;
876 spin_unlock_irq(&b->bm_lock);
879 /* clear all bits in the bitmap */
880 void drbd_bm_clear_all(struct drbd_conf *mdev)
882 struct drbd_bitmap *b = mdev->bitmap;
884 ERR_IF(!b->bm_pages) return;
886 spin_lock_irq(&b->bm_lock);
887 bm_memset(b, 0, 0, b->bm_words);
889 spin_unlock_irq(&b->bm_lock);
893 struct drbd_conf *mdev;
895 struct completion done;
897 #define BM_AIO_COPY_PAGES 1
901 /* bv_page may be a copy, or may be the original */
902 static void bm_async_io_complete(struct bio *bio, int error)
904 struct bm_aio_ctx *ctx = bio->bi_private;
905 struct drbd_conf *mdev = ctx->mdev;
906 struct drbd_bitmap *b = mdev->bitmap;
907 unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page);
908 int uptodate = bio_flagged(bio, BIO_UPTODATE);
911 /* strange behavior of some lower level drivers...
912 * fail the request by clearing the uptodate flag,
913 * but do not return any error?!
914 * do we want to WARN() on this? */
915 if (!error && !uptodate)
918 if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 &&
919 !bm_test_page_unchanged(b->bm_pages[idx]))
920 dev_warn(DEV, "bitmap page idx %u changed during IO!\n", idx);
923 /* ctx error will hold the completed-last non-zero error code,
924 * in case error codes differ. */
926 bm_set_page_io_err(b->bm_pages[idx]);
927 /* Not identical to on disk version of it.
928 * Is BM_PAGE_IO_ERROR enough? */
929 if (__ratelimit(&drbd_ratelimit_state))
930 dev_err(DEV, "IO ERROR %d on bitmap page idx %u\n",
933 bm_clear_page_io_err(b->bm_pages[idx]);
934 dynamic_dev_dbg(DEV, "bitmap page idx %u completed\n", idx);
937 bm_page_unlock_io(mdev, idx);
939 /* FIXME give back to page pool */
940 if (ctx->flags & BM_AIO_COPY_PAGES)
941 put_page(bio->bi_io_vec[0].bv_page);
945 if (atomic_dec_and_test(&ctx->in_flight))
946 complete(&ctx->done);
949 static void bm_page_io_async(struct bm_aio_ctx *ctx, int page_nr, int rw) __must_hold(local)
951 /* we are process context. we always get a bio */
952 struct bio *bio = bio_alloc(GFP_KERNEL, 1);
953 struct drbd_conf *mdev = ctx->mdev;
954 struct drbd_bitmap *b = mdev->bitmap;
958 sector_t on_disk_sector =
959 mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset;
960 on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);
962 /* this might happen with very small
963 * flexible external meta data device,
964 * or with PAGE_SIZE > 4k */
965 len = min_t(unsigned int, PAGE_SIZE,
966 (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);
968 /* serialize IO on this page */
969 bm_page_lock_io(mdev, page_nr);
970 /* before memcpy and submit,
971 * so it can be redirtied any time */
972 bm_set_page_unchanged(b->bm_pages[page_nr]);
974 if (ctx->flags & BM_AIO_COPY_PAGES) {
975 /* FIXME alloc_page is good enough for now, but actually needs
976 * to use pre-allocated page pool */
978 page = alloc_page(__GFP_HIGHMEM|__GFP_WAIT);
979 dest = kmap_atomic(page, KM_USER0);
980 src = kmap_atomic(b->bm_pages[page_nr], KM_USER1);
981 memcpy(dest, src, PAGE_SIZE);
982 kunmap_atomic(src, KM_USER1);
983 kunmap_atomic(dest, KM_USER0);
984 bm_store_page_idx(page, page_nr);
986 page = b->bm_pages[page_nr];
988 bio->bi_bdev = mdev->ldev->md_bdev;
989 bio->bi_sector = on_disk_sector;
990 bio_add_page(bio, page, len, 0);
991 bio->bi_private = ctx;
992 bio->bi_end_io = bm_async_io_complete;
994 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
996 bio_endio(bio, -EIO);
1003 * bm_rw: read/write the whole bitmap from/to its on disk location.
1005 static int bm_rw(struct drbd_conf *mdev, int rw, unsigned lazy_writeout_upper_idx) __must_hold(local)
1007 struct bm_aio_ctx ctx = {
1009 .in_flight = ATOMIC_INIT(1),
1010 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1011 .flags = lazy_writeout_upper_idx ? BM_AIO_COPY_PAGES : 0,
1013 struct drbd_bitmap *b = mdev->bitmap;
1014 int num_pages, i, count = 0;
1020 * We are protected against bitmap disappearing/resizing by holding an
1021 * ldev reference (caller must have called get_ldev()).
1022 * For read/write, we are protected against changes to the bitmap by
1023 * the bitmap lock (see drbd_bitmap_io).
1024 * For lazy writeout, we don't care for ongoing changes to the bitmap,
1025 * as we submit copies of pages anyways.
1028 WARN_ON(!(BM_LOCKED_MASK & b->bm_flags));
1030 num_pages = b->bm_number_of_pages;
1034 /* let the layers below us try to merge these bios... */
1035 for (i = 0; i < num_pages; i++) {
1036 /* ignore completely unchanged pages */
1037 if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
1040 if (bm_test_page_unchanged(b->bm_pages[i])) {
1041 dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
1044 /* during lazy writeout,
1045 * ignore those pages not marked for lazy writeout. */
1046 if (lazy_writeout_upper_idx &&
1047 !bm_test_page_lazy_writeout(b->bm_pages[i])) {
1048 dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
1052 atomic_inc(&ctx.in_flight);
1053 bm_page_io_async(&ctx, i, rw);
1059 * We initialize ctx.in_flight to one to make sure bm_async_io_complete
1060 * will not complete() early, and decrement / test it here. If there
1061 * are still some bios in flight, we need to wait for them here.
1063 if (!atomic_dec_and_test(&ctx.in_flight))
1064 wait_for_completion(&ctx.done);
1065 dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
1066 rw == WRITE ? "WRITE" : "READ",
1067 count, jiffies - now);
1070 dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
1071 drbd_chk_io_error(mdev, 1, true);
1072 err = -EIO; /* ctx.error ? */
1077 drbd_md_flush(mdev);
1078 } else /* rw == READ */ {
1079 b->bm_set = bm_count_bits(b);
1080 dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
1085 dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
1086 ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
1092 * drbd_bm_read() - Read the whole bitmap from its on disk location.
1093 * @mdev: DRBD device.
1095 int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
1097 return bm_rw(mdev, READ, 0);
1101 * drbd_bm_write() - Write the whole bitmap to its on disk location.
1102 * @mdev: DRBD device.
1104 * Will only write pages that have changed since last IO.
1106 int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
1108 return bm_rw(mdev, WRITE, 0);
1112 * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
1113 * @mdev: DRBD device.
1114 * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
1116 int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
1118 return bm_rw(mdev, WRITE, upper_idx);
1123 * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
1124 * @mdev: DRBD device.
1125 * @idx: bitmap page index
1127 * We don't want to special case on logical_block_size of the backend device,
1128 * so we submit PAGE_SIZE aligned pieces.
1129 * Note that on "most" systems, PAGE_SIZE is 4k.
1131 * In case this becomes an issue on systems with larger PAGE_SIZE,
1132 * we may want to change this again to write 4k aligned 4k pieces.
1134 int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
1136 struct bm_aio_ctx ctx = {
1138 .in_flight = ATOMIC_INIT(1),
1139 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1140 .flags = BM_AIO_COPY_PAGES,
1143 if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
1144 dynamic_dev_dbg(DEV, "skipped bm page write for idx %u\n", idx);
1148 bm_page_io_async(&ctx, idx, WRITE_SYNC);
1149 wait_for_completion(&ctx.done);
1152 drbd_chk_io_error(mdev, 1, true);
1153 /* that should force detach, so the in memory bitmap will be
1154 * gone in a moment as well. */
1156 mdev->bm_writ_cnt++;
1161 * find_first_bit returns int, we return unsigned long.
1162 * For this to work on 32bit arch with bitnumbers > (1<<32),
1163 * we'd need to return u64, and get a whole lot of other places
1164 * fixed where we still use unsigned long.
1166 * this returns a bit number, NOT a sector!
1168 static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
1169 const int find_zero_bit, const enum km_type km)
1171 struct drbd_bitmap *b = mdev->bitmap;
1172 unsigned long *p_addr;
1173 unsigned long bit_offset;
1177 if (bm_fo > b->bm_bits) {
1178 dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
1179 bm_fo = DRBD_END_OF_BITMAP;
1181 while (bm_fo < b->bm_bits) {
1182 /* bit offset of the first bit in the page */
1183 bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
1184 p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
1187 i = generic_find_next_zero_le_bit(p_addr,
1188 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
1190 i = generic_find_next_le_bit(p_addr,
1191 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
1193 __bm_unmap(p_addr, km);
1194 if (i < PAGE_SIZE*8) {
1195 bm_fo = bit_offset + i;
1196 if (bm_fo >= b->bm_bits)
1200 bm_fo = bit_offset + PAGE_SIZE*8;
1202 bm_fo = DRBD_END_OF_BITMAP;
1208 static unsigned long bm_find_next(struct drbd_conf *mdev,
1209 unsigned long bm_fo, const int find_zero_bit)
1211 struct drbd_bitmap *b = mdev->bitmap;
1212 unsigned long i = DRBD_END_OF_BITMAP;
1214 ERR_IF(!b) return i;
1215 ERR_IF(!b->bm_pages) return i;
1217 spin_lock_irq(&b->bm_lock);
1218 if (BM_DONT_TEST & b->bm_flags)
1219 bm_print_lock_info(mdev);
1221 i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);
1223 spin_unlock_irq(&b->bm_lock);
1227 unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1229 return bm_find_next(mdev, bm_fo, 0);
1233 /* not yet needed for anything. */
1234 unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1236 return bm_find_next(mdev, bm_fo, 1);
1240 /* does not spin_lock_irqsave.
1241 * you must take drbd_bm_lock() first */
1242 unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1244 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
1245 return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
1248 unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1250 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
1251 return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
1254 /* returns number of bits actually changed.
1255 * for val != 0, we change 0 -> 1, return code positive
1256 * for val == 0, we change 1 -> 0, return code negative
1257 * wants bitnr, not sector.
1258 * expected to be called for only a few bits (e - s about BITS_PER_LONG).
1259 * Must hold bitmap lock already. */
1260 static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
1261 unsigned long e, int val, const enum km_type km)
1263 struct drbd_bitmap *b = mdev->bitmap;
1264 unsigned long *p_addr = NULL;
1265 unsigned long bitnr;
1266 unsigned int last_page_nr = -1U;
1268 int changed_total = 0;
1270 if (e >= b->bm_bits) {
1271 dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
1273 e = b->bm_bits ? b->bm_bits -1 : 0;
1275 for (bitnr = s; bitnr <= e; bitnr++) {
1276 unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
1277 if (page_nr != last_page_nr) {
1279 __bm_unmap(p_addr, km);
1281 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1283 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1286 p_addr = __bm_map_pidx(b, page_nr, km);
1287 last_page_nr = page_nr;
1290 c += (0 == generic___test_and_set_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr));
1292 c -= (0 != generic___test_and_clear_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr));
1295 __bm_unmap(p_addr, km);
1297 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1299 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1301 b->bm_set += changed_total;
1302 return changed_total;
1305 /* returns number of bits actually changed.
1306 * for val != 0, we change 0 -> 1, return code positive
1307 * for val == 0, we change 1 -> 0, return code negative
1308 * wants bitnr, not sector */
1309 static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
1310 const unsigned long e, int val)
1312 unsigned long flags;
1313 struct drbd_bitmap *b = mdev->bitmap;
1316 ERR_IF(!b) return 1;
1317 ERR_IF(!b->bm_pages) return 0;
1319 spin_lock_irqsave(&b->bm_lock, flags);
1320 if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags)
1321 bm_print_lock_info(mdev);
1323 c = __bm_change_bits_to(mdev, s, e, val, KM_IRQ1);
1325 spin_unlock_irqrestore(&b->bm_lock, flags);
1329 /* returns number of bits changed 0 -> 1 */
1330 int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1332 return bm_change_bits_to(mdev, s, e, 1);
1335 /* returns number of bits changed 1 -> 0 */
1336 int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1338 return -bm_change_bits_to(mdev, s, e, 0);
1341 /* sets all bits in full words,
1342 * from first_word up to, but not including, last_word */
1343 static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
1344 int page_nr, int first_word, int last_word)
1348 unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_USER0);
1349 for (i = first_word; i < last_word; i++) {
1350 bits = hweight_long(paddr[i]);
1352 b->bm_set += BITS_PER_LONG - bits;
1354 kunmap_atomic(paddr, KM_USER0);
1357 /* Same thing as drbd_bm_set_bits, but without taking the spin_lock_irqsave.
1358 * You must first drbd_bm_lock().
1359 * Can be called to set the whole bitmap in one go.
1360 * Sets bits from s to e _inclusive_. */
1361 void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1363 /* First set_bit from the first bit (s)
1364 * up to the next long boundary (sl),
1365 * then assign full words up to the last long boundary (el),
1366 * then set_bit up to and including the last bit (e).
1368 * Do not use memset, because we must account for changes,
1369 * so we need to loop over the words with hweight() anyways.
1371 unsigned long sl = ALIGN(s,BITS_PER_LONG);
1372 unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
1379 if (e - s <= 3*BITS_PER_LONG) {
1380 /* don't bother; el and sl may even be wrong. */
1381 __bm_change_bits_to(mdev, s, e, 1, KM_USER0);
1385 /* difference is large enough that we can trust sl and el */
1387 /* bits filling the current long */
1389 __bm_change_bits_to(mdev, s, sl-1, 1, KM_USER0);
1391 first_page = sl >> (3 + PAGE_SHIFT);
1392 last_page = el >> (3 + PAGE_SHIFT);
1394 /* MLPP: modulo longs per page */
1395 /* LWPP: long words per page */
1396 first_word = MLPP(sl >> LN2_BPL);
1399 /* first and full pages, unless first page == last page */
1400 for (page_nr = first_page; page_nr < last_page; page_nr++) {
1401 bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
1406 /* last page (respectively only page, for first page == last page) */
1407 last_word = MLPP(el >> LN2_BPL);
1408 bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);
1410 /* possibly trailing bits.
1411 * example: (e & 63) == 63, el will be e+1.
1412 * if that even was the very last bit,
1413 * it would trigger an assert in __bm_change_bits_to()
1416 __bm_change_bits_to(mdev, el, e, 1, KM_USER0);
1419 /* returns bit state
1420 * wants bitnr, NOT sector.
1421 * inherently racy... area needs to be locked by means of {al,rs}_lru
1424 * -1 ... first out of bounds access, stop testing for bits!
1426 int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
1428 unsigned long flags;
1429 struct drbd_bitmap *b = mdev->bitmap;
1430 unsigned long *p_addr;
1433 ERR_IF(!b) return 0;
1434 ERR_IF(!b->bm_pages) return 0;
1436 spin_lock_irqsave(&b->bm_lock, flags);
1437 if (BM_DONT_TEST & b->bm_flags)
1438 bm_print_lock_info(mdev);
1439 if (bitnr < b->bm_bits) {
1440 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
1441 i = generic_test_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
1443 } else if (bitnr == b->bm_bits) {
1445 } else { /* (bitnr > b->bm_bits) */
1446 dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
1450 spin_unlock_irqrestore(&b->bm_lock, flags);
1454 /* returns number of bits set in the range [s, e] */
1455 int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1457 unsigned long flags;
1458 struct drbd_bitmap *b = mdev->bitmap;
1459 unsigned long *p_addr = NULL;
1460 unsigned long bitnr;
1461 unsigned int page_nr = -1U;
1464 /* If this is called without a bitmap, that is a bug. But just to be
1465 * robust in case we screwed up elsewhere, in that case pretend there
1466 * was one dirty bit in the requested area, so we won't try to do a
1467 * local read there (no bitmap probably implies no disk) */
1468 ERR_IF(!b) return 1;
1469 ERR_IF(!b->bm_pages) return 1;
1471 spin_lock_irqsave(&b->bm_lock, flags);
1472 if (BM_DONT_TEST & b->bm_flags)
1473 bm_print_lock_info(mdev);
1474 for (bitnr = s; bitnr <= e; bitnr++) {
1475 unsigned int idx = bm_bit_to_page_idx(b, bitnr);
1476 if (page_nr != idx) {
1480 p_addr = bm_map_pidx(b, idx);
1482 ERR_IF (bitnr >= b->bm_bits) {
1483 dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
1485 c += (0 != generic_test_le_bit(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
1490 spin_unlock_irqrestore(&b->bm_lock, flags);
1495 /* inherently racy...
1496 * return value may be already out-of-date when this function returns.
1497 * but the general usage is that this is only use during a cstate when bits are
1498 * only cleared, not set, and typically only care for the case when the return
1499 * value is zero, or we already "locked" this "bitmap extent" by other means.
1501 * enr is bm-extent number, since we chose to name one sector (512 bytes)
1502 * worth of the bitmap a "bitmap extent".
1505 * I think since we use it like a reference count, we should use the real
1506 * reference count of some bitmap extent element from some lru instead...
1509 int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
1511 struct drbd_bitmap *b = mdev->bitmap;
1513 unsigned long flags;
1514 unsigned long *p_addr, *bm;
1516 ERR_IF(!b) return 0;
1517 ERR_IF(!b->bm_pages) return 0;
1519 spin_lock_irqsave(&b->bm_lock, flags);
1520 if (BM_DONT_TEST & b->bm_flags)
1521 bm_print_lock_info(mdev);
1524 e = min((size_t)S2W(enr+1), b->bm_words);
1526 if (s < b->bm_words) {
1528 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
1529 bm = p_addr + MLPP(s);
1531 count += hweight_long(*bm++);
1534 dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
1536 spin_unlock_irqrestore(&b->bm_lock, flags);
1540 /* Set all bits covered by the AL-extent al_enr.
1541 * Returns number of bits changed. */
1542 unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
1544 struct drbd_bitmap *b = mdev->bitmap;
1545 unsigned long *p_addr, *bm;
1546 unsigned long weight;
1548 int count, i, do_now;
1549 ERR_IF(!b) return 0;
1550 ERR_IF(!b->bm_pages) return 0;
1552 spin_lock_irq(&b->bm_lock);
1553 if (BM_DONT_SET & b->bm_flags)
1554 bm_print_lock_info(mdev);
1557 s = al_enr * BM_WORDS_PER_AL_EXT;
1558 e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
1559 /* assert that s and e are on the same page */
1560 D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
1561 == s >> (PAGE_SHIFT - LN2_BPL + 3));
1563 if (s < b->bm_words) {
1565 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
1566 bm = p_addr + MLPP(s);
1568 count += hweight_long(*bm);
1573 b->bm_set += do_now*BITS_PER_LONG - count;
1574 if (e == b->bm_words)
1575 b->bm_set -= bm_clear_surplus(b);
1577 dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s);
1579 weight = b->bm_set - weight;
1580 spin_unlock_irq(&b->bm_lock);