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
;
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
;
535 __bm_change_bits_to(mdev
, obits
,
536 ALIGN(obits
, BITS_PER_LONG
),
539 bm_memset(b
, owords
, 0x00, words
-owords
);
544 /* implicit: (opages != NULL) && (opages != npages) */
545 bm_free_pages(opages
+ want
, have
- want
);
548 p_addr
= bm_map_paddr(b
, words
);
549 bm
= p_addr
+ MLPP(words
);
553 (void)bm_clear_surplus(b
);
555 spin_unlock_irq(&b
->bm_lock
);
556 if (opages
!= npages
)
557 bm_vk_free(opages
, opages_vmalloced
);
559 b
->bm_set
= bm_count_bits(b
);
560 dev_info(DEV
, "resync bitmap: bits=%lu words=%lu\n", bits
, words
);
563 drbd_bm_unlock(mdev
);
568 * if not protected by other means, return value may be out of date when
569 * leaving this function...
570 * we still need to lock it, since it is important that this returns
571 * bm_set == 0 precisely.
573 * maybe bm_set should be atomic_t ?
575 static unsigned long _drbd_bm_total_weight(struct drbd_conf
*mdev
)
577 struct drbd_bitmap
*b
= mdev
->bitmap
;
582 ERR_IF(!b
->bm_pages
) return 0;
584 spin_lock_irqsave(&b
->bm_lock
, flags
);
586 spin_unlock_irqrestore(&b
->bm_lock
, flags
);
591 unsigned long drbd_bm_total_weight(struct drbd_conf
*mdev
)
594 /* if I don't have a disk, I don't know about out-of-sync status */
595 if (!get_ldev_if_state(mdev
, D_NEGOTIATING
))
597 s
= _drbd_bm_total_weight(mdev
);
602 size_t drbd_bm_words(struct drbd_conf
*mdev
)
604 struct drbd_bitmap
*b
= mdev
->bitmap
;
606 ERR_IF(!b
->bm_pages
) return 0;
611 unsigned long drbd_bm_bits(struct drbd_conf
*mdev
)
613 struct drbd_bitmap
*b
= mdev
->bitmap
;
619 /* merge number words from buffer into the bitmap starting at offset.
620 * buffer[i] is expected to be little endian unsigned long.
621 * bitmap must be locked by drbd_bm_lock.
622 * currently only used from receive_bitmap.
624 void drbd_bm_merge_lel(struct drbd_conf
*mdev
, size_t offset
, size_t number
,
625 unsigned long *buffer
)
627 struct drbd_bitmap
*b
= mdev
->bitmap
;
628 unsigned long *p_addr
, *bm
;
629 unsigned long word
, bits
;
632 end
= offset
+ number
;
635 ERR_IF(!b
->bm_pages
) return;
638 WARN_ON(offset
>= b
->bm_words
);
639 WARN_ON(end
> b
->bm_words
);
641 spin_lock_irq(&b
->bm_lock
);
642 while (offset
< end
) {
643 do_now
= min_t(size_t, ALIGN(offset
+1, LWPP
), end
) - offset
;
644 p_addr
= bm_map_paddr(b
, offset
);
645 bm
= p_addr
+ MLPP(offset
);
648 bits
= hweight_long(*bm
);
649 word
= *bm
| lel_to_cpu(*buffer
++);
651 b
->bm_set
+= hweight_long(word
) - bits
;
655 /* with 32bit <-> 64bit cross-platform connect
656 * this is only correct for current usage,
657 * where we _know_ that we are 64 bit aligned,
658 * and know that this function is used in this way, too...
660 if (end
== b
->bm_words
)
661 b
->bm_set
-= bm_clear_surplus(b
);
663 spin_unlock_irq(&b
->bm_lock
);
666 /* copy number words from the bitmap starting at offset into the buffer.
667 * buffer[i] will be little endian unsigned long.
669 void drbd_bm_get_lel(struct drbd_conf
*mdev
, size_t offset
, size_t number
,
670 unsigned long *buffer
)
672 struct drbd_bitmap
*b
= mdev
->bitmap
;
673 unsigned long *p_addr
, *bm
;
676 end
= offset
+ number
;
679 ERR_IF(!b
->bm_pages
) return;
681 spin_lock_irq(&b
->bm_lock
);
682 if ((offset
>= b
->bm_words
) ||
683 (end
> b
->bm_words
) ||
685 dev_err(DEV
, "offset=%lu number=%lu bm_words=%lu\n",
686 (unsigned long) offset
,
687 (unsigned long) number
,
688 (unsigned long) b
->bm_words
);
690 while (offset
< end
) {
691 do_now
= min_t(size_t, ALIGN(offset
+1, LWPP
), end
) - offset
;
692 p_addr
= bm_map_paddr(b
, offset
);
693 bm
= p_addr
+ MLPP(offset
);
696 *buffer
++ = cpu_to_lel(*bm
++);
700 spin_unlock_irq(&b
->bm_lock
);
703 /* set all bits in the bitmap */
704 void drbd_bm_set_all(struct drbd_conf
*mdev
)
706 struct drbd_bitmap
*b
= mdev
->bitmap
;
708 ERR_IF(!b
->bm_pages
) return;
710 spin_lock_irq(&b
->bm_lock
);
711 bm_memset(b
, 0, 0xff, b
->bm_words
);
712 (void)bm_clear_surplus(b
);
713 b
->bm_set
= b
->bm_bits
;
714 spin_unlock_irq(&b
->bm_lock
);
717 /* clear all bits in the bitmap */
718 void drbd_bm_clear_all(struct drbd_conf
*mdev
)
720 struct drbd_bitmap
*b
= mdev
->bitmap
;
722 ERR_IF(!b
->bm_pages
) return;
724 spin_lock_irq(&b
->bm_lock
);
725 bm_memset(b
, 0, 0, b
->bm_words
);
727 spin_unlock_irq(&b
->bm_lock
);
730 static void bm_async_io_complete(struct bio
*bio
, int error
)
732 struct drbd_bitmap
*b
= bio
->bi_private
;
733 int uptodate
= bio_flagged(bio
, BIO_UPTODATE
);
736 /* strange behavior of some lower level drivers...
737 * fail the request by clearing the uptodate flag,
738 * but do not return any error?!
739 * do we want to WARN() on this? */
740 if (!error
&& !uptodate
)
745 * for now, set all bits, and flag MD_IO_ERROR */
746 __set_bit(BM_MD_IO_ERROR
, &b
->bm_flags
);
748 if (atomic_dec_and_test(&b
->bm_async_io
))
749 wake_up(&b
->bm_io_wait
);
754 static void bm_page_io_async(struct drbd_conf
*mdev
, struct drbd_bitmap
*b
, int page_nr
, int rw
) __must_hold(local
)
756 /* we are process context. we always get a bio */
757 struct bio
*bio
= bio_alloc(GFP_KERNEL
, 1);
759 sector_t on_disk_sector
=
760 mdev
->ldev
->md
.md_offset
+ mdev
->ldev
->md
.bm_offset
;
761 on_disk_sector
+= ((sector_t
)page_nr
) << (PAGE_SHIFT
-9);
763 /* this might happen with very small
764 * flexible external meta data device */
765 len
= min_t(unsigned int, PAGE_SIZE
,
766 (drbd_md_last_sector(mdev
->ldev
) - on_disk_sector
+ 1)<<9);
768 bio
->bi_bdev
= mdev
->ldev
->md_bdev
;
769 bio
->bi_sector
= on_disk_sector
;
770 bio_add_page(bio
, b
->bm_pages
[page_nr
], len
, 0);
772 bio
->bi_end_io
= bm_async_io_complete
;
774 if (FAULT_ACTIVE(mdev
, (rw
& WRITE
) ? DRBD_FAULT_MD_WR
: DRBD_FAULT_MD_RD
)) {
776 bio_endio(bio
, -EIO
);
782 # if defined(__LITTLE_ENDIAN)
783 /* nothing to do, on disk == in memory */
784 # define bm_cpu_to_lel(x) ((void)0)
786 static void bm_cpu_to_lel(struct drbd_bitmap
*b
)
788 /* need to cpu_to_lel all the pages ...
789 * this may be optimized by using
790 * cpu_to_lel(-1) == -1 and cpu_to_lel(0) == 0;
791 * the following is still not optimal, but better than nothing */
793 unsigned long *p_addr
, *bm
;
794 if (b
->bm_set
== 0) {
795 /* no page at all; avoid swap if all is 0 */
796 i
= b
->bm_number_of_pages
;
797 } else if (b
->bm_set
== b
->bm_bits
) {
798 /* only the last page */
799 i
= b
->bm_number_of_pages
- 1;
804 for (; i
< b
->bm_number_of_pages
; i
++) {
805 p_addr
= kmap_atomic(b
->bm_pages
[i
], KM_USER0
);
806 for (bm
= p_addr
; bm
< p_addr
+ PAGE_SIZE
/sizeof(long); bm
++)
807 *bm
= cpu_to_lel(*bm
);
808 kunmap_atomic(p_addr
, KM_USER0
);
812 /* lel_to_cpu == cpu_to_lel */
813 # define bm_lel_to_cpu(x) bm_cpu_to_lel(x)
816 * bm_rw: read/write the whole bitmap from/to its on disk location.
818 static int bm_rw(struct drbd_conf
*mdev
, int rw
) __must_hold(local
)
820 struct drbd_bitmap
*b
= mdev
->bitmap
;
821 /* sector_t sector; */
822 int bm_words
, num_pages
, i
;
827 WARN_ON(!bm_is_locked(b
));
829 /* no spinlock here, the drbd_bm_lock should be enough! */
831 bm_words
= drbd_bm_words(mdev
);
832 num_pages
= (bm_words
*sizeof(long) + PAGE_SIZE
-1) >> PAGE_SHIFT
;
834 /* on disk bitmap is little endian */
839 atomic_set(&b
->bm_async_io
, num_pages
);
840 __clear_bit(BM_MD_IO_ERROR
, &b
->bm_flags
);
842 /* let the layers below us try to merge these bios... */
843 for (i
= 0; i
< num_pages
; i
++)
844 bm_page_io_async(mdev
, b
, i
, rw
);
846 drbd_blk_run_queue(bdev_get_queue(mdev
->ldev
->md_bdev
));
847 wait_event(b
->bm_io_wait
, atomic_read(&b
->bm_async_io
) == 0);
849 if (test_bit(BM_MD_IO_ERROR
, &b
->bm_flags
)) {
850 dev_alert(DEV
, "we had at least one MD IO ERROR during bitmap IO\n");
851 drbd_chk_io_error(mdev
, 1, TRUE
);
857 /* swap back endianness */
859 /* flush bitmap to stable storage */
861 } else /* rw == READ */ {
862 /* just read, if necessary adjust endianness */
863 b
->bm_set
= bm_count_bits_swap_endian(b
);
864 dev_info(DEV
, "recounting of set bits took additional %lu jiffies\n",
869 dev_info(DEV
, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
870 ppsize(ppb
, now
<< (BM_BLOCK_SHIFT
-10)), now
);
876 * drbd_bm_read() - Read the whole bitmap from its on disk location.
877 * @mdev: DRBD device.
879 int drbd_bm_read(struct drbd_conf
*mdev
) __must_hold(local
)
881 return bm_rw(mdev
, READ
);
885 * drbd_bm_write() - Write the whole bitmap to its on disk location.
886 * @mdev: DRBD device.
888 int drbd_bm_write(struct drbd_conf
*mdev
) __must_hold(local
)
890 return bm_rw(mdev
, WRITE
);
894 * drbd_bm_write_sect: Writes a 512 (MD_SECTOR_SIZE) byte piece of the bitmap
895 * @mdev: DRBD device.
896 * @enr: Extent number in the resync lru (happens to be sector offset)
898 * The BM_EXT_SIZE is on purpose exactly the amount of the bitmap covered
899 * by a single sector write. Therefore enr == sector offset from the
900 * start of the bitmap.
902 int drbd_bm_write_sect(struct drbd_conf
*mdev
, unsigned long enr
) __must_hold(local
)
904 sector_t on_disk_sector
= enr
+ mdev
->ldev
->md
.md_offset
905 + mdev
->ldev
->md
.bm_offset
;
906 int bm_words
, num_words
, offset
;
909 mutex_lock(&mdev
->md_io_mutex
);
910 bm_words
= drbd_bm_words(mdev
);
911 offset
= S2W(enr
); /* word offset into bitmap */
912 num_words
= min(S2W(1), bm_words
- offset
);
913 if (num_words
< S2W(1))
914 memset(page_address(mdev
->md_io_page
), 0, MD_SECTOR_SIZE
);
915 drbd_bm_get_lel(mdev
, offset
, num_words
,
916 page_address(mdev
->md_io_page
));
917 if (!drbd_md_sync_page_io(mdev
, mdev
->ldev
, on_disk_sector
, WRITE
)) {
920 dev_err(DEV
, "IO ERROR writing bitmap sector %lu "
921 "(meta-disk sector %llus)\n",
922 enr
, (unsigned long long)on_disk_sector
);
923 drbd_chk_io_error(mdev
, 1, TRUE
);
924 for (i
= 0; i
< AL_EXT_PER_BM_SECT
; i
++)
925 drbd_bm_ALe_set_all(mdev
, enr
*AL_EXT_PER_BM_SECT
+i
);
928 mutex_unlock(&mdev
->md_io_mutex
);
933 * find_first_bit returns int, we return unsigned long.
934 * should not make much difference anyways, but ...
936 * this returns a bit number, NOT a sector!
938 #define BPP_MASK ((1UL << (PAGE_SHIFT+3)) - 1)
939 static unsigned long __bm_find_next(struct drbd_conf
*mdev
, unsigned long bm_fo
,
940 const int find_zero_bit
, const enum km_type km
)
942 struct drbd_bitmap
*b
= mdev
->bitmap
;
943 unsigned long i
= -1UL;
944 unsigned long *p_addr
;
945 unsigned long bit_offset
; /* bit offset of the mapped page. */
947 if (bm_fo
> b
->bm_bits
) {
948 dev_err(DEV
, "bm_fo=%lu bm_bits=%lu\n", bm_fo
, b
->bm_bits
);
950 while (bm_fo
< b
->bm_bits
) {
951 unsigned long offset
;
952 bit_offset
= bm_fo
& ~BPP_MASK
; /* bit offset of the page */
953 offset
= bit_offset
>> LN2_BPL
; /* word offset of the page */
954 p_addr
= __bm_map_paddr(b
, offset
, km
);
957 i
= find_next_zero_bit(p_addr
, PAGE_SIZE
*8, bm_fo
& BPP_MASK
);
959 i
= find_next_bit(p_addr
, PAGE_SIZE
*8, bm_fo
& BPP_MASK
);
961 __bm_unmap(p_addr
, km
);
962 if (i
< PAGE_SIZE
*8) {
968 bm_fo
= bit_offset
+ PAGE_SIZE
*8;
976 static unsigned long bm_find_next(struct drbd_conf
*mdev
,
977 unsigned long bm_fo
, const int find_zero_bit
)
979 struct drbd_bitmap
*b
= mdev
->bitmap
;
980 unsigned long i
= -1UL;
983 ERR_IF(!b
->bm_pages
) return i
;
985 spin_lock_irq(&b
->bm_lock
);
987 bm_print_lock_info(mdev
);
989 i
= __bm_find_next(mdev
, bm_fo
, find_zero_bit
, KM_IRQ1
);
991 spin_unlock_irq(&b
->bm_lock
);
995 unsigned long drbd_bm_find_next(struct drbd_conf
*mdev
, unsigned long bm_fo
)
997 return bm_find_next(mdev
, bm_fo
, 0);
1001 /* not yet needed for anything. */
1002 unsigned long drbd_bm_find_next_zero(struct drbd_conf
*mdev
, unsigned long bm_fo
)
1004 return bm_find_next(mdev
, bm_fo
, 1);
1008 /* does not spin_lock_irqsave.
1009 * you must take drbd_bm_lock() first */
1010 unsigned long _drbd_bm_find_next(struct drbd_conf
*mdev
, unsigned long bm_fo
)
1012 /* WARN_ON(!bm_is_locked(mdev)); */
1013 return __bm_find_next(mdev
, bm_fo
, 0, KM_USER1
);
1016 unsigned long _drbd_bm_find_next_zero(struct drbd_conf
*mdev
, unsigned long bm_fo
)
1018 /* WARN_ON(!bm_is_locked(mdev)); */
1019 return __bm_find_next(mdev
, bm_fo
, 1, KM_USER1
);
1022 /* returns number of bits actually changed.
1023 * for val != 0, we change 0 -> 1, return code positive
1024 * for val == 0, we change 1 -> 0, return code negative
1025 * wants bitnr, not sector.
1026 * expected to be called for only a few bits (e - s about BITS_PER_LONG).
1027 * Must hold bitmap lock already. */
1028 static int __bm_change_bits_to(struct drbd_conf
*mdev
, const unsigned long s
,
1029 unsigned long e
, int val
, const enum km_type km
)
1031 struct drbd_bitmap
*b
= mdev
->bitmap
;
1032 unsigned long *p_addr
= NULL
;
1033 unsigned long bitnr
;
1034 unsigned long last_page_nr
= -1UL;
1037 if (e
>= b
->bm_bits
) {
1038 dev_err(DEV
, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
1040 e
= b
->bm_bits
? b
->bm_bits
-1 : 0;
1042 for (bitnr
= s
; bitnr
<= e
; bitnr
++) {
1043 unsigned long offset
= bitnr
>>LN2_BPL
;
1044 unsigned long page_nr
= offset
>> (PAGE_SHIFT
- LN2_BPL
+ 3);
1045 if (page_nr
!= last_page_nr
) {
1047 __bm_unmap(p_addr
, km
);
1048 p_addr
= __bm_map_paddr(b
, offset
, km
);
1049 last_page_nr
= page_nr
;
1052 c
+= (0 == __test_and_set_bit(bitnr
& BPP_MASK
, p_addr
));
1054 c
-= (0 != __test_and_clear_bit(bitnr
& BPP_MASK
, p_addr
));
1057 __bm_unmap(p_addr
, km
);
1062 /* returns number of bits actually changed.
1063 * for val != 0, we change 0 -> 1, return code positive
1064 * for val == 0, we change 1 -> 0, return code negative
1065 * wants bitnr, not sector */
1066 static int bm_change_bits_to(struct drbd_conf
*mdev
, const unsigned long s
,
1067 const unsigned long e
, int val
)
1069 unsigned long flags
;
1070 struct drbd_bitmap
*b
= mdev
->bitmap
;
1073 ERR_IF(!b
) return 1;
1074 ERR_IF(!b
->bm_pages
) return 0;
1076 spin_lock_irqsave(&b
->bm_lock
, flags
);
1077 if (bm_is_locked(b
))
1078 bm_print_lock_info(mdev
);
1080 c
= __bm_change_bits_to(mdev
, s
, e
, val
, KM_IRQ1
);
1082 spin_unlock_irqrestore(&b
->bm_lock
, flags
);
1086 /* returns number of bits changed 0 -> 1 */
1087 int drbd_bm_set_bits(struct drbd_conf
*mdev
, const unsigned long s
, const unsigned long e
)
1089 return bm_change_bits_to(mdev
, s
, e
, 1);
1092 /* returns number of bits changed 1 -> 0 */
1093 int drbd_bm_clear_bits(struct drbd_conf
*mdev
, const unsigned long s
, const unsigned long e
)
1095 return -bm_change_bits_to(mdev
, s
, e
, 0);
1098 /* sets all bits in full words,
1099 * from first_word up to, but not including, last_word */
1100 static inline void bm_set_full_words_within_one_page(struct drbd_bitmap
*b
,
1101 int page_nr
, int first_word
, int last_word
)
1105 unsigned long *paddr
= kmap_atomic(b
->bm_pages
[page_nr
], KM_USER0
);
1106 for (i
= first_word
; i
< last_word
; i
++) {
1107 bits
= hweight_long(paddr
[i
]);
1109 b
->bm_set
+= BITS_PER_LONG
- bits
;
1111 kunmap_atomic(paddr
, KM_USER0
);
1114 /* Same thing as drbd_bm_set_bits, but without taking the spin_lock_irqsave.
1115 * You must first drbd_bm_lock().
1116 * Can be called to set the whole bitmap in one go.
1117 * Sets bits from s to e _inclusive_. */
1118 void _drbd_bm_set_bits(struct drbd_conf
*mdev
, const unsigned long s
, const unsigned long e
)
1120 /* First set_bit from the first bit (s)
1121 * up to the next long boundary (sl),
1122 * then assign full words up to the last long boundary (el),
1123 * then set_bit up to and including the last bit (e).
1125 * Do not use memset, because we must account for changes,
1126 * so we need to loop over the words with hweight() anyways.
1128 unsigned long sl
= ALIGN(s
,BITS_PER_LONG
);
1129 unsigned long el
= (e
+1) & ~((unsigned long)BITS_PER_LONG
-1);
1136 if (e
- s
<= 3*BITS_PER_LONG
) {
1137 /* don't bother; el and sl may even be wrong. */
1138 __bm_change_bits_to(mdev
, s
, e
, 1, KM_USER0
);
1142 /* difference is large enough that we can trust sl and el */
1144 /* bits filling the current long */
1146 __bm_change_bits_to(mdev
, s
, sl
-1, 1, KM_USER0
);
1148 first_page
= sl
>> (3 + PAGE_SHIFT
);
1149 last_page
= el
>> (3 + PAGE_SHIFT
);
1151 /* MLPP: modulo longs per page */
1152 /* LWPP: long words per page */
1153 first_word
= MLPP(sl
>> LN2_BPL
);
1156 /* first and full pages, unless first page == last page */
1157 for (page_nr
= first_page
; page_nr
< last_page
; page_nr
++) {
1158 bm_set_full_words_within_one_page(mdev
->bitmap
, page_nr
, first_word
, last_word
);
1163 /* last page (respectively only page, for first page == last page) */
1164 last_word
= MLPP(el
>> LN2_BPL
);
1165 bm_set_full_words_within_one_page(mdev
->bitmap
, last_page
, first_word
, last_word
);
1167 /* possibly trailing bits.
1168 * example: (e & 63) == 63, el will be e+1.
1169 * if that even was the very last bit,
1170 * it would trigger an assert in __bm_change_bits_to()
1173 __bm_change_bits_to(mdev
, el
, e
, 1, KM_USER0
);
1176 /* returns bit state
1177 * wants bitnr, NOT sector.
1178 * inherently racy... area needs to be locked by means of {al,rs}_lru
1181 * -1 ... first out of bounds access, stop testing for bits!
1183 int drbd_bm_test_bit(struct drbd_conf
*mdev
, const unsigned long bitnr
)
1185 unsigned long flags
;
1186 struct drbd_bitmap
*b
= mdev
->bitmap
;
1187 unsigned long *p_addr
;
1190 ERR_IF(!b
) return 0;
1191 ERR_IF(!b
->bm_pages
) return 0;
1193 spin_lock_irqsave(&b
->bm_lock
, flags
);
1194 if (bm_is_locked(b
))
1195 bm_print_lock_info(mdev
);
1196 if (bitnr
< b
->bm_bits
) {
1197 unsigned long offset
= bitnr
>>LN2_BPL
;
1198 p_addr
= bm_map_paddr(b
, offset
);
1199 i
= test_bit(bitnr
& BPP_MASK
, p_addr
) ? 1 : 0;
1201 } else if (bitnr
== b
->bm_bits
) {
1203 } else { /* (bitnr > b->bm_bits) */
1204 dev_err(DEV
, "bitnr=%lu > bm_bits=%lu\n", bitnr
, b
->bm_bits
);
1208 spin_unlock_irqrestore(&b
->bm_lock
, flags
);
1212 /* returns number of bits set in the range [s, e] */
1213 int drbd_bm_count_bits(struct drbd_conf
*mdev
, const unsigned long s
, const unsigned long e
)
1215 unsigned long flags
;
1216 struct drbd_bitmap
*b
= mdev
->bitmap
;
1217 unsigned long *p_addr
= NULL
, page_nr
= -1;
1218 unsigned long bitnr
;
1222 /* If this is called without a bitmap, that is a bug. But just to be
1223 * robust in case we screwed up elsewhere, in that case pretend there
1224 * was one dirty bit in the requested area, so we won't try to do a
1225 * local read there (no bitmap probably implies no disk) */
1226 ERR_IF(!b
) return 1;
1227 ERR_IF(!b
->bm_pages
) return 1;
1229 spin_lock_irqsave(&b
->bm_lock
, flags
);
1230 if (bm_is_locked(b
))
1231 bm_print_lock_info(mdev
);
1232 for (bitnr
= s
; bitnr
<= e
; bitnr
++) {
1233 w
= bitnr
>> LN2_BPL
;
1234 if (page_nr
!= w
>> (PAGE_SHIFT
- LN2_BPL
+ 3)) {
1235 page_nr
= w
>> (PAGE_SHIFT
- LN2_BPL
+ 3);
1238 p_addr
= bm_map_paddr(b
, w
);
1240 ERR_IF (bitnr
>= b
->bm_bits
) {
1241 dev_err(DEV
, "bitnr=%lu bm_bits=%lu\n", bitnr
, b
->bm_bits
);
1243 c
+= (0 != test_bit(bitnr
- (page_nr
<< (PAGE_SHIFT
+3)), p_addr
));
1248 spin_unlock_irqrestore(&b
->bm_lock
, flags
);
1253 /* inherently racy...
1254 * return value may be already out-of-date when this function returns.
1255 * but the general usage is that this is only use during a cstate when bits are
1256 * only cleared, not set, and typically only care for the case when the return
1257 * value is zero, or we already "locked" this "bitmap extent" by other means.
1259 * enr is bm-extent number, since we chose to name one sector (512 bytes)
1260 * worth of the bitmap a "bitmap extent".
1263 * I think since we use it like a reference count, we should use the real
1264 * reference count of some bitmap extent element from some lru instead...
1267 int drbd_bm_e_weight(struct drbd_conf
*mdev
, unsigned long enr
)
1269 struct drbd_bitmap
*b
= mdev
->bitmap
;
1271 unsigned long flags
;
1272 unsigned long *p_addr
, *bm
;
1274 ERR_IF(!b
) return 0;
1275 ERR_IF(!b
->bm_pages
) return 0;
1277 spin_lock_irqsave(&b
->bm_lock
, flags
);
1278 if (bm_is_locked(b
))
1279 bm_print_lock_info(mdev
);
1282 e
= min((size_t)S2W(enr
+1), b
->bm_words
);
1284 if (s
< b
->bm_words
) {
1286 p_addr
= bm_map_paddr(b
, s
);
1287 bm
= p_addr
+ MLPP(s
);
1289 count
+= hweight_long(*bm
++);
1292 dev_err(DEV
, "start offset (%d) too large in drbd_bm_e_weight\n", s
);
1294 spin_unlock_irqrestore(&b
->bm_lock
, flags
);
1298 /* set all bits covered by the AL-extent al_enr */
1299 unsigned long drbd_bm_ALe_set_all(struct drbd_conf
*mdev
, unsigned long al_enr
)
1301 struct drbd_bitmap
*b
= mdev
->bitmap
;
1302 unsigned long *p_addr
, *bm
;
1303 unsigned long weight
;
1304 int count
, s
, e
, i
, do_now
;
1305 ERR_IF(!b
) return 0;
1306 ERR_IF(!b
->bm_pages
) return 0;
1308 spin_lock_irq(&b
->bm_lock
);
1309 if (bm_is_locked(b
))
1310 bm_print_lock_info(mdev
);
1313 s
= al_enr
* BM_WORDS_PER_AL_EXT
;
1314 e
= min_t(size_t, s
+ BM_WORDS_PER_AL_EXT
, b
->bm_words
);
1315 /* assert that s and e are on the same page */
1316 D_ASSERT((e
-1) >> (PAGE_SHIFT
- LN2_BPL
+ 3)
1317 == s
>> (PAGE_SHIFT
- LN2_BPL
+ 3));
1319 if (s
< b
->bm_words
) {
1321 p_addr
= bm_map_paddr(b
, s
);
1322 bm
= p_addr
+ MLPP(s
);
1324 count
+= hweight_long(*bm
);
1329 b
->bm_set
+= do_now
*BITS_PER_LONG
- count
;
1330 if (e
== b
->bm_words
)
1331 b
->bm_set
-= bm_clear_surplus(b
);
1333 dev_err(DEV
, "start offset (%d) too large in drbd_bm_ALe_set_all\n", s
);
1335 weight
= b
->bm_set
- weight
;
1336 spin_unlock_irq(&b
->bm_lock
);