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>
33 /* OPAQUE outside this file!
34 * interface defined in drbd_int.h
37 * function name drbd_bm_... => used elsewhere, "public".
38 * function name bm_... => internal to implementation, "private".
40 * Note that since find_first_bit returns int, at the current granularity of
41 * the bitmap (4KB per byte), this implementation "only" supports up to
42 * 1<<(32+12) == 16 TB...
47 * Access to the *bm_pages is protected by bm_lock.
48 * It is safe to read the other members within the lock.
50 * drbd_bm_set_bits is called from bio_endio callbacks,
51 * We may be called with irq already disabled,
52 * so we need spin_lock_irqsave().
53 * And we need the kmap_atomic.
56 struct page
**bm_pages
;
58 /* WARNING unsigned long bm_*:
59 * 32bit number of bit offset is just enough for 512 MB bitmap.
60 * it will blow up if we make the bitmap bigger...
61 * not that it makes much sense to have a bitmap that large,
62 * rather change the granularity to 16k or 64k or something.
63 * (that implies other problems, however...)
65 unsigned long bm_set
; /* nr of set bits; THINK maybe atomic_t? */
66 unsigned long bm_bits
;
68 size_t bm_number_of_pages
;
69 sector_t bm_dev_capacity
;
70 struct mutex bm_change
; /* serializes resize operations */
73 wait_queue_head_t bm_io_wait
;
75 unsigned long bm_flags
;
77 /* debugging aid, in case we are still racy somewhere */
79 struct task_struct
*bm_task
;
82 /* definition of bits in bm_flags */
84 #define BM_MD_IO_ERROR 1
85 #define BM_P_VMALLOCED 2
87 static int __bm_change_bits_to(struct drbd_conf
*mdev
, const unsigned long s
,
88 unsigned long e
, int val
, const enum km_type km
);
90 static int bm_is_locked(struct drbd_bitmap
*b
)
92 return test_bit(BM_LOCKED
, &b
->bm_flags
);
95 #define bm_print_lock_info(m) __bm_print_lock_info(m, __func__)
96 static void __bm_print_lock_info(struct drbd_conf
*mdev
, const char *func
)
98 struct drbd_bitmap
*b
= mdev
->bitmap
;
99 if (!__ratelimit(&drbd_ratelimit_state
))
101 dev_err(DEV
, "FIXME %s in %s, bitmap locked for '%s' by %s\n",
102 current
== mdev
->receiver
.task
? "receiver" :
103 current
== mdev
->asender
.task
? "asender" :
104 current
== mdev
->worker
.task
? "worker" : current
->comm
,
105 func
, b
->bm_why
?: "?",
106 b
->bm_task
== mdev
->receiver
.task
? "receiver" :
107 b
->bm_task
== mdev
->asender
.task
? "asender" :
108 b
->bm_task
== mdev
->worker
.task
? "worker" : "?");
111 void drbd_bm_lock(struct drbd_conf
*mdev
, char *why
)
113 struct drbd_bitmap
*b
= mdev
->bitmap
;
117 dev_err(DEV
, "FIXME no bitmap in drbd_bm_lock!?\n");
121 trylock_failed
= !mutex_trylock(&b
->bm_change
);
123 if (trylock_failed
) {
124 dev_warn(DEV
, "%s going to '%s' but bitmap already 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 why
, 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 mutex_lock(&b
->bm_change
);
134 if (__test_and_set_bit(BM_LOCKED
, &b
->bm_flags
))
135 dev_err(DEV
, "FIXME bitmap already locked in bm_lock\n");
138 b
->bm_task
= current
;
141 void drbd_bm_unlock(struct drbd_conf
*mdev
)
143 struct drbd_bitmap
*b
= mdev
->bitmap
;
145 dev_err(DEV
, "FIXME no bitmap in drbd_bm_unlock!?\n");
149 if (!__test_and_clear_bit(BM_LOCKED
, &mdev
->bitmap
->bm_flags
))
150 dev_err(DEV
, "FIXME bitmap not locked in bm_unlock\n");
154 mutex_unlock(&b
->bm_change
);
157 /* word offset to long pointer */
158 static unsigned long *__bm_map_paddr(struct drbd_bitmap
*b
, unsigned long offset
, const enum km_type km
)
161 unsigned long page_nr
;
163 /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
164 page_nr
= offset
>> (PAGE_SHIFT
- LN2_BPL
+ 3);
165 BUG_ON(page_nr
>= b
->bm_number_of_pages
);
166 page
= b
->bm_pages
[page_nr
];
168 return (unsigned long *) kmap_atomic(page
, km
);
171 static unsigned long * bm_map_paddr(struct drbd_bitmap
*b
, unsigned long offset
)
173 return __bm_map_paddr(b
, offset
, KM_IRQ1
);
176 static void __bm_unmap(unsigned long *p_addr
, const enum km_type km
)
178 kunmap_atomic(p_addr
, km
);
181 static void bm_unmap(unsigned long *p_addr
)
183 return __bm_unmap(p_addr
, KM_IRQ1
);
186 /* long word offset of _bitmap_ sector */
187 #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
188 /* word offset from start of bitmap to word number _in_page_
189 * modulo longs per page
190 #define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
191 hm, well, Philipp thinks gcc might not optimze the % into & (... - 1)
194 #define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
196 /* Long words per page */
197 #define LWPP (PAGE_SIZE/sizeof(long))
200 * actually most functions herein should take a struct drbd_bitmap*, not a
201 * struct drbd_conf*, but for the debug macros I like to have the mdev around
202 * to be able to report device specific.
205 static void bm_free_pages(struct page
**pages
, unsigned long number
)
211 for (i
= 0; i
< number
; i
++) {
213 printk(KERN_ALERT
"drbd: bm_free_pages tried to free "
214 "a NULL pointer; i=%lu n=%lu\n",
218 __free_page(pages
[i
]);
223 static void bm_vk_free(void *ptr
, int v
)
232 * "have" and "want" are NUMBER OF PAGES.
234 static struct page
**bm_realloc_pages(struct drbd_bitmap
*b
, unsigned long want
)
236 struct page
**old_pages
= b
->bm_pages
;
237 struct page
**new_pages
, *page
;
238 unsigned int i
, bytes
, vmalloced
= 0;
239 unsigned long have
= b
->bm_number_of_pages
;
241 BUG_ON(have
== 0 && old_pages
!= NULL
);
242 BUG_ON(have
!= 0 && old_pages
== NULL
);
247 /* Trying kmalloc first, falling back to vmalloc.
248 * GFP_KERNEL is ok, as this is done when a lower level disk is
249 * "attached" to the drbd. Context is receiver thread or cqueue
250 * thread. As we have no disk yet, we are not in the IO path,
251 * not even the IO path of the peer. */
252 bytes
= sizeof(struct page
*)*want
;
253 new_pages
= kmalloc(bytes
, GFP_KERNEL
);
255 new_pages
= vmalloc(bytes
);
261 memset(new_pages
, 0, bytes
);
263 for (i
= 0; i
< have
; i
++)
264 new_pages
[i
] = old_pages
[i
];
265 for (; i
< want
; i
++) {
266 page
= alloc_page(GFP_HIGHUSER
);
268 bm_free_pages(new_pages
+ have
, i
- have
);
269 bm_vk_free(new_pages
, vmalloced
);
275 for (i
= 0; i
< want
; i
++)
276 new_pages
[i
] = old_pages
[i
];
277 /* NOT HERE, we are outside the spinlock!
278 bm_free_pages(old_pages + want, have - want);
283 set_bit(BM_P_VMALLOCED
, &b
->bm_flags
);
285 clear_bit(BM_P_VMALLOCED
, &b
->bm_flags
);
291 * called on driver init only. TODO call when a device is created.
292 * allocates the drbd_bitmap, and stores it in mdev->bitmap.
294 int drbd_bm_init(struct drbd_conf
*mdev
)
296 struct drbd_bitmap
*b
= mdev
->bitmap
;
298 b
= kzalloc(sizeof(struct drbd_bitmap
), GFP_KERNEL
);
301 spin_lock_init(&b
->bm_lock
);
302 mutex_init(&b
->bm_change
);
303 init_waitqueue_head(&b
->bm_io_wait
);
310 sector_t
drbd_bm_capacity(struct drbd_conf
*mdev
)
312 ERR_IF(!mdev
->bitmap
) return 0;
313 return mdev
->bitmap
->bm_dev_capacity
;
316 /* called on driver unload. TODO: call when a device is destroyed.
318 void drbd_bm_cleanup(struct drbd_conf
*mdev
)
320 ERR_IF (!mdev
->bitmap
) return;
321 bm_free_pages(mdev
->bitmap
->bm_pages
, mdev
->bitmap
->bm_number_of_pages
);
322 bm_vk_free(mdev
->bitmap
->bm_pages
, test_bit(BM_P_VMALLOCED
, &mdev
->bitmap
->bm_flags
));
328 * since (b->bm_bits % BITS_PER_LONG) != 0,
329 * this masks out the remaining bits.
330 * Returns the number of bits cleared.
332 static int bm_clear_surplus(struct drbd_bitmap
*b
)
334 const unsigned long mask
= (1UL << (b
->bm_bits
& (BITS_PER_LONG
-1))) - 1;
335 size_t w
= b
->bm_bits
>> LN2_BPL
;
337 unsigned long *p_addr
, *bm
;
339 p_addr
= bm_map_paddr(b
, w
);
340 bm
= p_addr
+ MLPP(w
);
341 if (w
< b
->bm_words
) {
342 cleared
= hweight_long(*bm
& ~mask
);
347 if (w
< b
->bm_words
) {
348 cleared
+= hweight_long(*bm
);
355 static void bm_set_surplus(struct drbd_bitmap
*b
)
357 const unsigned long mask
= (1UL << (b
->bm_bits
& (BITS_PER_LONG
-1))) - 1;
358 size_t w
= b
->bm_bits
>> LN2_BPL
;
359 unsigned long *p_addr
, *bm
;
361 p_addr
= bm_map_paddr(b
, w
);
362 bm
= p_addr
+ MLPP(w
);
363 if (w
< b
->bm_words
) {
368 if (w
< b
->bm_words
) {
374 static unsigned long __bm_count_bits(struct drbd_bitmap
*b
, const int swap_endian
)
376 unsigned long *p_addr
, *bm
, offset
= 0;
377 unsigned long bits
= 0;
378 unsigned long i
, do_now
;
380 while (offset
< b
->bm_words
) {
381 i
= do_now
= min_t(size_t, b
->bm_words
-offset
, LWPP
);
382 p_addr
= __bm_map_paddr(b
, offset
, KM_USER0
);
383 bm
= p_addr
+ MLPP(offset
);
385 #ifndef __LITTLE_ENDIAN
387 *bm
= lel_to_cpu(*bm
);
389 bits
+= hweight_long(*bm
++);
391 __bm_unmap(p_addr
, KM_USER0
);
399 static unsigned long bm_count_bits(struct drbd_bitmap
*b
)
401 return __bm_count_bits(b
, 0);
404 static unsigned long bm_count_bits_swap_endian(struct drbd_bitmap
*b
)
406 return __bm_count_bits(b
, 1);
409 /* offset and len in long words.*/
410 static void bm_memset(struct drbd_bitmap
*b
, size_t offset
, int c
, size_t len
)
412 unsigned long *p_addr
, *bm
;
415 #define BM_SECTORS_PER_BIT (BM_BLOCK_SIZE/512)
419 if (end
> b
->bm_words
) {
420 printk(KERN_ALERT
"drbd: bm_memset end > bm_words\n");
424 while (offset
< end
) {
425 do_now
= min_t(size_t, ALIGN(offset
+ 1, LWPP
), end
) - offset
;
426 p_addr
= bm_map_paddr(b
, offset
);
427 bm
= p_addr
+ MLPP(offset
);
428 if (bm
+do_now
> p_addr
+ LWPP
) {
429 printk(KERN_ALERT
"drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
430 p_addr
, bm
, (int)do_now
);
431 break; /* breaks to after catch_oob_access_end() only! */
433 memset(bm
, c
, do_now
* sizeof(long));
440 * make sure the bitmap has enough room for the attached storage,
441 * if necessary, resize.
442 * called whenever we may have changed the device size.
443 * returns -ENOMEM if we could not allocate enough memory, 0 on success.
444 * In case this is actually a resize, we copy the old bitmap into the new one.
445 * Otherwise, the bitmap is initialized to all bits set.
447 int drbd_bm_resize(struct drbd_conf
*mdev
, sector_t capacity
, int set_new_bits
)
449 struct drbd_bitmap
*b
= mdev
->bitmap
;
450 unsigned long bits
, words
, owords
, obits
, *p_addr
, *bm
;
451 unsigned long want
, have
, onpages
; /* number of pages */
452 struct page
**npages
, **opages
= NULL
;
453 int err
= 0, growing
;
454 int opages_vmalloced
;
456 ERR_IF(!b
) return -ENOMEM
;
458 drbd_bm_lock(mdev
, "resize");
460 dev_info(DEV
, "drbd_bm_resize called with capacity == %llu\n",
461 (unsigned long long)capacity
);
463 if (capacity
== b
->bm_dev_capacity
)
466 opages_vmalloced
= test_bit(BM_P_VMALLOCED
, &b
->bm_flags
);
469 spin_lock_irq(&b
->bm_lock
);
470 opages
= b
->bm_pages
;
471 onpages
= b
->bm_number_of_pages
;
472 owords
= b
->bm_words
;
474 b
->bm_number_of_pages
=
478 b
->bm_dev_capacity
= 0;
479 spin_unlock_irq(&b
->bm_lock
);
480 bm_free_pages(opages
, onpages
);
481 bm_vk_free(opages
, opages_vmalloced
);
484 bits
= BM_SECT_TO_BIT(ALIGN(capacity
, BM_SECT_PER_BIT
));
487 words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
488 a 32bit host could present the wrong number of words
491 words
= ALIGN(bits
, 64) >> LN2_BPL
;
493 if (get_ldev(mdev
)) {
494 D_ASSERT((u64
)bits
<= (((u64
)mdev
->ldev
->md
.md_size_sect
-MD_BM_OFFSET
) << 12));
498 /* one extra long to catch off by one errors */
499 want
= ALIGN((words
+1)*sizeof(long), PAGE_SIZE
) >> PAGE_SHIFT
;
500 have
= b
->bm_number_of_pages
;
502 D_ASSERT(b
->bm_pages
!= NULL
);
503 npages
= b
->bm_pages
;
505 if (FAULT_ACTIVE(mdev
, DRBD_FAULT_BM_ALLOC
))
508 npages
= bm_realloc_pages(b
, want
);
516 spin_lock_irq(&b
->bm_lock
);
517 opages
= b
->bm_pages
;
518 owords
= b
->bm_words
;
521 growing
= bits
> obits
;
522 if (opages
&& growing
&& set_new_bits
)
525 b
->bm_pages
= npages
;
526 b
->bm_number_of_pages
= want
;
529 b
->bm_dev_capacity
= capacity
;
533 bm_memset(b
, owords
, 0xff, words
-owords
);
534 b
->bm_set
+= bits
- obits
;
536 bm_memset(b
, owords
, 0x00, words
-owords
);
541 /* implicit: (opages != NULL) && (opages != npages) */
542 bm_free_pages(opages
+ want
, have
- want
);
545 p_addr
= bm_map_paddr(b
, words
);
546 bm
= p_addr
+ MLPP(words
);
550 (void)bm_clear_surplus(b
);
552 spin_unlock_irq(&b
->bm_lock
);
553 if (opages
!= npages
)
554 bm_vk_free(opages
, opages_vmalloced
);
556 b
->bm_set
= bm_count_bits(b
);
557 dev_info(DEV
, "resync bitmap: bits=%lu words=%lu\n", bits
, words
);
560 drbd_bm_unlock(mdev
);
565 * if not protected by other means, return value may be out of date when
566 * leaving this function...
567 * we still need to lock it, since it is important that this returns
568 * bm_set == 0 precisely.
570 * maybe bm_set should be atomic_t ?
572 unsigned long _drbd_bm_total_weight(struct drbd_conf
*mdev
)
574 struct drbd_bitmap
*b
= mdev
->bitmap
;
579 ERR_IF(!b
->bm_pages
) return 0;
581 spin_lock_irqsave(&b
->bm_lock
, flags
);
583 spin_unlock_irqrestore(&b
->bm_lock
, flags
);
588 unsigned long drbd_bm_total_weight(struct drbd_conf
*mdev
)
591 /* if I don't have a disk, I don't know about out-of-sync status */
592 if (!get_ldev_if_state(mdev
, D_NEGOTIATING
))
594 s
= _drbd_bm_total_weight(mdev
);
599 size_t drbd_bm_words(struct drbd_conf
*mdev
)
601 struct drbd_bitmap
*b
= mdev
->bitmap
;
603 ERR_IF(!b
->bm_pages
) return 0;
608 unsigned long drbd_bm_bits(struct drbd_conf
*mdev
)
610 struct drbd_bitmap
*b
= mdev
->bitmap
;
616 /* merge number words from buffer into the bitmap starting at offset.
617 * buffer[i] is expected to be little endian unsigned long.
618 * bitmap must be locked by drbd_bm_lock.
619 * currently only used from receive_bitmap.
621 void drbd_bm_merge_lel(struct drbd_conf
*mdev
, size_t offset
, size_t number
,
622 unsigned long *buffer
)
624 struct drbd_bitmap
*b
= mdev
->bitmap
;
625 unsigned long *p_addr
, *bm
;
626 unsigned long word
, bits
;
629 end
= offset
+ number
;
632 ERR_IF(!b
->bm_pages
) return;
635 WARN_ON(offset
>= b
->bm_words
);
636 WARN_ON(end
> b
->bm_words
);
638 spin_lock_irq(&b
->bm_lock
);
639 while (offset
< end
) {
640 do_now
= min_t(size_t, ALIGN(offset
+1, LWPP
), end
) - offset
;
641 p_addr
= bm_map_paddr(b
, offset
);
642 bm
= p_addr
+ MLPP(offset
);
645 bits
= hweight_long(*bm
);
646 word
= *bm
| lel_to_cpu(*buffer
++);
648 b
->bm_set
+= hweight_long(word
) - bits
;
652 /* with 32bit <-> 64bit cross-platform connect
653 * this is only correct for current usage,
654 * where we _know_ that we are 64 bit aligned,
655 * and know that this function is used in this way, too...
657 if (end
== b
->bm_words
)
658 b
->bm_set
-= bm_clear_surplus(b
);
660 spin_unlock_irq(&b
->bm_lock
);
663 /* copy number words from the bitmap starting at offset into the buffer.
664 * buffer[i] will be little endian unsigned long.
666 void drbd_bm_get_lel(struct drbd_conf
*mdev
, size_t offset
, size_t number
,
667 unsigned long *buffer
)
669 struct drbd_bitmap
*b
= mdev
->bitmap
;
670 unsigned long *p_addr
, *bm
;
673 end
= offset
+ number
;
676 ERR_IF(!b
->bm_pages
) return;
678 spin_lock_irq(&b
->bm_lock
);
679 if ((offset
>= b
->bm_words
) ||
680 (end
> b
->bm_words
) ||
682 dev_err(DEV
, "offset=%lu number=%lu bm_words=%lu\n",
683 (unsigned long) offset
,
684 (unsigned long) number
,
685 (unsigned long) b
->bm_words
);
687 while (offset
< end
) {
688 do_now
= min_t(size_t, ALIGN(offset
+1, LWPP
), end
) - offset
;
689 p_addr
= bm_map_paddr(b
, offset
);
690 bm
= p_addr
+ MLPP(offset
);
693 *buffer
++ = cpu_to_lel(*bm
++);
697 spin_unlock_irq(&b
->bm_lock
);
700 /* set all bits in the bitmap */
701 void drbd_bm_set_all(struct drbd_conf
*mdev
)
703 struct drbd_bitmap
*b
= mdev
->bitmap
;
705 ERR_IF(!b
->bm_pages
) return;
707 spin_lock_irq(&b
->bm_lock
);
708 bm_memset(b
, 0, 0xff, b
->bm_words
);
709 (void)bm_clear_surplus(b
);
710 b
->bm_set
= b
->bm_bits
;
711 spin_unlock_irq(&b
->bm_lock
);
714 /* clear all bits in the bitmap */
715 void drbd_bm_clear_all(struct drbd_conf
*mdev
)
717 struct drbd_bitmap
*b
= mdev
->bitmap
;
719 ERR_IF(!b
->bm_pages
) return;
721 spin_lock_irq(&b
->bm_lock
);
722 bm_memset(b
, 0, 0, b
->bm_words
);
724 spin_unlock_irq(&b
->bm_lock
);
727 static void bm_async_io_complete(struct bio
*bio
, int error
)
729 struct drbd_bitmap
*b
= bio
->bi_private
;
730 int uptodate
= bio_flagged(bio
, BIO_UPTODATE
);
733 /* strange behavior of some lower level drivers...
734 * fail the request by clearing the uptodate flag,
735 * but do not return any error?!
736 * do we want to WARN() on this? */
737 if (!error
&& !uptodate
)
742 * for now, set all bits, and flag MD_IO_ERROR */
743 __set_bit(BM_MD_IO_ERROR
, &b
->bm_flags
);
745 if (atomic_dec_and_test(&b
->bm_async_io
))
746 wake_up(&b
->bm_io_wait
);
751 static void bm_page_io_async(struct drbd_conf
*mdev
, struct drbd_bitmap
*b
, int page_nr
, int rw
) __must_hold(local
)
753 /* we are process context. we always get a bio */
754 struct bio
*bio
= bio_alloc(GFP_KERNEL
, 1);
756 sector_t on_disk_sector
=
757 mdev
->ldev
->md
.md_offset
+ mdev
->ldev
->md
.bm_offset
;
758 on_disk_sector
+= ((sector_t
)page_nr
) << (PAGE_SHIFT
-9);
760 /* this might happen with very small
761 * flexible external meta data device */
762 len
= min_t(unsigned int, PAGE_SIZE
,
763 (drbd_md_last_sector(mdev
->ldev
) - on_disk_sector
+ 1)<<9);
765 bio
->bi_bdev
= mdev
->ldev
->md_bdev
;
766 bio
->bi_sector
= on_disk_sector
;
767 bio_add_page(bio
, b
->bm_pages
[page_nr
], len
, 0);
769 bio
->bi_end_io
= bm_async_io_complete
;
771 if (FAULT_ACTIVE(mdev
, (rw
& WRITE
) ? DRBD_FAULT_MD_WR
: DRBD_FAULT_MD_RD
)) {
773 bio_endio(bio
, -EIO
);
779 # if defined(__LITTLE_ENDIAN)
780 /* nothing to do, on disk == in memory */
781 # define bm_cpu_to_lel(x) ((void)0)
783 static void bm_cpu_to_lel(struct drbd_bitmap
*b
)
785 /* need to cpu_to_lel all the pages ...
786 * this may be optimized by using
787 * cpu_to_lel(-1) == -1 and cpu_to_lel(0) == 0;
788 * the following is still not optimal, but better than nothing */
790 unsigned long *p_addr
, *bm
;
791 if (b
->bm_set
== 0) {
792 /* no page at all; avoid swap if all is 0 */
793 i
= b
->bm_number_of_pages
;
794 } else if (b
->bm_set
== b
->bm_bits
) {
795 /* only the last page */
796 i
= b
->bm_number_of_pages
- 1;
801 for (; i
< b
->bm_number_of_pages
; i
++) {
802 p_addr
= kmap_atomic(b
->bm_pages
[i
], KM_USER0
);
803 for (bm
= p_addr
; bm
< p_addr
+ PAGE_SIZE
/sizeof(long); bm
++)
804 *bm
= cpu_to_lel(*bm
);
805 kunmap_atomic(p_addr
, KM_USER0
);
809 /* lel_to_cpu == cpu_to_lel */
810 # define bm_lel_to_cpu(x) bm_cpu_to_lel(x)
813 * bm_rw: read/write the whole bitmap from/to its on disk location.
815 static int bm_rw(struct drbd_conf
*mdev
, int rw
) __must_hold(local
)
817 struct drbd_bitmap
*b
= mdev
->bitmap
;
818 /* sector_t sector; */
819 int bm_words
, num_pages
, i
;
824 WARN_ON(!bm_is_locked(b
));
826 /* no spinlock here, the drbd_bm_lock should be enough! */
828 bm_words
= drbd_bm_words(mdev
);
829 num_pages
= (bm_words
*sizeof(long) + PAGE_SIZE
-1) >> PAGE_SHIFT
;
831 /* on disk bitmap is little endian */
836 atomic_set(&b
->bm_async_io
, num_pages
);
837 __clear_bit(BM_MD_IO_ERROR
, &b
->bm_flags
);
839 /* let the layers below us try to merge these bios... */
840 for (i
= 0; i
< num_pages
; i
++)
841 bm_page_io_async(mdev
, b
, i
, rw
);
843 wait_event(b
->bm_io_wait
, atomic_read(&b
->bm_async_io
) == 0);
845 if (test_bit(BM_MD_IO_ERROR
, &b
->bm_flags
)) {
846 dev_alert(DEV
, "we had at least one MD IO ERROR during bitmap IO\n");
847 drbd_chk_io_error(mdev
, 1, TRUE
);
853 /* swap back endianness */
855 /* flush bitmap to stable storage */
857 } else /* rw == READ */ {
858 /* just read, if necessary adjust endianness */
859 b
->bm_set
= bm_count_bits_swap_endian(b
);
860 dev_info(DEV
, "recounting of set bits took additional %lu jiffies\n",
865 dev_info(DEV
, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
866 ppsize(ppb
, now
<< (BM_BLOCK_SHIFT
-10)), now
);
872 * drbd_bm_read() - Read the whole bitmap from its on disk location.
873 * @mdev: DRBD device.
875 int drbd_bm_read(struct drbd_conf
*mdev
) __must_hold(local
)
877 return bm_rw(mdev
, READ
);
881 * drbd_bm_write() - Write the whole bitmap to its on disk location.
882 * @mdev: DRBD device.
884 int drbd_bm_write(struct drbd_conf
*mdev
) __must_hold(local
)
886 return bm_rw(mdev
, WRITE
);
890 * drbd_bm_write_sect: Writes a 512 (MD_SECTOR_SIZE) byte piece of the bitmap
891 * @mdev: DRBD device.
892 * @enr: Extent number in the resync lru (happens to be sector offset)
894 * The BM_EXT_SIZE is on purpose exactly the amount of the bitmap covered
895 * by a single sector write. Therefore enr == sector offset from the
896 * start of the bitmap.
898 int drbd_bm_write_sect(struct drbd_conf
*mdev
, unsigned long enr
) __must_hold(local
)
900 sector_t on_disk_sector
= enr
+ mdev
->ldev
->md
.md_offset
901 + mdev
->ldev
->md
.bm_offset
;
902 int bm_words
, num_words
, offset
;
905 mutex_lock(&mdev
->md_io_mutex
);
906 bm_words
= drbd_bm_words(mdev
);
907 offset
= S2W(enr
); /* word offset into bitmap */
908 num_words
= min(S2W(1), bm_words
- offset
);
909 if (num_words
< S2W(1))
910 memset(page_address(mdev
->md_io_page
), 0, MD_SECTOR_SIZE
);
911 drbd_bm_get_lel(mdev
, offset
, num_words
,
912 page_address(mdev
->md_io_page
));
913 if (!drbd_md_sync_page_io(mdev
, mdev
->ldev
, on_disk_sector
, WRITE
)) {
916 dev_err(DEV
, "IO ERROR writing bitmap sector %lu "
917 "(meta-disk sector %llus)\n",
918 enr
, (unsigned long long)on_disk_sector
);
919 drbd_chk_io_error(mdev
, 1, TRUE
);
920 for (i
= 0; i
< AL_EXT_PER_BM_SECT
; i
++)
921 drbd_bm_ALe_set_all(mdev
, enr
*AL_EXT_PER_BM_SECT
+i
);
924 mutex_unlock(&mdev
->md_io_mutex
);
929 * find_first_bit returns int, we return unsigned long.
930 * should not make much difference anyways, but ...
932 * this returns a bit number, NOT a sector!
934 #define BPP_MASK ((1UL << (PAGE_SHIFT+3)) - 1)
935 static unsigned long __bm_find_next(struct drbd_conf
*mdev
, unsigned long bm_fo
,
936 const int find_zero_bit
, const enum km_type km
)
938 struct drbd_bitmap
*b
= mdev
->bitmap
;
939 unsigned long i
= -1UL;
940 unsigned long *p_addr
;
941 unsigned long bit_offset
; /* bit offset of the mapped page. */
943 if (bm_fo
> b
->bm_bits
) {
944 dev_err(DEV
, "bm_fo=%lu bm_bits=%lu\n", bm_fo
, b
->bm_bits
);
946 while (bm_fo
< b
->bm_bits
) {
947 unsigned long offset
;
948 bit_offset
= bm_fo
& ~BPP_MASK
; /* bit offset of the page */
949 offset
= bit_offset
>> LN2_BPL
; /* word offset of the page */
950 p_addr
= __bm_map_paddr(b
, offset
, km
);
953 i
= find_next_zero_bit(p_addr
, PAGE_SIZE
*8, bm_fo
& BPP_MASK
);
955 i
= find_next_bit(p_addr
, PAGE_SIZE
*8, bm_fo
& BPP_MASK
);
957 __bm_unmap(p_addr
, km
);
958 if (i
< PAGE_SIZE
*8) {
964 bm_fo
= bit_offset
+ PAGE_SIZE
*8;
972 static unsigned long bm_find_next(struct drbd_conf
*mdev
,
973 unsigned long bm_fo
, const int find_zero_bit
)
975 struct drbd_bitmap
*b
= mdev
->bitmap
;
976 unsigned long i
= -1UL;
979 ERR_IF(!b
->bm_pages
) return i
;
981 spin_lock_irq(&b
->bm_lock
);
983 bm_print_lock_info(mdev
);
985 i
= __bm_find_next(mdev
, bm_fo
, find_zero_bit
, KM_IRQ1
);
987 spin_unlock_irq(&b
->bm_lock
);
991 unsigned long drbd_bm_find_next(struct drbd_conf
*mdev
, unsigned long bm_fo
)
993 return bm_find_next(mdev
, bm_fo
, 0);
997 /* not yet needed for anything. */
998 unsigned long drbd_bm_find_next_zero(struct drbd_conf
*mdev
, unsigned long bm_fo
)
1000 return bm_find_next(mdev
, bm_fo
, 1);
1004 /* does not spin_lock_irqsave.
1005 * you must take drbd_bm_lock() first */
1006 unsigned long _drbd_bm_find_next(struct drbd_conf
*mdev
, unsigned long bm_fo
)
1008 /* WARN_ON(!bm_is_locked(mdev)); */
1009 return __bm_find_next(mdev
, bm_fo
, 0, KM_USER1
);
1012 unsigned long _drbd_bm_find_next_zero(struct drbd_conf
*mdev
, unsigned long bm_fo
)
1014 /* WARN_ON(!bm_is_locked(mdev)); */
1015 return __bm_find_next(mdev
, bm_fo
, 1, KM_USER1
);
1018 /* returns number of bits actually changed.
1019 * for val != 0, we change 0 -> 1, return code positive
1020 * for val == 0, we change 1 -> 0, return code negative
1021 * wants bitnr, not sector.
1022 * expected to be called for only a few bits (e - s about BITS_PER_LONG).
1023 * Must hold bitmap lock already. */
1024 static int __bm_change_bits_to(struct drbd_conf
*mdev
, const unsigned long s
,
1025 unsigned long e
, int val
, const enum km_type km
)
1027 struct drbd_bitmap
*b
= mdev
->bitmap
;
1028 unsigned long *p_addr
= NULL
;
1029 unsigned long bitnr
;
1030 unsigned long last_page_nr
= -1UL;
1033 if (e
>= b
->bm_bits
) {
1034 dev_err(DEV
, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
1036 e
= b
->bm_bits
? b
->bm_bits
-1 : 0;
1038 for (bitnr
= s
; bitnr
<= e
; bitnr
++) {
1039 unsigned long offset
= bitnr
>>LN2_BPL
;
1040 unsigned long page_nr
= offset
>> (PAGE_SHIFT
- LN2_BPL
+ 3);
1041 if (page_nr
!= last_page_nr
) {
1043 __bm_unmap(p_addr
, km
);
1044 p_addr
= __bm_map_paddr(b
, offset
, km
);
1045 last_page_nr
= page_nr
;
1048 c
+= (0 == __test_and_set_bit(bitnr
& BPP_MASK
, p_addr
));
1050 c
-= (0 != __test_and_clear_bit(bitnr
& BPP_MASK
, p_addr
));
1053 __bm_unmap(p_addr
, km
);
1058 /* returns number of bits actually changed.
1059 * for val != 0, we change 0 -> 1, return code positive
1060 * for val == 0, we change 1 -> 0, return code negative
1061 * wants bitnr, not sector */
1062 static int bm_change_bits_to(struct drbd_conf
*mdev
, const unsigned long s
,
1063 const unsigned long e
, int val
)
1065 unsigned long flags
;
1066 struct drbd_bitmap
*b
= mdev
->bitmap
;
1069 ERR_IF(!b
) return 1;
1070 ERR_IF(!b
->bm_pages
) return 0;
1072 spin_lock_irqsave(&b
->bm_lock
, flags
);
1073 if (bm_is_locked(b
))
1074 bm_print_lock_info(mdev
);
1076 c
= __bm_change_bits_to(mdev
, s
, e
, val
, KM_IRQ1
);
1078 spin_unlock_irqrestore(&b
->bm_lock
, flags
);
1082 /* returns number of bits changed 0 -> 1 */
1083 int drbd_bm_set_bits(struct drbd_conf
*mdev
, const unsigned long s
, const unsigned long e
)
1085 return bm_change_bits_to(mdev
, s
, e
, 1);
1088 /* returns number of bits changed 1 -> 0 */
1089 int drbd_bm_clear_bits(struct drbd_conf
*mdev
, const unsigned long s
, const unsigned long e
)
1091 return -bm_change_bits_to(mdev
, s
, e
, 0);
1094 /* sets all bits in full words,
1095 * from first_word up to, but not including, last_word */
1096 static inline void bm_set_full_words_within_one_page(struct drbd_bitmap
*b
,
1097 int page_nr
, int first_word
, int last_word
)
1101 unsigned long *paddr
= kmap_atomic(b
->bm_pages
[page_nr
], KM_USER0
);
1102 for (i
= first_word
; i
< last_word
; i
++) {
1103 bits
= hweight_long(paddr
[i
]);
1105 b
->bm_set
+= BITS_PER_LONG
- bits
;
1107 kunmap_atomic(paddr
, KM_USER0
);
1110 /* Same thing as drbd_bm_set_bits, but without taking the spin_lock_irqsave.
1111 * You must first drbd_bm_lock().
1112 * Can be called to set the whole bitmap in one go.
1113 * Sets bits from s to e _inclusive_. */
1114 void _drbd_bm_set_bits(struct drbd_conf
*mdev
, const unsigned long s
, const unsigned long e
)
1116 /* First set_bit from the first bit (s)
1117 * up to the next long boundary (sl),
1118 * then assign full words up to the last long boundary (el),
1119 * then set_bit up to and including the last bit (e).
1121 * Do not use memset, because we must account for changes,
1122 * so we need to loop over the words with hweight() anyways.
1124 unsigned long sl
= ALIGN(s
,BITS_PER_LONG
);
1125 unsigned long el
= (e
+1) & ~((unsigned long)BITS_PER_LONG
-1);
1132 if (e
- s
<= 3*BITS_PER_LONG
) {
1133 /* don't bother; el and sl may even be wrong. */
1134 __bm_change_bits_to(mdev
, s
, e
, 1, KM_USER0
);
1138 /* difference is large enough that we can trust sl and el */
1140 /* bits filling the current long */
1142 __bm_change_bits_to(mdev
, s
, sl
-1, 1, KM_USER0
);
1144 first_page
= sl
>> (3 + PAGE_SHIFT
);
1145 last_page
= el
>> (3 + PAGE_SHIFT
);
1147 /* MLPP: modulo longs per page */
1148 /* LWPP: long words per page */
1149 first_word
= MLPP(sl
>> LN2_BPL
);
1152 /* first and full pages, unless first page == last page */
1153 for (page_nr
= first_page
; page_nr
< last_page
; page_nr
++) {
1154 bm_set_full_words_within_one_page(mdev
->bitmap
, page_nr
, first_word
, last_word
);
1159 /* last page (respectively only page, for first page == last page) */
1160 last_word
= MLPP(el
>> LN2_BPL
);
1161 bm_set_full_words_within_one_page(mdev
->bitmap
, last_page
, first_word
, last_word
);
1163 /* possibly trailing bits.
1164 * example: (e & 63) == 63, el will be e+1.
1165 * if that even was the very last bit,
1166 * it would trigger an assert in __bm_change_bits_to()
1169 __bm_change_bits_to(mdev
, el
, e
, 1, KM_USER0
);
1172 /* returns bit state
1173 * wants bitnr, NOT sector.
1174 * inherently racy... area needs to be locked by means of {al,rs}_lru
1177 * -1 ... first out of bounds access, stop testing for bits!
1179 int drbd_bm_test_bit(struct drbd_conf
*mdev
, const unsigned long bitnr
)
1181 unsigned long flags
;
1182 struct drbd_bitmap
*b
= mdev
->bitmap
;
1183 unsigned long *p_addr
;
1186 ERR_IF(!b
) return 0;
1187 ERR_IF(!b
->bm_pages
) return 0;
1189 spin_lock_irqsave(&b
->bm_lock
, flags
);
1190 if (bm_is_locked(b
))
1191 bm_print_lock_info(mdev
);
1192 if (bitnr
< b
->bm_bits
) {
1193 unsigned long offset
= bitnr
>>LN2_BPL
;
1194 p_addr
= bm_map_paddr(b
, offset
);
1195 i
= test_bit(bitnr
& BPP_MASK
, p_addr
) ? 1 : 0;
1197 } else if (bitnr
== b
->bm_bits
) {
1199 } else { /* (bitnr > b->bm_bits) */
1200 dev_err(DEV
, "bitnr=%lu > bm_bits=%lu\n", bitnr
, b
->bm_bits
);
1204 spin_unlock_irqrestore(&b
->bm_lock
, flags
);
1208 /* returns number of bits set in the range [s, e] */
1209 int drbd_bm_count_bits(struct drbd_conf
*mdev
, const unsigned long s
, const unsigned long e
)
1211 unsigned long flags
;
1212 struct drbd_bitmap
*b
= mdev
->bitmap
;
1213 unsigned long *p_addr
= NULL
, page_nr
= -1;
1214 unsigned long bitnr
;
1218 /* If this is called without a bitmap, that is a bug. But just to be
1219 * robust in case we screwed up elsewhere, in that case pretend there
1220 * was one dirty bit in the requested area, so we won't try to do a
1221 * local read there (no bitmap probably implies no disk) */
1222 ERR_IF(!b
) return 1;
1223 ERR_IF(!b
->bm_pages
) return 1;
1225 spin_lock_irqsave(&b
->bm_lock
, flags
);
1226 if (bm_is_locked(b
))
1227 bm_print_lock_info(mdev
);
1228 for (bitnr
= s
; bitnr
<= e
; bitnr
++) {
1229 w
= bitnr
>> LN2_BPL
;
1230 if (page_nr
!= w
>> (PAGE_SHIFT
- LN2_BPL
+ 3)) {
1231 page_nr
= w
>> (PAGE_SHIFT
- LN2_BPL
+ 3);
1234 p_addr
= bm_map_paddr(b
, w
);
1236 ERR_IF (bitnr
>= b
->bm_bits
) {
1237 dev_err(DEV
, "bitnr=%lu bm_bits=%lu\n", bitnr
, b
->bm_bits
);
1239 c
+= (0 != test_bit(bitnr
- (page_nr
<< (PAGE_SHIFT
+3)), p_addr
));
1244 spin_unlock_irqrestore(&b
->bm_lock
, flags
);
1249 /* inherently racy...
1250 * return value may be already out-of-date when this function returns.
1251 * but the general usage is that this is only use during a cstate when bits are
1252 * only cleared, not set, and typically only care for the case when the return
1253 * value is zero, or we already "locked" this "bitmap extent" by other means.
1255 * enr is bm-extent number, since we chose to name one sector (512 bytes)
1256 * worth of the bitmap a "bitmap extent".
1259 * I think since we use it like a reference count, we should use the real
1260 * reference count of some bitmap extent element from some lru instead...
1263 int drbd_bm_e_weight(struct drbd_conf
*mdev
, unsigned long enr
)
1265 struct drbd_bitmap
*b
= mdev
->bitmap
;
1267 unsigned long flags
;
1268 unsigned long *p_addr
, *bm
;
1270 ERR_IF(!b
) return 0;
1271 ERR_IF(!b
->bm_pages
) return 0;
1273 spin_lock_irqsave(&b
->bm_lock
, flags
);
1274 if (bm_is_locked(b
))
1275 bm_print_lock_info(mdev
);
1278 e
= min((size_t)S2W(enr
+1), b
->bm_words
);
1280 if (s
< b
->bm_words
) {
1282 p_addr
= bm_map_paddr(b
, s
);
1283 bm
= p_addr
+ MLPP(s
);
1285 count
+= hweight_long(*bm
++);
1288 dev_err(DEV
, "start offset (%d) too large in drbd_bm_e_weight\n", s
);
1290 spin_unlock_irqrestore(&b
->bm_lock
, flags
);
1294 /* set all bits covered by the AL-extent al_enr */
1295 unsigned long drbd_bm_ALe_set_all(struct drbd_conf
*mdev
, unsigned long al_enr
)
1297 struct drbd_bitmap
*b
= mdev
->bitmap
;
1298 unsigned long *p_addr
, *bm
;
1299 unsigned long weight
;
1300 int count
, s
, e
, i
, do_now
;
1301 ERR_IF(!b
) return 0;
1302 ERR_IF(!b
->bm_pages
) return 0;
1304 spin_lock_irq(&b
->bm_lock
);
1305 if (bm_is_locked(b
))
1306 bm_print_lock_info(mdev
);
1309 s
= al_enr
* BM_WORDS_PER_AL_EXT
;
1310 e
= min_t(size_t, s
+ BM_WORDS_PER_AL_EXT
, b
->bm_words
);
1311 /* assert that s and e are on the same page */
1312 D_ASSERT((e
-1) >> (PAGE_SHIFT
- LN2_BPL
+ 3)
1313 == s
>> (PAGE_SHIFT
- LN2_BPL
+ 3));
1315 if (s
< b
->bm_words
) {
1317 p_addr
= bm_map_paddr(b
, s
);
1318 bm
= p_addr
+ MLPP(s
);
1320 count
+= hweight_long(*bm
);
1325 b
->bm_set
+= do_now
*BITS_PER_LONG
- count
;
1326 if (e
== b
->bm_words
)
1327 b
->bm_set
-= bm_clear_surplus(b
);
1329 dev_err(DEV
, "start offset (%d) too large in drbd_bm_ALe_set_all\n", s
);
1331 weight
= b
->bm_set
- weight
;
1332 spin_unlock_irq(&b
->bm_lock
);