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>
31
/* OPAQUE outside this file!
32
* interface defined in drbd_int.h
35
* function name drbd_bm_... => used elsewhere, "public".
36
* function name bm_... => internal to implementation, "private".
38
* Note that since find_first_bit returns int, at the current granularity of
39
* the bitmap (4KB per byte), this implementation "only" supports up to
40
* 1<<(32+12) == 16 TB...
45
* Access to the *bm_pages is protected by bm_lock.
46
* It is safe to read the other members within the lock.
48
* drbd_bm_set_bits is called from bio_endio callbacks,
49
* We may be called with irq already disabled,
50
* so we need spin_lock_irqsave().
51
* And we need the kmap_atomic.
54
struct page **bm_pages;
56
/* WARNING unsigned long bm_*:
57
* 32bit number of bit offset is just enough for 512 MB bitmap.
58
* it will blow up if we make the bitmap bigger...
59
* not that it makes much sense to have a bitmap that large,
60
* rather change the granularity to 16k or 64k or something.
61
* (that implies other problems, however...)
63
unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */
64
unsigned long bm_bits;
66
size_t bm_number_of_pages;
67
sector_t bm_dev_capacity;
68
struct semaphore bm_change; /* serializes resize operations */
71
wait_queue_head_t bm_io_wait;
73
unsigned long bm_flags;
75
/* debugging aid, in case we are still racy somewhere */
77
struct task_struct *bm_task;
80
/* definition of bits in bm_flags */
82
#define BM_MD_IO_ERROR 1
84
static inline int bm_is_locked(struct drbd_bitmap *b)
86
return test_bit(BM_LOCKED, &b->bm_flags);
89
#define bm_print_lock_info(m) __bm_print_lock_info(m, __func__)
90
static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func)
92
struct drbd_bitmap *b = mdev->bitmap;
93
if (!__ratelimit(&drbd_ratelimit_state))
95
ERR("FIXME %s in %s, bitmap locked for '%s' by %s\n",
96
current == mdev->receiver.task ? "receiver" :
97
current == mdev->asender.task ? "asender" :
98
current == mdev->worker.task ? "worker" : current->comm,
99
func, b->bm_why ?: "?",
100
b->bm_task == mdev->receiver.task ? "receiver" :
101
b->bm_task == mdev->asender.task ? "asender" :
102
b->bm_task == mdev->worker.task ? "worker" : "?");
105
void drbd_bm_lock(struct drbd_conf *mdev, char *why)
107
struct drbd_bitmap *b = mdev->bitmap;
111
ERR("FIXME no bitmap in drbd_bm_lock!?\n");
115
trylock_failed = down_trylock(&b->bm_change);
117
if (trylock_failed) {
118
DBG("%s going to '%s' but bitmap already locked for '%s' by %s\n",
119
current == mdev->receiver.task ? "receiver" :
120
current == mdev->asender.task ? "asender" :
121
current == mdev->worker.task ? "worker" : "?",
122
why, b->bm_why ?: "?",
123
b->bm_task == mdev->receiver.task ? "receiver" :
124
b->bm_task == mdev->asender.task ? "asender" :
125
b->bm_task == mdev->worker.task ? "worker" : "?");
128
if (__test_and_set_bit(BM_LOCKED, &b->bm_flags))
129
ERR("FIXME bitmap already locked in bm_lock\n");
132
b->bm_task = current;
135
void drbd_bm_unlock(struct drbd_conf *mdev)
137
struct drbd_bitmap *b = mdev->bitmap;
139
ERR("FIXME no bitmap in drbd_bm_unlock!?\n");
143
if (!__test_and_clear_bit(BM_LOCKED, &mdev->bitmap->bm_flags))
144
ERR("FIXME bitmap not locked in bm_unlock\n");
151
/* word offset to long pointer */
152
STATIC unsigned long *__bm_map_paddr(struct drbd_bitmap *b, unsigned long offset, const enum km_type km)
155
unsigned long page_nr;
157
/* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
158
page_nr = offset >> (PAGE_SHIFT - LN2_BPL + 3);
159
BUG_ON(page_nr >= b->bm_number_of_pages);
160
page = b->bm_pages[page_nr];
162
return (unsigned long *) kmap_atomic(page, km);
165
unsigned long * bm_map_paddr(struct drbd_bitmap *b, unsigned long offset)
167
return __bm_map_paddr(b, offset, KM_IRQ1);
170
void __bm_unmap(unsigned long *p_addr, const enum km_type km)
172
kunmap_atomic(p_addr, km);
175
void bm_unmap(unsigned long *p_addr)
177
return __bm_unmap(p_addr, KM_IRQ1);
180
/* long word offset of _bitmap_ sector */
181
#define S2W(s) ((s)<<(BM_EXT_SIZE_B-BM_BLOCK_SIZE_B-LN2_BPL))
182
/* word offset from start of bitmap to word number _in_page_
183
* modulo longs per page
184
#define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
185
hm, well, Philipp thinks gcc might not optimze the % into & (... - 1)
188
#define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
190
/* Long words per page */
191
#define LWPP (PAGE_SIZE/sizeof(long))
194
* actually most functions herein should take a struct drbd_bitmap*, not a
195
* struct drbd_conf*, but for the debug macros I like to have the mdev around
196
* to be able to report device specific.
199
STATIC void bm_free_pages(struct page **pages, unsigned long number)
205
for (i = 0; i < number; i++) {
207
printk(KERN_ALERT "drbd: bm_free_pages tried to free "
208
"a NULL pointer; i=%lu n=%lu\n",
212
__free_page(pages[i]);
218
* "have" and "want" are NUMBER OF PAGES.
220
STATIC struct page **bm_realloc_pages(struct page **old_pages,
224
struct page **new_pages, *page;
225
unsigned int i, bytes;
227
BUG_ON(have == 0 && old_pages != NULL);
228
BUG_ON(have != 0 && old_pages == NULL);
233
/* To use kmalloc here is ok, as long as we support 4TB at max...
234
* otherwise this might become bigger than 128KB, which is
235
* the maximum for kmalloc.
237
* no, it is not: on 64bit boxes, sizeof(void*) == 8,
238
* 128MB bitmap @ 4K pages -> 256K of page pointers.
239
* ==> use vmalloc for now again.
240
* then again, we could do something like
241
* if (nr_pages > watermark) vmalloc else kmalloc :*> ...
242
* or do cascading page arrays:
243
* one page for the page array of the page array,
244
* those pages for the real bitmap pages.
245
* there we could even add some optimization members,
246
* so we won't need to kmap_atomic in bm_find_next_bit just to see
247
* that the page has no bits set ...
248
* or we can try a "huge" page ;-)
250
bytes = sizeof(struct page *)*want;
251
new_pages = vmalloc(bytes);
255
memset(new_pages, 0, bytes);
257
for (i = 0; i < have; i++)
258
new_pages[i] = old_pages[i];
259
for (; i < want; i++) {
260
page = alloc_page(GFP_HIGHUSER);
262
bm_free_pages(new_pages + have, i - have);
269
for (i = 0; i < want; i++)
270
new_pages[i] = old_pages[i];
271
/* NOT HERE, we are outside the spinlock!
272
bm_free_pages(old_pages + want, have - want);
280
* called on driver init only. TODO call when a device is created.
281
* allocates the drbd_bitmap, and stores it in mdev->bitmap.
283
int drbd_bm_init(struct drbd_conf *mdev)
285
struct drbd_bitmap *b = mdev->bitmap;
287
b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
290
spin_lock_init(&b->bm_lock);
291
init_MUTEX(&b->bm_change);
292
init_waitqueue_head(&b->bm_io_wait);
299
sector_t drbd_bm_capacity(struct drbd_conf *mdev)
301
ERR_IF(!mdev->bitmap) return 0;
302
return mdev->bitmap->bm_dev_capacity;
305
/* called on driver unload. TODO: call when a device is destroyed.
307
void drbd_bm_cleanup(struct drbd_conf *mdev)
309
ERR_IF (!mdev->bitmap) return;
310
bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
311
vfree(mdev->bitmap->bm_pages);
317
* since (b->bm_bits % BITS_PER_LONG) != 0,
318
* this masks out the remaining bits.
319
* Rerturns the number of bits cleared.
321
STATIC int bm_clear_surplus(struct drbd_bitmap *b)
323
const unsigned long mask = (1UL << (b->bm_bits & (BITS_PER_LONG-1))) - 1;
324
size_t w = b->bm_bits >> LN2_BPL;
326
unsigned long *p_addr, *bm;
328
p_addr = bm_map_paddr(b, w);
329
bm = p_addr + MLPP(w);
330
if (w < b->bm_words) {
331
cleared = hweight_long(*bm & ~mask);
336
if (w < b->bm_words) {
337
cleared += hweight_long(*bm);
344
STATIC void bm_set_surplus(struct drbd_bitmap *b)
346
const unsigned long mask = (1UL << (b->bm_bits & (BITS_PER_LONG-1))) - 1;
347
size_t w = b->bm_bits >> LN2_BPL;
348
unsigned long *p_addr, *bm;
350
p_addr = bm_map_paddr(b, w);
351
bm = p_addr + MLPP(w);
352
if (w < b->bm_words) {
357
if (w < b->bm_words) {
363
STATIC unsigned long __bm_count_bits(struct drbd_bitmap *b, const int swap_endian)
365
unsigned long *p_addr, *bm, offset = 0;
366
unsigned long bits = 0;
367
unsigned long i, do_now;
369
while (offset < b->bm_words) {
370
i = do_now = min_t(size_t, b->bm_words-offset, LWPP);
371
p_addr = bm_map_paddr(b, offset);
372
bm = p_addr + MLPP(offset);
374
#ifndef __LITTLE_ENDIAN
376
*bm = lel_to_cpu(*bm);
378
bits += hweight_long(*bm++);
387
static inline unsigned long bm_count_bits(struct drbd_bitmap *b)
389
return __bm_count_bits(b, 0);
392
static inline unsigned long bm_count_bits_swap_endian(struct drbd_bitmap *b)
394
return __bm_count_bits(b, 1);
397
void _drbd_bm_recount_bits(struct drbd_conf *mdev, char *file, int line)
399
struct drbd_bitmap *b = mdev->bitmap;
400
unsigned long flags, bits;
404
/* IMO this should be inside drbd_bm_lock/unlock.
405
* Unfortunately it is used outside of the locks.
406
* And I'm not yet sure where we need to place the
407
* lock/unlock correctly.
410
spin_lock_irqsave(&b->bm_lock, flags);
411
bits = bm_count_bits(b);
412
if (bits != b->bm_set) {
413
ERR("bm_set was %lu, corrected to %lu. %s:%d\n",
414
b->bm_set, bits, file, line);
417
spin_unlock_irqrestore(&b->bm_lock, flags);
420
/* offset and len in long words.*/
421
STATIC void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
423
unsigned long *p_addr, *bm;
426
#define BM_SECTORS_PER_BIT (BM_BLOCK_SIZE/512)
430
if (end > b->bm_words) {
431
printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
435
while (offset < end) {
436
do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
437
p_addr = bm_map_paddr(b, offset);
438
bm = p_addr + MLPP(offset);
439
if (bm+do_now > p_addr + LWPP) {
440
printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
441
p_addr, bm, (int)do_now);
442
break; /* breaks to after catch_oob_access_end() only! */
444
memset(bm, c, do_now * sizeof(long));
451
* make sure the bitmap has enough room for the attached storage,
452
* if neccessary, resize.
453
* called whenever we may have changed the device size.
454
* returns -ENOMEM if we could not allocate enough memory, 0 on success.
455
* In case this is actually a resize, we copy the old bitmap into the new one.
456
* Otherwise, the bitmap is initiallized to all bits set.
458
int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity)
460
struct drbd_bitmap *b = mdev->bitmap;
461
unsigned long bits, words, owords, obits, *p_addr, *bm;
462
unsigned long want, have, onpages; /* number of pages */
463
struct page **npages, **opages = NULL;
464
int err = 0, growing;
466
ERR_IF(!b) return -ENOMEM;
468
drbd_bm_lock(mdev, "resize");
470
INFO("drbd_bm_resize called with capacity == %llu\n",
471
(unsigned long long)capacity);
473
if (capacity == b->bm_dev_capacity)
477
spin_lock_irq(&b->bm_lock);
478
opages = b->bm_pages;
479
onpages = b->bm_number_of_pages;
480
owords = b->bm_words;
482
b->bm_number_of_pages =
486
b->bm_dev_capacity = 0;
487
spin_unlock_irq(&b->bm_lock);
488
bm_free_pages(opages, onpages);
492
bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));
495
words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
496
a 32bit host could present the wrong number of words
499
words = ALIGN(bits, 64) >> LN2_BPL;
501
if (inc_local(mdev)) {
502
D_ASSERT((u64)bits <= (((u64)mdev->bc->md.md_size_sect-MD_BM_OFFSET) << 12));
506
/* one extra long to catch off by one errors */
507
want = ALIGN((words+1)*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT;
508
have = b->bm_number_of_pages;
510
D_ASSERT(b->bm_pages != NULL);
511
npages = b->bm_pages;
513
if (FAULT_ACTIVE(mdev, DRBD_FAULT_BM_ALLOC))
516
npages = bm_realloc_pages(b->bm_pages, have, want);
524
spin_lock_irq(&b->bm_lock);
525
opages = b->bm_pages;
526
owords = b->bm_words;
529
growing = bits > obits;
533
b->bm_pages = npages;
534
b->bm_number_of_pages = want;
537
b->bm_dev_capacity = capacity;
540
bm_memset(b, owords, 0xff, words-owords);
541
b->bm_set += bits - obits;
545
/* implicit: (opages != NULL) && (opages != npages) */
546
bm_free_pages(opages + want, have - want);
549
p_addr = bm_map_paddr(b, words);
550
bm = p_addr + MLPP(words);
554
(void)bm_clear_surplus(b);
556
b->bm_set = bm_count_bits(b);
558
spin_unlock_irq(&b->bm_lock);
559
if (opages != npages)
561
INFO("resync bitmap: bits=%lu words=%lu\n", bits, words);
564
drbd_bm_unlock(mdev);
569
* if not protected by other means, return value may be out of date when
570
* leaving this function...
571
* we still need to lock it, since it is important that this returns
572
* bm_set == 0 precisely.
574
* maybe bm_set should be atomic_t ?
576
unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
578
struct drbd_bitmap *b = mdev->bitmap;
582
/* if I don't have a disk, I don't know about out-of-sync status */
583
if (!inc_local_if_state(mdev, Negotiating))
587
ERR_IF(!b->bm_pages) return 0;
589
spin_lock_irqsave(&b->bm_lock, flags);
591
spin_unlock_irqrestore(&b->bm_lock, flags);
598
size_t drbd_bm_words(struct drbd_conf *mdev)
600
struct drbd_bitmap *b = mdev->bitmap;
602
ERR_IF(!b->bm_pages) return 0;
607
unsigned long drbd_bm_bits(struct drbd_conf *mdev)
609
struct drbd_bitmap *b = mdev->bitmap;
615
/* merge number words from buffer into the bitmap starting at offset.
616
* buffer[i] is expected to be little endian unsigned long.
617
* bitmap must be locked by drbd_bm_lock.
618
* currently only used from receive_bitmap.
620
void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
621
unsigned long *buffer)
623
struct drbd_bitmap *b = mdev->bitmap;
624
unsigned long *p_addr, *bm;
625
unsigned long word, bits;
628
end = offset + number;
631
ERR_IF(!b->bm_pages) return;
634
WARN_ON(offset >= b->bm_words);
635
WARN_ON(end > b->bm_words);
637
spin_lock_irq(&b->bm_lock);
638
while (offset < end) {
639
do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
640
p_addr = bm_map_paddr(b, offset);
641
bm = p_addr + MLPP(offset);
644
bits = hweight_long(*bm);
645
word = *bm | lel_to_cpu(*buffer++);
647
b->bm_set += hweight_long(word) - bits;
651
/* with 32bit <-> 64bit cross-platform connect
652
* this is only correct for current usage,
653
* where we _know_ that we are 64 bit aligned,
654
* and know that this function is used in this way, too...
656
if (end == b->bm_words)
657
b->bm_set -= bm_clear_surplus(b);
659
spin_unlock_irq(&b->bm_lock);
662
/* copy number words from the bitmap starting at offset into the buffer.
663
* buffer[i] will be little endian unsigned long.
665
void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
666
unsigned long *buffer)
668
struct drbd_bitmap *b = mdev->bitmap;
669
unsigned long *p_addr, *bm;
672
end = offset + number;
675
ERR_IF(!b->bm_pages) return;
677
spin_lock_irq(&b->bm_lock);
678
if ((offset >= b->bm_words) ||
679
(end > b->bm_words) ||
681
ERR("offset=%lu number=%lu bm_words=%lu\n",
682
(unsigned long) offset,
683
(unsigned long) number,
684
(unsigned long) b->bm_words);
686
while (offset < end) {
687
do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
688
p_addr = bm_map_paddr(b, offset);
689
bm = p_addr + MLPP(offset);
692
*buffer++ = cpu_to_lel(*bm++);
696
spin_unlock_irq(&b->bm_lock);
699
/* set all bits in the bitmap */
700
void drbd_bm_set_all(struct drbd_conf *mdev)
702
struct drbd_bitmap *b = mdev->bitmap;
704
ERR_IF(!b->bm_pages) return;
706
spin_lock_irq(&b->bm_lock);
707
bm_memset(b, 0, 0xff, b->bm_words);
708
(void)bm_clear_surplus(b);
709
b->bm_set = b->bm_bits;
710
spin_unlock_irq(&b->bm_lock);
713
/* clear all bits in the bitmap */
714
void drbd_bm_clear_all(struct drbd_conf *mdev)
716
struct drbd_bitmap *b = mdev->bitmap;
718
ERR_IF(!b->bm_pages) return;
720
spin_lock_irq(&b->bm_lock);
721
bm_memset(b, 0, 0, b->bm_words);
723
spin_unlock_irq(&b->bm_lock);
726
static void bm_async_io_complete(struct bio *bio, int error)
728
struct drbd_bitmap *b = bio->bi_private;
729
int uptodate = bio_flagged(bio, BIO_UPTODATE);
732
/* strange behaviour of some lower level drivers...
733
* fail the request by clearing the uptodate flag,
734
* but do not return any error?!
735
* do we want to WARN() on this? */
736
if (!error && !uptodate)
741
* for now, set all bits, and flag MD_IO_ERROR */
742
__set_bit(BM_MD_IO_ERROR, &b->bm_flags);
744
if (atomic_dec_and_test(&b->bm_async_io))
745
wake_up(&b->bm_io_wait);
750
STATIC void bm_page_io_async(struct drbd_conf *mdev, struct drbd_bitmap *b, int page_nr, int rw) __must_hold(local)
752
/* we are process context. we always get a bio */
753
struct bio *bio = bio_alloc(GFP_KERNEL, 1);
755
sector_t on_disk_sector =
756
mdev->bc->md.md_offset + mdev->bc->md.bm_offset;
757
on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);
759
/* this might happen with very small
760
* flexible external meta data device */
761
len = min_t(unsigned int, PAGE_SIZE,
762
(drbd_md_last_sector(mdev->bc) - on_disk_sector + 1)<<9);
764
bio->bi_bdev = mdev->bc->md_bdev;
765
bio->bi_sector = on_disk_sector;
766
bio_add_page(bio, b->bm_pages[page_nr], len, 0);
768
bio->bi_end_io = bm_async_io_complete;
770
if (FAULT_ACTIVE(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
772
bio_endio(bio, -EIO);
778
# if defined(__LITTLE_ENDIAN)
779
/* nothing to do, on disk == in memory */
780
# define bm_cpu_to_lel(x) ((void)0)
782
void bm_cpu_to_lel(struct drbd_bitmap *b)
784
/* need to cpu_to_lel all the pages ...
785
* this may be optimized by using
786
* cpu_to_lel(-1) == -1 and cpu_to_lel(0) == 0;
787
* the following is still not optimal, but better than nothing */
788
if (b->bm_set == 0) {
789
/* no page at all; avoid swap if all is 0 */
790
i = b->bm_number_of_pages;
791
} else if (b->bm_set == b->bm_bits) {
792
/* only the last page */
793
i = b->bm_number_of_pages - 1;
798
for (; i < b->bm_number_of_pages; i++) {
800
/* if you'd want to use kmap_atomic, you'd have to disable irq! */
801
p_addr = kmap(b->bm_pages[i]);
802
for (bm = p_addr; bm < p_addr + PAGE_SIZE/sizeof(long); bm++)
803
*bm = cpu_to_lel(*bm);
808
/* lel_to_cpu == cpu_to_lel */
809
# define bm_lel_to_cpu(x) bm_cpu_to_lel(x)
812
* bm_rw: read/write the whole bitmap from/to its on disk location.
814
STATIC int bm_rw(struct drbd_conf *mdev, int rw) __must_hold(local)
816
struct drbd_bitmap *b = mdev->bitmap;
817
/* sector_t sector; */
818
int bm_words, num_pages, i;
823
WARN_ON(!bm_is_locked(b));
825
/* no spinlock here, the drbd_bm_lock should be enough! */
827
bm_words = drbd_bm_words(mdev);
828
num_pages = (bm_words*sizeof(long) + PAGE_SIZE-1) >> PAGE_SHIFT;
830
/* on disk bitmap is little endian */
835
atomic_set(&b->bm_async_io, num_pages);
836
__clear_bit(BM_MD_IO_ERROR, &b->bm_flags);
838
/* let the layers below us try to merge these bios... */
839
for (i = 0; i < num_pages; i++)
840
bm_page_io_async(mdev, b, i, rw);
842
drbd_blk_run_queue(bdev_get_queue(mdev->bc->md_bdev));
843
wait_event(b->bm_io_wait, atomic_read(&b->bm_async_io) == 0);
845
MTRACE(TraceTypeMDIO, TraceLvlSummary,
846
INFO("%s of bitmap took %lu jiffies\n",
847
rw == READ ? "reading" : "writing", jiffies - now);
850
if (test_bit(BM_MD_IO_ERROR, &b->bm_flags)) {
851
ALERT("we had at least one MD IO ERROR during bitmap IO\n");
852
drbd_chk_io_error(mdev, 1, TRUE);
853
drbd_io_error(mdev, TRUE);
859
/* swap back endianness */
861
/* flush bitmap to stable storage */
863
} else /* rw == READ */ {
864
/* just read, if neccessary adjust endianness */
865
b->bm_set = bm_count_bits_swap_endian(b);
866
INFO("recounting of set bits took additional %lu jiffies\n",
871
INFO("%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
872
ppsize(ppb, now << (BM_BLOCK_SIZE_B-10)), now);
878
* drbd_bm_read: Read the whole bitmap from its on disk location.
880
* currently only called from "drbd_nl_disk_conf"
882
int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
884
return bm_rw(mdev, READ);
888
* drbd_bm_write: Write the whole bitmap to its on disk location.
890
* called at various occasions.
892
int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
894
return bm_rw(mdev, WRITE);
898
* drbd_bm_write_sect: Writes a 512 byte piece of the bitmap to its
899
* on disk location. On disk bitmap is little endian.
901
* @enr: The _sector_ offset from the start of the bitmap.
904
int drbd_bm_write_sect(struct drbd_conf *mdev, unsigned long enr) __must_hold(local)
906
sector_t on_disk_sector = enr + mdev->bc->md.md_offset
907
+ mdev->bc->md.bm_offset;
908
int bm_words, num_words, offset;
911
mutex_lock(&mdev->md_io_mutex);
912
bm_words = drbd_bm_words(mdev);
913
offset = S2W(enr); /* word offset into bitmap */
914
num_words = min(S2W(1), bm_words - offset);
915
if (num_words < S2W(1))
916
memset(page_address(mdev->md_io_page), 0, MD_HARDSECT);
917
drbd_bm_get_lel(mdev, offset, num_words,
918
page_address(mdev->md_io_page));
919
if (!drbd_md_sync_page_io(mdev, mdev->bc, on_disk_sector, WRITE)) {
922
ERR("IO ERROR writing bitmap sector %lu "
923
"(meta-disk sector %llus)\n",
924
enr, (unsigned long long)on_disk_sector);
925
drbd_chk_io_error(mdev, 1, TRUE);
926
drbd_io_error(mdev, TRUE);
927
for (i = 0; i < AL_EXT_PER_BM_SECT; i++)
928
drbd_bm_ALe_set_all(mdev, enr*AL_EXT_PER_BM_SECT+i);
931
mutex_unlock(&mdev->md_io_mutex);
936
* find_first_bit returns int, we return unsigned long.
937
* should not make much difference anyways, but ...
939
* this returns a bit number, NOT a sector!
941
#define BPP_MASK ((1UL << (PAGE_SHIFT+3)) - 1)
942
static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
943
const int find_zero_bit, const enum km_type km)
945
struct drbd_bitmap *b = mdev->bitmap;
946
unsigned long i = -1UL;
947
unsigned long *p_addr;
948
unsigned long bit_offset; /* bit offset of the mapped page. */
950
if (bm_fo > b->bm_bits) {
951
ERR("bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
953
while (bm_fo < b->bm_bits) {
954
unsigned long offset;
955
bit_offset = bm_fo & ~BPP_MASK; /* bit offset of the page */
956
offset = bit_offset >> LN2_BPL; /* word offset of the page */
957
p_addr = __bm_map_paddr(b, offset, km);
960
i = find_next_zero_bit(p_addr, PAGE_SIZE*8, bm_fo & BPP_MASK);
962
i = find_next_bit(p_addr, PAGE_SIZE*8, bm_fo & BPP_MASK);
964
__bm_unmap(p_addr, km);
965
if (i < PAGE_SIZE*8) {
971
bm_fo = bit_offset + PAGE_SIZE*8;
979
static unsigned long bm_find_next(struct drbd_conf *mdev,
980
unsigned long bm_fo, const int find_zero_bit)
982
struct drbd_bitmap *b = mdev->bitmap;
983
unsigned long i = -1UL;
986
ERR_IF(!b->bm_pages) return i;
988
spin_lock_irq(&b->bm_lock);
990
bm_print_lock_info(mdev);
992
i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);
994
spin_unlock_irq(&b->bm_lock);
998
unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1000
return bm_find_next(mdev, bm_fo, 0);
1004
/* not yet needed for anything. */
1005
unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1007
return bm_find_next(mdev, bm_fo, 1);
1011
/* does not spin_lock_irqsave.
1012
* you must take drbd_bm_lock() first */
1013
unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1015
/* WARN_ON(!bm_is_locked(mdev)); */
1016
return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
1019
unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1021
/* WARN_ON(!bm_is_locked(mdev)); */
1022
return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
1025
/* returns number of bits actually changed.
1026
* for val != 0, we change 0 -> 1, return code positiv
1027
* for val == 0, we change 1 -> 0, return code negative
1028
* wants bitnr, not sector.
1029
* Must hold bitmap lock already. */
1031
int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
1032
const unsigned long e, int val, const enum km_type km)
1034
struct drbd_bitmap *b = mdev->bitmap;
1035
unsigned long *p_addr = NULL;
1036
unsigned long bitnr;
1037
unsigned long last_page_nr = -1UL;
1040
for (bitnr = s; bitnr <= e; bitnr++) {
1041
ERR_IF (bitnr >= b->bm_bits) {
1042
ERR("bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
1044
unsigned long offset = bitnr>>LN2_BPL;
1045
unsigned long page_nr = offset >> (PAGE_SHIFT - LN2_BPL + 3);
1046
if (page_nr != last_page_nr) {
1048
__bm_unmap(p_addr, km);
1049
p_addr = __bm_map_paddr(b, offset, km);
1050
last_page_nr = page_nr;
1053
c += (0 == __test_and_set_bit(bitnr & BPP_MASK, p_addr));
1055
c -= (0 != __test_and_clear_bit(bitnr & BPP_MASK, p_addr));
1059
__bm_unmap(p_addr, km);
1064
/* returns number of bits actually changed.
1065
* for val != 0, we change 0 -> 1, return code positiv
1066
* for val == 0, we change 1 -> 0, return code negative
1067
* wants bitnr, not sector */
1068
int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
1069
const unsigned long e, int val)
1071
unsigned long flags;
1072
struct drbd_bitmap *b = mdev->bitmap;
1075
ERR_IF(!b) return 1;
1076
ERR_IF(!b->bm_pages) return 0;
1078
spin_lock_irqsave(&b->bm_lock, flags);
1079
if (bm_is_locked(b))
1080
bm_print_lock_info(mdev);
1082
c = __bm_change_bits_to(mdev, s, e, val, KM_IRQ1);
1084
spin_unlock_irqrestore(&b->bm_lock, flags);
1088
/* returns number of bits changed 0 -> 1 */
1089
int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1091
return bm_change_bits_to(mdev, s, e, 1);
1094
/* returns number of bits changed 1 -> 0 */
1095
int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1097
return -bm_change_bits_to(mdev, s, e, 0);
1100
/* the same thing, but without taking the spin_lock_irqsave.
1101
* you must first drbd_bm_lock(). */
1102
int _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1104
/* WARN_ON(!bm_is_locked(b)); */
1105
return __bm_change_bits_to(mdev, s, e, 1, KM_USER0);
1108
/* returns bit state
1109
* wants bitnr, NOT sector.
1110
* inherently racy... area needs to be locked by means of {al,rs}_lru
1113
* -1 ... first out of bounds access, stop testing for bits!
1115
int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
1117
unsigned long flags;
1118
struct drbd_bitmap *b = mdev->bitmap;
1119
unsigned long *p_addr;
1122
ERR_IF(!b) return 0;
1123
ERR_IF(!b->bm_pages) return 0;
1125
spin_lock_irqsave(&b->bm_lock, flags);
1126
if (bm_is_locked(b))
1127
bm_print_lock_info(mdev);
1128
if (bitnr < b->bm_bits) {
1129
unsigned long offset = bitnr>>LN2_BPL;
1130
p_addr = bm_map_paddr(b, offset);
1131
i = test_bit(bitnr & BPP_MASK, p_addr) ? 1 : 0;
1133
} else if (bitnr == b->bm_bits) {
1135
} else { /* (bitnr > b->bm_bits) */
1136
ERR("bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
1140
spin_unlock_irqrestore(&b->bm_lock, flags);
1144
/* returns number of bits set */
1145
int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1147
unsigned long flags;
1148
struct drbd_bitmap *b = mdev->bitmap;
1149
unsigned long *p_addr = NULL, page_nr = -1;
1150
unsigned long bitnr;
1154
/* If this is called without a bitmap, that is a bug. But just to be
1155
* robust in case we screwed up elsewhere, in that case pretend there
1156
* was one dirty bit in the requested area, so we won't try to do a
1157
* local read there (no bitmap probably implies no disk) */
1158
ERR_IF(!b) return 1;
1159
ERR_IF(!b->bm_pages) return 1;
1161
spin_lock_irqsave(&b->bm_lock, flags);
1162
for (bitnr = s; bitnr <= e; bitnr++) {
1163
w = bitnr >> LN2_BPL;
1164
if (page_nr != w >> (PAGE_SHIFT - LN2_BPL + 3)) {
1165
page_nr = w >> (PAGE_SHIFT - LN2_BPL + 3);
1168
p_addr = bm_map_paddr(b, w);
1170
ERR_IF (bitnr >= b->bm_bits) {
1171
ERR("bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
1173
c += (0 != test_bit(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
1178
spin_unlock_irqrestore(&b->bm_lock, flags);
1183
/* inherently racy...
1184
* return value may be already out-of-date when this function returns.
1185
* but the general usage is that this is only use during a cstate when bits are
1186
* only cleared, not set, and typically only care for the case when the return
1187
* value is zero, or we already "locked" this "bitmap extent" by other means.
1189
* enr is bm-extent number, since we chose to name one sector (512 bytes)
1190
* worth of the bitmap a "bitmap extent".
1193
* I think since we use it like a reference count, we should use the real
1194
* reference count of some bitmap extent element from some lru instead...
1197
int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
1199
struct drbd_bitmap *b = mdev->bitmap;
1201
unsigned long flags;
1202
unsigned long *p_addr, *bm;
1204
ERR_IF(!b) return 0;
1205
ERR_IF(!b->bm_pages) return 0;
1207
spin_lock_irqsave(&b->bm_lock, flags);
1208
if (bm_is_locked(b))
1209
bm_print_lock_info(mdev);
1212
e = min((size_t)S2W(enr+1), b->bm_words);
1214
if (s < b->bm_words) {
1216
p_addr = bm_map_paddr(b, s);
1217
bm = p_addr + MLPP(s);
1219
count += hweight_long(*bm++);
1222
ERR("start offset (%d) too large in drbd_bm_e_weight\n", s);
1224
spin_unlock_irqrestore(&b->bm_lock, flags);
1228
/* set all bits covered by the AL-extent al_enr */
1229
unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
1231
struct drbd_bitmap *b = mdev->bitmap;
1232
unsigned long *p_addr, *bm;
1233
unsigned long weight;
1234
int count, s, e, i, do_now;
1235
ERR_IF(!b) return 0;
1236
ERR_IF(!b->bm_pages) return 0;
1238
spin_lock_irq(&b->bm_lock);
1239
if (bm_is_locked(b))
1240
bm_print_lock_info(mdev);
1243
s = al_enr * BM_WORDS_PER_AL_EXT;
1244
e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
1245
/* assert that s and e are on the same page */
1246
D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
1247
== s >> (PAGE_SHIFT - LN2_BPL + 3));
1249
if (s < b->bm_words) {
1251
p_addr = bm_map_paddr(b, s);
1252
bm = p_addr + MLPP(s);
1254
count += hweight_long(*bm);
1259
b->bm_set += do_now*BITS_PER_LONG - count;
1260
if (e == b->bm_words)
1261
b->bm_set -= bm_clear_surplus(b);
1263
ERR("start offset (%d) too large in drbd_bm_ALe_set_all\n", s);
1265
weight = b->bm_set - weight;
1266
spin_unlock_irq(&b->bm_lock);