blob: 72cd41a96ef971b85126132312f6e33e8b8b45f9 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_bitmap.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
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>.
9
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.
14
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.
19
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.
23 */
24
25#include <linux/bitops.h>
26#include <linux/vmalloc.h>
27#include <linux/string.h>
28#include <linux/drbd.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Philipp Reisnerb411b362009-09-25 16:07:19 -070030#include <asm/kmap_types.h>
31#include "drbd_int.h"
32
Lars Ellenberg95a0f102010-12-15 08:59:09 +010033
Philipp Reisnerb411b362009-09-25 16:07:19 -070034/* OPAQUE outside this file!
35 * interface defined in drbd_int.h
36
37 * convention:
38 * function name drbd_bm_... => used elsewhere, "public".
39 * function name bm_... => internal to implementation, "private".
40
41 * Note that since find_first_bit returns int, at the current granularity of
42 * the bitmap (4KB per byte), this implementation "only" supports up to
43 * 1<<(32+12) == 16 TB...
44 */
45
46/*
47 * NOTE
48 * Access to the *bm_pages is protected by bm_lock.
49 * It is safe to read the other members within the lock.
50 *
51 * drbd_bm_set_bits is called from bio_endio callbacks,
52 * We may be called with irq already disabled,
53 * so we need spin_lock_irqsave().
54 * And we need the kmap_atomic.
55 */
56struct drbd_bitmap {
57 struct page **bm_pages;
58 spinlock_t bm_lock;
59 /* WARNING unsigned long bm_*:
60 * 32bit number of bit offset is just enough for 512 MB bitmap.
61 * it will blow up if we make the bitmap bigger...
62 * not that it makes much sense to have a bitmap that large,
63 * rather change the granularity to 16k or 64k or something.
64 * (that implies other problems, however...)
65 */
66 unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */
67 unsigned long bm_bits;
68 size_t bm_words;
69 size_t bm_number_of_pages;
70 sector_t bm_dev_capacity;
Thomas Gleixner8a03ae22010-01-29 20:39:07 +000071 struct mutex bm_change; /* serializes resize operations */
Philipp Reisnerb411b362009-09-25 16:07:19 -070072
Lars Ellenberg19f843a2010-12-15 08:59:11 +010073 wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */
Philipp Reisnerb411b362009-09-25 16:07:19 -070074
75 unsigned long bm_flags;
76
77 /* debugging aid, in case we are still racy somewhere */
78 char *bm_why;
79 struct task_struct *bm_task;
80};
81
82/* definition of bits in bm_flags */
83#define BM_LOCKED 0
Lars Ellenberg19f843a2010-12-15 08:59:11 +010084// #define BM_MD_IO_ERROR 1 unused now.
Philipp Reisnerb411b362009-09-25 16:07:19 -070085#define BM_P_VMALLOCED 2
86
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +020087static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Philipp Reisnerfd764382010-04-01 09:57:40 +020088 unsigned long e, int val, const enum km_type km);
89
Philipp Reisnerb411b362009-09-25 16:07:19 -070090static int bm_is_locked(struct drbd_bitmap *b)
91{
92 return test_bit(BM_LOCKED, &b->bm_flags);
93}
94
95#define bm_print_lock_info(m) __bm_print_lock_info(m, __func__)
96static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func)
97{
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" : "?");
109}
110
111void drbd_bm_lock(struct drbd_conf *mdev, char *why)
112{
113 struct drbd_bitmap *b = mdev->bitmap;
114 int trylock_failed;
115
116 if (!b) {
117 dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n");
118 return;
119 }
120
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000121 trylock_failed = !mutex_trylock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700122
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" : "?");
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000132 mutex_lock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700133 }
134 if (__test_and_set_bit(BM_LOCKED, &b->bm_flags))
135 dev_err(DEV, "FIXME bitmap already locked in bm_lock\n");
136
137 b->bm_why = why;
138 b->bm_task = current;
139}
140
141void drbd_bm_unlock(struct drbd_conf *mdev)
142{
143 struct drbd_bitmap *b = mdev->bitmap;
144 if (!b) {
145 dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n");
146 return;
147 }
148
149 if (!__test_and_clear_bit(BM_LOCKED, &mdev->bitmap->bm_flags))
150 dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");
151
152 b->bm_why = NULL;
153 b->bm_task = NULL;
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000154 mutex_unlock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700155}
156
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100157/* we store some "meta" info about our pages in page->private */
158/* at a granularity of 4k storage per bitmap bit:
159 * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks
160 * 1<<38 bits,
161 * 1<<23 4k bitmap pages.
162 * Use 24 bits as page index, covers 2 peta byte storage
163 * at a granularity of 4k per bit.
164 * Used to report the failed page idx on io error from the endio handlers.
165 */
166#define BM_PAGE_IDX_MASK ((1UL<<24)-1)
167/* this page is currently read in, or written back */
168#define BM_PAGE_IO_LOCK 31
169/* if there has been an IO error for this page */
170#define BM_PAGE_IO_ERROR 30
171/* this is to be able to intelligently skip disk IO,
172 * set if bits have been set since last IO. */
173#define BM_PAGE_NEED_WRITEOUT 29
174/* to mark for lazy writeout once syncer cleared all clearable bits,
175 * we if bits have been cleared since last IO. */
176#define BM_PAGE_LAZY_WRITEOUT 28
177
178/* store_page_idx uses non-atomic assingment. It is only used directly after
179 * allocating the page. All other bm_set_page_* and bm_clear_page_* need to
180 * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap
181 * changes) may happen from various contexts, and wait_on_bit/wake_up_bit
182 * requires it all to be atomic as well. */
183static void bm_store_page_idx(struct page *page, unsigned long idx)
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100184{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100185 BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
186 page_private(page) |= idx;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100187}
188
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100189static unsigned long bm_page_to_idx(struct page *page)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700190{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100191 return page_private(page) & BM_PAGE_IDX_MASK;
192}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700193
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100194/* As is very unlikely that the same page is under IO from more than one
195 * context, we can get away with a bit per page and one wait queue per bitmap.
196 */
197static void bm_page_lock_io(struct drbd_conf *mdev, int page_nr)
198{
199 struct drbd_bitmap *b = mdev->bitmap;
200 void *addr = &page_private(b->bm_pages[page_nr]);
201 wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
202}
203
204static void bm_page_unlock_io(struct drbd_conf *mdev, int page_nr)
205{
206 struct drbd_bitmap *b = mdev->bitmap;
207 void *addr = &page_private(b->bm_pages[page_nr]);
208 clear_bit(BM_PAGE_IO_LOCK, addr);
209 smp_mb__after_clear_bit();
210 wake_up(&mdev->bitmap->bm_io_wait);
211}
212
213/* set _before_ submit_io, so it may be reset due to being changed
214 * while this page is in flight... will get submitted later again */
215static void bm_set_page_unchanged(struct page *page)
216{
217 /* use cmpxchg? */
218 clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
219 clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
220}
221
222static void bm_set_page_need_writeout(struct page *page)
223{
224 set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
225}
226
227static int bm_test_page_unchanged(struct page *page)
228{
229 volatile const unsigned long *addr = &page_private(page);
230 return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0;
231}
232
233static void bm_set_page_io_err(struct page *page)
234{
235 set_bit(BM_PAGE_IO_ERROR, &page_private(page));
236}
237
238static void bm_clear_page_io_err(struct page *page)
239{
240 clear_bit(BM_PAGE_IO_ERROR, &page_private(page));
241}
242
243static void bm_set_page_lazy_writeout(struct page *page)
244{
245 set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
246}
247
248static int bm_test_page_lazy_writeout(struct page *page)
249{
250 return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
251}
252
253/* on a 32bit box, this would allow for exactly (2<<38) bits. */
254static unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr)
255{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700256 /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100257 unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700258 BUG_ON(page_nr >= b->bm_number_of_pages);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100259 return page_nr;
260}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700261
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100262static unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr)
263{
264 /* page_nr = (bitnr/8) >> PAGE_SHIFT; */
265 unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3);
266 BUG_ON(page_nr >= b->bm_number_of_pages);
267 return page_nr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700268}
269
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100270static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx, const enum km_type km)
271{
272 struct page *page = b->bm_pages[idx];
273 return (unsigned long *) kmap_atomic(page, km);
274}
275
276static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
277{
278 return __bm_map_pidx(b, idx, KM_IRQ1);
279}
280
Philipp Reisnerb411b362009-09-25 16:07:19 -0700281static void __bm_unmap(unsigned long *p_addr, const enum km_type km)
282{
283 kunmap_atomic(p_addr, km);
284};
285
286static void bm_unmap(unsigned long *p_addr)
287{
288 return __bm_unmap(p_addr, KM_IRQ1);
289}
290
291/* long word offset of _bitmap_ sector */
292#define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
293/* word offset from start of bitmap to word number _in_page_
294 * modulo longs per page
295#define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
296 hm, well, Philipp thinks gcc might not optimze the % into & (... - 1)
297 so do it explicitly:
298 */
299#define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
300
301/* Long words per page */
302#define LWPP (PAGE_SIZE/sizeof(long))
303
304/*
305 * actually most functions herein should take a struct drbd_bitmap*, not a
306 * struct drbd_conf*, but for the debug macros I like to have the mdev around
307 * to be able to report device specific.
308 */
309
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100310
Philipp Reisnerb411b362009-09-25 16:07:19 -0700311static void bm_free_pages(struct page **pages, unsigned long number)
312{
313 unsigned long i;
314 if (!pages)
315 return;
316
317 for (i = 0; i < number; i++) {
318 if (!pages[i]) {
319 printk(KERN_ALERT "drbd: bm_free_pages tried to free "
320 "a NULL pointer; i=%lu n=%lu\n",
321 i, number);
322 continue;
323 }
324 __free_page(pages[i]);
325 pages[i] = NULL;
326 }
327}
328
329static void bm_vk_free(void *ptr, int v)
330{
331 if (v)
332 vfree(ptr);
333 else
334 kfree(ptr);
335}
336
337/*
338 * "have" and "want" are NUMBER OF PAGES.
339 */
340static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
341{
342 struct page **old_pages = b->bm_pages;
343 struct page **new_pages, *page;
344 unsigned int i, bytes, vmalloced = 0;
345 unsigned long have = b->bm_number_of_pages;
346
347 BUG_ON(have == 0 && old_pages != NULL);
348 BUG_ON(have != 0 && old_pages == NULL);
349
350 if (have == want)
351 return old_pages;
352
353 /* Trying kmalloc first, falling back to vmalloc.
354 * GFP_KERNEL is ok, as this is done when a lower level disk is
355 * "attached" to the drbd. Context is receiver thread or cqueue
356 * thread. As we have no disk yet, we are not in the IO path,
357 * not even the IO path of the peer. */
358 bytes = sizeof(struct page *)*want;
359 new_pages = kmalloc(bytes, GFP_KERNEL);
360 if (!new_pages) {
361 new_pages = vmalloc(bytes);
362 if (!new_pages)
363 return NULL;
364 vmalloced = 1;
365 }
366
367 memset(new_pages, 0, bytes);
368 if (want >= have) {
369 for (i = 0; i < have; i++)
370 new_pages[i] = old_pages[i];
371 for (; i < want; i++) {
372 page = alloc_page(GFP_HIGHUSER);
373 if (!page) {
374 bm_free_pages(new_pages + have, i - have);
375 bm_vk_free(new_pages, vmalloced);
376 return NULL;
377 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100378 /* we want to know which page it is
379 * from the endio handlers */
380 bm_store_page_idx(page, i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700381 new_pages[i] = page;
382 }
383 } else {
384 for (i = 0; i < want; i++)
385 new_pages[i] = old_pages[i];
386 /* NOT HERE, we are outside the spinlock!
387 bm_free_pages(old_pages + want, have - want);
388 */
389 }
390
391 if (vmalloced)
392 set_bit(BM_P_VMALLOCED, &b->bm_flags);
393 else
394 clear_bit(BM_P_VMALLOCED, &b->bm_flags);
395
396 return new_pages;
397}
398
399/*
400 * called on driver init only. TODO call when a device is created.
401 * allocates the drbd_bitmap, and stores it in mdev->bitmap.
402 */
403int drbd_bm_init(struct drbd_conf *mdev)
404{
405 struct drbd_bitmap *b = mdev->bitmap;
406 WARN_ON(b != NULL);
407 b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
408 if (!b)
409 return -ENOMEM;
410 spin_lock_init(&b->bm_lock);
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000411 mutex_init(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700412 init_waitqueue_head(&b->bm_io_wait);
413
414 mdev->bitmap = b;
415
416 return 0;
417}
418
419sector_t drbd_bm_capacity(struct drbd_conf *mdev)
420{
421 ERR_IF(!mdev->bitmap) return 0;
422 return mdev->bitmap->bm_dev_capacity;
423}
424
425/* called on driver unload. TODO: call when a device is destroyed.
426 */
427void drbd_bm_cleanup(struct drbd_conf *mdev)
428{
429 ERR_IF (!mdev->bitmap) return;
430 bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
431 bm_vk_free(mdev->bitmap->bm_pages, test_bit(BM_P_VMALLOCED, &mdev->bitmap->bm_flags));
432 kfree(mdev->bitmap);
433 mdev->bitmap = NULL;
434}
435
436/*
437 * since (b->bm_bits % BITS_PER_LONG) != 0,
438 * this masks out the remaining bits.
439 * Returns the number of bits cleared.
440 */
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100441#define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3))
442#define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1)
443#define BITS_PER_LONG_MASK (BITS_PER_LONG - 1)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444static int bm_clear_surplus(struct drbd_bitmap *b)
445{
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100446 unsigned long mask;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700447 unsigned long *p_addr, *bm;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100448 int tmp;
449 int cleared = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700450
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100451 /* number of bits modulo bits per page */
452 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
453 /* mask the used bits of the word containing the last bit */
454 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
455 /* bitmap is always stored little endian,
456 * on disk and in core memory alike */
457 mask = cpu_to_lel(mask);
458
459 /* because of the "extra long to catch oob access" we allocate in
460 * drbd_bm_resize, bm_number_of_pages -1 is not necessarily the page
461 * containing the last _relevant_ bitmap word */
462 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, b->bm_bits - 1));
463 bm = p_addr + (tmp/BITS_PER_LONG);
464 if (mask) {
465 /* If mask != 0, we are not exactly aligned, so bm now points
466 * to the long containing the last bit.
467 * If mask == 0, bm already points to the word immediately
468 * after the last (long word aligned) bit. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700469 cleared = hweight_long(*bm & ~mask);
470 *bm &= mask;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100471 bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700472 }
473
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100474 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
475 /* on a 32bit arch, we may need to zero out
476 * a padding long to align with a 64bit remote */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700477 cleared += hweight_long(*bm);
478 *bm = 0;
479 }
480 bm_unmap(p_addr);
481 return cleared;
482}
483
484static void bm_set_surplus(struct drbd_bitmap *b)
485{
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100486 unsigned long mask;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700487 unsigned long *p_addr, *bm;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100488 int tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700489
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100490 /* number of bits modulo bits per page */
491 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
492 /* mask the used bits of the word containing the last bit */
493 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
494 /* bitmap is always stored little endian,
495 * on disk and in core memory alike */
496 mask = cpu_to_lel(mask);
497
498 /* because of the "extra long to catch oob access" we allocate in
499 * drbd_bm_resize, bm_number_of_pages -1 is not necessarily the page
500 * containing the last _relevant_ bitmap word */
501 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, b->bm_bits - 1));
502 bm = p_addr + (tmp/BITS_PER_LONG);
503 if (mask) {
504 /* If mask != 0, we are not exactly aligned, so bm now points
505 * to the long containing the last bit.
506 * If mask == 0, bm already points to the word immediately
507 * after the last (long word aligned) bit. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700508 *bm |= ~mask;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100509 bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700510 }
511
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100512 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
513 /* on a 32bit arch, we may need to zero out
514 * a padding long to align with a 64bit remote */
515 *bm = ~0UL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700516 }
517 bm_unmap(p_addr);
518}
519
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100520static unsigned long bm_count_bits(struct drbd_bitmap *b)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700521{
522 unsigned long *p_addr, *bm, offset = 0;
523 unsigned long bits = 0;
524 unsigned long i, do_now;
Lars Ellenberg7777a8b2010-12-15 23:21:39 +0100525 unsigned long words;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526
Lars Ellenberg7777a8b2010-12-15 23:21:39 +0100527 /* due to 64bit alignment, the last long on a 32bit arch
528 * may be not used at all. The last used long will likely
529 * be only partially used, always. Don't count those bits,
530 * but mask them out. */
531 words = (b->bm_bits + BITS_PER_LONG - 1) >> LN2_BPL;
532
533 while (offset < words) {
534 i = do_now = min_t(size_t, words-offset, LWPP);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100535 p_addr = __bm_map_pidx(b, bm_word_to_page_idx(b, offset), KM_USER0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536 bm = p_addr + MLPP(offset);
537 while (i--) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700538 bits += hweight_long(*bm++);
539 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700540 offset += do_now;
Lars Ellenberg7777a8b2010-12-15 23:21:39 +0100541 if (offset == words) {
542 /* last word may only be partially used,
543 * see also bm_clear_surplus. */
544 i = (1UL << (b->bm_bits & (BITS_PER_LONG-1))) -1;
545 if (i) {
546 bits -= hweight_long(p_addr[do_now-1] & ~i);
547 p_addr[do_now-1] &= i;
548 }
549 /* 32bit arch, may have an unused padding long */
550 if (words != b->bm_words)
551 p_addr[do_now] = 0;
552 }
553 __bm_unmap(p_addr, KM_USER0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700554 cond_resched();
555 }
556
557 return bits;
558}
559
Philipp Reisnerb411b362009-09-25 16:07:19 -0700560/* offset and len in long words.*/
561static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
562{
563 unsigned long *p_addr, *bm;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100564 unsigned int idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565 size_t do_now, end;
566
567#define BM_SECTORS_PER_BIT (BM_BLOCK_SIZE/512)
568
569 end = offset + len;
570
571 if (end > b->bm_words) {
572 printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
573 return;
574 }
575
576 while (offset < end) {
577 do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100578 idx = bm_word_to_page_idx(b, offset);
579 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700580 bm = p_addr + MLPP(offset);
581 if (bm+do_now > p_addr + LWPP) {
582 printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
583 p_addr, bm, (int)do_now);
584 break; /* breaks to after catch_oob_access_end() only! */
585 }
586 memset(bm, c, do_now * sizeof(long));
587 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100588 bm_set_page_need_writeout(b->bm_pages[idx]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700589 offset += do_now;
590 }
591}
592
593/*
594 * make sure the bitmap has enough room for the attached storage,
595 * if necessary, resize.
596 * called whenever we may have changed the device size.
597 * returns -ENOMEM if we could not allocate enough memory, 0 on success.
598 * In case this is actually a resize, we copy the old bitmap into the new one.
599 * Otherwise, the bitmap is initialized to all bits set.
600 */
Philipp Reisner02d9a942010-03-24 16:23:03 +0100601int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700602{
603 struct drbd_bitmap *b = mdev->bitmap;
604 unsigned long bits, words, owords, obits, *p_addr, *bm;
605 unsigned long want, have, onpages; /* number of pages */
606 struct page **npages, **opages = NULL;
607 int err = 0, growing;
608 int opages_vmalloced;
609
610 ERR_IF(!b) return -ENOMEM;
611
612 drbd_bm_lock(mdev, "resize");
613
614 dev_info(DEV, "drbd_bm_resize called with capacity == %llu\n",
615 (unsigned long long)capacity);
616
617 if (capacity == b->bm_dev_capacity)
618 goto out;
619
620 opages_vmalloced = test_bit(BM_P_VMALLOCED, &b->bm_flags);
621
622 if (capacity == 0) {
623 spin_lock_irq(&b->bm_lock);
624 opages = b->bm_pages;
625 onpages = b->bm_number_of_pages;
626 owords = b->bm_words;
627 b->bm_pages = NULL;
628 b->bm_number_of_pages =
629 b->bm_set =
630 b->bm_bits =
631 b->bm_words =
632 b->bm_dev_capacity = 0;
633 spin_unlock_irq(&b->bm_lock);
634 bm_free_pages(opages, onpages);
635 bm_vk_free(opages, opages_vmalloced);
636 goto out;
637 }
638 bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));
639
640 /* if we would use
641 words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
642 a 32bit host could present the wrong number of words
643 to a 64bit host.
644 */
645 words = ALIGN(bits, 64) >> LN2_BPL;
646
647 if (get_ldev(mdev)) {
648 D_ASSERT((u64)bits <= (((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12));
649 put_ldev(mdev);
650 }
651
652 /* one extra long to catch off by one errors */
653 want = ALIGN((words+1)*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT;
654 have = b->bm_number_of_pages;
655 if (want == have) {
656 D_ASSERT(b->bm_pages != NULL);
657 npages = b->bm_pages;
658 } else {
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100659 if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700660 npages = NULL;
661 else
662 npages = bm_realloc_pages(b, want);
663 }
664
665 if (!npages) {
666 err = -ENOMEM;
667 goto out;
668 }
669
670 spin_lock_irq(&b->bm_lock);
671 opages = b->bm_pages;
672 owords = b->bm_words;
673 obits = b->bm_bits;
674
675 growing = bits > obits;
Philipp Reisner52236712010-04-28 14:46:57 +0200676 if (opages && growing && set_new_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700677 bm_set_surplus(b);
678
679 b->bm_pages = npages;
680 b->bm_number_of_pages = want;
681 b->bm_bits = bits;
682 b->bm_words = words;
683 b->bm_dev_capacity = capacity;
684
685 if (growing) {
Philipp Reisner02d9a942010-03-24 16:23:03 +0100686 if (set_new_bits) {
687 bm_memset(b, owords, 0xff, words-owords);
688 b->bm_set += bits - obits;
689 } else
690 bm_memset(b, owords, 0x00, words-owords);
691
Philipp Reisnerb411b362009-09-25 16:07:19 -0700692 }
693
694 if (want < have) {
695 /* implicit: (opages != NULL) && (opages != npages) */
696 bm_free_pages(opages + want, have - want);
697 }
698
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100699 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, words));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700700 bm = p_addr + MLPP(words);
701 *bm = DRBD_MAGIC;
702 bm_unmap(p_addr);
703
704 (void)bm_clear_surplus(b);
705
706 spin_unlock_irq(&b->bm_lock);
707 if (opages != npages)
708 bm_vk_free(opages, opages_vmalloced);
709 if (!growing)
710 b->bm_set = bm_count_bits(b);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100711 dev_info(DEV, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700712
713 out:
714 drbd_bm_unlock(mdev);
715 return err;
716}
717
718/* inherently racy:
719 * if not protected by other means, return value may be out of date when
720 * leaving this function...
721 * we still need to lock it, since it is important that this returns
722 * bm_set == 0 precisely.
723 *
724 * maybe bm_set should be atomic_t ?
725 */
Philipp Reisner07782862010-08-31 12:00:50 +0200726unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700727{
728 struct drbd_bitmap *b = mdev->bitmap;
729 unsigned long s;
730 unsigned long flags;
731
732 ERR_IF(!b) return 0;
733 ERR_IF(!b->bm_pages) return 0;
734
735 spin_lock_irqsave(&b->bm_lock, flags);
736 s = b->bm_set;
737 spin_unlock_irqrestore(&b->bm_lock, flags);
738
739 return s;
740}
741
742unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
743{
744 unsigned long s;
745 /* if I don't have a disk, I don't know about out-of-sync status */
746 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
747 return 0;
748 s = _drbd_bm_total_weight(mdev);
749 put_ldev(mdev);
750 return s;
751}
752
753size_t drbd_bm_words(struct drbd_conf *mdev)
754{
755 struct drbd_bitmap *b = mdev->bitmap;
756 ERR_IF(!b) return 0;
757 ERR_IF(!b->bm_pages) return 0;
758
759 return b->bm_words;
760}
761
762unsigned long drbd_bm_bits(struct drbd_conf *mdev)
763{
764 struct drbd_bitmap *b = mdev->bitmap;
765 ERR_IF(!b) return 0;
766
767 return b->bm_bits;
768}
769
770/* merge number words from buffer into the bitmap starting at offset.
771 * buffer[i] is expected to be little endian unsigned long.
772 * bitmap must be locked by drbd_bm_lock.
773 * currently only used from receive_bitmap.
774 */
775void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
776 unsigned long *buffer)
777{
778 struct drbd_bitmap *b = mdev->bitmap;
779 unsigned long *p_addr, *bm;
780 unsigned long word, bits;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100781 unsigned int idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700782 size_t end, do_now;
783
784 end = offset + number;
785
786 ERR_IF(!b) return;
787 ERR_IF(!b->bm_pages) return;
788 if (number == 0)
789 return;
790 WARN_ON(offset >= b->bm_words);
791 WARN_ON(end > b->bm_words);
792
793 spin_lock_irq(&b->bm_lock);
794 while (offset < end) {
795 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100796 idx = bm_word_to_page_idx(b, offset);
797 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700798 bm = p_addr + MLPP(offset);
799 offset += do_now;
800 while (do_now--) {
801 bits = hweight_long(*bm);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100802 word = *bm | *buffer++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700803 *bm++ = word;
804 b->bm_set += hweight_long(word) - bits;
805 }
806 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100807 bm_set_page_need_writeout(b->bm_pages[idx]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700808 }
809 /* with 32bit <-> 64bit cross-platform connect
810 * this is only correct for current usage,
811 * where we _know_ that we are 64 bit aligned,
812 * and know that this function is used in this way, too...
813 */
814 if (end == b->bm_words)
815 b->bm_set -= bm_clear_surplus(b);
816
817 spin_unlock_irq(&b->bm_lock);
818}
819
820/* copy number words from the bitmap starting at offset into the buffer.
821 * buffer[i] will be little endian unsigned long.
822 */
823void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
824 unsigned long *buffer)
825{
826 struct drbd_bitmap *b = mdev->bitmap;
827 unsigned long *p_addr, *bm;
828 size_t end, do_now;
829
830 end = offset + number;
831
832 ERR_IF(!b) return;
833 ERR_IF(!b->bm_pages) return;
834
835 spin_lock_irq(&b->bm_lock);
836 if ((offset >= b->bm_words) ||
837 (end > b->bm_words) ||
838 (number <= 0))
839 dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n",
840 (unsigned long) offset,
841 (unsigned long) number,
842 (unsigned long) b->bm_words);
843 else {
844 while (offset < end) {
845 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100846 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700847 bm = p_addr + MLPP(offset);
848 offset += do_now;
849 while (do_now--)
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100850 *buffer++ = *bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700851 bm_unmap(p_addr);
852 }
853 }
854 spin_unlock_irq(&b->bm_lock);
855}
856
857/* set all bits in the bitmap */
858void drbd_bm_set_all(struct drbd_conf *mdev)
859{
860 struct drbd_bitmap *b = mdev->bitmap;
861 ERR_IF(!b) return;
862 ERR_IF(!b->bm_pages) return;
863
864 spin_lock_irq(&b->bm_lock);
865 bm_memset(b, 0, 0xff, b->bm_words);
866 (void)bm_clear_surplus(b);
867 b->bm_set = b->bm_bits;
868 spin_unlock_irq(&b->bm_lock);
869}
870
871/* clear all bits in the bitmap */
872void drbd_bm_clear_all(struct drbd_conf *mdev)
873{
874 struct drbd_bitmap *b = mdev->bitmap;
875 ERR_IF(!b) return;
876 ERR_IF(!b->bm_pages) return;
877
878 spin_lock_irq(&b->bm_lock);
879 bm_memset(b, 0, 0, b->bm_words);
880 b->bm_set = 0;
881 spin_unlock_irq(&b->bm_lock);
882}
883
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100884struct bm_aio_ctx {
885 struct drbd_conf *mdev;
886 atomic_t in_flight;
887 wait_queue_head_t io_wait;
888 unsigned flags;
889#define BM_AIO_COPY_PAGES 1
890 int error;
891};
892
893/* bv_page may be a copy, or may be the original */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700894static void bm_async_io_complete(struct bio *bio, int error)
895{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100896 struct bm_aio_ctx *ctx = bio->bi_private;
897 struct drbd_conf *mdev = ctx->mdev;
898 struct drbd_bitmap *b = mdev->bitmap;
899 unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700900 int uptodate = bio_flagged(bio, BIO_UPTODATE);
901
902
903 /* strange behavior of some lower level drivers...
904 * fail the request by clearing the uptodate flag,
905 * but do not return any error?!
906 * do we want to WARN() on this? */
907 if (!error && !uptodate)
908 error = -EIO;
909
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100910 if (!bm_test_page_unchanged(b->bm_pages[idx]))
911 dev_info(DEV, "bitmap page idx %u changed during IO!\n", idx);
912
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 if (error) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100914 /* ctx error will hold the completed-last non-zero error code,
915 * in case error codes differ. */
916 ctx->error = error;
917 bm_set_page_io_err(b->bm_pages[idx]);
918 /* Not identical to on disk version of it.
919 * Is BM_PAGE_IO_ERROR enough? */
920 if (__ratelimit(&drbd_ratelimit_state))
921 dev_err(DEV, "IO ERROR %d on bitmap page idx %u\n",
922 error, idx);
923 } else {
924 bm_clear_page_io_err(b->bm_pages[idx]);
925 dynamic_dev_dbg(DEV, "bitmap page idx %u completed\n", idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700926 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100927
928 bm_page_unlock_io(mdev, idx);
929
930 /* FIXME give back to page pool */
931 if (ctx->flags & BM_AIO_COPY_PAGES)
932 put_page(bio->bi_io_vec[0].bv_page);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700933
934 bio_put(bio);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100935
936 if (atomic_dec_and_test(&ctx->in_flight))
937 wake_up(&ctx->io_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700938}
939
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100940static void bm_page_io_async(struct bm_aio_ctx *ctx, int page_nr, int rw) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941{
942 /* we are process context. we always get a bio */
943 struct bio *bio = bio_alloc(GFP_KERNEL, 1);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100944 struct drbd_conf *mdev = ctx->mdev;
945 struct drbd_bitmap *b = mdev->bitmap;
946 struct page *page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947 unsigned int len;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100948
Philipp Reisnerb411b362009-09-25 16:07:19 -0700949 sector_t on_disk_sector =
950 mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset;
951 on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);
952
953 /* this might happen with very small
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100954 * flexible external meta data device,
955 * or with PAGE_SIZE > 4k */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700956 len = min_t(unsigned int, PAGE_SIZE,
957 (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);
958
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100959 /* serialize IO on this page */
960 bm_page_lock_io(mdev, page_nr);
961 /* before memcpy and submit,
962 * so it can be redirtied any time */
963 bm_set_page_unchanged(b->bm_pages[page_nr]);
964
965 if (ctx->flags & BM_AIO_COPY_PAGES) {
966 /* FIXME alloc_page is good enough for now, but actually needs
967 * to use pre-allocated page pool */
968 void *src, *dest;
969 page = alloc_page(__GFP_HIGHMEM|__GFP_WAIT);
970 dest = kmap_atomic(page, KM_USER0);
971 src = kmap_atomic(b->bm_pages[page_nr], KM_USER1);
972 memcpy(dest, src, PAGE_SIZE);
973 kunmap_atomic(src, KM_USER1);
974 kunmap_atomic(dest, KM_USER0);
975 bm_store_page_idx(page, page_nr);
976 } else
977 page = b->bm_pages[page_nr];
978
Philipp Reisnerb411b362009-09-25 16:07:19 -0700979 bio->bi_bdev = mdev->ldev->md_bdev;
980 bio->bi_sector = on_disk_sector;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100981 bio_add_page(bio, page, len, 0);
982 bio->bi_private = ctx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700983 bio->bi_end_io = bm_async_io_complete;
984
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100985 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700986 bio->bi_rw |= rw;
987 bio_endio(bio, -EIO);
988 } else {
989 submit_bio(rw, bio);
990 }
991}
992
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993/*
994 * bm_rw: read/write the whole bitmap from/to its on disk location.
995 */
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100996static int bm_rw(struct drbd_conf *mdev, int rw, unsigned lazy_writeout_upper_idx) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700997{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100998 struct bm_aio_ctx ctx =
999 { .flags = lazy_writeout_upper_idx ? BM_AIO_COPY_PAGES : 0 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001001 int last_page, i, count = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001002 unsigned long now;
1003 char ppb[10];
1004 int err = 0;
1005
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001006 /*
1007 * We are protected against bitmap disappearing/resizing by holding an
1008 * ldev reference (caller must have called get_ldev()).
1009 * For read/write, we are protected against changes to the bitmap by
1010 * the bitmap lock (see drbd_bitmap_io).
1011 * For lazy writeout, we don't care for ongoing changes to the bitmap,
1012 * as we submit copies of pages anyways.
1013 */
1014 if (!ctx.flags)
1015 WARN_ON(!bm_is_locked(b));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001016
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001017 /* because of the "extra long to catch oob access" we allocate in
1018 * drbd_bm_resize, bm_number_of_pages -1 is not necessarily the page
1019 * containing the last _relevant_ bitmap word */
1020 last_page = bm_word_to_page_idx(b, b->bm_words - 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001021
Philipp Reisnerb411b362009-09-25 16:07:19 -07001022 now = jiffies;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001023 ctx.mdev = mdev;
1024 atomic_set(&ctx.in_flight, 1); /* one extra ref */
1025 init_waitqueue_head(&ctx.io_wait);
1026 ctx.error = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001027
1028 /* let the layers below us try to merge these bios... */
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001029 for (i = 0; i <= last_page; i++) {
1030 /* ignore completely unchanged pages */
1031 if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
1032 break;
1033 if (rw & WRITE) {
1034 if (bm_test_page_unchanged(b->bm_pages[i])) {
1035 dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
1036 continue;
1037 }
1038 /* during lazy writeout,
1039 * ignore those pages not marked for lazy writeout. */
1040 if (lazy_writeout_upper_idx &&
1041 !bm_test_page_lazy_writeout(b->bm_pages[i])) {
1042 dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
1043 continue;
1044 }
1045 }
1046 atomic_inc(&ctx.in_flight);
1047 bm_page_io_async(&ctx, i, rw);
1048 ++count;
1049 cond_resched();
1050 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001051
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001052 atomic_dec(&ctx.in_flight); /* drop the extra ref */
1053 wait_event(ctx.io_wait, atomic_read(&ctx.in_flight) == 0);
1054 dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
1055 rw == WRITE ? "WRITE" : "READ",
1056 count, jiffies - now);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001057
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001058 if (ctx.error) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001059 dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001060 drbd_chk_io_error(mdev, 1, true);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001061 err = -EIO; /* ctx.error ? */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062 }
1063
1064 now = jiffies;
1065 if (rw == WRITE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001066 drbd_md_flush(mdev);
1067 } else /* rw == READ */ {
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001068 b->bm_set = bm_count_bits(b);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001069 dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
1070 jiffies - now);
1071 }
1072 now = b->bm_set;
1073
1074 dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
1075 ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
1076
1077 return err;
1078}
1079
1080/**
1081 * drbd_bm_read() - Read the whole bitmap from its on disk location.
1082 * @mdev: DRBD device.
1083 */
1084int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
1085{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001086 return bm_rw(mdev, READ, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001087}
1088
1089/**
1090 * drbd_bm_write() - Write the whole bitmap to its on disk location.
1091 * @mdev: DRBD device.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001092 *
1093 * Will only write pages that have changed since last IO.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001094 */
1095int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
1096{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001097 return bm_rw(mdev, WRITE, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001098}
1099
1100/**
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001101 * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001102 * @mdev: DRBD device.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001103 * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
Philipp Reisnerb411b362009-09-25 16:07:19 -07001104 */
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001105int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001106{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001107 return bm_rw(mdev, WRITE, upper_idx);
1108}
Philipp Reisnerb411b362009-09-25 16:07:19 -07001109
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001110
1111/**
1112 * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
1113 * @mdev: DRBD device.
1114 * @idx: bitmap page index
1115 *
1116 * We don't want to special case on logical_block_size of the underlaying
1117 * device, so we submit PAGE_SIZE aligned pieces containing the requested enr.
1118 * Note that on "most" systems, PAGE_SIZE is 4k.
1119 */
1120int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
1121{
1122 struct bm_aio_ctx ctx = { .flags = BM_AIO_COPY_PAGES, };
1123
1124 if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
1125 dev_info(DEV, "skipped bm page write for idx %u\n", idx);
1126 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001127 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001128
1129 ctx.mdev = mdev;
1130 atomic_set(&ctx.in_flight, 1);
1131 init_waitqueue_head(&ctx.io_wait);
1132
1133 bm_page_io_async(&ctx, idx, WRITE_SYNC);
1134 wait_event(ctx.io_wait, atomic_read(&ctx.in_flight) == 0);
1135
1136 if (ctx.error)
1137 drbd_chk_io_error(mdev, 1, true);
1138 /* that should force detach, so the in memory bitmap will be
1139 * gone in a moment as well. */
1140
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141 mdev->bm_writ_cnt++;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001142 return ctx.error;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001143}
1144
1145/* NOTE
1146 * find_first_bit returns int, we return unsigned long.
1147 * should not make much difference anyways, but ...
1148 *
1149 * this returns a bit number, NOT a sector!
1150 */
1151#define BPP_MASK ((1UL << (PAGE_SHIFT+3)) - 1)
1152static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
1153 const int find_zero_bit, const enum km_type km)
1154{
1155 struct drbd_bitmap *b = mdev->bitmap;
1156 unsigned long i = -1UL;
1157 unsigned long *p_addr;
1158 unsigned long bit_offset; /* bit offset of the mapped page. */
1159
1160 if (bm_fo > b->bm_bits) {
1161 dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
1162 } else {
1163 while (bm_fo < b->bm_bits) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001164 /* bit offset of the first bit in the page */
1165 bit_offset = bm_fo & ~BPP_MASK;
1166 p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001167
1168 if (find_zero_bit)
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001169 i = generic_find_next_zero_le_bit(p_addr, PAGE_SIZE*8, bm_fo & BPP_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170 else
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001171 i = generic_find_next_le_bit(p_addr, PAGE_SIZE*8, bm_fo & BPP_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001172
1173 __bm_unmap(p_addr, km);
1174 if (i < PAGE_SIZE*8) {
1175 i = bit_offset + i;
1176 if (i >= b->bm_bits)
1177 break;
1178 goto found;
1179 }
1180 bm_fo = bit_offset + PAGE_SIZE*8;
1181 }
1182 i = -1UL;
1183 }
1184 found:
1185 return i;
1186}
1187
1188static unsigned long bm_find_next(struct drbd_conf *mdev,
1189 unsigned long bm_fo, const int find_zero_bit)
1190{
1191 struct drbd_bitmap *b = mdev->bitmap;
1192 unsigned long i = -1UL;
1193
1194 ERR_IF(!b) return i;
1195 ERR_IF(!b->bm_pages) return i;
1196
1197 spin_lock_irq(&b->bm_lock);
1198 if (bm_is_locked(b))
1199 bm_print_lock_info(mdev);
1200
1201 i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);
1202
1203 spin_unlock_irq(&b->bm_lock);
1204 return i;
1205}
1206
1207unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1208{
1209 return bm_find_next(mdev, bm_fo, 0);
1210}
1211
1212#if 0
1213/* not yet needed for anything. */
1214unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1215{
1216 return bm_find_next(mdev, bm_fo, 1);
1217}
1218#endif
1219
1220/* does not spin_lock_irqsave.
1221 * you must take drbd_bm_lock() first */
1222unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1223{
1224 /* WARN_ON(!bm_is_locked(mdev)); */
1225 return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
1226}
1227
1228unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1229{
1230 /* WARN_ON(!bm_is_locked(mdev)); */
1231 return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
1232}
1233
1234/* returns number of bits actually changed.
1235 * for val != 0, we change 0 -> 1, return code positive
1236 * for val == 0, we change 1 -> 0, return code negative
1237 * wants bitnr, not sector.
1238 * expected to be called for only a few bits (e - s about BITS_PER_LONG).
1239 * Must hold bitmap lock already. */
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +02001240static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001241 unsigned long e, int val, const enum km_type km)
1242{
1243 struct drbd_bitmap *b = mdev->bitmap;
1244 unsigned long *p_addr = NULL;
1245 unsigned long bitnr;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001246 unsigned int last_page_nr = -1U;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001247 int c = 0;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001248 int changed_total = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001249
1250 if (e >= b->bm_bits) {
1251 dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
1252 s, e, b->bm_bits);
1253 e = b->bm_bits ? b->bm_bits -1 : 0;
1254 }
1255 for (bitnr = s; bitnr <= e; bitnr++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001256 unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001257 if (page_nr != last_page_nr) {
1258 if (p_addr)
1259 __bm_unmap(p_addr, km);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001260 if (c < 0)
1261 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1262 else if (c > 0)
1263 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1264 changed_total += c;
1265 c = 0;
1266 p_addr = __bm_map_pidx(b, page_nr, km);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001267 last_page_nr = page_nr;
1268 }
1269 if (val)
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001270 c += (0 == generic___test_and_set_le_bit(bitnr & BPP_MASK, p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001271 else
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001272 c -= (0 != generic___test_and_clear_le_bit(bitnr & BPP_MASK, p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001273 }
1274 if (p_addr)
1275 __bm_unmap(p_addr, km);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001276 if (c < 0)
1277 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1278 else if (c > 0)
1279 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1280 changed_total += c;
1281 b->bm_set += changed_total;
1282 return changed_total;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001283}
1284
1285/* returns number of bits actually changed.
1286 * for val != 0, we change 0 -> 1, return code positive
1287 * for val == 0, we change 1 -> 0, return code negative
1288 * wants bitnr, not sector */
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +02001289static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001290 const unsigned long e, int val)
1291{
1292 unsigned long flags;
1293 struct drbd_bitmap *b = mdev->bitmap;
1294 int c = 0;
1295
1296 ERR_IF(!b) return 1;
1297 ERR_IF(!b->bm_pages) return 0;
1298
1299 spin_lock_irqsave(&b->bm_lock, flags);
1300 if (bm_is_locked(b))
1301 bm_print_lock_info(mdev);
1302
1303 c = __bm_change_bits_to(mdev, s, e, val, KM_IRQ1);
1304
1305 spin_unlock_irqrestore(&b->bm_lock, flags);
1306 return c;
1307}
1308
1309/* returns number of bits changed 0 -> 1 */
1310int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1311{
1312 return bm_change_bits_to(mdev, s, e, 1);
1313}
1314
1315/* returns number of bits changed 1 -> 0 */
1316int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1317{
1318 return -bm_change_bits_to(mdev, s, e, 0);
1319}
1320
1321/* sets all bits in full words,
1322 * from first_word up to, but not including, last_word */
1323static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
1324 int page_nr, int first_word, int last_word)
1325{
1326 int i;
1327 int bits;
1328 unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_USER0);
1329 for (i = first_word; i < last_word; i++) {
1330 bits = hweight_long(paddr[i]);
1331 paddr[i] = ~0UL;
1332 b->bm_set += BITS_PER_LONG - bits;
1333 }
1334 kunmap_atomic(paddr, KM_USER0);
1335}
1336
1337/* Same thing as drbd_bm_set_bits, but without taking the spin_lock_irqsave.
1338 * You must first drbd_bm_lock().
1339 * Can be called to set the whole bitmap in one go.
1340 * Sets bits from s to e _inclusive_. */
1341void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1342{
1343 /* First set_bit from the first bit (s)
1344 * up to the next long boundary (sl),
1345 * then assign full words up to the last long boundary (el),
1346 * then set_bit up to and including the last bit (e).
1347 *
1348 * Do not use memset, because we must account for changes,
1349 * so we need to loop over the words with hweight() anyways.
1350 */
1351 unsigned long sl = ALIGN(s,BITS_PER_LONG);
1352 unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
1353 int first_page;
1354 int last_page;
1355 int page_nr;
1356 int first_word;
1357 int last_word;
1358
1359 if (e - s <= 3*BITS_PER_LONG) {
1360 /* don't bother; el and sl may even be wrong. */
1361 __bm_change_bits_to(mdev, s, e, 1, KM_USER0);
1362 return;
1363 }
1364
1365 /* difference is large enough that we can trust sl and el */
1366
1367 /* bits filling the current long */
1368 if (sl)
1369 __bm_change_bits_to(mdev, s, sl-1, 1, KM_USER0);
1370
1371 first_page = sl >> (3 + PAGE_SHIFT);
1372 last_page = el >> (3 + PAGE_SHIFT);
1373
1374 /* MLPP: modulo longs per page */
1375 /* LWPP: long words per page */
1376 first_word = MLPP(sl >> LN2_BPL);
1377 last_word = LWPP;
1378
1379 /* first and full pages, unless first page == last page */
1380 for (page_nr = first_page; page_nr < last_page; page_nr++) {
1381 bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
1382 cond_resched();
1383 first_word = 0;
1384 }
1385
1386 /* last page (respectively only page, for first page == last page) */
1387 last_word = MLPP(el >> LN2_BPL);
1388 bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);
1389
1390 /* possibly trailing bits.
1391 * example: (e & 63) == 63, el will be e+1.
1392 * if that even was the very last bit,
1393 * it would trigger an assert in __bm_change_bits_to()
1394 */
1395 if (el <= e)
1396 __bm_change_bits_to(mdev, el, e, 1, KM_USER0);
1397}
1398
1399/* returns bit state
1400 * wants bitnr, NOT sector.
1401 * inherently racy... area needs to be locked by means of {al,rs}_lru
1402 * 1 ... bit set
1403 * 0 ... bit not set
1404 * -1 ... first out of bounds access, stop testing for bits!
1405 */
1406int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
1407{
1408 unsigned long flags;
1409 struct drbd_bitmap *b = mdev->bitmap;
1410 unsigned long *p_addr;
1411 int i;
1412
1413 ERR_IF(!b) return 0;
1414 ERR_IF(!b->bm_pages) return 0;
1415
1416 spin_lock_irqsave(&b->bm_lock, flags);
1417 if (bm_is_locked(b))
1418 bm_print_lock_info(mdev);
1419 if (bitnr < b->bm_bits) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001420 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001421 i = generic_test_le_bit(bitnr & BPP_MASK, p_addr) ? 1 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001422 bm_unmap(p_addr);
1423 } else if (bitnr == b->bm_bits) {
1424 i = -1;
1425 } else { /* (bitnr > b->bm_bits) */
1426 dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
1427 i = 0;
1428 }
1429
1430 spin_unlock_irqrestore(&b->bm_lock, flags);
1431 return i;
1432}
1433
1434/* returns number of bits set in the range [s, e] */
1435int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1436{
1437 unsigned long flags;
1438 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001439 unsigned long *p_addr = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001440 unsigned long bitnr;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001441 unsigned int page_nr = -1U;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001442 int c = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001443
1444 /* If this is called without a bitmap, that is a bug. But just to be
1445 * robust in case we screwed up elsewhere, in that case pretend there
1446 * was one dirty bit in the requested area, so we won't try to do a
1447 * local read there (no bitmap probably implies no disk) */
1448 ERR_IF(!b) return 1;
1449 ERR_IF(!b->bm_pages) return 1;
1450
1451 spin_lock_irqsave(&b->bm_lock, flags);
1452 if (bm_is_locked(b))
1453 bm_print_lock_info(mdev);
1454 for (bitnr = s; bitnr <= e; bitnr++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001455 unsigned int idx = bm_bit_to_page_idx(b, bitnr);
1456 if (page_nr != idx) {
1457 page_nr = idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001458 if (p_addr)
1459 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001460 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001461 }
1462 ERR_IF (bitnr >= b->bm_bits) {
1463 dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
1464 } else {
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001465 c += (0 != generic_test_le_bit(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001466 }
1467 }
1468 if (p_addr)
1469 bm_unmap(p_addr);
1470 spin_unlock_irqrestore(&b->bm_lock, flags);
1471 return c;
1472}
1473
1474
1475/* inherently racy...
1476 * return value may be already out-of-date when this function returns.
1477 * but the general usage is that this is only use during a cstate when bits are
1478 * only cleared, not set, and typically only care for the case when the return
1479 * value is zero, or we already "locked" this "bitmap extent" by other means.
1480 *
1481 * enr is bm-extent number, since we chose to name one sector (512 bytes)
1482 * worth of the bitmap a "bitmap extent".
1483 *
1484 * TODO
1485 * I think since we use it like a reference count, we should use the real
1486 * reference count of some bitmap extent element from some lru instead...
1487 *
1488 */
1489int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
1490{
1491 struct drbd_bitmap *b = mdev->bitmap;
1492 int count, s, e;
1493 unsigned long flags;
1494 unsigned long *p_addr, *bm;
1495
1496 ERR_IF(!b) return 0;
1497 ERR_IF(!b->bm_pages) return 0;
1498
1499 spin_lock_irqsave(&b->bm_lock, flags);
1500 if (bm_is_locked(b))
1501 bm_print_lock_info(mdev);
1502
1503 s = S2W(enr);
1504 e = min((size_t)S2W(enr+1), b->bm_words);
1505 count = 0;
1506 if (s < b->bm_words) {
1507 int n = e-s;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001508 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509 bm = p_addr + MLPP(s);
1510 while (n--)
1511 count += hweight_long(*bm++);
1512 bm_unmap(p_addr);
1513 } else {
1514 dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
1515 }
1516 spin_unlock_irqrestore(&b->bm_lock, flags);
1517 return count;
1518}
1519
1520/* set all bits covered by the AL-extent al_enr */
1521unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
1522{
1523 struct drbd_bitmap *b = mdev->bitmap;
1524 unsigned long *p_addr, *bm;
1525 unsigned long weight;
1526 int count, s, e, i, do_now;
1527 ERR_IF(!b) return 0;
1528 ERR_IF(!b->bm_pages) return 0;
1529
1530 spin_lock_irq(&b->bm_lock);
1531 if (bm_is_locked(b))
1532 bm_print_lock_info(mdev);
1533 weight = b->bm_set;
1534
1535 s = al_enr * BM_WORDS_PER_AL_EXT;
1536 e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
1537 /* assert that s and e are on the same page */
1538 D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
1539 == s >> (PAGE_SHIFT - LN2_BPL + 3));
1540 count = 0;
1541 if (s < b->bm_words) {
1542 i = do_now = e-s;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001543 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001544 bm = p_addr + MLPP(s);
1545 while (i--) {
1546 count += hweight_long(*bm);
1547 *bm = -1UL;
1548 bm++;
1549 }
1550 bm_unmap(p_addr);
1551 b->bm_set += do_now*BITS_PER_LONG - count;
1552 if (e == b->bm_words)
1553 b->bm_set -= bm_clear_surplus(b);
1554 } else {
1555 dev_err(DEV, "start offset (%d) too large in drbd_bm_ALe_set_all\n", s);
1556 }
1557 weight = b->bm_set - weight;
1558 spin_unlock_irq(&b->bm_lock);
1559 return weight;
1560}