drbd: Added some missing statics
[linux-2.6/next.git] / drivers / block / drbd / drbd_bitmap.c
blobaa7e23cf7e220d96b9d26ba23731fc5b3e38ce26
1 /*
2 drbd_bitmap.c
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)
13 any later version.
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>
31 #include "drbd_int.h"
33 /* OPAQUE outside this file!
34 * interface defined in drbd_int.h
36 * convention:
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...
46 * NOTE
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.
55 struct drbd_bitmap {
56 struct page **bm_pages;
57 spinlock_t bm_lock;
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;
67 size_t bm_words;
68 size_t bm_number_of_pages;
69 sector_t bm_dev_capacity;
70 struct mutex bm_change; /* serializes resize operations */
72 atomic_t bm_async_io;
73 wait_queue_head_t bm_io_wait;
75 unsigned long bm_flags;
77 /* debugging aid, in case we are still racy somewhere */
78 char *bm_why;
79 struct task_struct *bm_task;
82 /* definition of bits in bm_flags */
83 #define BM_LOCKED 0
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))
100 return;
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;
114 int trylock_failed;
116 if (!b) {
117 dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n");
118 return;
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");
137 b->bm_why = why;
138 b->bm_task = current;
141 void drbd_bm_unlock(struct drbd_conf *mdev)
143 struct drbd_bitmap *b = mdev->bitmap;
144 if (!b) {
145 dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n");
146 return;
149 if (!__test_and_clear_bit(BM_LOCKED, &mdev->bitmap->bm_flags))
150 dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");
152 b->bm_why = NULL;
153 b->bm_task = NULL;
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)
160 struct page *page;
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)
192 so do it explicitly:
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)
207 unsigned long i;
208 if (!pages)
209 return;
211 for (i = 0; i < number; i++) {
212 if (!pages[i]) {
213 printk(KERN_ALERT "drbd: bm_free_pages tried to free "
214 "a NULL pointer; i=%lu n=%lu\n",
215 i, number);
216 continue;
218 __free_page(pages[i]);
219 pages[i] = NULL;
223 static void bm_vk_free(void *ptr, int v)
225 if (v)
226 vfree(ptr);
227 else
228 kfree(ptr);
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);
244 if (have == want)
245 return old_pages;
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);
254 if (!new_pages) {
255 new_pages = vmalloc(bytes);
256 if (!new_pages)
257 return NULL;
258 vmalloced = 1;
261 memset(new_pages, 0, bytes);
262 if (want >= have) {
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);
267 if (!page) {
268 bm_free_pages(new_pages + have, i - have);
269 bm_vk_free(new_pages, vmalloced);
270 return NULL;
272 new_pages[i] = page;
274 } else {
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);
282 if (vmalloced)
283 set_bit(BM_P_VMALLOCED, &b->bm_flags);
284 else
285 clear_bit(BM_P_VMALLOCED, &b->bm_flags);
287 return new_pages;
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;
297 WARN_ON(b != NULL);
298 b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
299 if (!b)
300 return -ENOMEM;
301 spin_lock_init(&b->bm_lock);
302 mutex_init(&b->bm_change);
303 init_waitqueue_head(&b->bm_io_wait);
305 mdev->bitmap = b;
307 return 0;
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));
323 kfree(mdev->bitmap);
324 mdev->bitmap = NULL;
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;
336 int cleared = 0;
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);
343 *bm &= mask;
344 w++; bm++;
347 if (w < b->bm_words) {
348 cleared += hweight_long(*bm);
349 *bm = 0;
351 bm_unmap(p_addr);
352 return cleared;
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) {
364 *bm |= ~mask;
365 bm++; w++;
368 if (w < b->bm_words) {
369 *bm = ~(0UL);
371 bm_unmap(p_addr);
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);
384 while (i--) {
385 #ifndef __LITTLE_ENDIAN
386 if (swap_endian)
387 *bm = lel_to_cpu(*bm);
388 #endif
389 bits += hweight_long(*bm++);
391 __bm_unmap(p_addr, KM_USER0);
392 offset += do_now;
393 cond_resched();
396 return bits;
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;
413 size_t do_now, end;
415 #define BM_SECTORS_PER_BIT (BM_BLOCK_SIZE/512)
417 end = offset + len;
419 if (end > b->bm_words) {
420 printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
421 return;
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));
434 bm_unmap(p_addr);
435 offset += do_now;
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)
464 goto out;
466 opages_vmalloced = test_bit(BM_P_VMALLOCED, &b->bm_flags);
468 if (capacity == 0) {
469 spin_lock_irq(&b->bm_lock);
470 opages = b->bm_pages;
471 onpages = b->bm_number_of_pages;
472 owords = b->bm_words;
473 b->bm_pages = NULL;
474 b->bm_number_of_pages =
475 b->bm_set =
476 b->bm_bits =
477 b->bm_words =
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);
482 goto out;
484 bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));
486 /* if we would use
487 words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
488 a 32bit host could present the wrong number of words
489 to a 64bit host.
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));
495 put_ldev(mdev);
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;
501 if (want == have) {
502 D_ASSERT(b->bm_pages != NULL);
503 npages = b->bm_pages;
504 } else {
505 if (FAULT_ACTIVE(mdev, DRBD_FAULT_BM_ALLOC))
506 npages = NULL;
507 else
508 npages = bm_realloc_pages(b, want);
511 if (!npages) {
512 err = -ENOMEM;
513 goto out;
516 spin_lock_irq(&b->bm_lock);
517 opages = b->bm_pages;
518 owords = b->bm_words;
519 obits = b->bm_bits;
521 growing = bits > obits;
522 if (opages)
523 bm_set_surplus(b);
525 b->bm_pages = npages;
526 b->bm_number_of_pages = want;
527 b->bm_bits = bits;
528 b->bm_words = words;
529 b->bm_dev_capacity = capacity;
531 if (growing) {
532 if (set_new_bits) {
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),
537 1, KM_IRQ1);
538 } else
539 bm_memset(b, owords, 0x00, words-owords);
543 if (want < have) {
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);
550 *bm = DRBD_MAGIC;
551 bm_unmap(p_addr);
553 (void)bm_clear_surplus(b);
555 spin_unlock_irq(&b->bm_lock);
556 if (opages != npages)
557 bm_vk_free(opages, opages_vmalloced);
558 if (!growing)
559 b->bm_set = bm_count_bits(b);
560 dev_info(DEV, "resync bitmap: bits=%lu words=%lu\n", bits, words);
562 out:
563 drbd_bm_unlock(mdev);
564 return err;
567 /* inherently racy:
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;
578 unsigned long s;
579 unsigned long flags;
581 ERR_IF(!b) return 0;
582 ERR_IF(!b->bm_pages) return 0;
584 spin_lock_irqsave(&b->bm_lock, flags);
585 s = b->bm_set;
586 spin_unlock_irqrestore(&b->bm_lock, flags);
588 return s;
591 unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
593 unsigned long s;
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))
596 return 0;
597 s = _drbd_bm_total_weight(mdev);
598 put_ldev(mdev);
599 return s;
602 size_t drbd_bm_words(struct drbd_conf *mdev)
604 struct drbd_bitmap *b = mdev->bitmap;
605 ERR_IF(!b) return 0;
606 ERR_IF(!b->bm_pages) return 0;
608 return b->bm_words;
611 unsigned long drbd_bm_bits(struct drbd_conf *mdev)
613 struct drbd_bitmap *b = mdev->bitmap;
614 ERR_IF(!b) return 0;
616 return b->bm_bits;
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;
630 size_t end, do_now;
632 end = offset + number;
634 ERR_IF(!b) return;
635 ERR_IF(!b->bm_pages) return;
636 if (number == 0)
637 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);
646 offset += do_now;
647 while (do_now--) {
648 bits = hweight_long(*bm);
649 word = *bm | lel_to_cpu(*buffer++);
650 *bm++ = word;
651 b->bm_set += hweight_long(word) - bits;
653 bm_unmap(p_addr);
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;
674 size_t end, do_now;
676 end = offset + number;
678 ERR_IF(!b) return;
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) ||
684 (number <= 0))
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);
689 else {
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);
694 offset += do_now;
695 while (do_now--)
696 *buffer++ = cpu_to_lel(*bm++);
697 bm_unmap(p_addr);
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;
707 ERR_IF(!b) return;
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;
721 ERR_IF(!b) return;
722 ERR_IF(!b->bm_pages) return;
724 spin_lock_irq(&b->bm_lock);
725 bm_memset(b, 0, 0, b->bm_words);
726 b->bm_set = 0;
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)
741 error = -EIO;
743 if (error) {
744 /* doh. what now?
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);
751 bio_put(bio);
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);
758 unsigned int len;
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);
771 bio->bi_private = b;
772 bio->bi_end_io = bm_async_io_complete;
774 if (FAULT_ACTIVE(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
775 bio->bi_rw |= rw;
776 bio_endio(bio, -EIO);
777 } else {
778 submit_bio(rw, bio);
782 # if defined(__LITTLE_ENDIAN)
783 /* nothing to do, on disk == in memory */
784 # define bm_cpu_to_lel(x) ((void)0)
785 # else
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 */
792 unsigned int i;
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;
800 } else {
801 /* all pages */
802 i = 0;
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);
811 # endif
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;
823 unsigned long now;
824 char ppb[10];
825 int err = 0;
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 */
835 if (rw == WRITE)
836 bm_cpu_to_lel(b);
838 now = jiffies;
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);
852 err = -EIO;
855 now = jiffies;
856 if (rw == WRITE) {
857 /* swap back endianness */
858 bm_lel_to_cpu(b);
859 /* flush bitmap to stable storage */
860 drbd_md_flush(mdev);
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",
865 jiffies - now);
867 now = b->bm_set;
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);
872 return err;
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;
907 int err = 0;
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)) {
918 int i;
919 err = -EIO;
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);
927 mdev->bm_writ_cnt++;
928 mutex_unlock(&mdev->md_io_mutex);
929 return err;
932 /* NOTE
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);
949 } else {
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);
956 if (find_zero_bit)
957 i = find_next_zero_bit(p_addr, PAGE_SIZE*8, bm_fo & BPP_MASK);
958 else
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) {
963 i = bit_offset + i;
964 if (i >= b->bm_bits)
965 break;
966 goto found;
968 bm_fo = bit_offset + PAGE_SIZE*8;
970 i = -1UL;
972 found:
973 return i;
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;
982 ERR_IF(!b) return i;
983 ERR_IF(!b->bm_pages) return i;
985 spin_lock_irq(&b->bm_lock);
986 if (bm_is_locked(b))
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);
992 return i;
995 unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
997 return bm_find_next(mdev, bm_fo, 0);
1000 #if 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);
1006 #endif
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;
1035 int c = 0;
1037 if (e >= b->bm_bits) {
1038 dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
1039 s, e, b->bm_bits);
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) {
1046 if (p_addr)
1047 __bm_unmap(p_addr, km);
1048 p_addr = __bm_map_paddr(b, offset, km);
1049 last_page_nr = page_nr;
1051 if (val)
1052 c += (0 == __test_and_set_bit(bitnr & BPP_MASK, p_addr));
1053 else
1054 c -= (0 != __test_and_clear_bit(bitnr & BPP_MASK, p_addr));
1056 if (p_addr)
1057 __bm_unmap(p_addr, km);
1058 b->bm_set += c;
1059 return c;
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;
1071 int c = 0;
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);
1083 return c;
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)
1103 int i;
1104 int bits;
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]);
1108 paddr[i] = ~0UL;
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);
1130 int first_page;
1131 int last_page;
1132 int page_nr;
1133 int first_word;
1134 int last_word;
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);
1139 return;
1142 /* difference is large enough that we can trust sl and el */
1144 /* bits filling the current long */
1145 if (sl)
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);
1154 last_word = LWPP;
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);
1159 cond_resched();
1160 first_word = 0;
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()
1172 if (el <= e)
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
1179 * 1 ... bit set
1180 * 0 ... bit not set
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;
1188 int i;
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;
1200 bm_unmap(p_addr);
1201 } else if (bitnr == b->bm_bits) {
1202 i = -1;
1203 } else { /* (bitnr > b->bm_bits) */
1204 dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
1205 i = 0;
1208 spin_unlock_irqrestore(&b->bm_lock, flags);
1209 return i;
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;
1219 int c = 0;
1220 size_t w;
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);
1236 if (p_addr)
1237 bm_unmap(p_addr);
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);
1242 } else {
1243 c += (0 != test_bit(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
1246 if (p_addr)
1247 bm_unmap(p_addr);
1248 spin_unlock_irqrestore(&b->bm_lock, flags);
1249 return c;
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".
1262 * TODO
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;
1270 int count, s, e;
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);
1281 s = S2W(enr);
1282 e = min((size_t)S2W(enr+1), b->bm_words);
1283 count = 0;
1284 if (s < b->bm_words) {
1285 int n = e-s;
1286 p_addr = bm_map_paddr(b, s);
1287 bm = p_addr + MLPP(s);
1288 while (n--)
1289 count += hweight_long(*bm++);
1290 bm_unmap(p_addr);
1291 } else {
1292 dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
1294 spin_unlock_irqrestore(&b->bm_lock, flags);
1295 return count;
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);
1311 weight = b->bm_set;
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));
1318 count = 0;
1319 if (s < b->bm_words) {
1320 i = do_now = e-s;
1321 p_addr = bm_map_paddr(b, s);
1322 bm = p_addr + MLPP(s);
1323 while (i--) {
1324 count += hweight_long(*bm);
1325 *bm = -1UL;
1326 bm++;
1328 bm_unmap(p_addr);
1329 b->bm_set += do_now*BITS_PER_LONG - count;
1330 if (e == b->bm_words)
1331 b->bm_set -= bm_clear_surplus(b);
1332 } else {
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);
1337 return weight;