blob: 25428bc28476fe38fdfc85fc42b36911a007f1f4 [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".
Lars Ellenberg4b0715f2010-12-14 15:13:04 +010040 */
Philipp Reisnerb411b362009-09-25 16:07:19 -070041
Lars Ellenberg4b0715f2010-12-14 15:13:04 +010042
43/*
44 * LIMITATIONS:
45 * We want to support >= peta byte of backend storage, while for now still using
46 * a granularity of one bit per 4KiB of storage.
47 * 1 << 50 bytes backend storage (1 PiB)
48 * 1 << (50 - 12) bits needed
49 * 38 --> we need u64 to index and count bits
50 * 1 << (38 - 3) bitmap bytes needed
51 * 35 --> we still need u64 to index and count bytes
52 * (that's 32 GiB of bitmap for 1 PiB storage)
53 * 1 << (35 - 2) 32bit longs needed
54 * 33 --> we'd even need u64 to index and count 32bit long words.
55 * 1 << (35 - 3) 64bit longs needed
56 * 32 --> we could get away with a 32bit unsigned int to index and count
57 * 64bit long words, but I rather stay with unsigned long for now.
58 * We probably should neither count nor point to bytes or long words
59 * directly, but either by bitnumber, or by page index and offset.
60 * 1 << (35 - 12)
61 * 22 --> we need that much 4KiB pages of bitmap.
62 * 1 << (22 + 3) --> on a 64bit arch,
63 * we need 32 MiB to store the array of page pointers.
64 *
65 * Because I'm lazy, and because the resulting patch was too large, too ugly
66 * and still incomplete, on 32bit we still "only" support 16 TiB (minus some),
67 * (1 << 32) bits * 4k storage.
68 *
69
70 * bitmap storage and IO:
71 * Bitmap is stored little endian on disk, and is kept little endian in
72 * core memory. Currently we still hold the full bitmap in core as long
73 * as we are "attached" to a local disk, which at 32 GiB for 1PiB storage
74 * seems excessive.
75 *
76 * We plan to reduce the amount of in-core bitmap pages by pageing them in
77 * and out against their on-disk location as necessary, but need to make
78 * sure we don't cause too much meta data IO, and must not deadlock in
79 * tight memory situations. This needs some more work.
Philipp Reisnerb411b362009-09-25 16:07:19 -070080 */
81
82/*
83 * NOTE
84 * Access to the *bm_pages is protected by bm_lock.
85 * It is safe to read the other members within the lock.
86 *
87 * drbd_bm_set_bits is called from bio_endio callbacks,
88 * We may be called with irq already disabled,
89 * so we need spin_lock_irqsave().
90 * And we need the kmap_atomic.
91 */
92struct drbd_bitmap {
93 struct page **bm_pages;
94 spinlock_t bm_lock;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +010095
96 /* see LIMITATIONS: above */
97
Philipp Reisnerb411b362009-09-25 16:07:19 -070098 unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */
99 unsigned long bm_bits;
100 size_t bm_words;
101 size_t bm_number_of_pages;
102 sector_t bm_dev_capacity;
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000103 struct mutex bm_change; /* serializes resize operations */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700104
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100105 wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700106
107 unsigned long bm_flags;
108
109 /* debugging aid, in case we are still racy somewhere */
110 char *bm_why;
111 struct task_struct *bm_task;
112};
113
114/* definition of bits in bm_flags */
115#define BM_LOCKED 0
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100116// #define BM_MD_IO_ERROR 1 unused now.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700117#define BM_P_VMALLOCED 2
118
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +0200119static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Philipp Reisnerfd764382010-04-01 09:57:40 +0200120 unsigned long e, int val, const enum km_type km);
121
Philipp Reisnerb411b362009-09-25 16:07:19 -0700122static int bm_is_locked(struct drbd_bitmap *b)
123{
124 return test_bit(BM_LOCKED, &b->bm_flags);
125}
126
127#define bm_print_lock_info(m) __bm_print_lock_info(m, __func__)
128static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func)
129{
130 struct drbd_bitmap *b = mdev->bitmap;
131 if (!__ratelimit(&drbd_ratelimit_state))
132 return;
133 dev_err(DEV, "FIXME %s in %s, bitmap locked for '%s' by %s\n",
134 current == mdev->receiver.task ? "receiver" :
135 current == mdev->asender.task ? "asender" :
136 current == mdev->worker.task ? "worker" : current->comm,
137 func, b->bm_why ?: "?",
138 b->bm_task == mdev->receiver.task ? "receiver" :
139 b->bm_task == mdev->asender.task ? "asender" :
140 b->bm_task == mdev->worker.task ? "worker" : "?");
141}
142
143void drbd_bm_lock(struct drbd_conf *mdev, char *why)
144{
145 struct drbd_bitmap *b = mdev->bitmap;
146 int trylock_failed;
147
148 if (!b) {
149 dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n");
150 return;
151 }
152
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000153 trylock_failed = !mutex_trylock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700154
155 if (trylock_failed) {
156 dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n",
157 current == mdev->receiver.task ? "receiver" :
158 current == mdev->asender.task ? "asender" :
159 current == mdev->worker.task ? "worker" : current->comm,
160 why, b->bm_why ?: "?",
161 b->bm_task == mdev->receiver.task ? "receiver" :
162 b->bm_task == mdev->asender.task ? "asender" :
163 b->bm_task == mdev->worker.task ? "worker" : "?");
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000164 mutex_lock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700165 }
166 if (__test_and_set_bit(BM_LOCKED, &b->bm_flags))
167 dev_err(DEV, "FIXME bitmap already locked in bm_lock\n");
168
169 b->bm_why = why;
170 b->bm_task = current;
171}
172
173void drbd_bm_unlock(struct drbd_conf *mdev)
174{
175 struct drbd_bitmap *b = mdev->bitmap;
176 if (!b) {
177 dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n");
178 return;
179 }
180
181 if (!__test_and_clear_bit(BM_LOCKED, &mdev->bitmap->bm_flags))
182 dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");
183
184 b->bm_why = NULL;
185 b->bm_task = NULL;
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000186 mutex_unlock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700187}
188
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100189/* we store some "meta" info about our pages in page->private */
190/* at a granularity of 4k storage per bitmap bit:
191 * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks
192 * 1<<38 bits,
193 * 1<<23 4k bitmap pages.
194 * Use 24 bits as page index, covers 2 peta byte storage
195 * at a granularity of 4k per bit.
196 * Used to report the failed page idx on io error from the endio handlers.
197 */
198#define BM_PAGE_IDX_MASK ((1UL<<24)-1)
199/* this page is currently read in, or written back */
200#define BM_PAGE_IO_LOCK 31
201/* if there has been an IO error for this page */
202#define BM_PAGE_IO_ERROR 30
203/* this is to be able to intelligently skip disk IO,
204 * set if bits have been set since last IO. */
205#define BM_PAGE_NEED_WRITEOUT 29
206/* to mark for lazy writeout once syncer cleared all clearable bits,
207 * we if bits have been cleared since last IO. */
208#define BM_PAGE_LAZY_WRITEOUT 28
209
210/* store_page_idx uses non-atomic assingment. It is only used directly after
211 * allocating the page. All other bm_set_page_* and bm_clear_page_* need to
212 * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap
213 * changes) may happen from various contexts, and wait_on_bit/wake_up_bit
214 * requires it all to be atomic as well. */
215static void bm_store_page_idx(struct page *page, unsigned long idx)
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100216{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100217 BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
218 page_private(page) |= idx;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100219}
220
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100221static unsigned long bm_page_to_idx(struct page *page)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700222{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100223 return page_private(page) & BM_PAGE_IDX_MASK;
224}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700225
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100226/* As is very unlikely that the same page is under IO from more than one
227 * context, we can get away with a bit per page and one wait queue per bitmap.
228 */
229static void bm_page_lock_io(struct drbd_conf *mdev, int page_nr)
230{
231 struct drbd_bitmap *b = mdev->bitmap;
232 void *addr = &page_private(b->bm_pages[page_nr]);
233 wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
234}
235
236static void bm_page_unlock_io(struct drbd_conf *mdev, int page_nr)
237{
238 struct drbd_bitmap *b = mdev->bitmap;
239 void *addr = &page_private(b->bm_pages[page_nr]);
240 clear_bit(BM_PAGE_IO_LOCK, addr);
241 smp_mb__after_clear_bit();
242 wake_up(&mdev->bitmap->bm_io_wait);
243}
244
245/* set _before_ submit_io, so it may be reset due to being changed
246 * while this page is in flight... will get submitted later again */
247static void bm_set_page_unchanged(struct page *page)
248{
249 /* use cmpxchg? */
250 clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
251 clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
252}
253
254static void bm_set_page_need_writeout(struct page *page)
255{
256 set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
257}
258
259static int bm_test_page_unchanged(struct page *page)
260{
261 volatile const unsigned long *addr = &page_private(page);
262 return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0;
263}
264
265static void bm_set_page_io_err(struct page *page)
266{
267 set_bit(BM_PAGE_IO_ERROR, &page_private(page));
268}
269
270static void bm_clear_page_io_err(struct page *page)
271{
272 clear_bit(BM_PAGE_IO_ERROR, &page_private(page));
273}
274
275static void bm_set_page_lazy_writeout(struct page *page)
276{
277 set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
278}
279
280static int bm_test_page_lazy_writeout(struct page *page)
281{
282 return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
283}
284
285/* on a 32bit box, this would allow for exactly (2<<38) bits. */
286static unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr)
287{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700288 /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100289 unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700290 BUG_ON(page_nr >= b->bm_number_of_pages);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100291 return page_nr;
292}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700293
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100294static unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr)
295{
296 /* page_nr = (bitnr/8) >> PAGE_SHIFT; */
297 unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3);
298 BUG_ON(page_nr >= b->bm_number_of_pages);
299 return page_nr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700300}
301
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100302static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx, const enum km_type km)
303{
304 struct page *page = b->bm_pages[idx];
305 return (unsigned long *) kmap_atomic(page, km);
306}
307
308static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
309{
310 return __bm_map_pidx(b, idx, KM_IRQ1);
311}
312
Philipp Reisnerb411b362009-09-25 16:07:19 -0700313static void __bm_unmap(unsigned long *p_addr, const enum km_type km)
314{
315 kunmap_atomic(p_addr, km);
316};
317
318static void bm_unmap(unsigned long *p_addr)
319{
320 return __bm_unmap(p_addr, KM_IRQ1);
321}
322
323/* long word offset of _bitmap_ sector */
324#define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
325/* word offset from start of bitmap to word number _in_page_
326 * modulo longs per page
327#define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
328 hm, well, Philipp thinks gcc might not optimze the % into & (... - 1)
329 so do it explicitly:
330 */
331#define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
332
333/* Long words per page */
334#define LWPP (PAGE_SIZE/sizeof(long))
335
336/*
337 * actually most functions herein should take a struct drbd_bitmap*, not a
338 * struct drbd_conf*, but for the debug macros I like to have the mdev around
339 * to be able to report device specific.
340 */
341
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100342
Philipp Reisnerb411b362009-09-25 16:07:19 -0700343static void bm_free_pages(struct page **pages, unsigned long number)
344{
345 unsigned long i;
346 if (!pages)
347 return;
348
349 for (i = 0; i < number; i++) {
350 if (!pages[i]) {
351 printk(KERN_ALERT "drbd: bm_free_pages tried to free "
352 "a NULL pointer; i=%lu n=%lu\n",
353 i, number);
354 continue;
355 }
356 __free_page(pages[i]);
357 pages[i] = NULL;
358 }
359}
360
361static void bm_vk_free(void *ptr, int v)
362{
363 if (v)
364 vfree(ptr);
365 else
366 kfree(ptr);
367}
368
369/*
370 * "have" and "want" are NUMBER OF PAGES.
371 */
372static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
373{
374 struct page **old_pages = b->bm_pages;
375 struct page **new_pages, *page;
376 unsigned int i, bytes, vmalloced = 0;
377 unsigned long have = b->bm_number_of_pages;
378
379 BUG_ON(have == 0 && old_pages != NULL);
380 BUG_ON(have != 0 && old_pages == NULL);
381
382 if (have == want)
383 return old_pages;
384
385 /* Trying kmalloc first, falling back to vmalloc.
386 * GFP_KERNEL is ok, as this is done when a lower level disk is
387 * "attached" to the drbd. Context is receiver thread or cqueue
388 * thread. As we have no disk yet, we are not in the IO path,
389 * not even the IO path of the peer. */
390 bytes = sizeof(struct page *)*want;
391 new_pages = kmalloc(bytes, GFP_KERNEL);
392 if (!new_pages) {
393 new_pages = vmalloc(bytes);
394 if (!new_pages)
395 return NULL;
396 vmalloced = 1;
397 }
398
399 memset(new_pages, 0, bytes);
400 if (want >= have) {
401 for (i = 0; i < have; i++)
402 new_pages[i] = old_pages[i];
403 for (; i < want; i++) {
404 page = alloc_page(GFP_HIGHUSER);
405 if (!page) {
406 bm_free_pages(new_pages + have, i - have);
407 bm_vk_free(new_pages, vmalloced);
408 return NULL;
409 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100410 /* we want to know which page it is
411 * from the endio handlers */
412 bm_store_page_idx(page, i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700413 new_pages[i] = page;
414 }
415 } else {
416 for (i = 0; i < want; i++)
417 new_pages[i] = old_pages[i];
418 /* NOT HERE, we are outside the spinlock!
419 bm_free_pages(old_pages + want, have - want);
420 */
421 }
422
423 if (vmalloced)
424 set_bit(BM_P_VMALLOCED, &b->bm_flags);
425 else
426 clear_bit(BM_P_VMALLOCED, &b->bm_flags);
427
428 return new_pages;
429}
430
431/*
432 * called on driver init only. TODO call when a device is created.
433 * allocates the drbd_bitmap, and stores it in mdev->bitmap.
434 */
435int drbd_bm_init(struct drbd_conf *mdev)
436{
437 struct drbd_bitmap *b = mdev->bitmap;
438 WARN_ON(b != NULL);
439 b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
440 if (!b)
441 return -ENOMEM;
442 spin_lock_init(&b->bm_lock);
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000443 mutex_init(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700444 init_waitqueue_head(&b->bm_io_wait);
445
446 mdev->bitmap = b;
447
448 return 0;
449}
450
451sector_t drbd_bm_capacity(struct drbd_conf *mdev)
452{
453 ERR_IF(!mdev->bitmap) return 0;
454 return mdev->bitmap->bm_dev_capacity;
455}
456
457/* called on driver unload. TODO: call when a device is destroyed.
458 */
459void drbd_bm_cleanup(struct drbd_conf *mdev)
460{
461 ERR_IF (!mdev->bitmap) return;
462 bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
463 bm_vk_free(mdev->bitmap->bm_pages, test_bit(BM_P_VMALLOCED, &mdev->bitmap->bm_flags));
464 kfree(mdev->bitmap);
465 mdev->bitmap = NULL;
466}
467
468/*
469 * since (b->bm_bits % BITS_PER_LONG) != 0,
470 * this masks out the remaining bits.
471 * Returns the number of bits cleared.
472 */
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100473#define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3))
474#define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1)
475#define BITS_PER_LONG_MASK (BITS_PER_LONG - 1)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700476static int bm_clear_surplus(struct drbd_bitmap *b)
477{
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100478 unsigned long mask;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700479 unsigned long *p_addr, *bm;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100480 int tmp;
481 int cleared = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700482
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100483 /* number of bits modulo bits per page */
484 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
485 /* mask the used bits of the word containing the last bit */
486 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
487 /* bitmap is always stored little endian,
488 * on disk and in core memory alike */
489 mask = cpu_to_lel(mask);
490
Lars Ellenberg6850c442010-12-16 00:32:38 +0100491 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100492 bm = p_addr + (tmp/BITS_PER_LONG);
493 if (mask) {
494 /* If mask != 0, we are not exactly aligned, so bm now points
495 * to the long containing the last bit.
496 * If mask == 0, bm already points to the word immediately
497 * after the last (long word aligned) bit. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700498 cleared = hweight_long(*bm & ~mask);
499 *bm &= mask;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100500 bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501 }
502
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100503 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
504 /* on a 32bit arch, we may need to zero out
505 * a padding long to align with a 64bit remote */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700506 cleared += hweight_long(*bm);
507 *bm = 0;
508 }
509 bm_unmap(p_addr);
510 return cleared;
511}
512
513static void bm_set_surplus(struct drbd_bitmap *b)
514{
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100515 unsigned long mask;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700516 unsigned long *p_addr, *bm;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100517 int tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700518
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100519 /* number of bits modulo bits per page */
520 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
521 /* mask the used bits of the word containing the last bit */
522 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
523 /* bitmap is always stored little endian,
524 * on disk and in core memory alike */
525 mask = cpu_to_lel(mask);
526
Lars Ellenberg6850c442010-12-16 00:32:38 +0100527 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100528 bm = p_addr + (tmp/BITS_PER_LONG);
529 if (mask) {
530 /* If mask != 0, we are not exactly aligned, so bm now points
531 * to the long containing the last bit.
532 * If mask == 0, bm already points to the word immediately
533 * after the last (long word aligned) bit. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700534 *bm |= ~mask;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100535 bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700536 }
537
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100538 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
539 /* on a 32bit arch, we may need to zero out
540 * a padding long to align with a 64bit remote */
541 *bm = ~0UL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700542 }
543 bm_unmap(p_addr);
544}
545
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100546/* you better not modify the bitmap while this is running,
547 * or its results will be stale */
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100548static unsigned long bm_count_bits(struct drbd_bitmap *b)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700549{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100550 unsigned long *p_addr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700551 unsigned long bits = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100552 unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1;
Lars Ellenberg6850c442010-12-16 00:32:38 +0100553 int idx, i, last_word;
Lars Ellenberg7777a8b2010-12-15 23:21:39 +0100554
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100555 /* all but last page */
Lars Ellenberg6850c442010-12-16 00:32:38 +0100556 for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100557 p_addr = __bm_map_pidx(b, idx, KM_USER0);
558 for (i = 0; i < LWPP; i++)
559 bits += hweight_long(p_addr[i]);
Lars Ellenberg7777a8b2010-12-15 23:21:39 +0100560 __bm_unmap(p_addr, KM_USER0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700561 cond_resched();
562 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100563 /* last (or only) page */
564 last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL;
565 p_addr = __bm_map_pidx(b, idx, KM_USER0);
566 for (i = 0; i < last_word; i++)
567 bits += hweight_long(p_addr[i]);
568 p_addr[last_word] &= cpu_to_lel(mask);
569 bits += hweight_long(p_addr[last_word]);
570 /* 32bit arch, may have an unused padding long */
571 if (BITS_PER_LONG == 32 && (last_word & 1) == 0)
572 p_addr[last_word+1] = 0;
573 __bm_unmap(p_addr, KM_USER0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700574 return bits;
575}
576
Philipp Reisnerb411b362009-09-25 16:07:19 -0700577/* offset and len in long words.*/
578static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
579{
580 unsigned long *p_addr, *bm;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100581 unsigned int idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700582 size_t do_now, end;
583
Philipp Reisnerb411b362009-09-25 16:07:19 -0700584 end = offset + len;
585
586 if (end > b->bm_words) {
587 printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
588 return;
589 }
590
591 while (offset < end) {
592 do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100593 idx = bm_word_to_page_idx(b, offset);
594 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700595 bm = p_addr + MLPP(offset);
596 if (bm+do_now > p_addr + LWPP) {
597 printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
598 p_addr, bm, (int)do_now);
Lars Ellenberg84e7c0f2010-12-16 00:37:57 +0100599 } else
600 memset(bm, c, do_now * sizeof(long));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700601 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100602 bm_set_page_need_writeout(b->bm_pages[idx]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700603 offset += do_now;
604 }
605}
606
607/*
608 * make sure the bitmap has enough room for the attached storage,
609 * if necessary, resize.
610 * called whenever we may have changed the device size.
611 * returns -ENOMEM if we could not allocate enough memory, 0 on success.
612 * In case this is actually a resize, we copy the old bitmap into the new one.
613 * Otherwise, the bitmap is initialized to all bits set.
614 */
Philipp Reisner02d9a942010-03-24 16:23:03 +0100615int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700616{
617 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg6850c442010-12-16 00:32:38 +0100618 unsigned long bits, words, owords, obits;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700619 unsigned long want, have, onpages; /* number of pages */
620 struct page **npages, **opages = NULL;
621 int err = 0, growing;
622 int opages_vmalloced;
623
624 ERR_IF(!b) return -ENOMEM;
625
626 drbd_bm_lock(mdev, "resize");
627
628 dev_info(DEV, "drbd_bm_resize called with capacity == %llu\n",
629 (unsigned long long)capacity);
630
631 if (capacity == b->bm_dev_capacity)
632 goto out;
633
634 opages_vmalloced = test_bit(BM_P_VMALLOCED, &b->bm_flags);
635
636 if (capacity == 0) {
637 spin_lock_irq(&b->bm_lock);
638 opages = b->bm_pages;
639 onpages = b->bm_number_of_pages;
640 owords = b->bm_words;
641 b->bm_pages = NULL;
642 b->bm_number_of_pages =
643 b->bm_set =
644 b->bm_bits =
645 b->bm_words =
646 b->bm_dev_capacity = 0;
647 spin_unlock_irq(&b->bm_lock);
648 bm_free_pages(opages, onpages);
649 bm_vk_free(opages, opages_vmalloced);
650 goto out;
651 }
652 bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));
653
654 /* if we would use
655 words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
656 a 32bit host could present the wrong number of words
657 to a 64bit host.
658 */
659 words = ALIGN(bits, 64) >> LN2_BPL;
660
661 if (get_ldev(mdev)) {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100662 u64 bits_on_disk = ((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700663 put_ldev(mdev);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100664 if (bits > bits_on_disk) {
665 dev_info(DEV, "bits = %lu\n", bits);
666 dev_info(DEV, "bits_on_disk = %llu\n", bits_on_disk);
667 err = -ENOSPC;
668 goto out;
669 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700670 }
671
Lars Ellenberg6850c442010-12-16 00:32:38 +0100672 want = ALIGN(words*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700673 have = b->bm_number_of_pages;
674 if (want == have) {
675 D_ASSERT(b->bm_pages != NULL);
676 npages = b->bm_pages;
677 } else {
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100678 if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700679 npages = NULL;
680 else
681 npages = bm_realloc_pages(b, want);
682 }
683
684 if (!npages) {
685 err = -ENOMEM;
686 goto out;
687 }
688
689 spin_lock_irq(&b->bm_lock);
690 opages = b->bm_pages;
691 owords = b->bm_words;
692 obits = b->bm_bits;
693
694 growing = bits > obits;
Philipp Reisner52236712010-04-28 14:46:57 +0200695 if (opages && growing && set_new_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700696 bm_set_surplus(b);
697
698 b->bm_pages = npages;
699 b->bm_number_of_pages = want;
700 b->bm_bits = bits;
701 b->bm_words = words;
702 b->bm_dev_capacity = capacity;
703
704 if (growing) {
Philipp Reisner02d9a942010-03-24 16:23:03 +0100705 if (set_new_bits) {
706 bm_memset(b, owords, 0xff, words-owords);
707 b->bm_set += bits - obits;
708 } else
709 bm_memset(b, owords, 0x00, words-owords);
710
Philipp Reisnerb411b362009-09-25 16:07:19 -0700711 }
712
713 if (want < have) {
714 /* implicit: (opages != NULL) && (opages != npages) */
715 bm_free_pages(opages + want, have - want);
716 }
717
Philipp Reisnerb411b362009-09-25 16:07:19 -0700718 (void)bm_clear_surplus(b);
719
720 spin_unlock_irq(&b->bm_lock);
721 if (opages != npages)
722 bm_vk_free(opages, opages_vmalloced);
723 if (!growing)
724 b->bm_set = bm_count_bits(b);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100725 dev_info(DEV, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700726
727 out:
728 drbd_bm_unlock(mdev);
729 return err;
730}
731
732/* inherently racy:
733 * if not protected by other means, return value may be out of date when
734 * leaving this function...
735 * we still need to lock it, since it is important that this returns
736 * bm_set == 0 precisely.
737 *
738 * maybe bm_set should be atomic_t ?
739 */
Philipp Reisner07782862010-08-31 12:00:50 +0200740unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700741{
742 struct drbd_bitmap *b = mdev->bitmap;
743 unsigned long s;
744 unsigned long flags;
745
746 ERR_IF(!b) return 0;
747 ERR_IF(!b->bm_pages) return 0;
748
749 spin_lock_irqsave(&b->bm_lock, flags);
750 s = b->bm_set;
751 spin_unlock_irqrestore(&b->bm_lock, flags);
752
753 return s;
754}
755
756unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
757{
758 unsigned long s;
759 /* if I don't have a disk, I don't know about out-of-sync status */
760 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
761 return 0;
762 s = _drbd_bm_total_weight(mdev);
763 put_ldev(mdev);
764 return s;
765}
766
767size_t drbd_bm_words(struct drbd_conf *mdev)
768{
769 struct drbd_bitmap *b = mdev->bitmap;
770 ERR_IF(!b) return 0;
771 ERR_IF(!b->bm_pages) return 0;
772
773 return b->bm_words;
774}
775
776unsigned long drbd_bm_bits(struct drbd_conf *mdev)
777{
778 struct drbd_bitmap *b = mdev->bitmap;
779 ERR_IF(!b) return 0;
780
781 return b->bm_bits;
782}
783
784/* merge number words from buffer into the bitmap starting at offset.
785 * buffer[i] is expected to be little endian unsigned long.
786 * bitmap must be locked by drbd_bm_lock.
787 * currently only used from receive_bitmap.
788 */
789void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
790 unsigned long *buffer)
791{
792 struct drbd_bitmap *b = mdev->bitmap;
793 unsigned long *p_addr, *bm;
794 unsigned long word, bits;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100795 unsigned int idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700796 size_t end, do_now;
797
798 end = offset + number;
799
800 ERR_IF(!b) return;
801 ERR_IF(!b->bm_pages) return;
802 if (number == 0)
803 return;
804 WARN_ON(offset >= b->bm_words);
805 WARN_ON(end > b->bm_words);
806
807 spin_lock_irq(&b->bm_lock);
808 while (offset < end) {
809 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100810 idx = bm_word_to_page_idx(b, offset);
811 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700812 bm = p_addr + MLPP(offset);
813 offset += do_now;
814 while (do_now--) {
815 bits = hweight_long(*bm);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100816 word = *bm | *buffer++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700817 *bm++ = word;
818 b->bm_set += hweight_long(word) - bits;
819 }
820 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100821 bm_set_page_need_writeout(b->bm_pages[idx]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700822 }
823 /* with 32bit <-> 64bit cross-platform connect
824 * this is only correct for current usage,
825 * where we _know_ that we are 64 bit aligned,
826 * and know that this function is used in this way, too...
827 */
828 if (end == b->bm_words)
829 b->bm_set -= bm_clear_surplus(b);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700830 spin_unlock_irq(&b->bm_lock);
831}
832
833/* copy number words from the bitmap starting at offset into the buffer.
834 * buffer[i] will be little endian unsigned long.
835 */
836void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
837 unsigned long *buffer)
838{
839 struct drbd_bitmap *b = mdev->bitmap;
840 unsigned long *p_addr, *bm;
841 size_t end, do_now;
842
843 end = offset + number;
844
845 ERR_IF(!b) return;
846 ERR_IF(!b->bm_pages) return;
847
848 spin_lock_irq(&b->bm_lock);
849 if ((offset >= b->bm_words) ||
850 (end > b->bm_words) ||
851 (number <= 0))
852 dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n",
853 (unsigned long) offset,
854 (unsigned long) number,
855 (unsigned long) b->bm_words);
856 else {
857 while (offset < end) {
858 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100859 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700860 bm = p_addr + MLPP(offset);
861 offset += do_now;
862 while (do_now--)
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100863 *buffer++ = *bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700864 bm_unmap(p_addr);
865 }
866 }
867 spin_unlock_irq(&b->bm_lock);
868}
869
870/* set all bits in the bitmap */
871void drbd_bm_set_all(struct drbd_conf *mdev)
872{
873 struct drbd_bitmap *b = mdev->bitmap;
874 ERR_IF(!b) return;
875 ERR_IF(!b->bm_pages) return;
876
877 spin_lock_irq(&b->bm_lock);
878 bm_memset(b, 0, 0xff, b->bm_words);
879 (void)bm_clear_surplus(b);
880 b->bm_set = b->bm_bits;
881 spin_unlock_irq(&b->bm_lock);
882}
883
884/* clear all bits in the bitmap */
885void drbd_bm_clear_all(struct drbd_conf *mdev)
886{
887 struct drbd_bitmap *b = mdev->bitmap;
888 ERR_IF(!b) return;
889 ERR_IF(!b->bm_pages) return;
890
891 spin_lock_irq(&b->bm_lock);
892 bm_memset(b, 0, 0, b->bm_words);
893 b->bm_set = 0;
894 spin_unlock_irq(&b->bm_lock);
895}
896
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100897struct bm_aio_ctx {
898 struct drbd_conf *mdev;
899 atomic_t in_flight;
Lars Ellenberg725a97e2010-12-19 11:29:55 +0100900 struct completion done;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100901 unsigned flags;
902#define BM_AIO_COPY_PAGES 1
903 int error;
904};
905
906/* bv_page may be a copy, or may be the original */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700907static void bm_async_io_complete(struct bio *bio, int error)
908{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100909 struct bm_aio_ctx *ctx = bio->bi_private;
910 struct drbd_conf *mdev = ctx->mdev;
911 struct drbd_bitmap *b = mdev->bitmap;
912 unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913 int uptodate = bio_flagged(bio, BIO_UPTODATE);
914
915
916 /* strange behavior of some lower level drivers...
917 * fail the request by clearing the uptodate flag,
918 * but do not return any error?!
919 * do we want to WARN() on this? */
920 if (!error && !uptodate)
921 error = -EIO;
922
Lars Ellenberg7648cdf2010-12-17 23:58:41 +0100923 if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 &&
924 !bm_test_page_unchanged(b->bm_pages[idx]))
925 dev_warn(DEV, "bitmap page idx %u changed during IO!\n", idx);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100926
Philipp Reisnerb411b362009-09-25 16:07:19 -0700927 if (error) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100928 /* ctx error will hold the completed-last non-zero error code,
929 * in case error codes differ. */
930 ctx->error = error;
931 bm_set_page_io_err(b->bm_pages[idx]);
932 /* Not identical to on disk version of it.
933 * Is BM_PAGE_IO_ERROR enough? */
934 if (__ratelimit(&drbd_ratelimit_state))
935 dev_err(DEV, "IO ERROR %d on bitmap page idx %u\n",
936 error, idx);
937 } else {
938 bm_clear_page_io_err(b->bm_pages[idx]);
939 dynamic_dev_dbg(DEV, "bitmap page idx %u completed\n", idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700940 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100941
942 bm_page_unlock_io(mdev, idx);
943
944 /* FIXME give back to page pool */
945 if (ctx->flags & BM_AIO_COPY_PAGES)
946 put_page(bio->bi_io_vec[0].bv_page);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700947
948 bio_put(bio);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100949
950 if (atomic_dec_and_test(&ctx->in_flight))
Lars Ellenberg725a97e2010-12-19 11:29:55 +0100951 complete(&ctx->done);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700952}
953
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100954static 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 -0700955{
956 /* we are process context. we always get a bio */
957 struct bio *bio = bio_alloc(GFP_KERNEL, 1);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100958 struct drbd_conf *mdev = ctx->mdev;
959 struct drbd_bitmap *b = mdev->bitmap;
960 struct page *page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700961 unsigned int len;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100962
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963 sector_t on_disk_sector =
964 mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset;
965 on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);
966
967 /* this might happen with very small
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100968 * flexible external meta data device,
969 * or with PAGE_SIZE > 4k */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700970 len = min_t(unsigned int, PAGE_SIZE,
971 (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);
972
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100973 /* serialize IO on this page */
974 bm_page_lock_io(mdev, page_nr);
975 /* before memcpy and submit,
976 * so it can be redirtied any time */
977 bm_set_page_unchanged(b->bm_pages[page_nr]);
978
979 if (ctx->flags & BM_AIO_COPY_PAGES) {
980 /* FIXME alloc_page is good enough for now, but actually needs
981 * to use pre-allocated page pool */
982 void *src, *dest;
983 page = alloc_page(__GFP_HIGHMEM|__GFP_WAIT);
984 dest = kmap_atomic(page, KM_USER0);
985 src = kmap_atomic(b->bm_pages[page_nr], KM_USER1);
986 memcpy(dest, src, PAGE_SIZE);
987 kunmap_atomic(src, KM_USER1);
988 kunmap_atomic(dest, KM_USER0);
989 bm_store_page_idx(page, page_nr);
990 } else
991 page = b->bm_pages[page_nr];
992
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993 bio->bi_bdev = mdev->ldev->md_bdev;
994 bio->bi_sector = on_disk_sector;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100995 bio_add_page(bio, page, len, 0);
996 bio->bi_private = ctx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700997 bio->bi_end_io = bm_async_io_complete;
998
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100999 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000 bio->bi_rw |= rw;
1001 bio_endio(bio, -EIO);
1002 } else {
1003 submit_bio(rw, bio);
1004 }
1005}
1006
Philipp Reisnerb411b362009-09-25 16:07:19 -07001007/*
1008 * bm_rw: read/write the whole bitmap from/to its on disk location.
1009 */
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001010static int bm_rw(struct drbd_conf *mdev, int rw, unsigned lazy_writeout_upper_idx) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001011{
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001012 struct bm_aio_ctx ctx = {
1013 .mdev = mdev,
1014 .in_flight = ATOMIC_INIT(1),
1015 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1016 .flags = lazy_writeout_upper_idx ? BM_AIO_COPY_PAGES : 0,
1017 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001018 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg6850c442010-12-16 00:32:38 +01001019 int num_pages, i, count = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001020 unsigned long now;
1021 char ppb[10];
1022 int err = 0;
1023
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001024 /*
1025 * We are protected against bitmap disappearing/resizing by holding an
1026 * ldev reference (caller must have called get_ldev()).
1027 * For read/write, we are protected against changes to the bitmap by
1028 * the bitmap lock (see drbd_bitmap_io).
1029 * For lazy writeout, we don't care for ongoing changes to the bitmap,
1030 * as we submit copies of pages anyways.
1031 */
1032 if (!ctx.flags)
1033 WARN_ON(!bm_is_locked(b));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001034
Lars Ellenberg6850c442010-12-16 00:32:38 +01001035 num_pages = b->bm_number_of_pages;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001036
Philipp Reisnerb411b362009-09-25 16:07:19 -07001037 now = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001038
1039 /* let the layers below us try to merge these bios... */
Lars Ellenberg6850c442010-12-16 00:32:38 +01001040 for (i = 0; i < num_pages; i++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001041 /* ignore completely unchanged pages */
1042 if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
1043 break;
1044 if (rw & WRITE) {
1045 if (bm_test_page_unchanged(b->bm_pages[i])) {
1046 dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
1047 continue;
1048 }
1049 /* during lazy writeout,
1050 * ignore those pages not marked for lazy writeout. */
1051 if (lazy_writeout_upper_idx &&
1052 !bm_test_page_lazy_writeout(b->bm_pages[i])) {
1053 dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
1054 continue;
1055 }
1056 }
1057 atomic_inc(&ctx.in_flight);
1058 bm_page_io_async(&ctx, i, rw);
1059 ++count;
1060 cond_resched();
1061 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001063 /*
1064 * We initialize ctx.in_flight to one to make sure bm_async_io_complete
1065 * will not complete() early, and decrement / test it here. If there
1066 * are still some bios in flight, we need to wait for them here.
1067 */
1068 if (!atomic_dec_and_test(&ctx.in_flight))
1069 wait_for_completion(&ctx.done);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001070 dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
1071 rw == WRITE ? "WRITE" : "READ",
1072 count, jiffies - now);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001073
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001074 if (ctx.error) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075 dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001076 drbd_chk_io_error(mdev, 1, true);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001077 err = -EIO; /* ctx.error ? */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001078 }
1079
1080 now = jiffies;
1081 if (rw == WRITE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082 drbd_md_flush(mdev);
1083 } else /* rw == READ */ {
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001084 b->bm_set = bm_count_bits(b);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001085 dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
1086 jiffies - now);
1087 }
1088 now = b->bm_set;
1089
1090 dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
1091 ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
1092
1093 return err;
1094}
1095
1096/**
1097 * drbd_bm_read() - Read the whole bitmap from its on disk location.
1098 * @mdev: DRBD device.
1099 */
1100int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
1101{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001102 return bm_rw(mdev, READ, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001103}
1104
1105/**
1106 * drbd_bm_write() - Write the whole bitmap to its on disk location.
1107 * @mdev: DRBD device.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001108 *
1109 * Will only write pages that have changed since last IO.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001110 */
1111int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
1112{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001113 return bm_rw(mdev, WRITE, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001114}
1115
1116/**
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001117 * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001118 * @mdev: DRBD device.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001119 * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
Philipp Reisnerb411b362009-09-25 16:07:19 -07001120 */
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001121int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001122{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001123 return bm_rw(mdev, WRITE, upper_idx);
1124}
Philipp Reisnerb411b362009-09-25 16:07:19 -07001125
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001126
1127/**
1128 * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
1129 * @mdev: DRBD device.
1130 * @idx: bitmap page index
1131 *
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001132 * We don't want to special case on logical_block_size of the backend device,
1133 * so we submit PAGE_SIZE aligned pieces.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001134 * Note that on "most" systems, PAGE_SIZE is 4k.
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001135 *
1136 * In case this becomes an issue on systems with larger PAGE_SIZE,
1137 * we may want to change this again to write 4k aligned 4k pieces.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001138 */
1139int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
1140{
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001141 struct bm_aio_ctx ctx = {
1142 .mdev = mdev,
1143 .in_flight = ATOMIC_INIT(1),
1144 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1145 .flags = BM_AIO_COPY_PAGES,
1146 };
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001147
1148 if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
Lars Ellenberg7648cdf2010-12-17 23:58:41 +01001149 dynamic_dev_dbg(DEV, "skipped bm page write for idx %u\n", idx);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001150 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001151 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001152
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001153 bm_page_io_async(&ctx, idx, WRITE_SYNC);
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001154 wait_for_completion(&ctx.done);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001155
1156 if (ctx.error)
1157 drbd_chk_io_error(mdev, 1, true);
1158 /* that should force detach, so the in memory bitmap will be
1159 * gone in a moment as well. */
1160
Philipp Reisnerb411b362009-09-25 16:07:19 -07001161 mdev->bm_writ_cnt++;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001162 return ctx.error;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001163}
1164
1165/* NOTE
1166 * find_first_bit returns int, we return unsigned long.
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001167 * For this to work on 32bit arch with bitnumbers > (1<<32),
1168 * we'd need to return u64, and get a whole lot of other places
1169 * fixed where we still use unsigned long.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170 *
1171 * this returns a bit number, NOT a sector!
1172 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001173static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
1174 const int find_zero_bit, const enum km_type km)
1175{
1176 struct drbd_bitmap *b = mdev->bitmap;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001177 unsigned long *p_addr;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001178 unsigned long bit_offset;
1179 unsigned i;
1180
Philipp Reisnerb411b362009-09-25 16:07:19 -07001181
1182 if (bm_fo > b->bm_bits) {
1183 dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001184 bm_fo = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001185 } else {
1186 while (bm_fo < b->bm_bits) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001187 /* bit offset of the first bit in the page */
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001188 bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001189 p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190
1191 if (find_zero_bit)
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001192 i = generic_find_next_zero_le_bit(p_addr,
1193 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194 else
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001195 i = generic_find_next_le_bit(p_addr,
1196 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001197
1198 __bm_unmap(p_addr, km);
1199 if (i < PAGE_SIZE*8) {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001200 bm_fo = bit_offset + i;
1201 if (bm_fo >= b->bm_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202 break;
1203 goto found;
1204 }
1205 bm_fo = bit_offset + PAGE_SIZE*8;
1206 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001207 bm_fo = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001208 }
1209 found:
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001210 return bm_fo;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001211}
1212
1213static unsigned long bm_find_next(struct drbd_conf *mdev,
1214 unsigned long bm_fo, const int find_zero_bit)
1215{
1216 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001217 unsigned long i = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001218
1219 ERR_IF(!b) return i;
1220 ERR_IF(!b->bm_pages) return i;
1221
1222 spin_lock_irq(&b->bm_lock);
1223 if (bm_is_locked(b))
1224 bm_print_lock_info(mdev);
1225
1226 i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);
1227
1228 spin_unlock_irq(&b->bm_lock);
1229 return i;
1230}
1231
1232unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1233{
1234 return bm_find_next(mdev, bm_fo, 0);
1235}
1236
1237#if 0
1238/* not yet needed for anything. */
1239unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1240{
1241 return bm_find_next(mdev, bm_fo, 1);
1242}
1243#endif
1244
1245/* does not spin_lock_irqsave.
1246 * you must take drbd_bm_lock() first */
1247unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1248{
1249 /* WARN_ON(!bm_is_locked(mdev)); */
1250 return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
1251}
1252
1253unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1254{
1255 /* WARN_ON(!bm_is_locked(mdev)); */
1256 return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
1257}
1258
1259/* returns number of bits actually changed.
1260 * for val != 0, we change 0 -> 1, return code positive
1261 * for val == 0, we change 1 -> 0, return code negative
1262 * wants bitnr, not sector.
1263 * expected to be called for only a few bits (e - s about BITS_PER_LONG).
1264 * Must hold bitmap lock already. */
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +02001265static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001266 unsigned long e, int val, const enum km_type km)
1267{
1268 struct drbd_bitmap *b = mdev->bitmap;
1269 unsigned long *p_addr = NULL;
1270 unsigned long bitnr;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001271 unsigned int last_page_nr = -1U;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001272 int c = 0;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001273 int changed_total = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274
1275 if (e >= b->bm_bits) {
1276 dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
1277 s, e, b->bm_bits);
1278 e = b->bm_bits ? b->bm_bits -1 : 0;
1279 }
1280 for (bitnr = s; bitnr <= e; bitnr++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001281 unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001282 if (page_nr != last_page_nr) {
1283 if (p_addr)
1284 __bm_unmap(p_addr, km);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001285 if (c < 0)
1286 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1287 else if (c > 0)
1288 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1289 changed_total += c;
1290 c = 0;
1291 p_addr = __bm_map_pidx(b, page_nr, km);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001292 last_page_nr = page_nr;
1293 }
1294 if (val)
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001295 c += (0 == generic___test_and_set_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001296 else
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001297 c -= (0 != generic___test_and_clear_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001298 }
1299 if (p_addr)
1300 __bm_unmap(p_addr, km);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001301 if (c < 0)
1302 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1303 else if (c > 0)
1304 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1305 changed_total += c;
1306 b->bm_set += changed_total;
1307 return changed_total;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001308}
1309
1310/* returns number of bits actually changed.
1311 * for val != 0, we change 0 -> 1, return code positive
1312 * for val == 0, we change 1 -> 0, return code negative
1313 * wants bitnr, not sector */
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +02001314static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001315 const unsigned long e, int val)
1316{
1317 unsigned long flags;
1318 struct drbd_bitmap *b = mdev->bitmap;
1319 int c = 0;
1320
1321 ERR_IF(!b) return 1;
1322 ERR_IF(!b->bm_pages) return 0;
1323
1324 spin_lock_irqsave(&b->bm_lock, flags);
1325 if (bm_is_locked(b))
1326 bm_print_lock_info(mdev);
1327
1328 c = __bm_change_bits_to(mdev, s, e, val, KM_IRQ1);
1329
1330 spin_unlock_irqrestore(&b->bm_lock, flags);
1331 return c;
1332}
1333
1334/* returns number of bits changed 0 -> 1 */
1335int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1336{
1337 return bm_change_bits_to(mdev, s, e, 1);
1338}
1339
1340/* returns number of bits changed 1 -> 0 */
1341int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1342{
1343 return -bm_change_bits_to(mdev, s, e, 0);
1344}
1345
1346/* sets all bits in full words,
1347 * from first_word up to, but not including, last_word */
1348static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
1349 int page_nr, int first_word, int last_word)
1350{
1351 int i;
1352 int bits;
1353 unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_USER0);
1354 for (i = first_word; i < last_word; i++) {
1355 bits = hweight_long(paddr[i]);
1356 paddr[i] = ~0UL;
1357 b->bm_set += BITS_PER_LONG - bits;
1358 }
1359 kunmap_atomic(paddr, KM_USER0);
1360}
1361
1362/* Same thing as drbd_bm_set_bits, but without taking the spin_lock_irqsave.
1363 * You must first drbd_bm_lock().
1364 * Can be called to set the whole bitmap in one go.
1365 * Sets bits from s to e _inclusive_. */
1366void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1367{
1368 /* First set_bit from the first bit (s)
1369 * up to the next long boundary (sl),
1370 * then assign full words up to the last long boundary (el),
1371 * then set_bit up to and including the last bit (e).
1372 *
1373 * Do not use memset, because we must account for changes,
1374 * so we need to loop over the words with hweight() anyways.
1375 */
1376 unsigned long sl = ALIGN(s,BITS_PER_LONG);
1377 unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
1378 int first_page;
1379 int last_page;
1380 int page_nr;
1381 int first_word;
1382 int last_word;
1383
1384 if (e - s <= 3*BITS_PER_LONG) {
1385 /* don't bother; el and sl may even be wrong. */
1386 __bm_change_bits_to(mdev, s, e, 1, KM_USER0);
1387 return;
1388 }
1389
1390 /* difference is large enough that we can trust sl and el */
1391
1392 /* bits filling the current long */
1393 if (sl)
1394 __bm_change_bits_to(mdev, s, sl-1, 1, KM_USER0);
1395
1396 first_page = sl >> (3 + PAGE_SHIFT);
1397 last_page = el >> (3 + PAGE_SHIFT);
1398
1399 /* MLPP: modulo longs per page */
1400 /* LWPP: long words per page */
1401 first_word = MLPP(sl >> LN2_BPL);
1402 last_word = LWPP;
1403
1404 /* first and full pages, unless first page == last page */
1405 for (page_nr = first_page; page_nr < last_page; page_nr++) {
1406 bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
1407 cond_resched();
1408 first_word = 0;
1409 }
1410
1411 /* last page (respectively only page, for first page == last page) */
1412 last_word = MLPP(el >> LN2_BPL);
1413 bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);
1414
1415 /* possibly trailing bits.
1416 * example: (e & 63) == 63, el will be e+1.
1417 * if that even was the very last bit,
1418 * it would trigger an assert in __bm_change_bits_to()
1419 */
1420 if (el <= e)
1421 __bm_change_bits_to(mdev, el, e, 1, KM_USER0);
1422}
1423
1424/* returns bit state
1425 * wants bitnr, NOT sector.
1426 * inherently racy... area needs to be locked by means of {al,rs}_lru
1427 * 1 ... bit set
1428 * 0 ... bit not set
1429 * -1 ... first out of bounds access, stop testing for bits!
1430 */
1431int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
1432{
1433 unsigned long flags;
1434 struct drbd_bitmap *b = mdev->bitmap;
1435 unsigned long *p_addr;
1436 int i;
1437
1438 ERR_IF(!b) return 0;
1439 ERR_IF(!b->bm_pages) return 0;
1440
1441 spin_lock_irqsave(&b->bm_lock, flags);
1442 if (bm_is_locked(b))
1443 bm_print_lock_info(mdev);
1444 if (bitnr < b->bm_bits) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001445 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001446 i = generic_test_le_bit(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001447 bm_unmap(p_addr);
1448 } else if (bitnr == b->bm_bits) {
1449 i = -1;
1450 } else { /* (bitnr > b->bm_bits) */
1451 dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
1452 i = 0;
1453 }
1454
1455 spin_unlock_irqrestore(&b->bm_lock, flags);
1456 return i;
1457}
1458
1459/* returns number of bits set in the range [s, e] */
1460int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1461{
1462 unsigned long flags;
1463 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001464 unsigned long *p_addr = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001465 unsigned long bitnr;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001466 unsigned int page_nr = -1U;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001467 int c = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001468
1469 /* If this is called without a bitmap, that is a bug. But just to be
1470 * robust in case we screwed up elsewhere, in that case pretend there
1471 * was one dirty bit in the requested area, so we won't try to do a
1472 * local read there (no bitmap probably implies no disk) */
1473 ERR_IF(!b) return 1;
1474 ERR_IF(!b->bm_pages) return 1;
1475
1476 spin_lock_irqsave(&b->bm_lock, flags);
1477 if (bm_is_locked(b))
1478 bm_print_lock_info(mdev);
1479 for (bitnr = s; bitnr <= e; bitnr++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001480 unsigned int idx = bm_bit_to_page_idx(b, bitnr);
1481 if (page_nr != idx) {
1482 page_nr = idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001483 if (p_addr)
1484 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001485 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001486 }
1487 ERR_IF (bitnr >= b->bm_bits) {
1488 dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
1489 } else {
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001490 c += (0 != generic_test_le_bit(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001491 }
1492 }
1493 if (p_addr)
1494 bm_unmap(p_addr);
1495 spin_unlock_irqrestore(&b->bm_lock, flags);
1496 return c;
1497}
1498
1499
1500/* inherently racy...
1501 * return value may be already out-of-date when this function returns.
1502 * but the general usage is that this is only use during a cstate when bits are
1503 * only cleared, not set, and typically only care for the case when the return
1504 * value is zero, or we already "locked" this "bitmap extent" by other means.
1505 *
1506 * enr is bm-extent number, since we chose to name one sector (512 bytes)
1507 * worth of the bitmap a "bitmap extent".
1508 *
1509 * TODO
1510 * I think since we use it like a reference count, we should use the real
1511 * reference count of some bitmap extent element from some lru instead...
1512 *
1513 */
1514int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
1515{
1516 struct drbd_bitmap *b = mdev->bitmap;
1517 int count, s, e;
1518 unsigned long flags;
1519 unsigned long *p_addr, *bm;
1520
1521 ERR_IF(!b) return 0;
1522 ERR_IF(!b->bm_pages) return 0;
1523
1524 spin_lock_irqsave(&b->bm_lock, flags);
1525 if (bm_is_locked(b))
1526 bm_print_lock_info(mdev);
1527
1528 s = S2W(enr);
1529 e = min((size_t)S2W(enr+1), b->bm_words);
1530 count = 0;
1531 if (s < b->bm_words) {
1532 int n = e-s;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001533 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001534 bm = p_addr + MLPP(s);
1535 while (n--)
1536 count += hweight_long(*bm++);
1537 bm_unmap(p_addr);
1538 } else {
1539 dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
1540 }
1541 spin_unlock_irqrestore(&b->bm_lock, flags);
1542 return count;
1543}
1544
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001545/* Set all bits covered by the AL-extent al_enr.
1546 * Returns number of bits changed. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001547unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
1548{
1549 struct drbd_bitmap *b = mdev->bitmap;
1550 unsigned long *p_addr, *bm;
1551 unsigned long weight;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001552 unsigned long s, e;
1553 int count, i, do_now;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001554 ERR_IF(!b) return 0;
1555 ERR_IF(!b->bm_pages) return 0;
1556
1557 spin_lock_irq(&b->bm_lock);
1558 if (bm_is_locked(b))
1559 bm_print_lock_info(mdev);
1560 weight = b->bm_set;
1561
1562 s = al_enr * BM_WORDS_PER_AL_EXT;
1563 e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
1564 /* assert that s and e are on the same page */
1565 D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
1566 == s >> (PAGE_SHIFT - LN2_BPL + 3));
1567 count = 0;
1568 if (s < b->bm_words) {
1569 i = do_now = e-s;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001570 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001571 bm = p_addr + MLPP(s);
1572 while (i--) {
1573 count += hweight_long(*bm);
1574 *bm = -1UL;
1575 bm++;
1576 }
1577 bm_unmap(p_addr);
1578 b->bm_set += do_now*BITS_PER_LONG - count;
1579 if (e == b->bm_words)
1580 b->bm_set -= bm_clear_surplus(b);
1581 } else {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001582 dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001583 }
1584 weight = b->bm_set - weight;
1585 spin_unlock_irq(&b->bm_lock);
1586 return weight;
1587}