blob: 4da4c322fa5670bc0398272f055532d8c69fb0d0 [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 *
Bart Van Assche24c48302011-05-21 18:32:29 +020077 * We plan to reduce the amount of in-core bitmap pages by paging them in
Lars Ellenberg4b0715f2010-12-14 15:13:04 +010078 * 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 Reisnerb411b362009-09-25 16:07:19 -0700115#define bm_print_lock_info(m) __bm_print_lock_info(m, __func__)
116static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func)
117{
118 struct drbd_bitmap *b = mdev->bitmap;
119 if (!__ratelimit(&drbd_ratelimit_state))
120 return;
121 dev_err(DEV, "FIXME %s in %s, bitmap locked for '%s' by %s\n",
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100122 current == mdev->tconn->receiver.task ? "receiver" :
123 current == mdev->tconn->asender.task ? "asender" :
124 current == mdev->tconn->worker.task ? "worker" : current->comm,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700125 func, b->bm_why ?: "?",
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100126 b->bm_task == mdev->tconn->receiver.task ? "receiver" :
127 b->bm_task == mdev->tconn->asender.task ? "asender" :
128 b->bm_task == mdev->tconn->worker.task ? "worker" : "?");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700129}
130
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100131void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700132{
133 struct drbd_bitmap *b = mdev->bitmap;
134 int trylock_failed;
135
136 if (!b) {
137 dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n");
138 return;
139 }
140
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000141 trylock_failed = !mutex_trylock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700142
143 if (trylock_failed) {
144 dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n",
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100145 current == mdev->tconn->receiver.task ? "receiver" :
146 current == mdev->tconn->asender.task ? "asender" :
147 current == mdev->tconn->worker.task ? "worker" : current->comm,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700148 why, b->bm_why ?: "?",
Philipp Reisnere6b3ea82011-01-19 14:02:01 +0100149 b->bm_task == mdev->tconn->receiver.task ? "receiver" :
150 b->bm_task == mdev->tconn->asender.task ? "asender" :
151 b->bm_task == mdev->tconn->worker.task ? "worker" : "?");
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000152 mutex_lock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700153 }
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100154 if (BM_LOCKED_MASK & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700155 dev_err(DEV, "FIXME bitmap already locked in bm_lock\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100156 b->bm_flags |= flags & BM_LOCKED_MASK;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700157
158 b->bm_why = why;
159 b->bm_task = current;
160}
161
162void drbd_bm_unlock(struct drbd_conf *mdev)
163{
164 struct drbd_bitmap *b = mdev->bitmap;
165 if (!b) {
166 dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n");
167 return;
168 }
169
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100170 if (!(BM_LOCKED_MASK & mdev->bitmap->bm_flags))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700171 dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");
172
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100173 b->bm_flags &= ~BM_LOCKED_MASK;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700174 b->bm_why = NULL;
175 b->bm_task = NULL;
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000176 mutex_unlock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700177}
178
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100179/* we store some "meta" info about our pages in page->private */
180/* at a granularity of 4k storage per bitmap bit:
181 * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks
182 * 1<<38 bits,
183 * 1<<23 4k bitmap pages.
184 * Use 24 bits as page index, covers 2 peta byte storage
185 * at a granularity of 4k per bit.
186 * Used to report the failed page idx on io error from the endio handlers.
187 */
188#define BM_PAGE_IDX_MASK ((1UL<<24)-1)
189/* this page is currently read in, or written back */
190#define BM_PAGE_IO_LOCK 31
191/* if there has been an IO error for this page */
192#define BM_PAGE_IO_ERROR 30
193/* this is to be able to intelligently skip disk IO,
194 * set if bits have been set since last IO. */
195#define BM_PAGE_NEED_WRITEOUT 29
196/* to mark for lazy writeout once syncer cleared all clearable bits,
197 * we if bits have been cleared since last IO. */
198#define BM_PAGE_LAZY_WRITEOUT 28
199
Bart Van Assche24c48302011-05-21 18:32:29 +0200200/* store_page_idx uses non-atomic assignment. It is only used directly after
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100201 * allocating the page. All other bm_set_page_* and bm_clear_page_* need to
202 * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap
203 * changes) may happen from various contexts, and wait_on_bit/wake_up_bit
204 * requires it all to be atomic as well. */
205static void bm_store_page_idx(struct page *page, unsigned long idx)
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100206{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100207 BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
208 page_private(page) |= idx;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100209}
210
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100211static unsigned long bm_page_to_idx(struct page *page)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700212{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100213 return page_private(page) & BM_PAGE_IDX_MASK;
214}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700215
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100216/* As is very unlikely that the same page is under IO from more than one
217 * context, we can get away with a bit per page and one wait queue per bitmap.
218 */
219static void bm_page_lock_io(struct drbd_conf *mdev, int page_nr)
220{
221 struct drbd_bitmap *b = mdev->bitmap;
222 void *addr = &page_private(b->bm_pages[page_nr]);
223 wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
224}
225
226static void bm_page_unlock_io(struct drbd_conf *mdev, int page_nr)
227{
228 struct drbd_bitmap *b = mdev->bitmap;
229 void *addr = &page_private(b->bm_pages[page_nr]);
230 clear_bit(BM_PAGE_IO_LOCK, addr);
231 smp_mb__after_clear_bit();
232 wake_up(&mdev->bitmap->bm_io_wait);
233}
234
235/* set _before_ submit_io, so it may be reset due to being changed
236 * while this page is in flight... will get submitted later again */
237static void bm_set_page_unchanged(struct page *page)
238{
239 /* use cmpxchg? */
240 clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
241 clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
242}
243
244static void bm_set_page_need_writeout(struct page *page)
245{
246 set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
247}
248
249static int bm_test_page_unchanged(struct page *page)
250{
251 volatile const unsigned long *addr = &page_private(page);
252 return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0;
253}
254
255static void bm_set_page_io_err(struct page *page)
256{
257 set_bit(BM_PAGE_IO_ERROR, &page_private(page));
258}
259
260static void bm_clear_page_io_err(struct page *page)
261{
262 clear_bit(BM_PAGE_IO_ERROR, &page_private(page));
263}
264
265static void bm_set_page_lazy_writeout(struct page *page)
266{
267 set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
268}
269
270static int bm_test_page_lazy_writeout(struct page *page)
271{
272 return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
273}
274
275/* on a 32bit box, this would allow for exactly (2<<38) bits. */
276static unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr)
277{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700278 /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100279 unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700280 BUG_ON(page_nr >= b->bm_number_of_pages);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100281 return page_nr;
282}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700283
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100284static unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr)
285{
286 /* page_nr = (bitnr/8) >> PAGE_SHIFT; */
287 unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3);
288 BUG_ON(page_nr >= b->bm_number_of_pages);
289 return page_nr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700290}
291
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100292static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx, const enum km_type km)
293{
294 struct page *page = b->bm_pages[idx];
295 return (unsigned long *) kmap_atomic(page, km);
296}
297
298static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
299{
300 return __bm_map_pidx(b, idx, KM_IRQ1);
301}
302
Philipp Reisnerb411b362009-09-25 16:07:19 -0700303static void __bm_unmap(unsigned long *p_addr, const enum km_type km)
304{
305 kunmap_atomic(p_addr, km);
306};
307
308static void bm_unmap(unsigned long *p_addr)
309{
310 return __bm_unmap(p_addr, KM_IRQ1);
311}
312
313/* long word offset of _bitmap_ sector */
314#define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
315/* word offset from start of bitmap to word number _in_page_
316 * modulo longs per page
317#define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
Bart Van Assche24c48302011-05-21 18:32:29 +0200318 hm, well, Philipp thinks gcc might not optimize the % into & (... - 1)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700319 so do it explicitly:
320 */
321#define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
322
323/* Long words per page */
324#define LWPP (PAGE_SIZE/sizeof(long))
325
326/*
327 * actually most functions herein should take a struct drbd_bitmap*, not a
328 * struct drbd_conf*, but for the debug macros I like to have the mdev around
329 * to be able to report device specific.
330 */
331
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100332
Philipp Reisnerb411b362009-09-25 16:07:19 -0700333static void bm_free_pages(struct page **pages, unsigned long number)
334{
335 unsigned long i;
336 if (!pages)
337 return;
338
339 for (i = 0; i < number; i++) {
340 if (!pages[i]) {
341 printk(KERN_ALERT "drbd: bm_free_pages tried to free "
342 "a NULL pointer; i=%lu n=%lu\n",
343 i, number);
344 continue;
345 }
346 __free_page(pages[i]);
347 pages[i] = NULL;
348 }
349}
350
351static void bm_vk_free(void *ptr, int v)
352{
353 if (v)
354 vfree(ptr);
355 else
356 kfree(ptr);
357}
358
359/*
360 * "have" and "want" are NUMBER OF PAGES.
361 */
362static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
363{
364 struct page **old_pages = b->bm_pages;
365 struct page **new_pages, *page;
366 unsigned int i, bytes, vmalloced = 0;
367 unsigned long have = b->bm_number_of_pages;
368
369 BUG_ON(have == 0 && old_pages != NULL);
370 BUG_ON(have != 0 && old_pages == NULL);
371
372 if (have == want)
373 return old_pages;
374
375 /* Trying kmalloc first, falling back to vmalloc.
376 * GFP_KERNEL is ok, as this is done when a lower level disk is
377 * "attached" to the drbd. Context is receiver thread or cqueue
378 * thread. As we have no disk yet, we are not in the IO path,
379 * not even the IO path of the peer. */
380 bytes = sizeof(struct page *)*want;
381 new_pages = kmalloc(bytes, GFP_KERNEL);
382 if (!new_pages) {
383 new_pages = vmalloc(bytes);
384 if (!new_pages)
385 return NULL;
386 vmalloced = 1;
387 }
388
389 memset(new_pages, 0, bytes);
390 if (want >= have) {
391 for (i = 0; i < have; i++)
392 new_pages[i] = old_pages[i];
393 for (; i < want; i++) {
394 page = alloc_page(GFP_HIGHUSER);
395 if (!page) {
396 bm_free_pages(new_pages + have, i - have);
397 bm_vk_free(new_pages, vmalloced);
398 return NULL;
399 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100400 /* we want to know which page it is
401 * from the endio handlers */
402 bm_store_page_idx(page, i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700403 new_pages[i] = page;
404 }
405 } else {
406 for (i = 0; i < want; i++)
407 new_pages[i] = old_pages[i];
408 /* NOT HERE, we are outside the spinlock!
409 bm_free_pages(old_pages + want, have - want);
410 */
411 }
412
413 if (vmalloced)
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100414 b->bm_flags |= BM_P_VMALLOCED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700415 else
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100416 b->bm_flags &= ~BM_P_VMALLOCED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700417
418 return new_pages;
419}
420
421/*
422 * called on driver init only. TODO call when a device is created.
423 * allocates the drbd_bitmap, and stores it in mdev->bitmap.
424 */
425int drbd_bm_init(struct drbd_conf *mdev)
426{
427 struct drbd_bitmap *b = mdev->bitmap;
428 WARN_ON(b != NULL);
429 b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
430 if (!b)
431 return -ENOMEM;
432 spin_lock_init(&b->bm_lock);
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000433 mutex_init(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700434 init_waitqueue_head(&b->bm_io_wait);
435
436 mdev->bitmap = b;
437
438 return 0;
439}
440
441sector_t drbd_bm_capacity(struct drbd_conf *mdev)
442{
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100443 if (!expect(mdev->bitmap))
444 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700445 return mdev->bitmap->bm_dev_capacity;
446}
447
448/* called on driver unload. TODO: call when a device is destroyed.
449 */
450void drbd_bm_cleanup(struct drbd_conf *mdev)
451{
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100452 if (!expect(mdev->bitmap))
453 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700454 bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100455 bm_vk_free(mdev->bitmap->bm_pages, (BM_P_VMALLOCED & mdev->bitmap->bm_flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700456 kfree(mdev->bitmap);
457 mdev->bitmap = NULL;
458}
459
460/*
461 * since (b->bm_bits % BITS_PER_LONG) != 0,
462 * this masks out the remaining bits.
463 * Returns the number of bits cleared.
464 */
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100465#define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3))
466#define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1)
467#define BITS_PER_LONG_MASK (BITS_PER_LONG - 1)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700468static int bm_clear_surplus(struct drbd_bitmap *b)
469{
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100470 unsigned long mask;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700471 unsigned long *p_addr, *bm;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100472 int tmp;
473 int cleared = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700474
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100475 /* number of bits modulo bits per page */
476 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
477 /* mask the used bits of the word containing the last bit */
478 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
479 /* bitmap is always stored little endian,
480 * on disk and in core memory alike */
481 mask = cpu_to_lel(mask);
482
Lars Ellenberg6850c442010-12-16 00:32:38 +0100483 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100484 bm = p_addr + (tmp/BITS_PER_LONG);
485 if (mask) {
486 /* If mask != 0, we are not exactly aligned, so bm now points
487 * to the long containing the last bit.
488 * If mask == 0, bm already points to the word immediately
489 * after the last (long word aligned) bit. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700490 cleared = hweight_long(*bm & ~mask);
491 *bm &= mask;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100492 bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700493 }
494
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100495 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
496 /* on a 32bit arch, we may need to zero out
497 * a padding long to align with a 64bit remote */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700498 cleared += hweight_long(*bm);
499 *bm = 0;
500 }
501 bm_unmap(p_addr);
502 return cleared;
503}
504
505static void bm_set_surplus(struct drbd_bitmap *b)
506{
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100507 unsigned long mask;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700508 unsigned long *p_addr, *bm;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100509 int tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700510
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100511 /* number of bits modulo bits per page */
512 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
513 /* mask the used bits of the word containing the last bit */
514 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
515 /* bitmap is always stored little endian,
516 * on disk and in core memory alike */
517 mask = cpu_to_lel(mask);
518
Lars Ellenberg6850c442010-12-16 00:32:38 +0100519 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100520 bm = p_addr + (tmp/BITS_PER_LONG);
521 if (mask) {
522 /* If mask != 0, we are not exactly aligned, so bm now points
523 * to the long containing the last bit.
524 * If mask == 0, bm already points to the word immediately
525 * after the last (long word aligned) bit. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700526 *bm |= ~mask;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100527 bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700528 }
529
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100530 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
531 /* on a 32bit arch, we may need to zero out
532 * a padding long to align with a 64bit remote */
533 *bm = ~0UL;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700534 }
535 bm_unmap(p_addr);
536}
537
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100538/* you better not modify the bitmap while this is running,
539 * or its results will be stale */
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100540static unsigned long bm_count_bits(struct drbd_bitmap *b)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700541{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100542 unsigned long *p_addr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700543 unsigned long bits = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100544 unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1;
Lars Ellenberg6850c442010-12-16 00:32:38 +0100545 int idx, i, last_word;
Lars Ellenberg7777a8b2010-12-15 23:21:39 +0100546
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100547 /* all but last page */
Lars Ellenberg6850c442010-12-16 00:32:38 +0100548 for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100549 p_addr = __bm_map_pidx(b, idx, KM_USER0);
550 for (i = 0; i < LWPP; i++)
551 bits += hweight_long(p_addr[i]);
Lars Ellenberg7777a8b2010-12-15 23:21:39 +0100552 __bm_unmap(p_addr, KM_USER0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700553 cond_resched();
554 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100555 /* last (or only) page */
556 last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL;
557 p_addr = __bm_map_pidx(b, idx, KM_USER0);
558 for (i = 0; i < last_word; i++)
559 bits += hweight_long(p_addr[i]);
560 p_addr[last_word] &= cpu_to_lel(mask);
561 bits += hweight_long(p_addr[last_word]);
562 /* 32bit arch, may have an unused padding long */
563 if (BITS_PER_LONG == 32 && (last_word & 1) == 0)
564 p_addr[last_word+1] = 0;
565 __bm_unmap(p_addr, KM_USER0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700566 return bits;
567}
568
Philipp Reisnerb411b362009-09-25 16:07:19 -0700569/* offset and len in long words.*/
570static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
571{
572 unsigned long *p_addr, *bm;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100573 unsigned int idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700574 size_t do_now, end;
575
Philipp Reisnerb411b362009-09-25 16:07:19 -0700576 end = offset + len;
577
578 if (end > b->bm_words) {
579 printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
580 return;
581 }
582
583 while (offset < end) {
584 do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100585 idx = bm_word_to_page_idx(b, offset);
586 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700587 bm = p_addr + MLPP(offset);
588 if (bm+do_now > p_addr + LWPP) {
589 printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
590 p_addr, bm, (int)do_now);
Lars Ellenberg84e7c0f2010-12-16 00:37:57 +0100591 } else
592 memset(bm, c, do_now * sizeof(long));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700593 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100594 bm_set_page_need_writeout(b->bm_pages[idx]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700595 offset += do_now;
596 }
597}
598
599/*
600 * make sure the bitmap has enough room for the attached storage,
601 * if necessary, resize.
602 * called whenever we may have changed the device size.
603 * returns -ENOMEM if we could not allocate enough memory, 0 on success.
604 * In case this is actually a resize, we copy the old bitmap into the new one.
605 * Otherwise, the bitmap is initialized to all bits set.
606 */
Philipp Reisner02d9a942010-03-24 16:23:03 +0100607int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700608{
609 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg6850c442010-12-16 00:32:38 +0100610 unsigned long bits, words, owords, obits;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700611 unsigned long want, have, onpages; /* number of pages */
612 struct page **npages, **opages = NULL;
613 int err = 0, growing;
614 int opages_vmalloced;
615
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100616 if (!expect(b))
617 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700618
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
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100739 if (!expect(b))
740 return 0;
741 if (!expect(b->bm_pages))
742 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700743
744 spin_lock_irqsave(&b->bm_lock, flags);
745 s = b->bm_set;
746 spin_unlock_irqrestore(&b->bm_lock, flags);
747
748 return s;
749}
750
751unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
752{
753 unsigned long s;
754 /* if I don't have a disk, I don't know about out-of-sync status */
755 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
756 return 0;
757 s = _drbd_bm_total_weight(mdev);
758 put_ldev(mdev);
759 return s;
760}
761
762size_t drbd_bm_words(struct drbd_conf *mdev)
763{
764 struct drbd_bitmap *b = mdev->bitmap;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100765 if (!expect(b))
766 return 0;
767 if (!expect(b->bm_pages))
768 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700769
770 return b->bm_words;
771}
772
773unsigned long drbd_bm_bits(struct drbd_conf *mdev)
774{
775 struct drbd_bitmap *b = mdev->bitmap;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100776 if (!expect(b))
777 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700778
779 return b->bm_bits;
780}
781
782/* merge number words from buffer into the bitmap starting at offset.
783 * buffer[i] is expected to be little endian unsigned long.
784 * bitmap must be locked by drbd_bm_lock.
785 * currently only used from receive_bitmap.
786 */
787void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
788 unsigned long *buffer)
789{
790 struct drbd_bitmap *b = mdev->bitmap;
791 unsigned long *p_addr, *bm;
792 unsigned long word, bits;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100793 unsigned int idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700794 size_t end, do_now;
795
796 end = offset + number;
797
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100798 if (!expect(b))
799 return;
800 if (!expect(b->bm_pages))
801 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700802 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
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100845 if (!expect(b))
846 return;
847 if (!expect(b->bm_pages))
848 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700849
850 spin_lock_irq(&b->bm_lock);
851 if ((offset >= b->bm_words) ||
852 (end > b->bm_words) ||
853 (number <= 0))
854 dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n",
855 (unsigned long) offset,
856 (unsigned long) number,
857 (unsigned long) b->bm_words);
858 else {
859 while (offset < end) {
860 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100861 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700862 bm = p_addr + MLPP(offset);
863 offset += do_now;
864 while (do_now--)
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100865 *buffer++ = *bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700866 bm_unmap(p_addr);
867 }
868 }
869 spin_unlock_irq(&b->bm_lock);
870}
871
872/* set all bits in the bitmap */
873void drbd_bm_set_all(struct drbd_conf *mdev)
874{
875 struct drbd_bitmap *b = mdev->bitmap;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100876 if (!expect(b))
877 return;
878 if (!expect(b->bm_pages))
879 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700880
881 spin_lock_irq(&b->bm_lock);
882 bm_memset(b, 0, 0xff, b->bm_words);
883 (void)bm_clear_surplus(b);
884 b->bm_set = b->bm_bits;
885 spin_unlock_irq(&b->bm_lock);
886}
887
888/* clear all bits in the bitmap */
889void drbd_bm_clear_all(struct drbd_conf *mdev)
890{
891 struct drbd_bitmap *b = mdev->bitmap;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100892 if (!expect(b))
893 return;
894 if (!expect(b->bm_pages))
895 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700896
897 spin_lock_irq(&b->bm_lock);
898 bm_memset(b, 0, 0, b->bm_words);
899 b->bm_set = 0;
900 spin_unlock_irq(&b->bm_lock);
901}
902
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100903struct bm_aio_ctx {
904 struct drbd_conf *mdev;
905 atomic_t in_flight;
Lars Ellenberg725a97e2010-12-19 11:29:55 +0100906 struct completion done;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100907 unsigned flags;
908#define BM_AIO_COPY_PAGES 1
909 int error;
910};
911
912/* bv_page may be a copy, or may be the original */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700913static void bm_async_io_complete(struct bio *bio, int error)
914{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100915 struct bm_aio_ctx *ctx = bio->bi_private;
916 struct drbd_conf *mdev = ctx->mdev;
917 struct drbd_bitmap *b = mdev->bitmap;
918 unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700919 int uptodate = bio_flagged(bio, BIO_UPTODATE);
920
921
922 /* strange behavior of some lower level drivers...
923 * fail the request by clearing the uptodate flag,
924 * but do not return any error?!
925 * do we want to WARN() on this? */
926 if (!error && !uptodate)
927 error = -EIO;
928
Lars Ellenberg7648cdf2010-12-17 23:58:41 +0100929 if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 &&
930 !bm_test_page_unchanged(b->bm_pages[idx]))
931 dev_warn(DEV, "bitmap page idx %u changed during IO!\n", idx);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100932
Philipp Reisnerb411b362009-09-25 16:07:19 -0700933 if (error) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100934 /* ctx error will hold the completed-last non-zero error code,
935 * in case error codes differ. */
936 ctx->error = error;
937 bm_set_page_io_err(b->bm_pages[idx]);
938 /* Not identical to on disk version of it.
939 * Is BM_PAGE_IO_ERROR enough? */
940 if (__ratelimit(&drbd_ratelimit_state))
941 dev_err(DEV, "IO ERROR %d on bitmap page idx %u\n",
942 error, idx);
943 } else {
944 bm_clear_page_io_err(b->bm_pages[idx]);
945 dynamic_dev_dbg(DEV, "bitmap page idx %u completed\n", idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700946 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100947
948 bm_page_unlock_io(mdev, idx);
949
950 /* FIXME give back to page pool */
951 if (ctx->flags & BM_AIO_COPY_PAGES)
952 put_page(bio->bi_io_vec[0].bv_page);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700953
954 bio_put(bio);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100955
956 if (atomic_dec_and_test(&ctx->in_flight))
Lars Ellenberg725a97e2010-12-19 11:29:55 +0100957 complete(&ctx->done);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958}
959
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100960static 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 -0700961{
962 /* we are process context. we always get a bio */
963 struct bio *bio = bio_alloc(GFP_KERNEL, 1);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100964 struct drbd_conf *mdev = ctx->mdev;
965 struct drbd_bitmap *b = mdev->bitmap;
966 struct page *page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967 unsigned int len;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100968
Philipp Reisnerb411b362009-09-25 16:07:19 -0700969 sector_t on_disk_sector =
970 mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset;
971 on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);
972
973 /* this might happen with very small
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100974 * flexible external meta data device,
975 * or with PAGE_SIZE > 4k */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700976 len = min_t(unsigned int, PAGE_SIZE,
977 (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);
978
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100979 /* serialize IO on this page */
980 bm_page_lock_io(mdev, page_nr);
981 /* before memcpy and submit,
982 * so it can be redirtied any time */
983 bm_set_page_unchanged(b->bm_pages[page_nr]);
984
985 if (ctx->flags & BM_AIO_COPY_PAGES) {
986 /* FIXME alloc_page is good enough for now, but actually needs
987 * to use pre-allocated page pool */
988 void *src, *dest;
989 page = alloc_page(__GFP_HIGHMEM|__GFP_WAIT);
990 dest = kmap_atomic(page, KM_USER0);
991 src = kmap_atomic(b->bm_pages[page_nr], KM_USER1);
992 memcpy(dest, src, PAGE_SIZE);
993 kunmap_atomic(src, KM_USER1);
994 kunmap_atomic(dest, KM_USER0);
995 bm_store_page_idx(page, page_nr);
996 } else
997 page = b->bm_pages[page_nr];
998
Philipp Reisnerb411b362009-09-25 16:07:19 -0700999 bio->bi_bdev = mdev->ldev->md_bdev;
1000 bio->bi_sector = on_disk_sector;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001001 bio_add_page(bio, page, len, 0);
1002 bio->bi_private = ctx;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001003 bio->bi_end_io = bm_async_io_complete;
1004
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +01001005 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001006 bio->bi_rw |= rw;
1007 bio_endio(bio, -EIO);
1008 } else {
1009 submit_bio(rw, bio);
Lars Ellenberg5a8b4242011-06-14 14:18:23 +02001010 /* this should not count as user activity and cause the
1011 * resync to throttle -- see drbd_rs_should_slow_down(). */
1012 atomic_add(len >> 9, &mdev->rs_sect_ev);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001013 }
1014}
1015
Philipp Reisnerb411b362009-09-25 16:07:19 -07001016/*
1017 * bm_rw: read/write the whole bitmap from/to its on disk location.
1018 */
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001019static int bm_rw(struct drbd_conf *mdev, int rw, unsigned lazy_writeout_upper_idx) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001020{
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001021 struct bm_aio_ctx ctx = {
1022 .mdev = mdev,
1023 .in_flight = ATOMIC_INIT(1),
1024 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1025 .flags = lazy_writeout_upper_idx ? BM_AIO_COPY_PAGES : 0,
1026 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001027 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg6850c442010-12-16 00:32:38 +01001028 int num_pages, i, count = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001029 unsigned long now;
1030 char ppb[10];
1031 int err = 0;
1032
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001033 /*
1034 * We are protected against bitmap disappearing/resizing by holding an
1035 * ldev reference (caller must have called get_ldev()).
1036 * For read/write, we are protected against changes to the bitmap by
1037 * the bitmap lock (see drbd_bitmap_io).
1038 * For lazy writeout, we don't care for ongoing changes to the bitmap,
1039 * as we submit copies of pages anyways.
1040 */
1041 if (!ctx.flags)
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001042 WARN_ON(!(BM_LOCKED_MASK & b->bm_flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001043
Lars Ellenberg6850c442010-12-16 00:32:38 +01001044 num_pages = b->bm_number_of_pages;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001045
Philipp Reisnerb411b362009-09-25 16:07:19 -07001046 now = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001047
1048 /* let the layers below us try to merge these bios... */
Lars Ellenberg6850c442010-12-16 00:32:38 +01001049 for (i = 0; i < num_pages; i++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001050 /* ignore completely unchanged pages */
1051 if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
1052 break;
1053 if (rw & WRITE) {
1054 if (bm_test_page_unchanged(b->bm_pages[i])) {
1055 dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
1056 continue;
1057 }
1058 /* during lazy writeout,
1059 * ignore those pages not marked for lazy writeout. */
1060 if (lazy_writeout_upper_idx &&
1061 !bm_test_page_lazy_writeout(b->bm_pages[i])) {
1062 dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
1063 continue;
1064 }
1065 }
1066 atomic_inc(&ctx.in_flight);
1067 bm_page_io_async(&ctx, i, rw);
1068 ++count;
1069 cond_resched();
1070 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001071
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001072 /*
1073 * We initialize ctx.in_flight to one to make sure bm_async_io_complete
1074 * will not complete() early, and decrement / test it here. If there
1075 * are still some bios in flight, we need to wait for them here.
1076 */
1077 if (!atomic_dec_and_test(&ctx.in_flight))
1078 wait_for_completion(&ctx.done);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001079 dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
1080 rw == WRITE ? "WRITE" : "READ",
1081 count, jiffies - now);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001083 if (ctx.error) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001084 dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001085 drbd_chk_io_error(mdev, 1, true);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001086 err = -EIO; /* ctx.error ? */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001087 }
1088
1089 now = jiffies;
1090 if (rw == WRITE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001091 drbd_md_flush(mdev);
1092 } else /* rw == READ */ {
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001093 b->bm_set = bm_count_bits(b);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001094 dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
1095 jiffies - now);
1096 }
1097 now = b->bm_set;
1098
1099 dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
1100 ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
1101
1102 return err;
1103}
1104
1105/**
1106 * drbd_bm_read() - Read the whole bitmap from its on disk location.
1107 * @mdev: DRBD device.
1108 */
1109int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
1110{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001111 return bm_rw(mdev, READ, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001112}
1113
1114/**
1115 * drbd_bm_write() - Write the whole bitmap to its on disk location.
1116 * @mdev: DRBD device.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001117 *
1118 * Will only write pages that have changed since last IO.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001119 */
1120int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
1121{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001122 return bm_rw(mdev, WRITE, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001123}
1124
1125/**
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001126 * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001127 * @mdev: DRBD device.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001128 * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
Philipp Reisnerb411b362009-09-25 16:07:19 -07001129 */
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001130int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001131{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001132 return bm_rw(mdev, WRITE, upper_idx);
1133}
Philipp Reisnerb411b362009-09-25 16:07:19 -07001134
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001135
1136/**
1137 * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
1138 * @mdev: DRBD device.
1139 * @idx: bitmap page index
1140 *
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001141 * We don't want to special case on logical_block_size of the backend device,
1142 * so we submit PAGE_SIZE aligned pieces.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001143 * Note that on "most" systems, PAGE_SIZE is 4k.
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001144 *
1145 * In case this becomes an issue on systems with larger PAGE_SIZE,
1146 * we may want to change this again to write 4k aligned 4k pieces.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001147 */
1148int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
1149{
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001150 struct bm_aio_ctx ctx = {
1151 .mdev = mdev,
1152 .in_flight = ATOMIC_INIT(1),
1153 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1154 .flags = BM_AIO_COPY_PAGES,
1155 };
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001156
1157 if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
Lars Ellenberg7648cdf2010-12-17 23:58:41 +01001158 dynamic_dev_dbg(DEV, "skipped bm page write for idx %u\n", idx);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001159 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001160 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001161
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001162 bm_page_io_async(&ctx, idx, WRITE_SYNC);
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001163 wait_for_completion(&ctx.done);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001164
1165 if (ctx.error)
1166 drbd_chk_io_error(mdev, 1, true);
1167 /* that should force detach, so the in memory bitmap will be
1168 * gone in a moment as well. */
1169
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170 mdev->bm_writ_cnt++;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001171 return ctx.error;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001172}
1173
1174/* NOTE
1175 * find_first_bit returns int, we return unsigned long.
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001176 * For this to work on 32bit arch with bitnumbers > (1<<32),
1177 * we'd need to return u64, and get a whole lot of other places
1178 * fixed where we still use unsigned long.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001179 *
1180 * this returns a bit number, NOT a sector!
1181 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001182static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
1183 const int find_zero_bit, const enum km_type km)
1184{
1185 struct drbd_bitmap *b = mdev->bitmap;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001186 unsigned long *p_addr;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001187 unsigned long bit_offset;
1188 unsigned i;
1189
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190
1191 if (bm_fo > b->bm_bits) {
1192 dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001193 bm_fo = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194 } else {
1195 while (bm_fo < b->bm_bits) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001196 /* bit offset of the first bit in the page */
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001197 bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001198 p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001199
1200 if (find_zero_bit)
Linus Torvalds7e599e62011-03-28 07:42:58 -07001201 i = find_next_zero_bit_le(p_addr,
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001202 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001203 else
Linus Torvalds7e599e62011-03-28 07:42:58 -07001204 i = find_next_bit_le(p_addr,
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001205 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001206
1207 __bm_unmap(p_addr, km);
1208 if (i < PAGE_SIZE*8) {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001209 bm_fo = bit_offset + i;
1210 if (bm_fo >= b->bm_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001211 break;
1212 goto found;
1213 }
1214 bm_fo = bit_offset + PAGE_SIZE*8;
1215 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001216 bm_fo = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001217 }
1218 found:
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001219 return bm_fo;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001220}
1221
1222static unsigned long bm_find_next(struct drbd_conf *mdev,
1223 unsigned long bm_fo, const int find_zero_bit)
1224{
1225 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001226 unsigned long i = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001227
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001228 if (!expect(b))
1229 return i;
1230 if (!expect(b->bm_pages))
1231 return i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001232
1233 spin_lock_irq(&b->bm_lock);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001234 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001235 bm_print_lock_info(mdev);
1236
1237 i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);
1238
1239 spin_unlock_irq(&b->bm_lock);
1240 return i;
1241}
1242
1243unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1244{
1245 return bm_find_next(mdev, bm_fo, 0);
1246}
1247
1248#if 0
1249/* not yet needed for anything. */
1250unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1251{
1252 return bm_find_next(mdev, bm_fo, 1);
1253}
1254#endif
1255
1256/* does not spin_lock_irqsave.
1257 * you must take drbd_bm_lock() first */
1258unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1259{
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001260 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001261 return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
1262}
1263
1264unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1265{
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001266 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001267 return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
1268}
1269
1270/* returns number of bits actually changed.
1271 * for val != 0, we change 0 -> 1, return code positive
1272 * for val == 0, we change 1 -> 0, return code negative
1273 * wants bitnr, not sector.
1274 * expected to be called for only a few bits (e - s about BITS_PER_LONG).
1275 * Must hold bitmap lock already. */
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +02001276static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Lars Ellenberg829c6082011-06-03 21:18:13 +02001277 unsigned long e, int val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001278{
1279 struct drbd_bitmap *b = mdev->bitmap;
1280 unsigned long *p_addr = NULL;
1281 unsigned long bitnr;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001282 unsigned int last_page_nr = -1U;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001283 int c = 0;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001284 int changed_total = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001285
1286 if (e >= b->bm_bits) {
1287 dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
1288 s, e, b->bm_bits);
1289 e = b->bm_bits ? b->bm_bits -1 : 0;
1290 }
1291 for (bitnr = s; bitnr <= e; bitnr++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001292 unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001293 if (page_nr != last_page_nr) {
1294 if (p_addr)
Lars Ellenberg829c6082011-06-03 21:18:13 +02001295 __bm_unmap(p_addr, KM_IRQ1);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001296 if (c < 0)
1297 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1298 else if (c > 0)
1299 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1300 changed_total += c;
1301 c = 0;
Lars Ellenberg829c6082011-06-03 21:18:13 +02001302 p_addr = __bm_map_pidx(b, page_nr, KM_IRQ1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001303 last_page_nr = page_nr;
1304 }
1305 if (val)
Linus Torvalds7e599e62011-03-28 07:42:58 -07001306 c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001307 else
Linus Torvalds7e599e62011-03-28 07:42:58 -07001308 c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001309 }
1310 if (p_addr)
Lars Ellenberg829c6082011-06-03 21:18:13 +02001311 __bm_unmap(p_addr, KM_IRQ1);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001312 if (c < 0)
1313 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1314 else if (c > 0)
1315 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1316 changed_total += c;
1317 b->bm_set += changed_total;
1318 return changed_total;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001319}
1320
1321/* returns number of bits actually changed.
1322 * for val != 0, we change 0 -> 1, return code positive
1323 * for val == 0, we change 1 -> 0, return code negative
1324 * wants bitnr, not sector */
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +02001325static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001326 const unsigned long e, int val)
1327{
1328 unsigned long flags;
1329 struct drbd_bitmap *b = mdev->bitmap;
1330 int c = 0;
1331
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001332 if (!expect(b))
1333 return 1;
1334 if (!expect(b->bm_pages))
1335 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001336
1337 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001338 if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001339 bm_print_lock_info(mdev);
1340
Lars Ellenberg829c6082011-06-03 21:18:13 +02001341 c = __bm_change_bits_to(mdev, s, e, val);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001342
1343 spin_unlock_irqrestore(&b->bm_lock, flags);
1344 return c;
1345}
1346
1347/* returns number of bits changed 0 -> 1 */
1348int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1349{
1350 return bm_change_bits_to(mdev, s, e, 1);
1351}
1352
1353/* returns number of bits changed 1 -> 0 */
1354int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1355{
1356 return -bm_change_bits_to(mdev, s, e, 0);
1357}
1358
1359/* sets all bits in full words,
1360 * from first_word up to, but not including, last_word */
1361static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
1362 int page_nr, int first_word, int last_word)
1363{
1364 int i;
1365 int bits;
Lars Ellenberg829c6082011-06-03 21:18:13 +02001366 unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_IRQ1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001367 for (i = first_word; i < last_word; i++) {
1368 bits = hweight_long(paddr[i]);
1369 paddr[i] = ~0UL;
1370 b->bm_set += BITS_PER_LONG - bits;
1371 }
Lars Ellenberg829c6082011-06-03 21:18:13 +02001372 kunmap_atomic(paddr, KM_IRQ1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001373}
1374
Lars Ellenberg829c6082011-06-03 21:18:13 +02001375/* Same thing as drbd_bm_set_bits,
1376 * but more efficient for a large bit range.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001377 * You must first drbd_bm_lock().
1378 * Can be called to set the whole bitmap in one go.
1379 * Sets bits from s to e _inclusive_. */
1380void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1381{
1382 /* First set_bit from the first bit (s)
1383 * up to the next long boundary (sl),
1384 * then assign full words up to the last long boundary (el),
1385 * then set_bit up to and including the last bit (e).
1386 *
1387 * Do not use memset, because we must account for changes,
1388 * so we need to loop over the words with hweight() anyways.
1389 */
Lars Ellenberg829c6082011-06-03 21:18:13 +02001390 struct drbd_bitmap *b = mdev->bitmap;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001391 unsigned long sl = ALIGN(s,BITS_PER_LONG);
1392 unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
1393 int first_page;
1394 int last_page;
1395 int page_nr;
1396 int first_word;
1397 int last_word;
1398
1399 if (e - s <= 3*BITS_PER_LONG) {
1400 /* don't bother; el and sl may even be wrong. */
Lars Ellenberg829c6082011-06-03 21:18:13 +02001401 spin_lock_irq(&b->bm_lock);
1402 __bm_change_bits_to(mdev, s, e, 1);
1403 spin_unlock_irq(&b->bm_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001404 return;
1405 }
1406
1407 /* difference is large enough that we can trust sl and el */
1408
Lars Ellenberg829c6082011-06-03 21:18:13 +02001409 spin_lock_irq(&b->bm_lock);
1410
Philipp Reisnerb411b362009-09-25 16:07:19 -07001411 /* bits filling the current long */
1412 if (sl)
Lars Ellenberg829c6082011-06-03 21:18:13 +02001413 __bm_change_bits_to(mdev, s, sl-1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001414
1415 first_page = sl >> (3 + PAGE_SHIFT);
1416 last_page = el >> (3 + PAGE_SHIFT);
1417
1418 /* MLPP: modulo longs per page */
1419 /* LWPP: long words per page */
1420 first_word = MLPP(sl >> LN2_BPL);
1421 last_word = LWPP;
1422
1423 /* first and full pages, unless first page == last page */
1424 for (page_nr = first_page; page_nr < last_page; page_nr++) {
1425 bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
Lars Ellenberg8ccee202011-06-06 11:31:42 +02001426 spin_unlock_irq(&b->bm_lock);
1427 cond_resched();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001428 first_word = 0;
Lars Ellenberg8ccee202011-06-06 11:31:42 +02001429 spin_lock_irq(&b->bm_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001430 }
1431
1432 /* last page (respectively only page, for first page == last page) */
1433 last_word = MLPP(el >> LN2_BPL);
1434 bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);
1435
1436 /* possibly trailing bits.
1437 * example: (e & 63) == 63, el will be e+1.
1438 * if that even was the very last bit,
1439 * it would trigger an assert in __bm_change_bits_to()
1440 */
1441 if (el <= e)
Lars Ellenberg829c6082011-06-03 21:18:13 +02001442 __bm_change_bits_to(mdev, el, e, 1);
1443 spin_unlock_irq(&b->bm_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001444}
1445
1446/* returns bit state
1447 * wants bitnr, NOT sector.
1448 * inherently racy... area needs to be locked by means of {al,rs}_lru
1449 * 1 ... bit set
1450 * 0 ... bit not set
1451 * -1 ... first out of bounds access, stop testing for bits!
1452 */
1453int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
1454{
1455 unsigned long flags;
1456 struct drbd_bitmap *b = mdev->bitmap;
1457 unsigned long *p_addr;
1458 int i;
1459
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001460 if (!expect(b))
1461 return 0;
1462 if (!expect(b->bm_pages))
1463 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001464
1465 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001466 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001467 bm_print_lock_info(mdev);
1468 if (bitnr < b->bm_bits) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001469 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
Linus Torvalds7e599e62011-03-28 07:42:58 -07001470 i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001471 bm_unmap(p_addr);
1472 } else if (bitnr == b->bm_bits) {
1473 i = -1;
1474 } else { /* (bitnr > b->bm_bits) */
1475 dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
1476 i = 0;
1477 }
1478
1479 spin_unlock_irqrestore(&b->bm_lock, flags);
1480 return i;
1481}
1482
1483/* returns number of bits set in the range [s, e] */
1484int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1485{
1486 unsigned long flags;
1487 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001488 unsigned long *p_addr = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001489 unsigned long bitnr;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001490 unsigned int page_nr = -1U;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001491 int c = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001492
1493 /* If this is called without a bitmap, that is a bug. But just to be
1494 * robust in case we screwed up elsewhere, in that case pretend there
1495 * was one dirty bit in the requested area, so we won't try to do a
1496 * local read there (no bitmap probably implies no disk) */
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001497 if (!expect(b))
1498 return 1;
1499 if (!expect(b->bm_pages))
1500 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001501
1502 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001503 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001504 bm_print_lock_info(mdev);
1505 for (bitnr = s; bitnr <= e; bitnr++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001506 unsigned int idx = bm_bit_to_page_idx(b, bitnr);
1507 if (page_nr != idx) {
1508 page_nr = idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001509 if (p_addr)
1510 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001511 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001512 }
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001513 if (expect(bitnr < b->bm_bits))
Linus Torvalds7e599e62011-03-28 07:42:58 -07001514 c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001515 else
1516 dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001517 }
1518 if (p_addr)
1519 bm_unmap(p_addr);
1520 spin_unlock_irqrestore(&b->bm_lock, flags);
1521 return c;
1522}
1523
1524
1525/* inherently racy...
1526 * return value may be already out-of-date when this function returns.
1527 * but the general usage is that this is only use during a cstate when bits are
1528 * only cleared, not set, and typically only care for the case when the return
1529 * value is zero, or we already "locked" this "bitmap extent" by other means.
1530 *
1531 * enr is bm-extent number, since we chose to name one sector (512 bytes)
1532 * worth of the bitmap a "bitmap extent".
1533 *
1534 * TODO
1535 * I think since we use it like a reference count, we should use the real
1536 * reference count of some bitmap extent element from some lru instead...
1537 *
1538 */
1539int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
1540{
1541 struct drbd_bitmap *b = mdev->bitmap;
1542 int count, s, e;
1543 unsigned long flags;
1544 unsigned long *p_addr, *bm;
1545
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001546 if (!expect(b))
1547 return 0;
1548 if (!expect(b->bm_pages))
1549 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001550
1551 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001552 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001553 bm_print_lock_info(mdev);
1554
1555 s = S2W(enr);
1556 e = min((size_t)S2W(enr+1), b->bm_words);
1557 count = 0;
1558 if (s < b->bm_words) {
1559 int n = e-s;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001560 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001561 bm = p_addr + MLPP(s);
1562 while (n--)
1563 count += hweight_long(*bm++);
1564 bm_unmap(p_addr);
1565 } else {
1566 dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
1567 }
1568 spin_unlock_irqrestore(&b->bm_lock, flags);
1569 return count;
1570}
1571
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001572/* Set all bits covered by the AL-extent al_enr.
1573 * Returns number of bits changed. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001574unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
1575{
1576 struct drbd_bitmap *b = mdev->bitmap;
1577 unsigned long *p_addr, *bm;
1578 unsigned long weight;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001579 unsigned long s, e;
1580 int count, i, do_now;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001581 if (!expect(b))
1582 return 0;
1583 if (!expect(b->bm_pages))
1584 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001585
1586 spin_lock_irq(&b->bm_lock);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001587 if (BM_DONT_SET & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001588 bm_print_lock_info(mdev);
1589 weight = b->bm_set;
1590
1591 s = al_enr * BM_WORDS_PER_AL_EXT;
1592 e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
1593 /* assert that s and e are on the same page */
1594 D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
1595 == s >> (PAGE_SHIFT - LN2_BPL + 3));
1596 count = 0;
1597 if (s < b->bm_words) {
1598 i = do_now = e-s;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001599 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001600 bm = p_addr + MLPP(s);
1601 while (i--) {
1602 count += hweight_long(*bm);
1603 *bm = -1UL;
1604 bm++;
1605 }
1606 bm_unmap(p_addr);
1607 b->bm_set += do_now*BITS_PER_LONG - count;
1608 if (e == b->bm_words)
1609 b->bm_set -= bm_clear_surplus(b);
1610 } else {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001611 dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001612 }
1613 weight = b->bm_set - weight;
1614 spin_unlock_irq(&b->bm_lock);
1615 return weight;
1616}