lru_cache: consolidate lc_get and lc_try_get
[deliverable/linux.git] / drivers / block / drbd / drbd_bitmap.c
CommitLineData
b411b363
PR
1/*
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>
5a0e3ad6 29#include <linux/slab.h>
b411b363 30#include <asm/kmap_types.h>
f0ff1357 31
b411b363
PR
32#include "drbd_int.h"
33
95a0f10c 34
b411b363
PR
35/* 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".
4b0715f0
LE
41 */
42
b411b363 43
4b0715f0
LE
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 *
24c4830c 77 * We plan to reduce the amount of in-core bitmap pages by paging them in
4b0715f0
LE
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.
b411b363
PR
81 */
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;
4b0715f0
LE
96
97 /* see LIMITATIONS: above */
98
b411b363
PR
99 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;
8a03ae2a 104 struct mutex bm_change; /* serializes resize operations */
b411b363 105
19f843aa 106 wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */
b411b363 107
20ceb2b2 108 enum bm_flag bm_flags;
b411b363
PR
109
110 /* debugging aid, in case we are still racy somewhere */
111 char *bm_why;
112 struct task_struct *bm_task;
113};
114
b411b363
PR
115#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",
392c8801 122 drbd_task_to_thread_name(mdev->tconn, current),
bed879ae 123 func, b->bm_why ?: "?",
392c8801 124 drbd_task_to_thread_name(mdev->tconn, b->bm_task));
b411b363
PR
125}
126
20ceb2b2 127void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags)
b411b363
PR
128{
129 struct drbd_bitmap *b = mdev->bitmap;
130 int trylock_failed;
131
132 if (!b) {
133 dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n");
134 return;
135 }
136
8a03ae2a 137 trylock_failed = !mutex_trylock(&b->bm_change);
b411b363
PR
138
139 if (trylock_failed) {
140 dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n",
392c8801 141 drbd_task_to_thread_name(mdev->tconn, current),
bed879ae 142 why, b->bm_why ?: "?",
392c8801 143 drbd_task_to_thread_name(mdev->tconn, b->bm_task));
8a03ae2a 144 mutex_lock(&b->bm_change);
b411b363 145 }
20ceb2b2 146 if (BM_LOCKED_MASK & b->bm_flags)
b411b363 147 dev_err(DEV, "FIXME bitmap already locked in bm_lock\n");
20ceb2b2 148 b->bm_flags |= flags & BM_LOCKED_MASK;
b411b363
PR
149
150 b->bm_why = why;
151 b->bm_task = current;
152}
153
154void drbd_bm_unlock(struct drbd_conf *mdev)
155{
156 struct drbd_bitmap *b = mdev->bitmap;
157 if (!b) {
158 dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n");
159 return;
160 }
161
20ceb2b2 162 if (!(BM_LOCKED_MASK & mdev->bitmap->bm_flags))
b411b363
PR
163 dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");
164
20ceb2b2 165 b->bm_flags &= ~BM_LOCKED_MASK;
b411b363
PR
166 b->bm_why = NULL;
167 b->bm_task = NULL;
8a03ae2a 168 mutex_unlock(&b->bm_change);
b411b363
PR
169}
170
19f843aa
LE
171/* we store some "meta" info about our pages in page->private */
172/* at a granularity of 4k storage per bitmap bit:
173 * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks
174 * 1<<38 bits,
175 * 1<<23 4k bitmap pages.
176 * Use 24 bits as page index, covers 2 peta byte storage
177 * at a granularity of 4k per bit.
178 * Used to report the failed page idx on io error from the endio handlers.
179 */
180#define BM_PAGE_IDX_MASK ((1UL<<24)-1)
181/* this page is currently read in, or written back */
182#define BM_PAGE_IO_LOCK 31
183/* if there has been an IO error for this page */
184#define BM_PAGE_IO_ERROR 30
185/* this is to be able to intelligently skip disk IO,
186 * set if bits have been set since last IO. */
187#define BM_PAGE_NEED_WRITEOUT 29
188/* to mark for lazy writeout once syncer cleared all clearable bits,
189 * we if bits have been cleared since last IO. */
190#define BM_PAGE_LAZY_WRITEOUT 28
191
24c4830c 192/* store_page_idx uses non-atomic assignment. It is only used directly after
19f843aa
LE
193 * allocating the page. All other bm_set_page_* and bm_clear_page_* need to
194 * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap
195 * changes) may happen from various contexts, and wait_on_bit/wake_up_bit
196 * requires it all to be atomic as well. */
197static void bm_store_page_idx(struct page *page, unsigned long idx)
95a0f10c 198{
19f843aa
LE
199 BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
200 page_private(page) |= idx;
95a0f10c
LE
201}
202
19f843aa 203static unsigned long bm_page_to_idx(struct page *page)
b411b363 204{
19f843aa
LE
205 return page_private(page) & BM_PAGE_IDX_MASK;
206}
207
208/* As is very unlikely that the same page is under IO from more than one
209 * context, we can get away with a bit per page and one wait queue per bitmap.
210 */
211static void bm_page_lock_io(struct drbd_conf *mdev, int page_nr)
212{
213 struct drbd_bitmap *b = mdev->bitmap;
214 void *addr = &page_private(b->bm_pages[page_nr]);
215 wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
216}
217
218static void bm_page_unlock_io(struct drbd_conf *mdev, int page_nr)
219{
220 struct drbd_bitmap *b = mdev->bitmap;
221 void *addr = &page_private(b->bm_pages[page_nr]);
4738fa16 222 clear_bit_unlock(BM_PAGE_IO_LOCK, addr);
19f843aa
LE
223 wake_up(&mdev->bitmap->bm_io_wait);
224}
225
226/* set _before_ submit_io, so it may be reset due to being changed
227 * while this page is in flight... will get submitted later again */
228static void bm_set_page_unchanged(struct page *page)
229{
230 /* use cmpxchg? */
231 clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
232 clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
233}
234
235static void bm_set_page_need_writeout(struct page *page)
236{
237 set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
238}
239
240static int bm_test_page_unchanged(struct page *page)
241{
242 volatile const unsigned long *addr = &page_private(page);
243 return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0;
244}
b411b363 245
19f843aa
LE
246static void bm_set_page_io_err(struct page *page)
247{
248 set_bit(BM_PAGE_IO_ERROR, &page_private(page));
249}
250
251static void bm_clear_page_io_err(struct page *page)
252{
253 clear_bit(BM_PAGE_IO_ERROR, &page_private(page));
254}
255
256static void bm_set_page_lazy_writeout(struct page *page)
257{
258 set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
259}
260
261static int bm_test_page_lazy_writeout(struct page *page)
262{
263 return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
264}
265
266/* on a 32bit box, this would allow for exactly (2<<38) bits. */
267static unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr)
268{
b411b363 269 /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
19f843aa 270 unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
b411b363 271 BUG_ON(page_nr >= b->bm_number_of_pages);
19f843aa
LE
272 return page_nr;
273}
b411b363 274
19f843aa
LE
275static unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr)
276{
277 /* page_nr = (bitnr/8) >> PAGE_SHIFT; */
278 unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3);
279 BUG_ON(page_nr >= b->bm_number_of_pages);
280 return page_nr;
b411b363
PR
281}
282
95a0f10c
LE
283static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx, const enum km_type km)
284{
285 struct page *page = b->bm_pages[idx];
286 return (unsigned long *) kmap_atomic(page, km);
287}
288
289static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
290{
291 return __bm_map_pidx(b, idx, KM_IRQ1);
292}
293
b411b363
PR
294static void __bm_unmap(unsigned long *p_addr, const enum km_type km)
295{
296 kunmap_atomic(p_addr, km);
297};
298
299static void bm_unmap(unsigned long *p_addr)
300{
301 return __bm_unmap(p_addr, KM_IRQ1);
302}
303
304/* long word offset of _bitmap_ sector */
305#define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
306/* word offset from start of bitmap to word number _in_page_
307 * modulo longs per page
308#define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
24c4830c 309 hm, well, Philipp thinks gcc might not optimize the % into & (... - 1)
b411b363
PR
310 so do it explicitly:
311 */
312#define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))
313
314/* Long words per page */
315#define LWPP (PAGE_SIZE/sizeof(long))
316
317/*
318 * actually most functions herein should take a struct drbd_bitmap*, not a
319 * struct drbd_conf*, but for the debug macros I like to have the mdev around
320 * to be able to report device specific.
321 */
322
19f843aa 323
b411b363
PR
324static void bm_free_pages(struct page **pages, unsigned long number)
325{
326 unsigned long i;
327 if (!pages)
328 return;
329
330 for (i = 0; i < number; i++) {
331 if (!pages[i]) {
332 printk(KERN_ALERT "drbd: bm_free_pages tried to free "
333 "a NULL pointer; i=%lu n=%lu\n",
334 i, number);
335 continue;
336 }
337 __free_page(pages[i]);
338 pages[i] = NULL;
339 }
340}
341
342static void bm_vk_free(void *ptr, int v)
343{
344 if (v)
345 vfree(ptr);
346 else
347 kfree(ptr);
348}
349
350/*
351 * "have" and "want" are NUMBER OF PAGES.
352 */
353static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
354{
355 struct page **old_pages = b->bm_pages;
356 struct page **new_pages, *page;
357 unsigned int i, bytes, vmalloced = 0;
358 unsigned long have = b->bm_number_of_pages;
359
360 BUG_ON(have == 0 && old_pages != NULL);
361 BUG_ON(have != 0 && old_pages == NULL);
362
363 if (have == want)
364 return old_pages;
365
366 /* Trying kmalloc first, falling back to vmalloc.
367 * GFP_KERNEL is ok, as this is done when a lower level disk is
368 * "attached" to the drbd. Context is receiver thread or cqueue
369 * thread. As we have no disk yet, we are not in the IO path,
370 * not even the IO path of the peer. */
371 bytes = sizeof(struct page *)*want;
372 new_pages = kmalloc(bytes, GFP_KERNEL);
373 if (!new_pages) {
374 new_pages = vmalloc(bytes);
375 if (!new_pages)
376 return NULL;
377 vmalloced = 1;
378 }
379
380 memset(new_pages, 0, bytes);
381 if (want >= have) {
382 for (i = 0; i < have; i++)
383 new_pages[i] = old_pages[i];
384 for (; i < want; i++) {
385 page = alloc_page(GFP_HIGHUSER);
386 if (!page) {
387 bm_free_pages(new_pages + have, i - have);
388 bm_vk_free(new_pages, vmalloced);
389 return NULL;
390 }
19f843aa
LE
391 /* we want to know which page it is
392 * from the endio handlers */
393 bm_store_page_idx(page, i);
b411b363
PR
394 new_pages[i] = page;
395 }
396 } else {
397 for (i = 0; i < want; i++)
398 new_pages[i] = old_pages[i];
399 /* NOT HERE, we are outside the spinlock!
400 bm_free_pages(old_pages + want, have - want);
401 */
402 }
403
404 if (vmalloced)
20ceb2b2 405 b->bm_flags |= BM_P_VMALLOCED;
b411b363 406 else
20ceb2b2 407 b->bm_flags &= ~BM_P_VMALLOCED;
b411b363
PR
408
409 return new_pages;
410}
411
412/*
413 * called on driver init only. TODO call when a device is created.
414 * allocates the drbd_bitmap, and stores it in mdev->bitmap.
415 */
416int drbd_bm_init(struct drbd_conf *mdev)
417{
418 struct drbd_bitmap *b = mdev->bitmap;
419 WARN_ON(b != NULL);
420 b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
421 if (!b)
422 return -ENOMEM;
423 spin_lock_init(&b->bm_lock);
8a03ae2a 424 mutex_init(&b->bm_change);
b411b363
PR
425 init_waitqueue_head(&b->bm_io_wait);
426
427 mdev->bitmap = b;
428
429 return 0;
430}
431
432sector_t drbd_bm_capacity(struct drbd_conf *mdev)
433{
841ce241
AG
434 if (!expect(mdev->bitmap))
435 return 0;
b411b363
PR
436 return mdev->bitmap->bm_dev_capacity;
437}
438
439/* called on driver unload. TODO: call when a device is destroyed.
440 */
441void drbd_bm_cleanup(struct drbd_conf *mdev)
442{
841ce241
AG
443 if (!expect(mdev->bitmap))
444 return;
b411b363 445 bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
20ceb2b2 446 bm_vk_free(mdev->bitmap->bm_pages, (BM_P_VMALLOCED & mdev->bitmap->bm_flags));
b411b363
PR
447 kfree(mdev->bitmap);
448 mdev->bitmap = NULL;
449}
450
451/*
452 * since (b->bm_bits % BITS_PER_LONG) != 0,
453 * this masks out the remaining bits.
454 * Returns the number of bits cleared.
455 */
95a0f10c
LE
456#define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3))
457#define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1)
458#define BITS_PER_LONG_MASK (BITS_PER_LONG - 1)
b411b363
PR
459static int bm_clear_surplus(struct drbd_bitmap *b)
460{
95a0f10c 461 unsigned long mask;
b411b363 462 unsigned long *p_addr, *bm;
95a0f10c
LE
463 int tmp;
464 int cleared = 0;
b411b363 465
95a0f10c
LE
466 /* number of bits modulo bits per page */
467 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
468 /* mask the used bits of the word containing the last bit */
469 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
470 /* bitmap is always stored little endian,
471 * on disk and in core memory alike */
472 mask = cpu_to_lel(mask);
473
6850c442 474 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
95a0f10c
LE
475 bm = p_addr + (tmp/BITS_PER_LONG);
476 if (mask) {
477 /* If mask != 0, we are not exactly aligned, so bm now points
478 * to the long containing the last bit.
479 * If mask == 0, bm already points to the word immediately
480 * after the last (long word aligned) bit. */
b411b363
PR
481 cleared = hweight_long(*bm & ~mask);
482 *bm &= mask;
95a0f10c 483 bm++;
b411b363
PR
484 }
485
95a0f10c
LE
486 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
487 /* on a 32bit arch, we may need to zero out
488 * a padding long to align with a 64bit remote */
b411b363
PR
489 cleared += hweight_long(*bm);
490 *bm = 0;
491 }
492 bm_unmap(p_addr);
493 return cleared;
494}
495
496static void bm_set_surplus(struct drbd_bitmap *b)
497{
95a0f10c 498 unsigned long mask;
b411b363 499 unsigned long *p_addr, *bm;
95a0f10c
LE
500 int tmp;
501
502 /* number of bits modulo bits per page */
503 tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
504 /* mask the used bits of the word containing the last bit */
505 mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
506 /* bitmap is always stored little endian,
507 * on disk and in core memory alike */
508 mask = cpu_to_lel(mask);
509
6850c442 510 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
95a0f10c
LE
511 bm = p_addr + (tmp/BITS_PER_LONG);
512 if (mask) {
513 /* If mask != 0, we are not exactly aligned, so bm now points
514 * to the long containing the last bit.
515 * If mask == 0, bm already points to the word immediately
516 * after the last (long word aligned) bit. */
b411b363 517 *bm |= ~mask;
95a0f10c 518 bm++;
b411b363
PR
519 }
520
95a0f10c
LE
521 if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
522 /* on a 32bit arch, we may need to zero out
523 * a padding long to align with a 64bit remote */
524 *bm = ~0UL;
b411b363
PR
525 }
526 bm_unmap(p_addr);
527}
528
4b0715f0
LE
529/* you better not modify the bitmap while this is running,
530 * or its results will be stale */
95a0f10c 531static unsigned long bm_count_bits(struct drbd_bitmap *b)
b411b363 532{
4b0715f0 533 unsigned long *p_addr;
b411b363 534 unsigned long bits = 0;
4b0715f0 535 unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1;
6850c442 536 int idx, i, last_word;
4b0715f0
LE
537
538 /* all but last page */
6850c442 539 for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) {
4b0715f0
LE
540 p_addr = __bm_map_pidx(b, idx, KM_USER0);
541 for (i = 0; i < LWPP; i++)
542 bits += hweight_long(p_addr[i]);
7777a8ba 543 __bm_unmap(p_addr, KM_USER0);
b411b363
PR
544 cond_resched();
545 }
4b0715f0
LE
546 /* last (or only) page */
547 last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL;
548 p_addr = __bm_map_pidx(b, idx, KM_USER0);
549 for (i = 0; i < last_word; i++)
550 bits += hweight_long(p_addr[i]);
551 p_addr[last_word] &= cpu_to_lel(mask);
552 bits += hweight_long(p_addr[last_word]);
553 /* 32bit arch, may have an unused padding long */
554 if (BITS_PER_LONG == 32 && (last_word & 1) == 0)
555 p_addr[last_word+1] = 0;
556 __bm_unmap(p_addr, KM_USER0);
b411b363
PR
557 return bits;
558}
559
b411b363
PR
560/* offset and len in long words.*/
561static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
562{
563 unsigned long *p_addr, *bm;
19f843aa 564 unsigned int idx;
b411b363
PR
565 size_t do_now, end;
566
b411b363
PR
567 end = offset + len;
568
569 if (end > b->bm_words) {
570 printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
571 return;
572 }
573
574 while (offset < end) {
575 do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
19f843aa
LE
576 idx = bm_word_to_page_idx(b, offset);
577 p_addr = bm_map_pidx(b, idx);
b411b363
PR
578 bm = p_addr + MLPP(offset);
579 if (bm+do_now > p_addr + LWPP) {
580 printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
581 p_addr, bm, (int)do_now);
84e7c0f7
LE
582 } else
583 memset(bm, c, do_now * sizeof(long));
b411b363 584 bm_unmap(p_addr);
19f843aa 585 bm_set_page_need_writeout(b->bm_pages[idx]);
b411b363
PR
586 offset += do_now;
587 }
588}
589
590/*
591 * make sure the bitmap has enough room for the attached storage,
592 * if necessary, resize.
593 * called whenever we may have changed the device size.
594 * returns -ENOMEM if we could not allocate enough memory, 0 on success.
595 * In case this is actually a resize, we copy the old bitmap into the new one.
596 * Otherwise, the bitmap is initialized to all bits set.
597 */
02d9a94b 598int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
b411b363
PR
599{
600 struct drbd_bitmap *b = mdev->bitmap;
6850c442 601 unsigned long bits, words, owords, obits;
b411b363
PR
602 unsigned long want, have, onpages; /* number of pages */
603 struct page **npages, **opages = NULL;
604 int err = 0, growing;
605 int opages_vmalloced;
606
841ce241
AG
607 if (!expect(b))
608 return -ENOMEM;
b411b363 609
20ceb2b2 610 drbd_bm_lock(mdev, "resize", BM_LOCKED_MASK);
b411b363
PR
611
612 dev_info(DEV, "drbd_bm_resize called with capacity == %llu\n",
613 (unsigned long long)capacity);
614
615 if (capacity == b->bm_dev_capacity)
616 goto out;
617
20ceb2b2 618 opages_vmalloced = (BM_P_VMALLOCED & b->bm_flags);
b411b363
PR
619
620 if (capacity == 0) {
621 spin_lock_irq(&b->bm_lock);
622 opages = b->bm_pages;
623 onpages = b->bm_number_of_pages;
624 owords = b->bm_words;
625 b->bm_pages = NULL;
626 b->bm_number_of_pages =
627 b->bm_set =
628 b->bm_bits =
629 b->bm_words =
630 b->bm_dev_capacity = 0;
631 spin_unlock_irq(&b->bm_lock);
632 bm_free_pages(opages, onpages);
633 bm_vk_free(opages, opages_vmalloced);
634 goto out;
635 }
636 bits = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));
637
638 /* if we would use
639 words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
640 a 32bit host could present the wrong number of words
641 to a 64bit host.
642 */
643 words = ALIGN(bits, 64) >> LN2_BPL;
644
645 if (get_ldev(mdev)) {
4b0715f0 646 u64 bits_on_disk = ((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12;
b411b363 647 put_ldev(mdev);
4b0715f0
LE
648 if (bits > bits_on_disk) {
649 dev_info(DEV, "bits = %lu\n", bits);
650 dev_info(DEV, "bits_on_disk = %llu\n", bits_on_disk);
651 err = -ENOSPC;
652 goto out;
653 }
b411b363
PR
654 }
655
6850c442 656 want = ALIGN(words*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT;
b411b363
PR
657 have = b->bm_number_of_pages;
658 if (want == have) {
659 D_ASSERT(b->bm_pages != NULL);
660 npages = b->bm_pages;
661 } else {
0cf9d27e 662 if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC))
b411b363
PR
663 npages = NULL;
664 else
665 npages = bm_realloc_pages(b, want);
666 }
667
668 if (!npages) {
669 err = -ENOMEM;
670 goto out;
671 }
672
673 spin_lock_irq(&b->bm_lock);
674 opages = b->bm_pages;
675 owords = b->bm_words;
676 obits = b->bm_bits;
677
678 growing = bits > obits;
5223671b 679 if (opages && growing && set_new_bits)
b411b363
PR
680 bm_set_surplus(b);
681
682 b->bm_pages = npages;
683 b->bm_number_of_pages = want;
684 b->bm_bits = bits;
685 b->bm_words = words;
686 b->bm_dev_capacity = capacity;
687
688 if (growing) {
02d9a94b
PR
689 if (set_new_bits) {
690 bm_memset(b, owords, 0xff, words-owords);
691 b->bm_set += bits - obits;
692 } else
693 bm_memset(b, owords, 0x00, words-owords);
694
b411b363
PR
695 }
696
697 if (want < have) {
698 /* implicit: (opages != NULL) && (opages != npages) */
699 bm_free_pages(opages + want, have - want);
700 }
701
b411b363
PR
702 (void)bm_clear_surplus(b);
703
704 spin_unlock_irq(&b->bm_lock);
705 if (opages != npages)
706 bm_vk_free(opages, opages_vmalloced);
707 if (!growing)
708 b->bm_set = bm_count_bits(b);
19f843aa 709 dev_info(DEV, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);
b411b363
PR
710
711 out:
712 drbd_bm_unlock(mdev);
713 return err;
714}
715
716/* inherently racy:
717 * if not protected by other means, return value may be out of date when
718 * leaving this function...
719 * we still need to lock it, since it is important that this returns
720 * bm_set == 0 precisely.
721 *
722 * maybe bm_set should be atomic_t ?
723 */
0778286a 724unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev)
b411b363
PR
725{
726 struct drbd_bitmap *b = mdev->bitmap;
727 unsigned long s;
728 unsigned long flags;
729
841ce241
AG
730 if (!expect(b))
731 return 0;
732 if (!expect(b->bm_pages))
733 return 0;
b411b363
PR
734
735 spin_lock_irqsave(&b->bm_lock, flags);
736 s = b->bm_set;
737 spin_unlock_irqrestore(&b->bm_lock, flags);
738
739 return s;
740}
741
742unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
743{
744 unsigned long s;
745 /* if I don't have a disk, I don't know about out-of-sync status */
746 if (!get_ldev_if_state(mdev, D_NEGOTIATING))
747 return 0;
748 s = _drbd_bm_total_weight(mdev);
749 put_ldev(mdev);
750 return s;
751}
752
753size_t drbd_bm_words(struct drbd_conf *mdev)
754{
755 struct drbd_bitmap *b = mdev->bitmap;
841ce241
AG
756 if (!expect(b))
757 return 0;
758 if (!expect(b->bm_pages))
759 return 0;
b411b363
PR
760
761 return b->bm_words;
762}
763
764unsigned long drbd_bm_bits(struct drbd_conf *mdev)
765{
766 struct drbd_bitmap *b = mdev->bitmap;
841ce241
AG
767 if (!expect(b))
768 return 0;
b411b363
PR
769
770 return b->bm_bits;
771}
772
773/* merge number words from buffer into the bitmap starting at offset.
774 * buffer[i] is expected to be little endian unsigned long.
775 * bitmap must be locked by drbd_bm_lock.
776 * currently only used from receive_bitmap.
777 */
778void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
779 unsigned long *buffer)
780{
781 struct drbd_bitmap *b = mdev->bitmap;
782 unsigned long *p_addr, *bm;
783 unsigned long word, bits;
19f843aa 784 unsigned int idx;
b411b363
PR
785 size_t end, do_now;
786
787 end = offset + number;
788
841ce241
AG
789 if (!expect(b))
790 return;
791 if (!expect(b->bm_pages))
792 return;
b411b363
PR
793 if (number == 0)
794 return;
795 WARN_ON(offset >= b->bm_words);
796 WARN_ON(end > b->bm_words);
797
798 spin_lock_irq(&b->bm_lock);
799 while (offset < end) {
800 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
19f843aa
LE
801 idx = bm_word_to_page_idx(b, offset);
802 p_addr = bm_map_pidx(b, idx);
b411b363
PR
803 bm = p_addr + MLPP(offset);
804 offset += do_now;
805 while (do_now--) {
806 bits = hweight_long(*bm);
95a0f10c 807 word = *bm | *buffer++;
b411b363
PR
808 *bm++ = word;
809 b->bm_set += hweight_long(word) - bits;
810 }
811 bm_unmap(p_addr);
19f843aa 812 bm_set_page_need_writeout(b->bm_pages[idx]);
b411b363
PR
813 }
814 /* with 32bit <-> 64bit cross-platform connect
815 * this is only correct for current usage,
816 * where we _know_ that we are 64 bit aligned,
817 * and know that this function is used in this way, too...
818 */
819 if (end == b->bm_words)
820 b->bm_set -= bm_clear_surplus(b);
b411b363
PR
821 spin_unlock_irq(&b->bm_lock);
822}
823
824/* copy number words from the bitmap starting at offset into the buffer.
825 * buffer[i] will be little endian unsigned long.
826 */
827void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
828 unsigned long *buffer)
829{
830 struct drbd_bitmap *b = mdev->bitmap;
831 unsigned long *p_addr, *bm;
832 size_t end, do_now;
833
834 end = offset + number;
835
841ce241
AG
836 if (!expect(b))
837 return;
838 if (!expect(b->bm_pages))
839 return;
b411b363
PR
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;
19f843aa 852 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
b411b363
PR
853 bm = p_addr + MLPP(offset);
854 offset += do_now;
855 while (do_now--)
95a0f10c 856 *buffer++ = *bm++;
b411b363
PR
857 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;
841ce241
AG
867 if (!expect(b))
868 return;
869 if (!expect(b->bm_pages))
870 return;
b411b363
PR
871
872 spin_lock_irq(&b->bm_lock);
873 bm_memset(b, 0, 0xff, b->bm_words);
874 (void)bm_clear_surplus(b);
875 b->bm_set = b->bm_bits;
876 spin_unlock_irq(&b->bm_lock);
877}
878
879/* clear all bits in the bitmap */
880void drbd_bm_clear_all(struct drbd_conf *mdev)
881{
882 struct drbd_bitmap *b = mdev->bitmap;
841ce241
AG
883 if (!expect(b))
884 return;
885 if (!expect(b->bm_pages))
886 return;
b411b363
PR
887
888 spin_lock_irq(&b->bm_lock);
889 bm_memset(b, 0, 0, b->bm_words);
890 b->bm_set = 0;
891 spin_unlock_irq(&b->bm_lock);
892}
893
19f843aa
LE
894struct bm_aio_ctx {
895 struct drbd_conf *mdev;
896 atomic_t in_flight;
725a97e4 897 struct completion done;
19f843aa
LE
898 unsigned flags;
899#define BM_AIO_COPY_PAGES 1
900 int error;
901};
902
903/* bv_page may be a copy, or may be the original */
b411b363
PR
904static void bm_async_io_complete(struct bio *bio, int error)
905{
19f843aa
LE
906 struct bm_aio_ctx *ctx = bio->bi_private;
907 struct drbd_conf *mdev = ctx->mdev;
908 struct drbd_bitmap *b = mdev->bitmap;
909 unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page);
b411b363
PR
910 int uptodate = bio_flagged(bio, BIO_UPTODATE);
911
912
913 /* strange behavior of some lower level drivers...
914 * fail the request by clearing the uptodate flag,
915 * but do not return any error?!
916 * do we want to WARN() on this? */
917 if (!error && !uptodate)
918 error = -EIO;
919
7648cdfe
LE
920 if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 &&
921 !bm_test_page_unchanged(b->bm_pages[idx]))
922 dev_warn(DEV, "bitmap page idx %u changed during IO!\n", idx);
19f843aa 923
b411b363 924 if (error) {
19f843aa
LE
925 /* ctx error will hold the completed-last non-zero error code,
926 * in case error codes differ. */
927 ctx->error = error;
928 bm_set_page_io_err(b->bm_pages[idx]);
929 /* Not identical to on disk version of it.
930 * Is BM_PAGE_IO_ERROR enough? */
931 if (__ratelimit(&drbd_ratelimit_state))
932 dev_err(DEV, "IO ERROR %d on bitmap page idx %u\n",
933 error, idx);
934 } else {
935 bm_clear_page_io_err(b->bm_pages[idx]);
936 dynamic_dev_dbg(DEV, "bitmap page idx %u completed\n", idx);
b411b363 937 }
19f843aa
LE
938
939 bm_page_unlock_io(mdev, idx);
940
941 /* FIXME give back to page pool */
942 if (ctx->flags & BM_AIO_COPY_PAGES)
943 put_page(bio->bi_io_vec[0].bv_page);
b411b363
PR
944
945 bio_put(bio);
19f843aa
LE
946
947 if (atomic_dec_and_test(&ctx->in_flight))
725a97e4 948 complete(&ctx->done);
b411b363
PR
949}
950
19f843aa 951static void bm_page_io_async(struct bm_aio_ctx *ctx, int page_nr, int rw) __must_hold(local)
b411b363
PR
952{
953 /* we are process context. we always get a bio */
954 struct bio *bio = bio_alloc(GFP_KERNEL, 1);
19f843aa
LE
955 struct drbd_conf *mdev = ctx->mdev;
956 struct drbd_bitmap *b = mdev->bitmap;
957 struct page *page;
b411b363 958 unsigned int len;
19f843aa 959
b411b363
PR
960 sector_t on_disk_sector =
961 mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset;
962 on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);
963
964 /* this might happen with very small
19f843aa
LE
965 * flexible external meta data device,
966 * or with PAGE_SIZE > 4k */
b411b363
PR
967 len = min_t(unsigned int, PAGE_SIZE,
968 (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);
969
19f843aa
LE
970 /* serialize IO on this page */
971 bm_page_lock_io(mdev, page_nr);
972 /* before memcpy and submit,
973 * so it can be redirtied any time */
974 bm_set_page_unchanged(b->bm_pages[page_nr]);
975
976 if (ctx->flags & BM_AIO_COPY_PAGES) {
977 /* FIXME alloc_page is good enough for now, but actually needs
978 * to use pre-allocated page pool */
979 void *src, *dest;
980 page = alloc_page(__GFP_HIGHMEM|__GFP_WAIT);
981 dest = kmap_atomic(page, KM_USER0);
982 src = kmap_atomic(b->bm_pages[page_nr], KM_USER1);
983 memcpy(dest, src, PAGE_SIZE);
984 kunmap_atomic(src, KM_USER1);
985 kunmap_atomic(dest, KM_USER0);
986 bm_store_page_idx(page, page_nr);
987 } else
988 page = b->bm_pages[page_nr];
989
b411b363
PR
990 bio->bi_bdev = mdev->ldev->md_bdev;
991 bio->bi_sector = on_disk_sector;
19f843aa
LE
992 bio_add_page(bio, page, len, 0);
993 bio->bi_private = ctx;
b411b363
PR
994 bio->bi_end_io = bm_async_io_complete;
995
0cf9d27e 996 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
b411b363
PR
997 bio->bi_rw |= rw;
998 bio_endio(bio, -EIO);
999 } else {
1000 submit_bio(rw, bio);
5a8b4242
LE
1001 /* this should not count as user activity and cause the
1002 * resync to throttle -- see drbd_rs_should_slow_down(). */
1003 atomic_add(len >> 9, &mdev->rs_sect_ev);
b411b363
PR
1004 }
1005}
1006
b411b363
PR
1007/*
1008 * bm_rw: read/write the whole bitmap from/to its on disk location.
1009 */
19f843aa 1010static int bm_rw(struct drbd_conf *mdev, int rw, unsigned lazy_writeout_upper_idx) __must_hold(local)
b411b363 1011{
725a97e4
LE
1012 struct bm_aio_ctx ctx = {
1013 .mdev = mdev,
1014 .in_flight = ATOMIC_INIT(1),
1015 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1016 .flags = lazy_writeout_upper_idx ? BM_AIO_COPY_PAGES : 0,
1017 };
b411b363 1018 struct drbd_bitmap *b = mdev->bitmap;
6850c442 1019 int num_pages, i, count = 0;
b411b363
PR
1020 unsigned long now;
1021 char ppb[10];
1022 int err = 0;
1023
19f843aa
LE
1024 /*
1025 * We are protected against bitmap disappearing/resizing by holding an
1026 * ldev reference (caller must have called get_ldev()).
1027 * For read/write, we are protected against changes to the bitmap by
1028 * the bitmap lock (see drbd_bitmap_io).
1029 * For lazy writeout, we don't care for ongoing changes to the bitmap,
1030 * as we submit copies of pages anyways.
1031 */
1032 if (!ctx.flags)
20ceb2b2 1033 WARN_ON(!(BM_LOCKED_MASK & b->bm_flags));
b411b363 1034
6850c442 1035 num_pages = b->bm_number_of_pages;
b411b363 1036
b411b363 1037 now = jiffies;
b411b363
PR
1038
1039 /* let the layers below us try to merge these bios... */
6850c442 1040 for (i = 0; i < num_pages; i++) {
19f843aa
LE
1041 /* ignore completely unchanged pages */
1042 if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
1043 break;
1044 if (rw & WRITE) {
1045 if (bm_test_page_unchanged(b->bm_pages[i])) {
1046 dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
1047 continue;
1048 }
1049 /* during lazy writeout,
1050 * ignore those pages not marked for lazy writeout. */
1051 if (lazy_writeout_upper_idx &&
1052 !bm_test_page_lazy_writeout(b->bm_pages[i])) {
1053 dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
1054 continue;
1055 }
1056 }
1057 atomic_inc(&ctx.in_flight);
1058 bm_page_io_async(&ctx, i, rw);
1059 ++count;
1060 cond_resched();
1061 }
b411b363 1062
725a97e4
LE
1063 /*
1064 * We initialize ctx.in_flight to one to make sure bm_async_io_complete
1065 * will not complete() early, and decrement / test it here. If there
1066 * are still some bios in flight, we need to wait for them here.
1067 */
1068 if (!atomic_dec_and_test(&ctx.in_flight))
1069 wait_for_completion(&ctx.done);
19f843aa
LE
1070 dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
1071 rw == WRITE ? "WRITE" : "READ",
1072 count, jiffies - now);
b411b363 1073
19f843aa 1074 if (ctx.error) {
b411b363 1075 dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
81e84650 1076 drbd_chk_io_error(mdev, 1, true);
19f843aa 1077 err = -EIO; /* ctx.error ? */
b411b363
PR
1078 }
1079
1080 now = jiffies;
1081 if (rw == WRITE) {
b411b363
PR
1082 drbd_md_flush(mdev);
1083 } else /* rw == READ */ {
95a0f10c 1084 b->bm_set = bm_count_bits(b);
b411b363
PR
1085 dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
1086 jiffies - now);
1087 }
1088 now = b->bm_set;
1089
1090 dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
1091 ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
1092
1093 return err;
1094}
1095
1096/**
1097 * drbd_bm_read() - Read the whole bitmap from its on disk location.
1098 * @mdev: DRBD device.
1099 */
1100int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
1101{
19f843aa 1102 return bm_rw(mdev, READ, 0);
b411b363
PR
1103}
1104
1105/**
1106 * drbd_bm_write() - Write the whole bitmap to its on disk location.
1107 * @mdev: DRBD device.
19f843aa
LE
1108 *
1109 * Will only write pages that have changed since last IO.
b411b363
PR
1110 */
1111int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
1112{
19f843aa 1113 return bm_rw(mdev, WRITE, 0);
b411b363
PR
1114}
1115
1116/**
19f843aa 1117 * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
b411b363 1118 * @mdev: DRBD device.
19f843aa
LE
1119 * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
1120 */
1121int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
1122{
1123 return bm_rw(mdev, WRITE, upper_idx);
1124}
1125
1126
1127/**
1128 * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
1129 * @mdev: DRBD device.
1130 * @idx: bitmap page index
b411b363 1131 *
4b0715f0
LE
1132 * We don't want to special case on logical_block_size of the backend device,
1133 * so we submit PAGE_SIZE aligned pieces.
19f843aa 1134 * Note that on "most" systems, PAGE_SIZE is 4k.
4b0715f0
LE
1135 *
1136 * In case this becomes an issue on systems with larger PAGE_SIZE,
1137 * we may want to change this again to write 4k aligned 4k pieces.
b411b363 1138 */
19f843aa 1139int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
b411b363 1140{
725a97e4
LE
1141 struct bm_aio_ctx ctx = {
1142 .mdev = mdev,
1143 .in_flight = ATOMIC_INIT(1),
1144 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1145 .flags = BM_AIO_COPY_PAGES,
1146 };
b411b363 1147
19f843aa 1148 if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
7648cdfe 1149 dynamic_dev_dbg(DEV, "skipped bm page write for idx %u\n", idx);
19f843aa 1150 return 0;
b411b363 1151 }
19f843aa 1152
19f843aa 1153 bm_page_io_async(&ctx, idx, WRITE_SYNC);
725a97e4 1154 wait_for_completion(&ctx.done);
19f843aa
LE
1155
1156 if (ctx.error)
1157 drbd_chk_io_error(mdev, 1, true);
1158 /* that should force detach, so the in memory bitmap will be
1159 * gone in a moment as well. */
1160
b411b363 1161 mdev->bm_writ_cnt++;
19f843aa 1162 return ctx.error;
b411b363
PR
1163}
1164
1165/* NOTE
1166 * find_first_bit returns int, we return unsigned long.
4b0715f0
LE
1167 * For this to work on 32bit arch with bitnumbers > (1<<32),
1168 * we'd need to return u64, and get a whole lot of other places
1169 * fixed where we still use unsigned long.
b411b363
PR
1170 *
1171 * this returns a bit number, NOT a sector!
1172 */
b411b363
PR
1173static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
1174 const int find_zero_bit, const enum km_type km)
1175{
1176 struct drbd_bitmap *b = mdev->bitmap;
b411b363 1177 unsigned long *p_addr;
4b0715f0
LE
1178 unsigned long bit_offset;
1179 unsigned i;
1180
b411b363
PR
1181
1182 if (bm_fo > b->bm_bits) {
1183 dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
4b0715f0 1184 bm_fo = DRBD_END_OF_BITMAP;
b411b363
PR
1185 } else {
1186 while (bm_fo < b->bm_bits) {
19f843aa 1187 /* bit offset of the first bit in the page */
4b0715f0 1188 bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
19f843aa 1189 p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
b411b363
PR
1190
1191 if (find_zero_bit)
7e599e6e 1192 i = find_next_zero_bit_le(p_addr,
4b0715f0 1193 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
b411b363 1194 else
7e599e6e 1195 i = find_next_bit_le(p_addr,
4b0715f0 1196 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
b411b363
PR
1197
1198 __bm_unmap(p_addr, km);
1199 if (i < PAGE_SIZE*8) {
4b0715f0
LE
1200 bm_fo = bit_offset + i;
1201 if (bm_fo >= b->bm_bits)
b411b363
PR
1202 break;
1203 goto found;
1204 }
1205 bm_fo = bit_offset + PAGE_SIZE*8;
1206 }
4b0715f0 1207 bm_fo = DRBD_END_OF_BITMAP;
b411b363
PR
1208 }
1209 found:
4b0715f0 1210 return bm_fo;
b411b363
PR
1211}
1212
1213static unsigned long bm_find_next(struct drbd_conf *mdev,
1214 unsigned long bm_fo, const int find_zero_bit)
1215{
1216 struct drbd_bitmap *b = mdev->bitmap;
4b0715f0 1217 unsigned long i = DRBD_END_OF_BITMAP;
b411b363 1218
841ce241
AG
1219 if (!expect(b))
1220 return i;
1221 if (!expect(b->bm_pages))
1222 return i;
b411b363
PR
1223
1224 spin_lock_irq(&b->bm_lock);
20ceb2b2 1225 if (BM_DONT_TEST & b->bm_flags)
b411b363
PR
1226 bm_print_lock_info(mdev);
1227
1228 i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);
1229
1230 spin_unlock_irq(&b->bm_lock);
1231 return i;
1232}
1233
1234unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1235{
1236 return bm_find_next(mdev, bm_fo, 0);
1237}
1238
1239#if 0
1240/* not yet needed for anything. */
1241unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1242{
1243 return bm_find_next(mdev, bm_fo, 1);
1244}
1245#endif
1246
1247/* does not spin_lock_irqsave.
1248 * you must take drbd_bm_lock() first */
1249unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
1250{
20ceb2b2 1251 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
b411b363
PR
1252 return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
1253}
1254
1255unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
1256{
20ceb2b2 1257 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
b411b363
PR
1258 return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
1259}
1260
1261/* returns number of bits actually changed.
1262 * for val != 0, we change 0 -> 1, return code positive
1263 * for val == 0, we change 1 -> 0, return code negative
1264 * wants bitnr, not sector.
1265 * expected to be called for only a few bits (e - s about BITS_PER_LONG).
1266 * Must hold bitmap lock already. */
b4ee79da 1267static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
829c6087 1268 unsigned long e, int val)
b411b363
PR
1269{
1270 struct drbd_bitmap *b = mdev->bitmap;
1271 unsigned long *p_addr = NULL;
1272 unsigned long bitnr;
19f843aa 1273 unsigned int last_page_nr = -1U;
b411b363 1274 int c = 0;
19f843aa 1275 int changed_total = 0;
b411b363
PR
1276
1277 if (e >= b->bm_bits) {
1278 dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
1279 s, e, b->bm_bits);
1280 e = b->bm_bits ? b->bm_bits -1 : 0;
1281 }
1282 for (bitnr = s; bitnr <= e; bitnr++) {
19f843aa 1283 unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
b411b363
PR
1284 if (page_nr != last_page_nr) {
1285 if (p_addr)
829c6087 1286 __bm_unmap(p_addr, KM_IRQ1);
19f843aa
LE
1287 if (c < 0)
1288 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1289 else if (c > 0)
1290 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1291 changed_total += c;
1292 c = 0;
829c6087 1293 p_addr = __bm_map_pidx(b, page_nr, KM_IRQ1);
b411b363
PR
1294 last_page_nr = page_nr;
1295 }
1296 if (val)
7e599e6e 1297 c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
b411b363 1298 else
7e599e6e 1299 c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
b411b363
PR
1300 }
1301 if (p_addr)
829c6087 1302 __bm_unmap(p_addr, KM_IRQ1);
19f843aa
LE
1303 if (c < 0)
1304 bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
1305 else if (c > 0)
1306 bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
1307 changed_total += c;
1308 b->bm_set += changed_total;
1309 return changed_total;
b411b363
PR
1310}
1311
1312/* returns number of bits actually changed.
1313 * for val != 0, we change 0 -> 1, return code positive
1314 * for val == 0, we change 1 -> 0, return code negative
1315 * wants bitnr, not sector */
b4ee79da 1316static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
b411b363
PR
1317 const unsigned long e, int val)
1318{
1319 unsigned long flags;
1320 struct drbd_bitmap *b = mdev->bitmap;
1321 int c = 0;
1322
841ce241
AG
1323 if (!expect(b))
1324 return 1;
1325 if (!expect(b->bm_pages))
1326 return 0;
b411b363
PR
1327
1328 spin_lock_irqsave(&b->bm_lock, flags);
20ceb2b2 1329 if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags)
b411b363
PR
1330 bm_print_lock_info(mdev);
1331
829c6087 1332 c = __bm_change_bits_to(mdev, s, e, val);
b411b363
PR
1333
1334 spin_unlock_irqrestore(&b->bm_lock, flags);
1335 return c;
1336}
1337
1338/* returns number of bits changed 0 -> 1 */
1339int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1340{
1341 return bm_change_bits_to(mdev, s, e, 1);
1342}
1343
1344/* returns number of bits changed 1 -> 0 */
1345int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1346{
1347 return -bm_change_bits_to(mdev, s, e, 0);
1348}
1349
1350/* sets all bits in full words,
1351 * from first_word up to, but not including, last_word */
1352static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
1353 int page_nr, int first_word, int last_word)
1354{
1355 int i;
1356 int bits;
829c6087 1357 unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_IRQ1);
b411b363
PR
1358 for (i = first_word; i < last_word; i++) {
1359 bits = hweight_long(paddr[i]);
1360 paddr[i] = ~0UL;
1361 b->bm_set += BITS_PER_LONG - bits;
1362 }
829c6087 1363 kunmap_atomic(paddr, KM_IRQ1);
b411b363
PR
1364}
1365
829c6087
LE
1366/* Same thing as drbd_bm_set_bits,
1367 * but more efficient for a large bit range.
b411b363
PR
1368 * You must first drbd_bm_lock().
1369 * Can be called to set the whole bitmap in one go.
1370 * Sets bits from s to e _inclusive_. */
1371void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1372{
1373 /* First set_bit from the first bit (s)
1374 * up to the next long boundary (sl),
1375 * then assign full words up to the last long boundary (el),
1376 * then set_bit up to and including the last bit (e).
1377 *
1378 * Do not use memset, because we must account for changes,
1379 * so we need to loop over the words with hweight() anyways.
1380 */
829c6087 1381 struct drbd_bitmap *b = mdev->bitmap;
b411b363
PR
1382 unsigned long sl = ALIGN(s,BITS_PER_LONG);
1383 unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
1384 int first_page;
1385 int last_page;
1386 int page_nr;
1387 int first_word;
1388 int last_word;
1389
1390 if (e - s <= 3*BITS_PER_LONG) {
1391 /* don't bother; el and sl may even be wrong. */
829c6087
LE
1392 spin_lock_irq(&b->bm_lock);
1393 __bm_change_bits_to(mdev, s, e, 1);
1394 spin_unlock_irq(&b->bm_lock);
b411b363
PR
1395 return;
1396 }
1397
1398 /* difference is large enough that we can trust sl and el */
1399
829c6087
LE
1400 spin_lock_irq(&b->bm_lock);
1401
b411b363
PR
1402 /* bits filling the current long */
1403 if (sl)
829c6087 1404 __bm_change_bits_to(mdev, s, sl-1, 1);
b411b363
PR
1405
1406 first_page = sl >> (3 + PAGE_SHIFT);
1407 last_page = el >> (3 + PAGE_SHIFT);
1408
1409 /* MLPP: modulo longs per page */
1410 /* LWPP: long words per page */
1411 first_word = MLPP(sl >> LN2_BPL);
1412 last_word = LWPP;
1413
1414 /* first and full pages, unless first page == last page */
1415 for (page_nr = first_page; page_nr < last_page; page_nr++) {
1416 bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
8ccee20e
LE
1417 spin_unlock_irq(&b->bm_lock);
1418 cond_resched();
b411b363 1419 first_word = 0;
8ccee20e 1420 spin_lock_irq(&b->bm_lock);
b411b363
PR
1421 }
1422
1423 /* last page (respectively only page, for first page == last page) */
1424 last_word = MLPP(el >> LN2_BPL);
1425 bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);
1426
1427 /* possibly trailing bits.
1428 * example: (e & 63) == 63, el will be e+1.
1429 * if that even was the very last bit,
1430 * it would trigger an assert in __bm_change_bits_to()
1431 */
1432 if (el <= e)
829c6087
LE
1433 __bm_change_bits_to(mdev, el, e, 1);
1434 spin_unlock_irq(&b->bm_lock);
b411b363
PR
1435}
1436
1437/* returns bit state
1438 * wants bitnr, NOT sector.
1439 * inherently racy... area needs to be locked by means of {al,rs}_lru
1440 * 1 ... bit set
1441 * 0 ... bit not set
1442 * -1 ... first out of bounds access, stop testing for bits!
1443 */
1444int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
1445{
1446 unsigned long flags;
1447 struct drbd_bitmap *b = mdev->bitmap;
1448 unsigned long *p_addr;
1449 int i;
1450
841ce241
AG
1451 if (!expect(b))
1452 return 0;
1453 if (!expect(b->bm_pages))
1454 return 0;
b411b363
PR
1455
1456 spin_lock_irqsave(&b->bm_lock, flags);
20ceb2b2 1457 if (BM_DONT_TEST & b->bm_flags)
b411b363
PR
1458 bm_print_lock_info(mdev);
1459 if (bitnr < b->bm_bits) {
19f843aa 1460 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
7e599e6e 1461 i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
b411b363
PR
1462 bm_unmap(p_addr);
1463 } else if (bitnr == b->bm_bits) {
1464 i = -1;
1465 } else { /* (bitnr > b->bm_bits) */
1466 dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
1467 i = 0;
1468 }
1469
1470 spin_unlock_irqrestore(&b->bm_lock, flags);
1471 return i;
1472}
1473
1474/* returns number of bits set in the range [s, e] */
1475int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
1476{
1477 unsigned long flags;
1478 struct drbd_bitmap *b = mdev->bitmap;
19f843aa 1479 unsigned long *p_addr = NULL;
b411b363 1480 unsigned long bitnr;
19f843aa 1481 unsigned int page_nr = -1U;
b411b363 1482 int c = 0;
b411b363
PR
1483
1484 /* If this is called without a bitmap, that is a bug. But just to be
1485 * robust in case we screwed up elsewhere, in that case pretend there
1486 * was one dirty bit in the requested area, so we won't try to do a
1487 * local read there (no bitmap probably implies no disk) */
841ce241
AG
1488 if (!expect(b))
1489 return 1;
1490 if (!expect(b->bm_pages))
1491 return 1;
b411b363
PR
1492
1493 spin_lock_irqsave(&b->bm_lock, flags);
20ceb2b2 1494 if (BM_DONT_TEST & b->bm_flags)
b411b363
PR
1495 bm_print_lock_info(mdev);
1496 for (bitnr = s; bitnr <= e; bitnr++) {
19f843aa
LE
1497 unsigned int idx = bm_bit_to_page_idx(b, bitnr);
1498 if (page_nr != idx) {
1499 page_nr = idx;
b411b363
PR
1500 if (p_addr)
1501 bm_unmap(p_addr);
19f843aa 1502 p_addr = bm_map_pidx(b, idx);
b411b363 1503 }
841ce241 1504 if (expect(bitnr < b->bm_bits))
7e599e6e 1505 c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
841ce241
AG
1506 else
1507 dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
b411b363
PR
1508 }
1509 if (p_addr)
1510 bm_unmap(p_addr);
1511 spin_unlock_irqrestore(&b->bm_lock, flags);
1512 return c;
1513}
1514
1515
1516/* inherently racy...
1517 * return value may be already out-of-date when this function returns.
1518 * but the general usage is that this is only use during a cstate when bits are
1519 * only cleared, not set, and typically only care for the case when the return
1520 * value is zero, or we already "locked" this "bitmap extent" by other means.
1521 *
1522 * enr is bm-extent number, since we chose to name one sector (512 bytes)
1523 * worth of the bitmap a "bitmap extent".
1524 *
1525 * TODO
1526 * I think since we use it like a reference count, we should use the real
1527 * reference count of some bitmap extent element from some lru instead...
1528 *
1529 */
1530int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
1531{
1532 struct drbd_bitmap *b = mdev->bitmap;
1533 int count, s, e;
1534 unsigned long flags;
1535 unsigned long *p_addr, *bm;
1536
841ce241
AG
1537 if (!expect(b))
1538 return 0;
1539 if (!expect(b->bm_pages))
1540 return 0;
b411b363
PR
1541
1542 spin_lock_irqsave(&b->bm_lock, flags);
20ceb2b2 1543 if (BM_DONT_TEST & b->bm_flags)
b411b363
PR
1544 bm_print_lock_info(mdev);
1545
1546 s = S2W(enr);
1547 e = min((size_t)S2W(enr+1), b->bm_words);
1548 count = 0;
1549 if (s < b->bm_words) {
1550 int n = e-s;
19f843aa 1551 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
b411b363
PR
1552 bm = p_addr + MLPP(s);
1553 while (n--)
1554 count += hweight_long(*bm++);
1555 bm_unmap(p_addr);
1556 } else {
1557 dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
1558 }
1559 spin_unlock_irqrestore(&b->bm_lock, flags);
1560 return count;
1561}
1562
4b0715f0
LE
1563/* Set all bits covered by the AL-extent al_enr.
1564 * Returns number of bits changed. */
b411b363
PR
1565unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
1566{
1567 struct drbd_bitmap *b = mdev->bitmap;
1568 unsigned long *p_addr, *bm;
1569 unsigned long weight;
4b0715f0
LE
1570 unsigned long s, e;
1571 int count, i, do_now;
841ce241
AG
1572 if (!expect(b))
1573 return 0;
1574 if (!expect(b->bm_pages))
1575 return 0;
b411b363
PR
1576
1577 spin_lock_irq(&b->bm_lock);
20ceb2b2 1578 if (BM_DONT_SET & b->bm_flags)
b411b363
PR
1579 bm_print_lock_info(mdev);
1580 weight = b->bm_set;
1581
1582 s = al_enr * BM_WORDS_PER_AL_EXT;
1583 e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
1584 /* assert that s and e are on the same page */
1585 D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
1586 == s >> (PAGE_SHIFT - LN2_BPL + 3));
1587 count = 0;
1588 if (s < b->bm_words) {
1589 i = do_now = e-s;
19f843aa 1590 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
b411b363
PR
1591 bm = p_addr + MLPP(s);
1592 while (i--) {
1593 count += hweight_long(*bm);
1594 *bm = -1UL;
1595 bm++;
1596 }
1597 bm_unmap(p_addr);
1598 b->bm_set += do_now*BITS_PER_LONG - count;
1599 if (e == b->bm_words)
1600 b->bm_set -= bm_clear_surplus(b);
1601 } else {
4b0715f0 1602 dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s);
b411b363
PR
1603 }
1604 weight = b->bm_set - weight;
1605 spin_unlock_irq(&b->bm_lock);
1606 return weight;
1607}
This page took 0.240526 seconds and 5 git commands to generate.