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>
34 /* OPAQUE outside this file!
35 * interface defined in drbd_int.h
38 * function name drbd_bm_... => used elsewhere, "public".
39 * function name bm_... => internal to implementation, "private".
45 * We want to support >= peta byte of backend storage, while for now still using
46 * a granularity of one bit per 4KiB of storage.
47 * 1 << 50 bytes backend storage (1 PiB)
48 * 1 << (50 - 12) bits needed
49 * 38 --> we need u64 to index and count bits
50 * 1 << (38 - 3) bitmap bytes needed
51 * 35 --> we still need u64 to index and count bytes
52 * (that's 32 GiB of bitmap for 1 PiB storage)
53 * 1 << (35 - 2) 32bit longs needed
54 * 33 --> we'd even need u64 to index and count 32bit long words.
55 * 1 << (35 - 3) 64bit longs needed
56 * 32 --> we could get away with a 32bit unsigned int to index and count
57 * 64bit long words, but I rather stay with unsigned long for now.
58 * We probably should neither count nor point to bytes or long words
59 * directly, but either by bitnumber, or by page index and offset.
61 * 22 --> we need that much 4KiB pages of bitmap.
62 * 1 << (22 + 3) --> on a 64bit arch,
63 * we need 32 MiB to store the array of page pointers.
65 * Because I'm lazy, and because the resulting patch was too large, too ugly
66 * and still incomplete, on 32bit we still "only" support 16 TiB (minus some),
67 * (1 << 32) bits * 4k storage.
70 * bitmap storage and IO:
71 * Bitmap is stored little endian on disk, and is kept little endian in
72 * core memory. Currently we still hold the full bitmap in core as long
73 * as we are "attached" to a local disk, which at 32 GiB for 1PiB storage
76 * We plan to reduce the amount of in-core bitmap pages by pageing them in
77 * and out against their on-disk location as necessary, but need to make
78 * sure we don't cause too much meta data IO, and must not deadlock in
79 * tight memory situations. This needs some more work.
84 * Access to the *bm_pages is protected by bm_lock.
85 * It is safe to read the other members within the lock.
87 * drbd_bm_set_bits is called from bio_endio callbacks,
88 * We may be called with irq already disabled,
89 * so we need spin_lock_irqsave().
90 * And we need the kmap_atomic.
93 struct page **bm_pages;
96 /* see LIMITATIONS: above */
98 unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */
99 unsigned long bm_bits;
101 size_t bm_number_of_pages;
102 sector_t bm_dev_capacity;
103 struct mutex bm_change; /* serializes resize operations */
105 wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */
107 unsigned long bm_flags;
109 /* debugging aid, in case we are still racy somewhere */
111 struct task_struct *bm_task;
114 /* definition of bits in bm_flags */
116 // #define BM_MD_IO_ERROR 1 unused now.
117 #define BM_P_VMALLOCED 2
119 static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
120 unsigned long e, int val, const enum km_type km);
122 static int bm_is_locked(struct drbd_bitmap *b)
124 return test_bit(BM_LOCKED, &b->bm_flags);
127 #define bm_print_lock_info(m) __bm_print_lock_info(m, __func__)
128 static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func)
130 struct drbd_bitmap *b = mdev->bitmap;
131 if (!__ratelimit(&drbd_ratelimit_state))
133 dev_err(DEV, "FIXME %s in %s, bitmap locked for '%s' by %s\n",
134 current == mdev->receiver.task ? "receiver" :
135 current == mdev->asender.task ? "asender" :
136 current == mdev->worker.task ? "worker" : current->comm,
137 func, b->bm_why ?: "?",
138 b->bm_task == mdev->receiver.task ? "receiver" :
139 b->bm_task == mdev->asender.task ? "asender" :
140 b->bm_task == mdev->worker.task ? "worker" : "?");
143 void drbd_bm_lock(struct drbd_conf *mdev, char *why)
145 struct drbd_bitmap *b = mdev->bitmap;
149 dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n");
153 trylock_failed = !mutex_trylock(&b->bm_change);
155 if (trylock_failed) {
156 dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n",
157 current == mdev->receiver.task ? "receiver" :
158 current == mdev->asender.task ? "asender" :
159 current == mdev->worker.task ? "worker" : current->comm,
160 why, b->bm_why ?: "?",
161 b->bm_task == mdev->receiver.task ? "receiver" :
162 b->bm_task == mdev->asender.task ? "asender" :
163 b->bm_task == mdev->worker.task ? "worker" : "?");
164 mutex_lock(&b->bm_change);
166 if (__test_and_set_bit(BM_LOCKED, &b->bm_flags))
167 dev_err(DEV, "FIXME bitmap already locked in bm_lock\n");
170 b->bm_task = current;
173 void drbd_bm_unlock(struct drbd_conf *mdev)
175 struct drbd_bitmap *b = mdev->bitmap;
177 dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n");
181 if (!__test_and_clear_bit(BM_LOCKED, &mdev->bitmap->bm_flags))
182 dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");
186 mutex_unlock(&b->bm_change);
189 /* we store some "meta" info about our pages in page->private */
190 /* at a granularity of 4k storage per bitmap bit:
191 * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks
193 * 1<<23 4k bitmap pages.
194 * Use 24 bits as page index, covers 2 peta byte storage
195 * at a granularity of 4k per bit.
196 * Used to report the failed page idx on io error from the endio handlers.
198 #define BM_PAGE_IDX_MASK ((1UL<<24)-1)
199 /* this page is currently read in, or written back */
200 #define BM_PAGE_IO_LOCK 31
201 /* if there has been an IO error for this page */
202 #define BM_PAGE_IO_ERROR 30
203 /* this is to be able to intelligently skip disk IO,
204 * set if bits have been set since last IO. */
205 #define BM_PAGE_NEED_WRITEOUT 29
206 /* to mark for lazy writeout once syncer cleared all clearable bits,
207 * we if bits have been cleared since last IO. */
208 #define BM_PAGE_LAZY_WRITEOUT 28
210 /* store_page_idx uses non-atomic assingment. It is only used directly after
211 * allocating the page. All other bm_set_page_* and bm_clear_page_* need to
212 * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap
213 * changes) may happen from various contexts, and wait_on_bit/wake_up_bit
214 * requires it all to be atomic as well. */
215 static void bm_store_page_idx(struct page *page, unsigned long idx)
217 BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
218 page_private(page) |= idx;
221 static unsigned long bm_page_to_idx(struct page *page)
223 return page_private(page) & BM_PAGE_IDX_MASK;
226 /* As is very unlikely that the same page is under IO from more than one
227 * context, we can get away with a bit per page and one wait queue per bitmap.
229 static void bm_page_lock_io(struct drbd_conf *mdev, int page_nr)
231 struct drbd_bitmap *b = mdev->bitmap;
232 void *addr = &page_private(b->bm_pages[page_nr]);
233 wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
236 static void bm_page_unlock_io(struct drbd_conf *mdev, int page_nr)
238 struct drbd_bitmap *b = mdev->bitmap;
239 void *addr = &page_private(b->bm_pages[page_nr]);
240 clear_bit(BM_PAGE_IO_LOCK, addr);
241 smp_mb__after_clear_bit();
242 wake_up(&mdev->bitmap->bm_io_wait);
245 /* set _before_ submit_io, so it may be reset due to being changed
246 * while this page is in flight... will get submitted later again */
247 static void bm_set_page_unchanged(struct page *page)
250 clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
251 clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
254 static void bm_set_page_need_writeout(struct page *page)
256 set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
259 static int bm_test_page_unchanged(struct page *page)
261 volatile const unsigned long *addr = &page_private(page);
262 return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0;
265 static void bm_set_page_io_err(struct page *page)
267 set_bit(BM_PAGE_IO_ERROR, &page_private(page));
270 static void bm_clear_page_io_err(struct page *page)
272 clear_bit(BM_PAGE_IO_ERROR, &page_private(page));
275 static void bm_set_page_lazy_writeout(struct page *page)
277 set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
280 static int bm_test_page_lazy_writeout(struct page *page)
282 return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
285 /* on a 32bit box, this would allow for exactly (2<<38) bits. */
286 static unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr)
288 /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
289 unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
290 BUG_ON(page_nr >= b->bm_number_of_pages);
294 static unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr)
296 /* page_nr = (bitnr/8) >> PAGE_SHIFT; */
297 unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3);
298 BUG_ON(page_nr >= b->bm_number_of_pages);
302 static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx, const enum km_type km)
304 struct page *page = b->bm_pages[idx];
305 return (unsigned long *) kmap_atomic(page, km);
308 static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
310 return __bm_map_pidx(b, idx, KM_IRQ1);
313 static void __bm_unmap(unsigned long *p_addr, const enum km_type km)
315 kunmap_atomic(p_addr, km);
318 static void bm_unmap(unsigned long *p_addr)
320 return __bm_unmap(p_addr, KM_IRQ1);
323 /* long word offset of _bitmap_ sector */
324 #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
325 /* word offset from start of bitmap to word number _in_page_
326 * modulo longs per page
327 #define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
328 hm, well, Philipp thinks gcc might not optimze the % into & (... - 1)
331 #define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
333 /* Long words per page */
334 #define LWPP (PAGE_SIZE/sizeof(long))
337 * actually most functions herein should take a struct drbd_bitmap*, not a
338 * struct drbd_conf*, but for the debug macros I like to have the mdev around
339 * to be able to report device specific.
343 static void bm_free_pages(struct page **pages, unsigned long number)
349 for (i = 0; i < number; i++) {
351 printk(KERN_ALERT "drbd: bm_free_pages tried to free "
352 "a NULL pointer; i=%lu n=%lu\n",
356 __free_page(pages[i]);
361 static void bm_vk_free(void *ptr, int v)
370 * "have" and "want" are NUMBER OF PAGES.
372 static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
374 struct page **old_pages = b->bm_pages;
375 struct page **new_pages, *page;
376 unsigned int i, bytes, vmalloced = 0;
377 unsigned long have = b->bm_number_of_pages;
379 BUG_ON(have == 0 && old_pages != NULL);
380 BUG_ON(have != 0 && old_pages == NULL);
385 /* Trying kmalloc first, falling back to vmalloc.
386 * GFP_KERNEL is ok, as this is done when a lower level disk is
387 * "attached" to the drbd. Context is receiver thread or cqueue
388 * thread. As we have no disk yet, we are not in the IO path,
389 * not even the IO path of the peer. */
390 bytes = sizeof(struct page *)*want;
391 new_pages = kmalloc(bytes, GFP_KERNEL);
393 new_pages = vmalloc(bytes);
399 memset(new_pages, 0, bytes);
401 for (i = 0; i < have; i++)
402 new_pages[i] = old_pages[i];
403 for (; i < want; i++) {
404 page = alloc_page(GFP_HIGHUSER);
406 bm_free_pages(new_pages + have, i - have);
407 bm_vk_free(new_pages, vmalloced);
410 /* we want to know which page it is
411 * from the endio handlers */
412 bm_store_page_idx(page, i);
416 for (i = 0; i < want; i++)
417 new_pages[i] = old_pages[i];
418 /* NOT HERE, we are outside the spinlock!
419 bm_free_pages(old_pages + want, have - want);
424 set_bit(BM_P_VMALLOCED, &b->bm_flags);
426 clear_bit(BM_P_VMALLOCED, &b->bm_flags);
432 * called on driver init only. TODO call when a device is created.
433 * allocates the drbd_bitmap, and stores it in mdev->bitmap.
435 int drbd_bm_init(struct drbd_conf *mdev)
437 struct drbd_bitmap *b = mdev->bitmap;
439 b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
442 spin_lock_init(&b->bm_lock);
443 mutex_init(&b->bm_change);
444 init_waitqueue_head(&b->bm_io_wait);
451 sector_t drbd_bm_capacity(struct drbd_conf *mdev)
453 ERR_IF(!mdev->bitmap) return 0;
454 return mdev->bitmap->bm_dev_capacity;
457 /* called on driver unload. TODO: call when a device is destroyed.
459 void drbd_bm_cleanup(struct drbd_conf *mdev)
461 ERR_IF (!mdev->bitmap) return;
462 bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
463 bm_vk_free(mdev->bitmap->bm_pages, test_bit(BM_P_VMALLOCED, &mdev->bitmap->bm_flags));
469 * since (b->bm_bits % BITS_PER_LONG) != 0,
470 * this masks out the remaining bits.
471 * Returns the number of bits cleared.
473 #define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3))
474 #define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1)
475 #define BITS_PER_LONG_MASK (BITS_PER_LONG - 1)
476 static int bm_clear_surplus(struct drbd_bitmap *b)
479 unsigned long *p_addr, *bm;
483 /* number of bits modulo bits per page */
484 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
485 /* mask the used bits of the word containing the last bit */
486 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
487 /* bitmap is always stored little endian,
488 * on disk and in core memory alike */
489 mask = cpu_to_lel(mask);
491 /* because of the "extra long to catch oob access" we allocate in
492 * drbd_bm_resize, bm_number_of_pages -1 is not necessarily the page
493 * containing the last _relevant_ bitmap word */
494 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, b->bm_bits - 1));
495 bm = p_addr + (tmp/BITS_PER_LONG);
497 /* If mask != 0, we are not exactly aligned, so bm now points
498 * to the long containing the last bit.
499 * If mask == 0, bm already points to the word immediately
500 * after the last (long word aligned) bit. */
501 cleared = hweight_long(*bm & ~mask);
506 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
507 /* on a 32bit arch, we may need to zero out
508 * a padding long to align with a 64bit remote */
509 cleared += hweight_long(*bm);
516 static void bm_set_surplus(struct drbd_bitmap *b)
519 unsigned long *p_addr, *bm;
522 /* number of bits modulo bits per page */
523 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
524 /* mask the used bits of the word containing the last bit */
525 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
526 /* bitmap is always stored little endian,
527 * on disk and in core memory alike */
528 mask = cpu_to_lel(mask);
530 /* because of the "extra long to catch oob access" we allocate in
531 * drbd_bm_resize, bm_number_of_pages -1 is not necessarily the page
532 * containing the last _relevant_ bitmap word */
533 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, b->bm_bits - 1));
534 bm = p_addr + (tmp/BITS_PER_LONG);
536 /* If mask != 0, we are not exactly aligned, so bm now points
537 * to the long containing the last bit.
538 * If mask == 0, bm already points to the word immediately
539 * after the last (long word aligned) bit. */
544 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
545 /* on a 32bit arch, we may need to zero out
546 * a padding long to align with a 64bit remote */
552 /* you better not modify the bitmap while this is running,
553 * or its results will be stale */
554 static unsigned long bm_count_bits(struct drbd_bitmap *b)
556 unsigned long *p_addr;
557 unsigned long bits = 0;
558 unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1;
559 int idx, last_page, i, last_word;
561 /* because of the "extra long to catch oob access" we allocate in
562 * drbd_bm_resize, bm_number_of_pages -1 is not necessarily the page
563 * containing the last _relevant_ bitmap word */
564 last_page = bm_bit_to_page_idx(b, b->bm_bits-1);
566 /* all but last page */
567 for (idx = 0; idx < last_page; idx++) {
568 p_addr = __bm_map_pidx(b, idx, KM_USER0);
569 for (i = 0; i < LWPP; i++)
570 bits += hweight_long(p_addr[i]);
571 __bm_unmap(p_addr, KM_USER0);
574 /* last (or only) page */
575 last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL;
576 p_addr = __bm_map_pidx(b, idx, KM_USER0);
577 for (i = 0; i < last_word; i++)
578 bits += hweight_long(p_addr[i]);
579 p_addr[last_word] &= cpu_to_lel(mask);
580 bits += hweight_long(p_addr[last_word]);
581 /* 32bit arch, may have an unused padding long */
582 if (BITS_PER_LONG == 32 && (last_word & 1) == 0)
583 p_addr[last_word+1] = 0;
584 __bm_unmap(p_addr, KM_USER0);
588 /* offset and len in long words.*/
589 static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
591 unsigned long *p_addr, *bm;
597 if (end > b->bm_words) {
598 printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
602 while (offset < end) {
603 do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
604 idx = bm_word_to_page_idx(b, offset);
605 p_addr = bm_map_pidx(b, idx);
606 bm = p_addr + MLPP(offset);
607 if (bm+do_now > p_addr + LWPP) {
608 printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
609 p_addr, bm, (int)do_now);
610 break; /* breaks to after catch_oob_access_end() only! */
612 memset(bm, c, do_now * sizeof(long));
614 bm_set_page_need_writeout(b->bm_pages[idx]);
620 * make sure the bitmap has enough room for the attached storage,
621 * if necessary, resize.
622 * called whenever we may have changed the device size.
623 * returns -ENOMEM if we could not allocate enough memory, 0 on success.
624 * In case this is actually a resize, we copy the old bitmap into the new one.
625 * Otherwise, the bitmap is initialized to all bits set.
627 int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
629 struct drbd_bitmap *b = mdev->bitmap;
630 unsigned long bits, words, owords, obits, *p_addr, *bm;
631 unsigned long want, have, onpages; /* number of pages */
632 struct page **npages, **opages = NULL;
633 int err = 0, growing;
634 int opages_vmalloced;
636 ERR_IF(!b) return -ENOMEM;
638 drbd_bm_lock(mdev, "resize");
640 dev_info(DEV, "drbd_bm_resize called with capacity == %llu\n",
641 (unsigned long long)capacity);
643 if (capacity == b->bm_dev_capacity)
646 opages_vmalloced = test_bit(BM_P_VMALLOCED, &b->bm_flags);
649 spin_lock_irq(&b->bm_lock);
650 opages = b->bm_pages;
651 onpages = b->bm_number_of_pages;
652 owords = b->bm_words;
654 b->bm_number_of_pages =
658 b->bm_dev_capacity = 0;
659 spin_unlock_irq(&b->bm_lock);
660 bm_free_pages(opages, onpages);
661 bm_vk_free(opages, opages_vmalloced);
664 bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));
667 words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
668 a 32bit host could present the wrong number of words
671 words = ALIGN(bits, 64) >> LN2_BPL;
673 if (get_ldev(mdev)) {
674 u64 bits_on_disk = ((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12;
676 if (bits > bits_on_disk) {
677 dev_info(DEV, "bits = %lu\n", bits);
678 dev_info(DEV, "bits_on_disk = %llu\n", bits_on_disk);
684 /* one extra long to catch off by one errors */
685 want = ALIGN((words+1)*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT;
686 have = b->bm_number_of_pages;
688 D_ASSERT(b->bm_pages != NULL);
689 npages = b->bm_pages;
691 if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC))
694 npages = bm_realloc_pages(b, want);
702 spin_lock_irq(&b->bm_lock);
703 opages = b->bm_pages;
704 owords = b->bm_words;
707 growing = bits > obits;
708 if (opages && growing && set_new_bits)
711 b->bm_pages = npages;
712 b->bm_number_of_pages = want;
715 b->bm_dev_capacity = capacity;
719 bm_memset(b, owords, 0xff, words-owords);
720 b->bm_set += bits - obits;
722 bm_memset(b, owords, 0x00, words-owords);
727 /* implicit: (opages != NULL) && (opages != npages) */
728 bm_free_pages(opages + want, have - want);
731 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, words));
732 bm = p_addr + MLPP(words);
736 (void)bm_clear_surplus(b);
738 spin_unlock_irq(&b->bm_lock);
739 if (opages != npages)
740 bm_vk_free(opages, opages_vmalloced);
742 b->bm_set = bm_count_bits(b);
743 dev_info(DEV, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);
746 drbd_bm_unlock(mdev);
751 * if not protected by other means, return value may be out of date when
752 * leaving this function...
753 * we still need to lock it, since it is important that this returns
754 * bm_set == 0 precisely.
756 * maybe bm_set should be atomic_t ?
758 unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev)
760 struct drbd_bitmap *b = mdev->bitmap;
765 ERR_IF(!b->bm_pages) return 0;
767 spin_lock_irqsave(&b->bm_lock, flags);
769 spin_unlock_irqrestore(&b->bm_lock, flags);
774 unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
777 /* if I don't have a disk, I don't know about out-of-sync status */
778 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
780 s = _drbd_bm_total_weight(mdev);
785 size_t drbd_bm_words(struct drbd_conf *mdev)
787 struct drbd_bitmap *b = mdev->bitmap;
789 ERR_IF(!b->bm_pages) return 0;
794 unsigned long drbd_bm_bits(struct drbd_conf *mdev)
796 struct drbd_bitmap *b = mdev->bitmap;
802 /* merge number words from buffer into the bitmap starting at offset.
803 * buffer[i] is expected to be little endian unsigned long.
804 * bitmap must be locked by drbd_bm_lock.
805 * currently only used from receive_bitmap.
807 void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
808 unsigned long *buffer)
810 struct drbd_bitmap *b = mdev->bitmap;
811 unsigned long *p_addr, *bm;
812 unsigned long word, bits;
816 end = offset + number;
819 ERR_IF(!b->bm_pages) return;
822 WARN_ON(offset >= b->bm_words);
823 WARN_ON(end > b->bm_words);
825 spin_lock_irq(&b->bm_lock);
826 while (offset < end) {
827 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
828 idx = bm_word_to_page_idx(b, offset);
829 p_addr = bm_map_pidx(b, idx);
830 bm = p_addr + MLPP(offset);
833 bits = hweight_long(*bm);
834 word = *bm | *buffer++;
836 b->bm_set += hweight_long(word) - bits;
839 bm_set_page_need_writeout(b->bm_pages[idx]);
841 /* with 32bit <-> 64bit cross-platform connect
842 * this is only correct for current usage,
843 * where we _know_ that we are 64 bit aligned,
844 * and know that this function is used in this way, too...
846 if (end == b->bm_words)
847 b->bm_set -= bm_clear_surplus(b);
849 spin_unlock_irq(&b->bm_lock);
852 /* copy number words from the bitmap starting at offset into the buffer.
853 * buffer[i] will be little endian unsigned long.
855 void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
856 unsigned long *buffer)
858 struct drbd_bitmap *b = mdev->bitmap;
859 unsigned long *p_addr, *bm;
862 end = offset + number;
865 ERR_IF(!b->bm_pages) return;
867 spin_lock_irq(&b->bm_lock);
868 if ((offset >= b->bm_words) ||
869 (end > b->bm_words) ||
871 dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n",
872 (unsigned long) offset,
873 (unsigned long) number,
874 (unsigned long) b->bm_words);
876 while (offset < end) {
877 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
878 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
879 bm = p_addr + MLPP(offset);
886 spin_unlock_irq(&b->bm_lock);
889 /* set all bits in the bitmap */
890 void drbd_bm_set_all(struct drbd_conf *mdev)
892 struct drbd_bitmap *b = mdev->bitmap;
894 ERR_IF(!b->bm_pages) return;
896 spin_lock_irq(&b->bm_lock);
897 bm_memset(b, 0, 0xff, b->bm_words);
898 (void)bm_clear_surplus(b);
899 b->bm_set = b->bm_bits;
900 spin_unlock_irq(&b->bm_lock);
903 /* clear all bits in the bitmap */
904 void drbd_bm_clear_all(struct drbd_conf *mdev)
906 struct drbd_bitmap *b = mdev->bitmap;
908 ERR_IF(!b->bm_pages) return;
910 spin_lock_irq(&b->bm_lock);
911 bm_memset(b, 0, 0, b->bm_words);
913 spin_unlock_irq(&b->bm_lock);
917 struct drbd_conf *mdev;
919 wait_queue_head_t io_wait;
921 #define BM_AIO_COPY_PAGES 1
925 /* bv_page may be a copy, or may be the original */
926 static void bm_async_io_complete(struct bio *bio, int error)
928 struct bm_aio_ctx *ctx = bio->bi_private;
929 struct drbd_conf *mdev = ctx->mdev;
930 struct drbd_bitmap *b = mdev->bitmap;
931 unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page);
932 int uptodate = bio_flagged(bio, BIO_UPTODATE);
935 /* strange behavior of some lower level drivers...
936 * fail the request by clearing the uptodate flag,
937 * but do not return any error?!
938 * do we want to WARN() on this? */
939 if (!error && !uptodate)
942 if (!bm_test_page_unchanged(b->bm_pages[idx]))
943 dev_info(DEV, "bitmap page idx %u changed during IO!\n", idx);
946 /* ctx error will hold the completed-last non-zero error code,
947 * in case error codes differ. */
949 bm_set_page_io_err(b->bm_pages[idx]);
950 /* Not identical to on disk version of it.
951 * Is BM_PAGE_IO_ERROR enough? */
952 if (__ratelimit(&drbd_ratelimit_state))
953 dev_err(DEV, "IO ERROR %d on bitmap page idx %u\n",
956 bm_clear_page_io_err(b->bm_pages[idx]);
957 dynamic_dev_dbg(DEV, "bitmap page idx %u completed\n", idx);
960 bm_page_unlock_io(mdev, idx);
962 /* FIXME give back to page pool */
963 if (ctx->flags & BM_AIO_COPY_PAGES)
964 put_page(bio->bi_io_vec[0].bv_page);
968 if (atomic_dec_and_test(&ctx->in_flight))
969 wake_up(&ctx->io_wait);
972 static void bm_page_io_async(struct bm_aio_ctx *ctx, int page_nr, int rw) __must_hold(local)
974 /* we are process context. we always get a bio */
975 struct bio *bio = bio_alloc(GFP_KERNEL, 1);
976 struct drbd_conf *mdev = ctx->mdev;
977 struct drbd_bitmap *b = mdev->bitmap;
981 sector_t on_disk_sector =
982 mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset;
983 on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);
985 /* this might happen with very small
986 * flexible external meta data device,
987 * or with PAGE_SIZE > 4k */
988 len = min_t(unsigned int, PAGE_SIZE,
989 (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);
991 /* serialize IO on this page */
992 bm_page_lock_io(mdev, page_nr);
993 /* before memcpy and submit,
994 * so it can be redirtied any time */
995 bm_set_page_unchanged(b->bm_pages[page_nr]);
997 if (ctx->flags & BM_AIO_COPY_PAGES) {
998 /* FIXME alloc_page is good enough for now, but actually needs
999 * to use pre-allocated page pool */
1001 page = alloc_page(__GFP_HIGHMEM|__GFP_WAIT);
1002 dest = kmap_atomic(page, KM_USER0);
1003 src = kmap_atomic(b->bm_pages[page_nr], KM_USER1);
1004 memcpy(dest, src, PAGE_SIZE);
1005 kunmap_atomic(src, KM_USER1);
1006 kunmap_atomic(dest, KM_USER0);
1007 bm_store_page_idx(page, page_nr);
1009 page = b->bm_pages[page_nr];
1011 bio->bi_bdev = mdev->ldev->md_bdev;
1012 bio->bi_sector = on_disk_sector;
1013 bio_add_page(bio, page, len, 0);
1014 bio->bi_private = ctx;
1015 bio->bi_end_io = bm_async_io_complete;
1017 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
1019 bio_endio(bio, -EIO);
1021 submit_bio(rw, bio);
1026 * bm_rw: read/write the whole bitmap from/to its on disk location.
1028 static int bm_rw(struct drbd_conf *mdev, int rw, unsigned lazy_writeout_upper_idx) __must_hold(local)
1030 struct bm_aio_ctx ctx =
1031 { .flags = lazy_writeout_upper_idx ? BM_AIO_COPY_PAGES : 0 };
1032 struct drbd_bitmap *b = mdev->bitmap;
1033 int last_page, i, count = 0;
1039 * We are protected against bitmap disappearing/resizing by holding an
1040 * ldev reference (caller must have called get_ldev()).
1041 * For read/write, we are protected against changes to the bitmap by
1042 * the bitmap lock (see drbd_bitmap_io).
1043 * For lazy writeout, we don't care for ongoing changes to the bitmap,
1044 * as we submit copies of pages anyways.
1047 WARN_ON(!bm_is_locked(b));
1049 /* because of the "extra long to catch oob access" we allocate in
1050 * drbd_bm_resize, bm_number_of_pages -1 is not necessarily the page
1051 * containing the last _relevant_ bitmap word */
1052 last_page = bm_word_to_page_idx(b, b->bm_words - 1);
1056 atomic_set(&ctx.in_flight, 1); /* one extra ref */
1057 init_waitqueue_head(&ctx.io_wait);
1060 /* let the layers below us try to merge these bios... */
1061 for (i = 0; i <= last_page; i++) {
1062 /* ignore completely unchanged pages */
1063 if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
1066 if (bm_test_page_unchanged(b->bm_pages[i])) {
1067 dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
1070 /* during lazy writeout,
1071 * ignore those pages not marked for lazy writeout. */
1072 if (lazy_writeout_upper_idx &&
1073 !bm_test_page_lazy_writeout(b->bm_pages[i])) {
1074 dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
1078 atomic_inc(&ctx.in_flight);
1079 bm_page_io_async(&ctx, i, rw);
1084 atomic_dec(&ctx.in_flight); /* drop the extra ref */
1085 wait_event(ctx.io_wait, atomic_read(&ctx.in_flight) == 0);
1086 dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
1087 rw == WRITE ? "WRITE" : "READ",
1088 count, jiffies - now);
1091 dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
1092 drbd_chk_io_error(mdev, 1, true);
1093 err = -EIO; /* ctx.error ? */
1098 drbd_md_flush(mdev);
1099 } else /* rw == READ */ {
1100 b->bm_set = bm_count_bits(b);
1101 dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
1106 dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
1107 ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
1113 * drbd_bm_read() - Read the whole bitmap from its on disk location.
1114 * @mdev: DRBD device.
1116 int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
1118 return bm_rw(mdev, READ, 0);
1122 * drbd_bm_write() - Write the whole bitmap to its on disk location.
1123 * @mdev: DRBD device.
1125 * Will only write pages that have changed since last IO.
1127 int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
1129 return bm_rw(mdev, WRITE, 0);
1133 * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
1134 * @mdev: DRBD device.
1135 * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
1137 int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
1139 return bm_rw(mdev, WRITE, upper_idx);
1144 * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
1145 * @mdev: DRBD device.
1146 * @idx: bitmap page index
1148 * We don't want to special case on logical_block_size of the backend device,
1149 * so we submit PAGE_SIZE aligned pieces.
1150 * Note that on "most" systems, PAGE_SIZE is 4k.
1152 * In case this becomes an issue on systems with larger PAGE_SIZE,
1153 * we may want to change this again to write 4k aligned 4k pieces.
1155 int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
1157 struct bm_aio_ctx ctx = { .flags = BM_AIO_COPY_PAGES, };
1159 if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
1160 dev_info(DEV, "skipped bm page write for idx %u\n", idx);
1165 atomic_set(&ctx.in_flight, 1);
1166 init_waitqueue_head(&ctx.io_wait);
1168 bm_page_io_async(&ctx, idx, WRITE_SYNC);
1169 wait_event(ctx.io_wait, atomic_read(&ctx.in_flight) == 0);
1172 drbd_chk_io_error(mdev, 1, true);
1173 /* that should force detach, so the in memory bitmap will be
1174 * gone in a moment as well. */
1176 mdev->bm_writ_cnt++;
1181 * find_first_bit returns int, we return unsigned long.
1182 * For this to work on 32bit arch with bitnumbers > (1<<32),
1183 * we'd need to return u64, and get a whole lot of other places
1184 * fixed where we still use unsigned long.
1186 * this returns a bit number, NOT a sector!
1188 static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
1189 const int find_zero_bit, const enum km_type km)
1191 struct drbd_bitmap *b = mdev->bitmap;
1192 unsigned long *p_addr;
1193 unsigned long bit_offset;
1197 if (bm_fo > b->bm_bits) {
1198 dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
1199 bm_fo = DRBD_END_OF_BITMAP;
1201 while (bm_fo < b->bm_bits) {
1202 /* bit offset of the first bit in the page */
1203 bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
1204 p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
1207 i = generic_find_next_zero_le_bit(p_addr,
1208 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
1210 i = generic_find_next_le_bit(p_addr,
1211 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
1213 __bm_unmap(p_addr, km);
1214 if (i < PAGE_SIZE*8) {
1215 bm_fo = bit_offset + i;
1216 if (bm_fo >= b->bm_bits)
1220 bm_fo = bit_offset + PAGE_SIZE*8;
1222 bm_fo = DRBD_END_OF_BITMAP;
1228 static unsigned long bm_find_next(struct drbd_conf *mdev,
1229 unsigned long bm_fo, const int find_zero_bit)
1231 struct drbd_bitmap *b = mdev->bitmap;
1232 unsigned long i = DRBD_END_OF_BITMAP;
1234 ERR_IF(!b) return i;
1235 ERR_IF(!b->bm_pages) return i;
1237 spin_lock_irq(&b->bm_lock);
1238 if (bm_is_locked(b))
1239 bm_print_lock_info(mdev);
1241 i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);
1243 spin_unlock_irq(&b->bm_lock);
1247 unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1249 return bm_find_next(mdev, bm_fo, 0);
1253 /* not yet needed for anything. */
1254 unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1256 return bm_find_next(mdev, bm_fo, 1);
1260 /* does not spin_lock_irqsave.
1261 * you must take drbd_bm_lock() first */
1262 unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1264 /* WARN_ON(!bm_is_locked(mdev)); */
1265 return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
1268 unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1270 /* WARN_ON(!bm_is_locked(mdev)); */
1271 return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
1274 /* returns number of bits actually changed.
1275 * for val != 0, we change 0 -> 1, return code positive
1276 * for val == 0, we change 1 -> 0, return code negative
1277 * wants bitnr, not sector.
1278 * expected to be called for only a few bits (e - s about BITS_PER_LONG).
1279 * Must hold bitmap lock already. */
1280 static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
1281 unsigned long e, int val, const enum km_type km)
1283 struct drbd_bitmap *b = mdev->bitmap;
1284 unsigned long *p_addr = NULL;
1285 unsigned long bitnr;
1286 unsigned int last_page_nr = -1U;
1288 int changed_total = 0;
1290 if (e >= b->bm_bits) {
1291 dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
1293 e = b->bm_bits ? b->bm_bits -1 : 0;
1295 for (bitnr = s; bitnr <= e; bitnr++) {
1296 unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
1297 if (page_nr != last_page_nr) {
1299 __bm_unmap(p_addr, km);
1301 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1303 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1306 p_addr = __bm_map_pidx(b, page_nr, km);
1307 last_page_nr = page_nr;
1310 c += (0 == generic___test_and_set_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr));
1312 c -= (0 != generic___test_and_clear_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr));
1315 __bm_unmap(p_addr, km);
1317 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1319 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1321 b->bm_set += changed_total;
1322 return changed_total;
1325 /* returns number of bits actually changed.
1326 * for val != 0, we change 0 -> 1, return code positive
1327 * for val == 0, we change 1 -> 0, return code negative
1328 * wants bitnr, not sector */
1329 static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
1330 const unsigned long e, int val)
1332 unsigned long flags;
1333 struct drbd_bitmap *b = mdev->bitmap;
1336 ERR_IF(!b) return 1;
1337 ERR_IF(!b->bm_pages) return 0;
1339 spin_lock_irqsave(&b->bm_lock, flags);
1340 if (bm_is_locked(b))
1341 bm_print_lock_info(mdev);
1343 c = __bm_change_bits_to(mdev, s, e, val, KM_IRQ1);
1345 spin_unlock_irqrestore(&b->bm_lock, flags);
1349 /* returns number of bits changed 0 -> 1 */
1350 int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1352 return bm_change_bits_to(mdev, s, e, 1);
1355 /* returns number of bits changed 1 -> 0 */
1356 int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1358 return -bm_change_bits_to(mdev, s, e, 0);
1361 /* sets all bits in full words,
1362 * from first_word up to, but not including, last_word */
1363 static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
1364 int page_nr, int first_word, int last_word)
1368 unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_USER0);
1369 for (i = first_word; i < last_word; i++) {
1370 bits = hweight_long(paddr[i]);
1372 b->bm_set += BITS_PER_LONG - bits;
1374 kunmap_atomic(paddr, KM_USER0);
1377 /* Same thing as drbd_bm_set_bits, but without taking the spin_lock_irqsave.
1378 * You must first drbd_bm_lock().
1379 * Can be called to set the whole bitmap in one go.
1380 * Sets bits from s to e _inclusive_. */
1381 void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1383 /* First set_bit from the first bit (s)
1384 * up to the next long boundary (sl),
1385 * then assign full words up to the last long boundary (el),
1386 * then set_bit up to and including the last bit (e).
1388 * Do not use memset, because we must account for changes,
1389 * so we need to loop over the words with hweight() anyways.
1391 unsigned long sl = ALIGN(s,BITS_PER_LONG);
1392 unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
1399 if (e - s <= 3*BITS_PER_LONG) {
1400 /* don't bother; el and sl may even be wrong. */
1401 __bm_change_bits_to(mdev, s, e, 1, KM_USER0);
1405 /* difference is large enough that we can trust sl and el */
1407 /* bits filling the current long */
1409 __bm_change_bits_to(mdev, s, sl-1, 1, KM_USER0);
1411 first_page = sl >> (3 + PAGE_SHIFT);
1412 last_page = el >> (3 + PAGE_SHIFT);
1414 /* MLPP: modulo longs per page */
1415 /* LWPP: long words per page */
1416 first_word = MLPP(sl >> LN2_BPL);
1419 /* first and full pages, unless first page == last page */
1420 for (page_nr = first_page; page_nr < last_page; page_nr++) {
1421 bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
1426 /* last page (respectively only page, for first page == last page) */
1427 last_word = MLPP(el >> LN2_BPL);
1428 bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);
1430 /* possibly trailing bits.
1431 * example: (e & 63) == 63, el will be e+1.
1432 * if that even was the very last bit,
1433 * it would trigger an assert in __bm_change_bits_to()
1436 __bm_change_bits_to(mdev, el, e, 1, KM_USER0);
1439 /* returns bit state
1440 * wants bitnr, NOT sector.
1441 * inherently racy... area needs to be locked by means of {al,rs}_lru
1444 * -1 ... first out of bounds access, stop testing for bits!
1446 int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
1448 unsigned long flags;
1449 struct drbd_bitmap *b = mdev->bitmap;
1450 unsigned long *p_addr;
1453 ERR_IF(!b) return 0;
1454 ERR_IF(!b->bm_pages) return 0;
1456 spin_lock_irqsave(&b->bm_lock, flags);
1457 if (bm_is_locked(b))
1458 bm_print_lock_info(mdev);
1459 if (bitnr < b->bm_bits) {
1460 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
1461 i = generic_test_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
1463 } else if (bitnr == b->bm_bits) {
1465 } else { /* (bitnr > b->bm_bits) */
1466 dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
1470 spin_unlock_irqrestore(&b->bm_lock, flags);
1474 /* returns number of bits set in the range [s, e] */
1475 int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1477 unsigned long flags;
1478 struct drbd_bitmap *b = mdev->bitmap;
1479 unsigned long *p_addr = NULL;
1480 unsigned long bitnr;
1481 unsigned int page_nr = -1U;
1484 /* If this is called without a bitmap, that is a bug. But just to be
1485 * robust in case we screwed up elsewhere, in that case pretend there
1486 * was one dirty bit in the requested area, so we won't try to do a
1487 * local read there (no bitmap probably implies no disk) */
1488 ERR_IF(!b) return 1;
1489 ERR_IF(!b->bm_pages) return 1;
1491 spin_lock_irqsave(&b->bm_lock, flags);
1492 if (bm_is_locked(b))
1493 bm_print_lock_info(mdev);
1494 for (bitnr = s; bitnr <= e; bitnr++) {
1495 unsigned int idx = bm_bit_to_page_idx(b, bitnr);
1496 if (page_nr != idx) {
1500 p_addr = bm_map_pidx(b, idx);
1502 ERR_IF (bitnr >= b->bm_bits) {
1503 dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
1505 c += (0 != generic_test_le_bit(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
1510 spin_unlock_irqrestore(&b->bm_lock, flags);
1515 /* inherently racy...
1516 * return value may be already out-of-date when this function returns.
1517 * but the general usage is that this is only use during a cstate when bits are
1518 * only cleared, not set, and typically only care for the case when the return
1519 * value is zero, or we already "locked" this "bitmap extent" by other means.
1521 * enr is bm-extent number, since we chose to name one sector (512 bytes)
1522 * worth of the bitmap a "bitmap extent".
1525 * I think since we use it like a reference count, we should use the real
1526 * reference count of some bitmap extent element from some lru instead...
1529 int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
1531 struct drbd_bitmap *b = mdev->bitmap;
1533 unsigned long flags;
1534 unsigned long *p_addr, *bm;
1536 ERR_IF(!b) return 0;
1537 ERR_IF(!b->bm_pages) return 0;
1539 spin_lock_irqsave(&b->bm_lock, flags);
1540 if (bm_is_locked(b))
1541 bm_print_lock_info(mdev);
1544 e = min((size_t)S2W(enr+1), b->bm_words);
1546 if (s < b->bm_words) {
1548 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
1549 bm = p_addr + MLPP(s);
1551 count += hweight_long(*bm++);
1554 dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
1556 spin_unlock_irqrestore(&b->bm_lock, flags);
1560 /* Set all bits covered by the AL-extent al_enr.
1561 * Returns number of bits changed. */
1562 unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
1564 struct drbd_bitmap *b = mdev->bitmap;
1565 unsigned long *p_addr, *bm;
1566 unsigned long weight;
1568 int count, i, do_now;
1569 ERR_IF(!b) return 0;
1570 ERR_IF(!b->bm_pages) return 0;
1572 spin_lock_irq(&b->bm_lock);
1573 if (bm_is_locked(b))
1574 bm_print_lock_info(mdev);
1577 s = al_enr * BM_WORDS_PER_AL_EXT;
1578 e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
1579 /* assert that s and e are on the same page */
1580 D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
1581 == s >> (PAGE_SHIFT - LN2_BPL + 3));
1583 if (s < b->bm_words) {
1585 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
1586 bm = p_addr + MLPP(s);
1588 count += hweight_long(*bm);
1593 b->bm_set += do_now*BITS_PER_LONG - count;
1594 if (e == b->bm_words)
1595 b->bm_set -= bm_clear_surplus(b);
1597 dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s);
1599 weight = b->bm_set - weight;
1600 spin_unlock_irq(&b->bm_lock);