blob: 76210ba401ac4d3e8871c4eac99336aa67d899cd [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>
Stephen Rothwellf0ff1352011-03-17 15:02:51 +010031
Philipp Reisnerb411b362009-09-25 16:07:19 -070032#include "drbd_int.h"
33
Lars Ellenberg95a0f102010-12-15 08:59:09 +010034
Philipp Reisnerb411b362009-09-25 16:07:19 -070035/* OPAQUE outside this file!
36 * interface defined in drbd_int.h
37
38 * convention:
39 * function name drbd_bm_... => used elsewhere, "public".
40 * function name bm_... => internal to implementation, "private".
Lars Ellenberg4b0715f2010-12-14 15:13:04 +010041 */
Philipp Reisnerb411b362009-09-25 16:07:19 -070042
Lars Ellenberg4b0715f2010-12-14 15:13:04 +010043
44/*
45 * LIMITATIONS:
46 * We want to support >= peta byte of backend storage, while for now still using
47 * a granularity of one bit per 4KiB of storage.
48 * 1 << 50 bytes backend storage (1 PiB)
49 * 1 << (50 - 12) bits needed
50 * 38 --> we need u64 to index and count bits
51 * 1 << (38 - 3) bitmap bytes needed
52 * 35 --> we still need u64 to index and count bytes
53 * (that's 32 GiB of bitmap for 1 PiB storage)
54 * 1 << (35 - 2) 32bit longs needed
55 * 33 --> we'd even need u64 to index and count 32bit long words.
56 * 1 << (35 - 3) 64bit longs needed
57 * 32 --> we could get away with a 32bit unsigned int to index and count
58 * 64bit long words, but I rather stay with unsigned long for now.
59 * We probably should neither count nor point to bytes or long words
60 * directly, but either by bitnumber, or by page index and offset.
61 * 1 << (35 - 12)
62 * 22 --> we need that much 4KiB pages of bitmap.
63 * 1 << (22 + 3) --> on a 64bit arch,
64 * we need 32 MiB to store the array of page pointers.
65 *
66 * Because I'm lazy, and because the resulting patch was too large, too ugly
67 * and still incomplete, on 32bit we still "only" support 16 TiB (minus some),
68 * (1 << 32) bits * 4k storage.
69 *
70
71 * bitmap storage and IO:
72 * Bitmap is stored little endian on disk, and is kept little endian in
73 * core memory. Currently we still hold the full bitmap in core as long
74 * as we are "attached" to a local disk, which at 32 GiB for 1PiB storage
75 * seems excessive.
76 *
77 * We plan to reduce the amount of in-core bitmap pages by pageing them in
78 * and out against their on-disk location as necessary, but need to make
79 * sure we don't cause too much meta data IO, and must not deadlock in
80 * tight memory situations. This needs some more work.
Philipp Reisnerb411b362009-09-25 16:07:19 -070081 */
82
83/*
84 * NOTE
85 * Access to the *bm_pages is protected by bm_lock.
86 * It is safe to read the other members within the lock.
87 *
88 * drbd_bm_set_bits is called from bio_endio callbacks,
89 * We may be called with irq already disabled,
90 * so we need spin_lock_irqsave().
91 * And we need the kmap_atomic.
92 */
93struct drbd_bitmap {
94 struct page **bm_pages;
95 spinlock_t bm_lock;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +010096
97 /* see LIMITATIONS: above */
98
Philipp Reisnerb411b362009-09-25 16:07:19 -070099 unsigned long bm_set; /* nr of set bits; THINK maybe atomic_t? */
100 unsigned long bm_bits;
101 size_t bm_words;
102 size_t bm_number_of_pages;
103 sector_t bm_dev_capacity;
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000104 struct mutex bm_change; /* serializes resize operations */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700105
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100106 wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700107
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100108 enum bm_flag bm_flags;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700109
110 /* debugging aid, in case we are still racy somewhere */
111 char *bm_why;
112 struct task_struct *bm_task;
113};
114
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +0200115static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Philipp Reisnerfd764382010-04-01 09:57:40 +0200116 unsigned long e, int val, const enum km_type km);
117
Philipp Reisnerb411b362009-09-25 16:07:19 -0700118#define bm_print_lock_info(m) __bm_print_lock_info(m, __func__)
119static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func)
120{
121 struct drbd_bitmap *b = mdev->bitmap;
122 if (!__ratelimit(&drbd_ratelimit_state))
123 return;
124 dev_err(DEV, "FIXME %s in %s, bitmap 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 func, b->bm_why ?: "?",
129 b->bm_task == mdev->receiver.task ? "receiver" :
130 b->bm_task == mdev->asender.task ? "asender" :
131 b->bm_task == mdev->worker.task ? "worker" : "?");
132}
133
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100134void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700135{
136 struct drbd_bitmap *b = mdev->bitmap;
137 int trylock_failed;
138
139 if (!b) {
140 dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n");
141 return;
142 }
143
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000144 trylock_failed = !mutex_trylock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700145
146 if (trylock_failed) {
147 dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n",
148 current == mdev->receiver.task ? "receiver" :
149 current == mdev->asender.task ? "asender" :
150 current == mdev->worker.task ? "worker" : current->comm,
151 why, b->bm_why ?: "?",
152 b->bm_task == mdev->receiver.task ? "receiver" :
153 b->bm_task == mdev->asender.task ? "asender" :
154 b->bm_task == mdev->worker.task ? "worker" : "?");
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000155 mutex_lock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700156 }
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100157 if (BM_LOCKED_MASK & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700158 dev_err(DEV, "FIXME bitmap already locked in bm_lock\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100159 b->bm_flags |= flags & BM_LOCKED_MASK;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700160
161 b->bm_why = why;
162 b->bm_task = current;
163}
164
165void drbd_bm_unlock(struct drbd_conf *mdev)
166{
167 struct drbd_bitmap *b = mdev->bitmap;
168 if (!b) {
169 dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n");
170 return;
171 }
172
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100173 if (!(BM_LOCKED_MASK & mdev->bitmap->bm_flags))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700174 dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");
175
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100176 b->bm_flags &= ~BM_LOCKED_MASK;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700177 b->bm_why = NULL;
178 b->bm_task = NULL;
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000179 mutex_unlock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700180}
181
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100182/* we store some "meta" info about our pages in page->private */
183/* at a granularity of 4k storage per bitmap bit:
184 * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks
185 * 1<<38 bits,
186 * 1<<23 4k bitmap pages.
187 * Use 24 bits as page index, covers 2 peta byte storage
188 * at a granularity of 4k per bit.
189 * Used to report the failed page idx on io error from the endio handlers.
190 */
191#define BM_PAGE_IDX_MASK ((1UL<<24)-1)
192/* this page is currently read in, or written back */
193#define BM_PAGE_IO_LOCK 31
194/* if there has been an IO error for this page */
195#define BM_PAGE_IO_ERROR 30
196/* this is to be able to intelligently skip disk IO,
197 * set if bits have been set since last IO. */
198#define BM_PAGE_NEED_WRITEOUT 29
199/* to mark for lazy writeout once syncer cleared all clearable bits,
200 * we if bits have been cleared since last IO. */
201#define BM_PAGE_LAZY_WRITEOUT 28
202
203/* store_page_idx uses non-atomic assingment. It is only used directly after
204 * allocating the page. All other bm_set_page_* and bm_clear_page_* need to
205 * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap
206 * changes) may happen from various contexts, and wait_on_bit/wake_up_bit
207 * requires it all to be atomic as well. */
208static void bm_store_page_idx(struct page *page, unsigned long idx)
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100209{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100210 BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
211 page_private(page) |= idx;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100212}
213
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100214static unsigned long bm_page_to_idx(struct page *page)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700215{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100216 return page_private(page) & BM_PAGE_IDX_MASK;
217}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700218
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100219/* As is very unlikely that the same page is under IO from more than one
220 * context, we can get away with a bit per page and one wait queue per bitmap.
221 */
222static void bm_page_lock_io(struct drbd_conf *mdev, int page_nr)
223{
224 struct drbd_bitmap *b = mdev->bitmap;
225 void *addr = &page_private(b->bm_pages[page_nr]);
226 wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
227}
228
229static void bm_page_unlock_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 clear_bit(BM_PAGE_IO_LOCK, addr);
234 smp_mb__after_clear_bit();
235 wake_up(&mdev->bitmap->bm_io_wait);
236}
237
238/* set _before_ submit_io, so it may be reset due to being changed
239 * while this page is in flight... will get submitted later again */
240static void bm_set_page_unchanged(struct page *page)
241{
242 /* use cmpxchg? */
243 clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
244 clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
245}
246
247static void bm_set_page_need_writeout(struct page *page)
248{
249 set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
250}
251
252static int bm_test_page_unchanged(struct page *page)
253{
254 volatile const unsigned long *addr = &page_private(page);
255 return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0;
256}
257
258static void bm_set_page_io_err(struct page *page)
259{
260 set_bit(BM_PAGE_IO_ERROR, &page_private(page));
261}
262
263static void bm_clear_page_io_err(struct page *page)
264{
265 clear_bit(BM_PAGE_IO_ERROR, &page_private(page));
266}
267
268static void bm_set_page_lazy_writeout(struct page *page)
269{
270 set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
271}
272
273static int bm_test_page_lazy_writeout(struct page *page)
274{
275 return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
276}
277
278/* on a 32bit box, this would allow for exactly (2<<38) bits. */
279static unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr)
280{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700281 /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100282 unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700283 BUG_ON(page_nr >= b->bm_number_of_pages);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100284 return page_nr;
285}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700286
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100287static unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr)
288{
289 /* page_nr = (bitnr/8) >> PAGE_SHIFT; */
290 unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3);
291 BUG_ON(page_nr >= b->bm_number_of_pages);
292 return page_nr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700293}
294
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100295static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx, const enum km_type km)
296{
297 struct page *page = b->bm_pages[idx];
298 return (unsigned long *) kmap_atomic(page, km);
299}
300
301static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
302{
303 return __bm_map_pidx(b, idx, KM_IRQ1);
304}
305
Philipp Reisnerb411b362009-09-25 16:07:19 -0700306static void __bm_unmap(unsigned long *p_addr, const enum km_type km)
307{
308 kunmap_atomic(p_addr, km);
309};
310
311static void bm_unmap(unsigned long *p_addr)
312{
313 return __bm_unmap(p_addr, KM_IRQ1);
314}
315
316/* long word offset of _bitmap_ sector */
317#define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
318/* word offset from start of bitmap to word number _in_page_
319 * modulo longs per page
320#define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
321 hm, well, Philipp thinks gcc might not optimze the % into & (... - 1)
322 so do it explicitly:
323 */
324#define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
325
326/* Long words per page */
327#define LWPP (PAGE_SIZE/sizeof(long))
328
329/*
330 * actually most functions herein should take a struct drbd_bitmap*, not a
331 * struct drbd_conf*, but for the debug macros I like to have the mdev around
332 * to be able to report device specific.
333 */
334
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100335
Philipp Reisnerb411b362009-09-25 16:07:19 -0700336static void bm_free_pages(struct page **pages, unsigned long number)
337{
338 unsigned long i;
339 if (!pages)
340 return;
341
342 for (i = 0; i < number; i++) {
343 if (!pages[i]) {
344 printk(KERN_ALERT "drbd: bm_free_pages tried to free "
345 "a NULL pointer; i=%lu n=%lu\n",
346 i, number);
347 continue;
348 }
349 __free_page(pages[i]);
350 pages[i] = NULL;
351 }
352}
353
354static void bm_vk_free(void *ptr, int v)
355{
356 if (v)
357 vfree(ptr);
358 else
359 kfree(ptr);
360}
361
362/*
363 * "have" and "want" are NUMBER OF PAGES.
364 */
365static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
366{
367 struct page **old_pages = b->bm_pages;
368 struct page **new_pages, *page;
369 unsigned int i, bytes, vmalloced = 0;
370 unsigned long have = b->bm_number_of_pages;
371
372 BUG_ON(have == 0 && old_pages != NULL);
373 BUG_ON(have != 0 && old_pages == NULL);
374
375 if (have == want)
376 return old_pages;
377
378 /* Trying kmalloc first, falling back to vmalloc.
379 * GFP_KERNEL is ok, as this is done when a lower level disk is
380 * "attached" to the drbd. Context is receiver thread or cqueue
381 * thread. As we have no disk yet, we are not in the IO path,
382 * not even the IO path of the peer. */
383 bytes = sizeof(struct page *)*want;
384 new_pages = kmalloc(bytes, GFP_KERNEL);
385 if (!new_pages) {
386 new_pages = vmalloc(bytes);
387 if (!new_pages)
388 return NULL;
389 vmalloced = 1;
390 }
391
392 memset(new_pages, 0, bytes);
393 if (want >= have) {
394 for (i = 0; i < have; i++)
395 new_pages[i] = old_pages[i];
396 for (; i < want; i++) {
397 page = alloc_page(GFP_HIGHUSER);
398 if (!page) {
399 bm_free_pages(new_pages + have, i - have);
400 bm_vk_free(new_pages, vmalloced);
401 return NULL;
402 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100403 /* we want to know which page it is
404 * from the endio handlers */
405 bm_store_page_idx(page, i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700406 new_pages[i] = page;
407 }
408 } else {
409 for (i = 0; i < want; i++)
410 new_pages[i] = old_pages[i];
411 /* NOT HERE, we are outside the spinlock!
412 bm_free_pages(old_pages + want, have - want);
413 */
414 }
415
416 if (vmalloced)
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100417 b->bm_flags |= BM_P_VMALLOCED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700418 else
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100419 b->bm_flags &= ~BM_P_VMALLOCED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700420
421 return new_pages;
422}
423
424/*
425 * called on driver init only. TODO call when a device is created.
426 * allocates the drbd_bitmap, and stores it in mdev->bitmap.
427 */
428int drbd_bm_init(struct drbd_conf *mdev)
429{
430 struct drbd_bitmap *b = mdev->bitmap;
431 WARN_ON(b != NULL);
432 b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
433 if (!b)
434 return -ENOMEM;
435 spin_lock_init(&b->bm_lock);
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000436 mutex_init(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700437 init_waitqueue_head(&b->bm_io_wait);
438
439 mdev->bitmap = b;
440
441 return 0;
442}
443
444sector_t drbd_bm_capacity(struct drbd_conf *mdev)
445{
446 ERR_IF(!mdev->bitmap) return 0;
447 return mdev->bitmap->bm_dev_capacity;
448}
449
450/* called on driver unload. TODO: call when a device is destroyed.
451 */
452void drbd_bm_cleanup(struct drbd_conf *mdev)
453{
454 ERR_IF (!mdev->bitmap) return;
455 bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100456 bm_vk_free(mdev->bitmap->bm_pages, (BM_P_VMALLOCED & mdev->bitmap->bm_flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700457 kfree(mdev->bitmap);
458 mdev->bitmap = NULL;
459}
460
461/*
462 * since (b->bm_bits % BITS_PER_LONG) != 0,
463 * this masks out the remaining bits.
464 * Returns the number of bits cleared.
465 */
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100466#define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3))
467#define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1)
468#define BITS_PER_LONG_MASK (BITS_PER_LONG - 1)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700469static int bm_clear_surplus(struct drbd_bitmap *b)
470{
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100471 unsigned long mask;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700472 unsigned long *p_addr, *bm;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100473 int tmp;
474 int cleared = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700475
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100476 /* number of bits modulo bits per page */
477 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
478 /* mask the used bits of the word containing the last bit */
479 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
480 /* bitmap is always stored little endian,
481 * on disk and in core memory alike */
482 mask = cpu_to_lel(mask);
483
Lars Ellenberg6850c442010-12-16 00:32:38 +0100484 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100485 bm = p_addr + (tmp/BITS_PER_LONG);
486 if (mask) {
487 /* If mask != 0, we are not exactly aligned, so bm now points
488 * to the long containing the last bit.
489 * If mask == 0, bm already points to the word immediately
490 * after the last (long word aligned) bit. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700491 cleared = hweight_long(*bm & ~mask);
492 *bm &= mask;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100493 bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700494 }
495
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100496 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
497 /* on a 32bit arch, we may need to zero out
498 * a padding long to align with a 64bit remote */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700499 cleared += hweight_long(*bm);
500 *bm = 0;
501 }
502 bm_unmap(p_addr);
503 return cleared;
504}
505
506static void bm_set_surplus(struct drbd_bitmap *b)
507{
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100508 unsigned long mask;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700509 unsigned long *p_addr, *bm;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100510 int tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700511
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100512 /* number of bits modulo bits per page */
513 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
514 /* mask the used bits of the word containing the last bit */
515 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
516 /* bitmap is always stored little endian,
517 * on disk and in core memory alike */
518 mask = cpu_to_lel(mask);
519
Lars Ellenberg6850c442010-12-16 00:32:38 +0100520 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100521 bm = p_addr + (tmp/BITS_PER_LONG);
522 if (mask) {
523 /* If mask != 0, we are not exactly aligned, so bm now points
524 * to the long containing the last bit.
525 * If mask == 0, bm already points to the word immediately
526 * after the last (long word aligned) bit. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700527 *bm |= ~mask;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100528 bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700529 }
530
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100531 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
532 /* on a 32bit arch, we may need to zero out
533 * a padding long to align with a 64bit remote */
534 *bm = ~0UL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700535 }
536 bm_unmap(p_addr);
537}
538
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100539/* you better not modify the bitmap while this is running,
540 * or its results will be stale */
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100541static unsigned long bm_count_bits(struct drbd_bitmap *b)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700542{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100543 unsigned long *p_addr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700544 unsigned long bits = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100545 unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1;
Lars Ellenberg6850c442010-12-16 00:32:38 +0100546 int idx, i, last_word;
Lars Ellenberg7777a8b2010-12-15 23:21:39 +0100547
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100548 /* all but last page */
Lars Ellenberg6850c442010-12-16 00:32:38 +0100549 for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100550 p_addr = __bm_map_pidx(b, idx, KM_USER0);
551 for (i = 0; i < LWPP; i++)
552 bits += hweight_long(p_addr[i]);
Lars Ellenberg7777a8b2010-12-15 23:21:39 +0100553 __bm_unmap(p_addr, KM_USER0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700554 cond_resched();
555 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100556 /* last (or only) page */
557 last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL;
558 p_addr = __bm_map_pidx(b, idx, KM_USER0);
559 for (i = 0; i < last_word; i++)
560 bits += hweight_long(p_addr[i]);
561 p_addr[last_word] &= cpu_to_lel(mask);
562 bits += hweight_long(p_addr[last_word]);
563 /* 32bit arch, may have an unused padding long */
564 if (BITS_PER_LONG == 32 && (last_word & 1) == 0)
565 p_addr[last_word+1] = 0;
566 __bm_unmap(p_addr, KM_USER0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700567 return bits;
568}
569
Philipp Reisnerb411b362009-09-25 16:07:19 -0700570/* offset and len in long words.*/
571static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
572{
573 unsigned long *p_addr, *bm;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100574 unsigned int idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700575 size_t do_now, end;
576
Philipp Reisnerb411b362009-09-25 16:07:19 -0700577 end = offset + len;
578
579 if (end > b->bm_words) {
580 printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
581 return;
582 }
583
584 while (offset < end) {
585 do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100586 idx = bm_word_to_page_idx(b, offset);
587 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700588 bm = p_addr + MLPP(offset);
589 if (bm+do_now > p_addr + LWPP) {
590 printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
591 p_addr, bm, (int)do_now);
Lars Ellenberg84e7c0f2010-12-16 00:37:57 +0100592 } else
593 memset(bm, c, do_now * sizeof(long));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700594 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100595 bm_set_page_need_writeout(b->bm_pages[idx]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700596 offset += do_now;
597 }
598}
599
600/*
601 * make sure the bitmap has enough room for the attached storage,
602 * if necessary, resize.
603 * called whenever we may have changed the device size.
604 * returns -ENOMEM if we could not allocate enough memory, 0 on success.
605 * In case this is actually a resize, we copy the old bitmap into the new one.
606 * Otherwise, the bitmap is initialized to all bits set.
607 */
Philipp Reisner02d9a942010-03-24 16:23:03 +0100608int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700609{
610 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg6850c442010-12-16 00:32:38 +0100611 unsigned long bits, words, owords, obits;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700612 unsigned long want, have, onpages; /* number of pages */
613 struct page **npages, **opages = NULL;
614 int err = 0, growing;
615 int opages_vmalloced;
616
617 ERR_IF(!b) return -ENOMEM;
618
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100619 drbd_bm_lock(mdev, "resize", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700620
621 dev_info(DEV, "drbd_bm_resize called with capacity == %llu\n",
622 (unsigned long long)capacity);
623
624 if (capacity == b->bm_dev_capacity)
625 goto out;
626
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100627 opages_vmalloced = (BM_P_VMALLOCED & b->bm_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700628
629 if (capacity == 0) {
630 spin_lock_irq(&b->bm_lock);
631 opages = b->bm_pages;
632 onpages = b->bm_number_of_pages;
633 owords = b->bm_words;
634 b->bm_pages = NULL;
635 b->bm_number_of_pages =
636 b->bm_set =
637 b->bm_bits =
638 b->bm_words =
639 b->bm_dev_capacity = 0;
640 spin_unlock_irq(&b->bm_lock);
641 bm_free_pages(opages, onpages);
642 bm_vk_free(opages, opages_vmalloced);
643 goto out;
644 }
645 bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));
646
647 /* if we would use
648 words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
649 a 32bit host could present the wrong number of words
650 to a 64bit host.
651 */
652 words = ALIGN(bits, 64) >> LN2_BPL;
653
654 if (get_ldev(mdev)) {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100655 u64 bits_on_disk = ((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700656 put_ldev(mdev);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100657 if (bits > bits_on_disk) {
658 dev_info(DEV, "bits = %lu\n", bits);
659 dev_info(DEV, "bits_on_disk = %llu\n", bits_on_disk);
660 err = -ENOSPC;
661 goto out;
662 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700663 }
664
Lars Ellenberg6850c442010-12-16 00:32:38 +0100665 want = ALIGN(words*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 have = b->bm_number_of_pages;
667 if (want == have) {
668 D_ASSERT(b->bm_pages != NULL);
669 npages = b->bm_pages;
670 } else {
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100671 if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700672 npages = NULL;
673 else
674 npages = bm_realloc_pages(b, want);
675 }
676
677 if (!npages) {
678 err = -ENOMEM;
679 goto out;
680 }
681
682 spin_lock_irq(&b->bm_lock);
683 opages = b->bm_pages;
684 owords = b->bm_words;
685 obits = b->bm_bits;
686
687 growing = bits > obits;
Philipp Reisner52236712010-04-28 14:46:57 +0200688 if (opages && growing && set_new_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700689 bm_set_surplus(b);
690
691 b->bm_pages = npages;
692 b->bm_number_of_pages = want;
693 b->bm_bits = bits;
694 b->bm_words = words;
695 b->bm_dev_capacity = capacity;
696
697 if (growing) {
Philipp Reisner02d9a942010-03-24 16:23:03 +0100698 if (set_new_bits) {
699 bm_memset(b, owords, 0xff, words-owords);
700 b->bm_set += bits - obits;
701 } else
702 bm_memset(b, owords, 0x00, words-owords);
703
Philipp Reisnerb411b362009-09-25 16:07:19 -0700704 }
705
706 if (want < have) {
707 /* implicit: (opages != NULL) && (opages != npages) */
708 bm_free_pages(opages + want, have - want);
709 }
710
Philipp Reisnerb411b362009-09-25 16:07:19 -0700711 (void)bm_clear_surplus(b);
712
713 spin_unlock_irq(&b->bm_lock);
714 if (opages != npages)
715 bm_vk_free(opages, opages_vmalloced);
716 if (!growing)
717 b->bm_set = bm_count_bits(b);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100718 dev_info(DEV, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700719
720 out:
721 drbd_bm_unlock(mdev);
722 return err;
723}
724
725/* inherently racy:
726 * if not protected by other means, return value may be out of date when
727 * leaving this function...
728 * we still need to lock it, since it is important that this returns
729 * bm_set == 0 precisely.
730 *
731 * maybe bm_set should be atomic_t ?
732 */
Philipp Reisner07782862010-08-31 12:00:50 +0200733unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700734{
735 struct drbd_bitmap *b = mdev->bitmap;
736 unsigned long s;
737 unsigned long flags;
738
739 ERR_IF(!b) return 0;
740 ERR_IF(!b->bm_pages) return 0;
741
742 spin_lock_irqsave(&b->bm_lock, flags);
743 s = b->bm_set;
744 spin_unlock_irqrestore(&b->bm_lock, flags);
745
746 return s;
747}
748
749unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
750{
751 unsigned long s;
752 /* if I don't have a disk, I don't know about out-of-sync status */
753 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
754 return 0;
755 s = _drbd_bm_total_weight(mdev);
756 put_ldev(mdev);
757 return s;
758}
759
760size_t drbd_bm_words(struct drbd_conf *mdev)
761{
762 struct drbd_bitmap *b = mdev->bitmap;
763 ERR_IF(!b) return 0;
764 ERR_IF(!b->bm_pages) return 0;
765
766 return b->bm_words;
767}
768
769unsigned long drbd_bm_bits(struct drbd_conf *mdev)
770{
771 struct drbd_bitmap *b = mdev->bitmap;
772 ERR_IF(!b) return 0;
773
774 return b->bm_bits;
775}
776
777/* merge number words from buffer into the bitmap starting at offset.
778 * buffer[i] is expected to be little endian unsigned long.
779 * bitmap must be locked by drbd_bm_lock.
780 * currently only used from receive_bitmap.
781 */
782void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
783 unsigned long *buffer)
784{
785 struct drbd_bitmap *b = mdev->bitmap;
786 unsigned long *p_addr, *bm;
787 unsigned long word, bits;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100788 unsigned int idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700789 size_t end, do_now;
790
791 end = offset + number;
792
793 ERR_IF(!b) return;
794 ERR_IF(!b->bm_pages) return;
795 if (number == 0)
796 return;
797 WARN_ON(offset >= b->bm_words);
798 WARN_ON(end > b->bm_words);
799
800 spin_lock_irq(&b->bm_lock);
801 while (offset < end) {
802 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100803 idx = bm_word_to_page_idx(b, offset);
804 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700805 bm = p_addr + MLPP(offset);
806 offset += do_now;
807 while (do_now--) {
808 bits = hweight_long(*bm);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100809 word = *bm | *buffer++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700810 *bm++ = word;
811 b->bm_set += hweight_long(word) - bits;
812 }
813 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100814 bm_set_page_need_writeout(b->bm_pages[idx]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700815 }
816 /* with 32bit <-> 64bit cross-platform connect
817 * this is only correct for current usage,
818 * where we _know_ that we are 64 bit aligned,
819 * and know that this function is used in this way, too...
820 */
821 if (end == b->bm_words)
822 b->bm_set -= bm_clear_surplus(b);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700823 spin_unlock_irq(&b->bm_lock);
824}
825
826/* copy number words from the bitmap starting at offset into the buffer.
827 * buffer[i] will be little endian unsigned long.
828 */
829void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
830 unsigned long *buffer)
831{
832 struct drbd_bitmap *b = mdev->bitmap;
833 unsigned long *p_addr, *bm;
834 size_t end, do_now;
835
836 end = offset + number;
837
838 ERR_IF(!b) return;
839 ERR_IF(!b->bm_pages) return;
840
841 spin_lock_irq(&b->bm_lock);
842 if ((offset >= b->bm_words) ||
843 (end > b->bm_words) ||
844 (number <= 0))
845 dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n",
846 (unsigned long) offset,
847 (unsigned long) number,
848 (unsigned long) b->bm_words);
849 else {
850 while (offset < end) {
851 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100852 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700853 bm = p_addr + MLPP(offset);
854 offset += do_now;
855 while (do_now--)
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100856 *buffer++ = *bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857 bm_unmap(p_addr);
858 }
859 }
860 spin_unlock_irq(&b->bm_lock);
861}
862
863/* set all bits in the bitmap */
864void drbd_bm_set_all(struct drbd_conf *mdev)
865{
866 struct drbd_bitmap *b = mdev->bitmap;
867 ERR_IF(!b) return;
868 ERR_IF(!b->bm_pages) return;
869
870 spin_lock_irq(&b->bm_lock);
871 bm_memset(b, 0, 0xff, b->bm_words);
872 (void)bm_clear_surplus(b);
873 b->bm_set = b->bm_bits;
874 spin_unlock_irq(&b->bm_lock);
875}
876
877/* clear all bits in the bitmap */
878void drbd_bm_clear_all(struct drbd_conf *mdev)
879{
880 struct drbd_bitmap *b = mdev->bitmap;
881 ERR_IF(!b) return;
882 ERR_IF(!b->bm_pages) return;
883
884 spin_lock_irq(&b->bm_lock);
885 bm_memset(b, 0, 0, b->bm_words);
886 b->bm_set = 0;
887 spin_unlock_irq(&b->bm_lock);
888}
889
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100890struct bm_aio_ctx {
891 struct drbd_conf *mdev;
892 atomic_t in_flight;
Lars Ellenberg725a97e2010-12-19 11:29:55 +0100893 struct completion done;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100894 unsigned flags;
895#define BM_AIO_COPY_PAGES 1
896 int error;
897};
898
899/* bv_page may be a copy, or may be the original */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700900static void bm_async_io_complete(struct bio *bio, int error)
901{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100902 struct bm_aio_ctx *ctx = bio->bi_private;
903 struct drbd_conf *mdev = ctx->mdev;
904 struct drbd_bitmap *b = mdev->bitmap;
905 unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700906 int uptodate = bio_flagged(bio, BIO_UPTODATE);
907
908
909 /* strange behavior of some lower level drivers...
910 * fail the request by clearing the uptodate flag,
911 * but do not return any error?!
912 * do we want to WARN() on this? */
913 if (!error && !uptodate)
914 error = -EIO;
915
Lars Ellenberg7648cdf2010-12-17 23:58:41 +0100916 if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 &&
917 !bm_test_page_unchanged(b->bm_pages[idx]))
918 dev_warn(DEV, "bitmap page idx %u changed during IO!\n", idx);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100919
Philipp Reisnerb411b362009-09-25 16:07:19 -0700920 if (error) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100921 /* ctx error will hold the completed-last non-zero error code,
922 * in case error codes differ. */
923 ctx->error = error;
924 bm_set_page_io_err(b->bm_pages[idx]);
925 /* Not identical to on disk version of it.
926 * Is BM_PAGE_IO_ERROR enough? */
927 if (__ratelimit(&drbd_ratelimit_state))
928 dev_err(DEV, "IO ERROR %d on bitmap page idx %u\n",
929 error, idx);
930 } else {
931 bm_clear_page_io_err(b->bm_pages[idx]);
932 dynamic_dev_dbg(DEV, "bitmap page idx %u completed\n", idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700933 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100934
935 bm_page_unlock_io(mdev, idx);
936
937 /* FIXME give back to page pool */
938 if (ctx->flags & BM_AIO_COPY_PAGES)
939 put_page(bio->bi_io_vec[0].bv_page);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700940
941 bio_put(bio);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100942
943 if (atomic_dec_and_test(&ctx->in_flight))
Lars Ellenberg725a97e2010-12-19 11:29:55 +0100944 complete(&ctx->done);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700945}
946
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100947static 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 -0700948{
949 /* we are process context. we always get a bio */
950 struct bio *bio = bio_alloc(GFP_KERNEL, 1);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100951 struct drbd_conf *mdev = ctx->mdev;
952 struct drbd_bitmap *b = mdev->bitmap;
953 struct page *page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700954 unsigned int len;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100955
Philipp Reisnerb411b362009-09-25 16:07:19 -0700956 sector_t on_disk_sector =
957 mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset;
958 on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);
959
960 /* this might happen with very small
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100961 * flexible external meta data device,
962 * or with PAGE_SIZE > 4k */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963 len = min_t(unsigned int, PAGE_SIZE,
964 (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);
965
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100966 /* serialize IO on this page */
967 bm_page_lock_io(mdev, page_nr);
968 /* before memcpy and submit,
969 * so it can be redirtied any time */
970 bm_set_page_unchanged(b->bm_pages[page_nr]);
971
972 if (ctx->flags & BM_AIO_COPY_PAGES) {
973 /* FIXME alloc_page is good enough for now, but actually needs
974 * to use pre-allocated page pool */
975 void *src, *dest;
976 page = alloc_page(__GFP_HIGHMEM|__GFP_WAIT);
977 dest = kmap_atomic(page, KM_USER0);
978 src = kmap_atomic(b->bm_pages[page_nr], KM_USER1);
979 memcpy(dest, src, PAGE_SIZE);
980 kunmap_atomic(src, KM_USER1);
981 kunmap_atomic(dest, KM_USER0);
982 bm_store_page_idx(page, page_nr);
983 } else
984 page = b->bm_pages[page_nr];
985
Philipp Reisnerb411b362009-09-25 16:07:19 -0700986 bio->bi_bdev = mdev->ldev->md_bdev;
987 bio->bi_sector = on_disk_sector;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100988 bio_add_page(bio, page, len, 0);
989 bio->bi_private = ctx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700990 bio->bi_end_io = bm_async_io_complete;
991
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100992 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700993 bio->bi_rw |= rw;
994 bio_endio(bio, -EIO);
995 } else {
996 submit_bio(rw, bio);
997 }
998}
999
Philipp Reisnerb411b362009-09-25 16:07:19 -07001000/*
1001 * bm_rw: read/write the whole bitmap from/to its on disk location.
1002 */
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001003static int bm_rw(struct drbd_conf *mdev, int rw, unsigned lazy_writeout_upper_idx) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001004{
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001005 struct bm_aio_ctx ctx = {
1006 .mdev = mdev,
1007 .in_flight = ATOMIC_INIT(1),
1008 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1009 .flags = lazy_writeout_upper_idx ? BM_AIO_COPY_PAGES : 0,
1010 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001011 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg6850c442010-12-16 00:32:38 +01001012 int num_pages, i, count = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001013 unsigned long now;
1014 char ppb[10];
1015 int err = 0;
1016
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001017 /*
1018 * We are protected against bitmap disappearing/resizing by holding an
1019 * ldev reference (caller must have called get_ldev()).
1020 * For read/write, we are protected against changes to the bitmap by
1021 * the bitmap lock (see drbd_bitmap_io).
1022 * For lazy writeout, we don't care for ongoing changes to the bitmap,
1023 * as we submit copies of pages anyways.
1024 */
1025 if (!ctx.flags)
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001026 WARN_ON(!(BM_LOCKED_MASK & b->bm_flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001027
Lars Ellenberg6850c442010-12-16 00:32:38 +01001028 num_pages = b->bm_number_of_pages;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001029
Philipp Reisnerb411b362009-09-25 16:07:19 -07001030 now = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001031
1032 /* let the layers below us try to merge these bios... */
Lars Ellenberg6850c442010-12-16 00:32:38 +01001033 for (i = 0; i < num_pages; i++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001034 /* ignore completely unchanged pages */
1035 if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
1036 break;
1037 if (rw & WRITE) {
1038 if (bm_test_page_unchanged(b->bm_pages[i])) {
1039 dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
1040 continue;
1041 }
1042 /* during lazy writeout,
1043 * ignore those pages not marked for lazy writeout. */
1044 if (lazy_writeout_upper_idx &&
1045 !bm_test_page_lazy_writeout(b->bm_pages[i])) {
1046 dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
1047 continue;
1048 }
1049 }
1050 atomic_inc(&ctx.in_flight);
1051 bm_page_io_async(&ctx, i, rw);
1052 ++count;
1053 cond_resched();
1054 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001055
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001056 /*
1057 * We initialize ctx.in_flight to one to make sure bm_async_io_complete
1058 * will not complete() early, and decrement / test it here. If there
1059 * are still some bios in flight, we need to wait for them here.
1060 */
1061 if (!atomic_dec_and_test(&ctx.in_flight))
1062 wait_for_completion(&ctx.done);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001063 dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
1064 rw == WRITE ? "WRITE" : "READ",
1065 count, jiffies - now);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001066
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001067 if (ctx.error) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001068 dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001069 drbd_chk_io_error(mdev, 1, true);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001070 err = -EIO; /* ctx.error ? */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001071 }
1072
1073 now = jiffies;
1074 if (rw == WRITE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075 drbd_md_flush(mdev);
1076 } else /* rw == READ */ {
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001077 b->bm_set = bm_count_bits(b);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001078 dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
1079 jiffies - now);
1080 }
1081 now = b->bm_set;
1082
1083 dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
1084 ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
1085
1086 return err;
1087}
1088
1089/**
1090 * drbd_bm_read() - Read the whole bitmap from its on disk location.
1091 * @mdev: DRBD device.
1092 */
1093int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
1094{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001095 return bm_rw(mdev, READ, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001096}
1097
1098/**
1099 * drbd_bm_write() - Write the whole bitmap to its on disk location.
1100 * @mdev: DRBD device.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001101 *
1102 * Will only write pages that have changed since last IO.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001103 */
1104int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
1105{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001106 return bm_rw(mdev, WRITE, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001107}
1108
1109/**
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001110 * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001111 * @mdev: DRBD device.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001112 * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
Philipp Reisnerb411b362009-09-25 16:07:19 -07001113 */
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001114int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001115{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001116 return bm_rw(mdev, WRITE, upper_idx);
1117}
Philipp Reisnerb411b362009-09-25 16:07:19 -07001118
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001119
1120/**
1121 * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
1122 * @mdev: DRBD device.
1123 * @idx: bitmap page index
1124 *
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001125 * We don't want to special case on logical_block_size of the backend device,
1126 * so we submit PAGE_SIZE aligned pieces.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001127 * Note that on "most" systems, PAGE_SIZE is 4k.
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001128 *
1129 * In case this becomes an issue on systems with larger PAGE_SIZE,
1130 * we may want to change this again to write 4k aligned 4k pieces.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001131 */
1132int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
1133{
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001134 struct bm_aio_ctx ctx = {
1135 .mdev = mdev,
1136 .in_flight = ATOMIC_INIT(1),
1137 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1138 .flags = BM_AIO_COPY_PAGES,
1139 };
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001140
1141 if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
Lars Ellenberg7648cdf2010-12-17 23:58:41 +01001142 dynamic_dev_dbg(DEV, "skipped bm page write for idx %u\n", idx);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001143 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001144 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001145
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001146 bm_page_io_async(&ctx, idx, WRITE_SYNC);
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001147 wait_for_completion(&ctx.done);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001148
1149 if (ctx.error)
1150 drbd_chk_io_error(mdev, 1, true);
1151 /* that should force detach, so the in memory bitmap will be
1152 * gone in a moment as well. */
1153
Philipp Reisnerb411b362009-09-25 16:07:19 -07001154 mdev->bm_writ_cnt++;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001155 return ctx.error;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001156}
1157
1158/* NOTE
1159 * find_first_bit returns int, we return unsigned long.
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001160 * For this to work on 32bit arch with bitnumbers > (1<<32),
1161 * we'd need to return u64, and get a whole lot of other places
1162 * fixed where we still use unsigned long.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001163 *
1164 * this returns a bit number, NOT a sector!
1165 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001166static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
1167 const int find_zero_bit, const enum km_type km)
1168{
1169 struct drbd_bitmap *b = mdev->bitmap;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170 unsigned long *p_addr;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001171 unsigned long bit_offset;
1172 unsigned i;
1173
Philipp Reisnerb411b362009-09-25 16:07:19 -07001174
1175 if (bm_fo > b->bm_bits) {
1176 dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001177 bm_fo = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001178 } else {
1179 while (bm_fo < b->bm_bits) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001180 /* bit offset of the first bit in the page */
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001181 bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001182 p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001183
1184 if (find_zero_bit)
Linus Torvalds7e599e62011-03-28 07:42:58 -07001185 i = find_next_zero_bit_le(p_addr,
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001186 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001187 else
Linus Torvalds7e599e62011-03-28 07:42:58 -07001188 i = find_next_bit_le(p_addr,
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001189 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190
1191 __bm_unmap(p_addr, km);
1192 if (i < PAGE_SIZE*8) {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001193 bm_fo = bit_offset + i;
1194 if (bm_fo >= b->bm_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195 break;
1196 goto found;
1197 }
1198 bm_fo = bit_offset + PAGE_SIZE*8;
1199 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001200 bm_fo = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001201 }
1202 found:
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001203 return bm_fo;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001204}
1205
1206static unsigned long bm_find_next(struct drbd_conf *mdev,
1207 unsigned long bm_fo, const int find_zero_bit)
1208{
1209 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001210 unsigned long i = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001211
1212 ERR_IF(!b) return i;
1213 ERR_IF(!b->bm_pages) return i;
1214
1215 spin_lock_irq(&b->bm_lock);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001216 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001217 bm_print_lock_info(mdev);
1218
1219 i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);
1220
1221 spin_unlock_irq(&b->bm_lock);
1222 return i;
1223}
1224
1225unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1226{
1227 return bm_find_next(mdev, bm_fo, 0);
1228}
1229
1230#if 0
1231/* not yet needed for anything. */
1232unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1233{
1234 return bm_find_next(mdev, bm_fo, 1);
1235}
1236#endif
1237
1238/* does not spin_lock_irqsave.
1239 * you must take drbd_bm_lock() first */
1240unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1241{
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001242 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243 return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
1244}
1245
1246unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1247{
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001248 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001249 return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
1250}
1251
1252/* returns number of bits actually changed.
1253 * for val != 0, we change 0 -> 1, return code positive
1254 * for val == 0, we change 1 -> 0, return code negative
1255 * wants bitnr, not sector.
1256 * expected to be called for only a few bits (e - s about BITS_PER_LONG).
1257 * Must hold bitmap lock already. */
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +02001258static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001259 unsigned long e, int val, const enum km_type km)
1260{
1261 struct drbd_bitmap *b = mdev->bitmap;
1262 unsigned long *p_addr = NULL;
1263 unsigned long bitnr;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001264 unsigned int last_page_nr = -1U;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001265 int c = 0;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001266 int changed_total = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001267
1268 if (e >= b->bm_bits) {
1269 dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
1270 s, e, b->bm_bits);
1271 e = b->bm_bits ? b->bm_bits -1 : 0;
1272 }
1273 for (bitnr = s; bitnr <= e; bitnr++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001274 unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001275 if (page_nr != last_page_nr) {
1276 if (p_addr)
1277 __bm_unmap(p_addr, km);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001278 if (c < 0)
1279 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1280 else if (c > 0)
1281 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1282 changed_total += c;
1283 c = 0;
1284 p_addr = __bm_map_pidx(b, page_nr, km);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001285 last_page_nr = page_nr;
1286 }
1287 if (val)
Linus Torvalds7e599e62011-03-28 07:42:58 -07001288 c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001289 else
Linus Torvalds7e599e62011-03-28 07:42:58 -07001290 c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001291 }
1292 if (p_addr)
1293 __bm_unmap(p_addr, km);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001294 if (c < 0)
1295 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1296 else if (c > 0)
1297 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1298 changed_total += c;
1299 b->bm_set += changed_total;
1300 return changed_total;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001301}
1302
1303/* returns number of bits actually changed.
1304 * for val != 0, we change 0 -> 1, return code positive
1305 * for val == 0, we change 1 -> 0, return code negative
1306 * wants bitnr, not sector */
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +02001307static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001308 const unsigned long e, int val)
1309{
1310 unsigned long flags;
1311 struct drbd_bitmap *b = mdev->bitmap;
1312 int c = 0;
1313
1314 ERR_IF(!b) return 1;
1315 ERR_IF(!b->bm_pages) return 0;
1316
1317 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001318 if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001319 bm_print_lock_info(mdev);
1320
1321 c = __bm_change_bits_to(mdev, s, e, val, KM_IRQ1);
1322
1323 spin_unlock_irqrestore(&b->bm_lock, flags);
1324 return c;
1325}
1326
1327/* returns number of bits changed 0 -> 1 */
1328int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1329{
1330 return bm_change_bits_to(mdev, s, e, 1);
1331}
1332
1333/* returns number of bits changed 1 -> 0 */
1334int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1335{
1336 return -bm_change_bits_to(mdev, s, e, 0);
1337}
1338
1339/* sets all bits in full words,
1340 * from first_word up to, but not including, last_word */
1341static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
1342 int page_nr, int first_word, int last_word)
1343{
1344 int i;
1345 int bits;
1346 unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_USER0);
1347 for (i = first_word; i < last_word; i++) {
1348 bits = hweight_long(paddr[i]);
1349 paddr[i] = ~0UL;
1350 b->bm_set += BITS_PER_LONG - bits;
1351 }
1352 kunmap_atomic(paddr, KM_USER0);
1353}
1354
1355/* Same thing as drbd_bm_set_bits, but without taking the spin_lock_irqsave.
1356 * You must first drbd_bm_lock().
1357 * Can be called to set the whole bitmap in one go.
1358 * Sets bits from s to e _inclusive_. */
1359void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1360{
1361 /* First set_bit from the first bit (s)
1362 * up to the next long boundary (sl),
1363 * then assign full words up to the last long boundary (el),
1364 * then set_bit up to and including the last bit (e).
1365 *
1366 * Do not use memset, because we must account for changes,
1367 * so we need to loop over the words with hweight() anyways.
1368 */
1369 unsigned long sl = ALIGN(s,BITS_PER_LONG);
1370 unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
1371 int first_page;
1372 int last_page;
1373 int page_nr;
1374 int first_word;
1375 int last_word;
1376
1377 if (e - s <= 3*BITS_PER_LONG) {
1378 /* don't bother; el and sl may even be wrong. */
1379 __bm_change_bits_to(mdev, s, e, 1, KM_USER0);
1380 return;
1381 }
1382
1383 /* difference is large enough that we can trust sl and el */
1384
1385 /* bits filling the current long */
1386 if (sl)
1387 __bm_change_bits_to(mdev, s, sl-1, 1, KM_USER0);
1388
1389 first_page = sl >> (3 + PAGE_SHIFT);
1390 last_page = el >> (3 + PAGE_SHIFT);
1391
1392 /* MLPP: modulo longs per page */
1393 /* LWPP: long words per page */
1394 first_word = MLPP(sl >> LN2_BPL);
1395 last_word = LWPP;
1396
1397 /* first and full pages, unless first page == last page */
1398 for (page_nr = first_page; page_nr < last_page; page_nr++) {
1399 bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
1400 cond_resched();
1401 first_word = 0;
1402 }
1403
1404 /* last page (respectively only page, for first page == last page) */
1405 last_word = MLPP(el >> LN2_BPL);
1406 bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);
1407
1408 /* possibly trailing bits.
1409 * example: (e & 63) == 63, el will be e+1.
1410 * if that even was the very last bit,
1411 * it would trigger an assert in __bm_change_bits_to()
1412 */
1413 if (el <= e)
1414 __bm_change_bits_to(mdev, el, e, 1, KM_USER0);
1415}
1416
1417/* returns bit state
1418 * wants bitnr, NOT sector.
1419 * inherently racy... area needs to be locked by means of {al,rs}_lru
1420 * 1 ... bit set
1421 * 0 ... bit not set
1422 * -1 ... first out of bounds access, stop testing for bits!
1423 */
1424int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
1425{
1426 unsigned long flags;
1427 struct drbd_bitmap *b = mdev->bitmap;
1428 unsigned long *p_addr;
1429 int i;
1430
1431 ERR_IF(!b) return 0;
1432 ERR_IF(!b->bm_pages) return 0;
1433
1434 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001435 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001436 bm_print_lock_info(mdev);
1437 if (bitnr < b->bm_bits) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001438 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
Linus Torvalds7e599e62011-03-28 07:42:58 -07001439 i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001440 bm_unmap(p_addr);
1441 } else if (bitnr == b->bm_bits) {
1442 i = -1;
1443 } else { /* (bitnr > b->bm_bits) */
1444 dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
1445 i = 0;
1446 }
1447
1448 spin_unlock_irqrestore(&b->bm_lock, flags);
1449 return i;
1450}
1451
1452/* returns number of bits set in the range [s, e] */
1453int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1454{
1455 unsigned long flags;
1456 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001457 unsigned long *p_addr = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001458 unsigned long bitnr;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001459 unsigned int page_nr = -1U;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001460 int c = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001461
1462 /* If this is called without a bitmap, that is a bug. But just to be
1463 * robust in case we screwed up elsewhere, in that case pretend there
1464 * was one dirty bit in the requested area, so we won't try to do a
1465 * local read there (no bitmap probably implies no disk) */
1466 ERR_IF(!b) return 1;
1467 ERR_IF(!b->bm_pages) return 1;
1468
1469 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001470 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471 bm_print_lock_info(mdev);
1472 for (bitnr = s; bitnr <= e; bitnr++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001473 unsigned int idx = bm_bit_to_page_idx(b, bitnr);
1474 if (page_nr != idx) {
1475 page_nr = idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001476 if (p_addr)
1477 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001478 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001479 }
1480 ERR_IF (bitnr >= b->bm_bits) {
1481 dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
1482 } else {
Linus Torvalds7e599e62011-03-28 07:42:58 -07001483 c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001484 }
1485 }
1486 if (p_addr)
1487 bm_unmap(p_addr);
1488 spin_unlock_irqrestore(&b->bm_lock, flags);
1489 return c;
1490}
1491
1492
1493/* inherently racy...
1494 * return value may be already out-of-date when this function returns.
1495 * but the general usage is that this is only use during a cstate when bits are
1496 * only cleared, not set, and typically only care for the case when the return
1497 * value is zero, or we already "locked" this "bitmap extent" by other means.
1498 *
1499 * enr is bm-extent number, since we chose to name one sector (512 bytes)
1500 * worth of the bitmap a "bitmap extent".
1501 *
1502 * TODO
1503 * I think since we use it like a reference count, we should use the real
1504 * reference count of some bitmap extent element from some lru instead...
1505 *
1506 */
1507int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
1508{
1509 struct drbd_bitmap *b = mdev->bitmap;
1510 int count, s, e;
1511 unsigned long flags;
1512 unsigned long *p_addr, *bm;
1513
1514 ERR_IF(!b) return 0;
1515 ERR_IF(!b->bm_pages) return 0;
1516
1517 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001518 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001519 bm_print_lock_info(mdev);
1520
1521 s = S2W(enr);
1522 e = min((size_t)S2W(enr+1), b->bm_words);
1523 count = 0;
1524 if (s < b->bm_words) {
1525 int n = e-s;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001526 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001527 bm = p_addr + MLPP(s);
1528 while (n--)
1529 count += hweight_long(*bm++);
1530 bm_unmap(p_addr);
1531 } else {
1532 dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
1533 }
1534 spin_unlock_irqrestore(&b->bm_lock, flags);
1535 return count;
1536}
1537
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001538/* Set all bits covered by the AL-extent al_enr.
1539 * Returns number of bits changed. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001540unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
1541{
1542 struct drbd_bitmap *b = mdev->bitmap;
1543 unsigned long *p_addr, *bm;
1544 unsigned long weight;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001545 unsigned long s, e;
1546 int count, i, do_now;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001547 ERR_IF(!b) return 0;
1548 ERR_IF(!b->bm_pages) return 0;
1549
1550 spin_lock_irq(&b->bm_lock);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001551 if (BM_DONT_SET & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001552 bm_print_lock_info(mdev);
1553 weight = b->bm_set;
1554
1555 s = al_enr * BM_WORDS_PER_AL_EXT;
1556 e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
1557 /* assert that s and e are on the same page */
1558 D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
1559 == s >> (PAGE_SHIFT - LN2_BPL + 3));
1560 count = 0;
1561 if (s < b->bm_words) {
1562 i = do_now = e-s;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001563 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001564 bm = p_addr + MLPP(s);
1565 while (i--) {
1566 count += hweight_long(*bm);
1567 *bm = -1UL;
1568 bm++;
1569 }
1570 bm_unmap(p_addr);
1571 b->bm_set += do_now*BITS_PER_LONG - count;
1572 if (e == b->bm_words)
1573 b->bm_set -= bm_clear_surplus(b);
1574 } else {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001575 dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576 }
1577 weight = b->bm_set - weight;
1578 spin_unlock_irq(&b->bm_lock);
1579 return weight;
1580}