blob: 4be737055718dd7f17c2b1217a3f67ca007fa45c [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 Reisner392c8802011-02-09 10:33:31 +0100122 drbd_task_to_thread_name(mdev->tconn, current),
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100123 func, b->bm_why ?: "?",
Philipp Reisner392c8802011-02-09 10:33:31 +0100124 drbd_task_to_thread_name(mdev->tconn, b->bm_task));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700125}
126
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100127void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700128{
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
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000137 trylock_failed = !mutex_trylock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700138
139 if (trylock_failed) {
140 dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n",
Philipp Reisner392c8802011-02-09 10:33:31 +0100141 drbd_task_to_thread_name(mdev->tconn, current),
Philipp Reisnerbed879a2011-02-04 14:00:37 +0100142 why, b->bm_why ?: "?",
Philipp Reisner392c8802011-02-09 10:33:31 +0100143 drbd_task_to_thread_name(mdev->tconn, b->bm_task));
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000144 mutex_lock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700145 }
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100146 if (BM_LOCKED_MASK & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700147 dev_err(DEV, "FIXME bitmap already locked in bm_lock\n");
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100148 b->bm_flags |= flags & BM_LOCKED_MASK;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700149
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
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100162 if (!(BM_LOCKED_MASK & mdev->bitmap->bm_flags))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700163 dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");
164
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100165 b->bm_flags &= ~BM_LOCKED_MASK;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700166 b->bm_why = NULL;
167 b->bm_task = NULL;
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000168 mutex_unlock(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700169}
170
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100171/* 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
Bart Van Assche24c48302011-05-21 18:32:29 +0200192/* store_page_idx uses non-atomic assignment. It is only used directly after
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100193 * 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)
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100198{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100199 BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
200 page_private(page) |= idx;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100201}
202
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100203static unsigned long bm_page_to_idx(struct page *page)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700204{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100205 return page_private(page) & BM_PAGE_IDX_MASK;
206}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700207
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100208/* 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]);
Lars Ellenberg4738fa12011-02-21 13:20:55 +0100222 clear_bit_unlock(BM_PAGE_IO_LOCK, addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100223 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}
245
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{
Philipp Reisnerb411b362009-09-25 16:07:19 -0700269 /* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100270 unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700271 BUG_ON(page_nr >= b->bm_number_of_pages);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100272 return page_nr;
273}
Philipp Reisnerb411b362009-09-25 16:07:19 -0700274
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100275static 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;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700281}
282
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100283static 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
Philipp Reisnerb411b362009-09-25 16:07:19 -0700294static 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))
Bart Van Assche24c48302011-05-21 18:32:29 +0200309 hm, well, Philipp thinks gcc might not optimize the % into & (... - 1)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700310 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
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100323
Philipp Reisnerb411b362009-09-25 16:07:19 -0700324static 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 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100391 /* we want to know which page it is
392 * from the endio handlers */
393 bm_store_page_idx(page, i);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700394 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)
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100405 b->bm_flags |= BM_P_VMALLOCED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700406 else
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100407 b->bm_flags &= ~BM_P_VMALLOCED;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700408
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);
Thomas Gleixner8a03ae22010-01-29 20:39:07 +0000424 mutex_init(&b->bm_change);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700425 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{
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100434 if (!expect(mdev->bitmap))
435 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700436 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{
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100443 if (!expect(mdev->bitmap))
444 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700445 bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100446 bm_vk_free(mdev->bitmap->bm_pages, (BM_P_VMALLOCED & mdev->bitmap->bm_flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700447 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 */
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100456#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)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700459static int bm_clear_surplus(struct drbd_bitmap *b)
460{
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100461 unsigned long mask;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700462 unsigned long *p_addr, *bm;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100463 int tmp;
464 int cleared = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700465
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100466 /* 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
Lars Ellenberg6850c442010-12-16 00:32:38 +0100474 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100475 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. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700481 cleared = hweight_long(*bm & ~mask);
482 *bm &= mask;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100483 bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700484 }
485
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100486 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 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700489 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{
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100498 unsigned long mask;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700499 unsigned long *p_addr, *bm;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100500 int tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700501
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100502 /* 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
Lars Ellenberg6850c442010-12-16 00:32:38 +0100510 p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100511 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. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700517 *bm |= ~mask;
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100518 bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700519 }
520
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100521 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;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700525 }
526 bm_unmap(p_addr);
527}
528
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100529/* you better not modify the bitmap while this is running,
530 * or its results will be stale */
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100531static unsigned long bm_count_bits(struct drbd_bitmap *b)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700532{
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100533 unsigned long *p_addr;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700534 unsigned long bits = 0;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100535 unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1;
Lars Ellenberg6850c442010-12-16 00:32:38 +0100536 int idx, i, last_word;
Lars Ellenberg7777a8b2010-12-15 23:21:39 +0100537
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100538 /* all but last page */
Lars Ellenberg6850c442010-12-16 00:32:38 +0100539 for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100540 p_addr = __bm_map_pidx(b, idx, KM_USER0);
541 for (i = 0; i < LWPP; i++)
542 bits += hweight_long(p_addr[i]);
Lars Ellenberg7777a8b2010-12-15 23:21:39 +0100543 __bm_unmap(p_addr, KM_USER0);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700544 cond_resched();
545 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100546 /* 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);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700557 return bits;
558}
559
Philipp Reisnerb411b362009-09-25 16:07:19 -0700560/* offset and len in long words.*/
561static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
562{
563 unsigned long *p_addr, *bm;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100564 unsigned int idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700565 size_t do_now, end;
566
Philipp Reisnerb411b362009-09-25 16:07:19 -0700567 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;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100576 idx = bm_word_to_page_idx(b, offset);
577 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700578 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);
Lars Ellenberg84e7c0f2010-12-16 00:37:57 +0100582 } else
583 memset(bm, c, do_now * sizeof(long));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700584 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100585 bm_set_page_need_writeout(b->bm_pages[idx]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700586 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 */
Philipp Reisner02d9a942010-03-24 16:23:03 +0100598int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700599{
600 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg6850c442010-12-16 00:32:38 +0100601 unsigned long bits, words, owords, obits;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700602 unsigned long want, have, onpages; /* number of pages */
603 struct page **npages, **opages = NULL;
604 int err = 0, growing;
605 int opages_vmalloced;
606
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100607 if (!expect(b))
608 return -ENOMEM;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700609
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100610 drbd_bm_lock(mdev, "resize", BM_LOCKED_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700611
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
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +0100618 opages_vmalloced = (BM_P_VMALLOCED & b->bm_flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700619
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)) {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100646 u64 bits_on_disk = ((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700647 put_ldev(mdev);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +0100648 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 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700654 }
655
Lars Ellenberg6850c442010-12-16 00:32:38 +0100656 want = ALIGN(words*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700657 have = b->bm_number_of_pages;
658 if (want == have) {
659 D_ASSERT(b->bm_pages != NULL);
660 npages = b->bm_pages;
661 } else {
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100662 if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700663 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;
Philipp Reisner52236712010-04-28 14:46:57 +0200679 if (opages && growing && set_new_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700680 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) {
Philipp Reisner02d9a942010-03-24 16:23:03 +0100689 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
Philipp Reisnerb411b362009-09-25 16:07:19 -0700695 }
696
697 if (want < have) {
698 /* implicit: (opages != NULL) && (opages != npages) */
699 bm_free_pages(opages + want, have - want);
700 }
701
Philipp Reisnerb411b362009-09-25 16:07:19 -0700702 (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);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100709 dev_info(DEV, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700710
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 */
Philipp Reisner07782862010-08-31 12:00:50 +0200724unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725{
726 struct drbd_bitmap *b = mdev->bitmap;
727 unsigned long s;
728 unsigned long flags;
729
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100730 if (!expect(b))
731 return 0;
732 if (!expect(b->bm_pages))
733 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700734
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;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100756 if (!expect(b))
757 return 0;
758 if (!expect(b->bm_pages))
759 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700760
761 return b->bm_words;
762}
763
764unsigned long drbd_bm_bits(struct drbd_conf *mdev)
765{
766 struct drbd_bitmap *b = mdev->bitmap;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100767 if (!expect(b))
768 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700769
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;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100784 unsigned int idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700785 size_t end, do_now;
786
787 end = offset + number;
788
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100789 if (!expect(b))
790 return;
791 if (!expect(b->bm_pages))
792 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700793 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;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100801 idx = bm_word_to_page_idx(b, offset);
802 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700803 bm = p_addr + MLPP(offset);
804 offset += do_now;
805 while (do_now--) {
806 bits = hweight_long(*bm);
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100807 word = *bm | *buffer++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700808 *bm++ = word;
809 b->bm_set += hweight_long(word) - bits;
810 }
811 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100812 bm_set_page_need_writeout(b->bm_pages[idx]);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700813 }
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);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700821 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
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100836 if (!expect(b))
837 return;
838 if (!expect(b->bm_pages))
839 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700840
841 spin_lock_irq(&b->bm_lock);
842 if ((offset >= b->bm_words) ||
843 (end > b->bm_words) ||
844 (number <= 0))
845 dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n",
846 (unsigned long) offset,
847 (unsigned long) number,
848 (unsigned long) b->bm_words);
849 else {
850 while (offset < end) {
851 do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100852 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700853 bm = p_addr + MLPP(offset);
854 offset += do_now;
855 while (do_now--)
Lars Ellenberg95a0f102010-12-15 08:59:09 +0100856 *buffer++ = *bm++;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857 bm_unmap(p_addr);
858 }
859 }
860 spin_unlock_irq(&b->bm_lock);
861}
862
863/* set all bits in the bitmap */
864void drbd_bm_set_all(struct drbd_conf *mdev)
865{
866 struct drbd_bitmap *b = mdev->bitmap;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100867 if (!expect(b))
868 return;
869 if (!expect(b->bm_pages))
870 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700871
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;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100883 if (!expect(b))
884 return;
885 if (!expect(b->bm_pages))
886 return;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700887
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
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100894struct bm_aio_ctx {
895 struct drbd_conf *mdev;
896 atomic_t in_flight;
Lars Ellenberg725a97e2010-12-19 11:29:55 +0100897 struct completion done;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100898 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 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700904static void bm_async_io_complete(struct bio *bio, int error)
905{
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100906 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);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700910 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
Lars Ellenberg7648cdf2010-12-17 23:58:41 +0100920 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);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100923
Philipp Reisnerb411b362009-09-25 16:07:19 -0700924 if (error) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100925 /* 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);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700937 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100938
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);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700944
945 bio_put(bio);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100946
947 if (atomic_dec_and_test(&ctx->in_flight))
Lars Ellenberg725a97e2010-12-19 11:29:55 +0100948 complete(&ctx->done);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700949}
950
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100951static 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 -0700952{
953 /* we are process context. we always get a bio */
954 struct bio *bio = bio_alloc(GFP_KERNEL, 1);
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100955 struct drbd_conf *mdev = ctx->mdev;
956 struct drbd_bitmap *b = mdev->bitmap;
957 struct page *page;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700958 unsigned int len;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100959
Philipp Reisnerb411b362009-09-25 16:07:19 -0700960 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
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100965 * flexible external meta data device,
966 * or with PAGE_SIZE > 4k */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700967 len = min_t(unsigned int, PAGE_SIZE,
968 (drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);
969
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100970 /* 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
Philipp Reisnerb411b362009-09-25 16:07:19 -0700990 bio->bi_bdev = mdev->ldev->md_bdev;
991 bio->bi_sector = on_disk_sector;
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100992 bio_add_page(bio, page, len, 0);
993 bio->bi_private = ctx;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700994 bio->bi_end_io = bm_async_io_complete;
995
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100996 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700997 bio->bi_rw |= rw;
998 bio_endio(bio, -EIO);
999 } else {
1000 submit_bio(rw, bio);
Lars Ellenberg5a8b4242011-06-14 14:18:23 +02001001 /* 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);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001004 }
1005}
1006
Philipp Reisnerb411b362009-09-25 16:07:19 -07001007/*
1008 * bm_rw: read/write the whole bitmap from/to its on disk location.
1009 */
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001010static int bm_rw(struct drbd_conf *mdev, int rw, unsigned lazy_writeout_upper_idx) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001011{
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001012 struct bm_aio_ctx ctx = {
1013 .mdev = mdev,
1014 .in_flight = ATOMIC_INIT(1),
1015 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1016 .flags = lazy_writeout_upper_idx ? BM_AIO_COPY_PAGES : 0,
1017 };
Philipp Reisnerb411b362009-09-25 16:07:19 -07001018 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg6850c442010-12-16 00:32:38 +01001019 int num_pages, i, count = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001020 unsigned long now;
1021 char ppb[10];
1022 int err = 0;
1023
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001024 /*
1025 * We are protected against bitmap disappearing/resizing by holding an
1026 * ldev reference (caller must have called get_ldev()).
1027 * For read/write, we are protected against changes to the bitmap by
1028 * the bitmap lock (see drbd_bitmap_io).
1029 * For lazy writeout, we don't care for ongoing changes to the bitmap,
1030 * as we submit copies of pages anyways.
1031 */
1032 if (!ctx.flags)
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001033 WARN_ON(!(BM_LOCKED_MASK & b->bm_flags));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001034
Lars Ellenberg6850c442010-12-16 00:32:38 +01001035 num_pages = b->bm_number_of_pages;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001036
Philipp Reisnerb411b362009-09-25 16:07:19 -07001037 now = jiffies;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001038
1039 /* let the layers below us try to merge these bios... */
Lars Ellenberg6850c442010-12-16 00:32:38 +01001040 for (i = 0; i < num_pages; i++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001041 /* ignore completely unchanged pages */
1042 if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
1043 break;
1044 if (rw & WRITE) {
1045 if (bm_test_page_unchanged(b->bm_pages[i])) {
1046 dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
1047 continue;
1048 }
1049 /* during lazy writeout,
1050 * ignore those pages not marked for lazy writeout. */
1051 if (lazy_writeout_upper_idx &&
1052 !bm_test_page_lazy_writeout(b->bm_pages[i])) {
1053 dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
1054 continue;
1055 }
1056 }
1057 atomic_inc(&ctx.in_flight);
1058 bm_page_io_async(&ctx, i, rw);
1059 ++count;
1060 cond_resched();
1061 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001062
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001063 /*
1064 * We initialize ctx.in_flight to one to make sure bm_async_io_complete
1065 * will not complete() early, and decrement / test it here. If there
1066 * are still some bios in flight, we need to wait for them here.
1067 */
1068 if (!atomic_dec_and_test(&ctx.in_flight))
1069 wait_for_completion(&ctx.done);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001070 dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
1071 rw == WRITE ? "WRITE" : "READ",
1072 count, jiffies - now);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001073
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001074 if (ctx.error) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075 dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001076 drbd_chk_io_error(mdev, 1, true);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001077 err = -EIO; /* ctx.error ? */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001078 }
1079
1080 now = jiffies;
1081 if (rw == WRITE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082 drbd_md_flush(mdev);
1083 } else /* rw == READ */ {
Lars Ellenberg95a0f102010-12-15 08:59:09 +01001084 b->bm_set = bm_count_bits(b);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001085 dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
1086 jiffies - now);
1087 }
1088 now = b->bm_set;
1089
1090 dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
1091 ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
1092
1093 return err;
1094}
1095
1096/**
1097 * drbd_bm_read() - Read the whole bitmap from its on disk location.
1098 * @mdev: DRBD device.
1099 */
1100int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
1101{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001102 return bm_rw(mdev, READ, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001103}
1104
1105/**
1106 * drbd_bm_write() - Write the whole bitmap to its on disk location.
1107 * @mdev: DRBD device.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001108 *
1109 * Will only write pages that have changed since last IO.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001110 */
1111int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
1112{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001113 return bm_rw(mdev, WRITE, 0);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001114}
1115
1116/**
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001117 * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001118 * @mdev: DRBD device.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001119 * @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
Philipp Reisnerb411b362009-09-25 16:07:19 -07001120 */
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001121int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001122{
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001123 return bm_rw(mdev, WRITE, upper_idx);
1124}
Philipp Reisnerb411b362009-09-25 16:07:19 -07001125
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001126
1127/**
1128 * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
1129 * @mdev: DRBD device.
1130 * @idx: bitmap page index
1131 *
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001132 * We don't want to special case on logical_block_size of the backend device,
1133 * so we submit PAGE_SIZE aligned pieces.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001134 * Note that on "most" systems, PAGE_SIZE is 4k.
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001135 *
1136 * In case this becomes an issue on systems with larger PAGE_SIZE,
1137 * we may want to change this again to write 4k aligned 4k pieces.
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001138 */
1139int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
1140{
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001141 struct bm_aio_ctx ctx = {
1142 .mdev = mdev,
1143 .in_flight = ATOMIC_INIT(1),
1144 .done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
1145 .flags = BM_AIO_COPY_PAGES,
1146 };
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001147
1148 if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
Lars Ellenberg7648cdf2010-12-17 23:58:41 +01001149 dynamic_dev_dbg(DEV, "skipped bm page write for idx %u\n", idx);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001150 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001151 }
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001152
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001153 bm_page_io_async(&ctx, idx, WRITE_SYNC);
Lars Ellenberg725a97e2010-12-19 11:29:55 +01001154 wait_for_completion(&ctx.done);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001155
1156 if (ctx.error)
1157 drbd_chk_io_error(mdev, 1, true);
1158 /* that should force detach, so the in memory bitmap will be
1159 * gone in a moment as well. */
1160
Philipp Reisnerb411b362009-09-25 16:07:19 -07001161 mdev->bm_writ_cnt++;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001162 return ctx.error;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001163}
1164
1165/* NOTE
1166 * find_first_bit returns int, we return unsigned long.
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001167 * For this to work on 32bit arch with bitnumbers > (1<<32),
1168 * we'd need to return u64, and get a whole lot of other places
1169 * fixed where we still use unsigned long.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170 *
1171 * this returns a bit number, NOT a sector!
1172 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001173static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
1174 const int find_zero_bit, const enum km_type km)
1175{
1176 struct drbd_bitmap *b = mdev->bitmap;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001177 unsigned long *p_addr;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001178 unsigned long bit_offset;
1179 unsigned i;
1180
Philipp Reisnerb411b362009-09-25 16:07:19 -07001181
1182 if (bm_fo > b->bm_bits) {
1183 dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001184 bm_fo = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001185 } else {
1186 while (bm_fo < b->bm_bits) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001187 /* bit offset of the first bit in the page */
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001188 bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001189 p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001190
1191 if (find_zero_bit)
Linus Torvalds7e599e62011-03-28 07:42:58 -07001192 i = find_next_zero_bit_le(p_addr,
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001193 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001194 else
Linus Torvalds7e599e62011-03-28 07:42:58 -07001195 i = find_next_bit_le(p_addr,
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001196 PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001197
1198 __bm_unmap(p_addr, km);
1199 if (i < PAGE_SIZE*8) {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001200 bm_fo = bit_offset + i;
1201 if (bm_fo >= b->bm_bits)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202 break;
1203 goto found;
1204 }
1205 bm_fo = bit_offset + PAGE_SIZE*8;
1206 }
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001207 bm_fo = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001208 }
1209 found:
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001210 return bm_fo;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001211}
1212
1213static unsigned long bm_find_next(struct drbd_conf *mdev,
1214 unsigned long bm_fo, const int find_zero_bit)
1215{
1216 struct drbd_bitmap *b = mdev->bitmap;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001217 unsigned long i = DRBD_END_OF_BITMAP;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001218
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001219 if (!expect(b))
1220 return i;
1221 if (!expect(b->bm_pages))
1222 return i;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001223
1224 spin_lock_irq(&b->bm_lock);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001225 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001226 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{
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001251 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001252 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{
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001257 /* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001258 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. */
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +02001267static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Lars Ellenberg829c6082011-06-03 21:18:13 +02001268 unsigned long e, int val)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001269{
1270 struct drbd_bitmap *b = mdev->bitmap;
1271 unsigned long *p_addr = NULL;
1272 unsigned long bitnr;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001273 unsigned int last_page_nr = -1U;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001274 int c = 0;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001275 int changed_total = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001276
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++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001283 unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001284 if (page_nr != last_page_nr) {
1285 if (p_addr)
Lars Ellenberg829c6082011-06-03 21:18:13 +02001286 __bm_unmap(p_addr, KM_IRQ1);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001287 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;
Lars Ellenberg829c6082011-06-03 21:18:13 +02001293 p_addr = __bm_map_pidx(b, page_nr, KM_IRQ1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001294 last_page_nr = page_nr;
1295 }
1296 if (val)
Linus Torvalds7e599e62011-03-28 07:42:58 -07001297 c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001298 else
Linus Torvalds7e599e62011-03-28 07:42:58 -07001299 c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001300 }
1301 if (p_addr)
Lars Ellenberg829c6082011-06-03 21:18:13 +02001302 __bm_unmap(p_addr, KM_IRQ1);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001303 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;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001310}
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 */
Philipp Reisnerb4ee79d2010-04-01 09:57:40 +02001316static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
Philipp Reisnerb411b362009-09-25 16:07:19 -07001317 const unsigned long e, int val)
1318{
1319 unsigned long flags;
1320 struct drbd_bitmap *b = mdev->bitmap;
1321 int c = 0;
1322
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001323 if (!expect(b))
1324 return 1;
1325 if (!expect(b->bm_pages))
1326 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001327
1328 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001329 if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001330 bm_print_lock_info(mdev);
1331
Lars Ellenberg829c6082011-06-03 21:18:13 +02001332 c = __bm_change_bits_to(mdev, s, e, val);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001333
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;
Lars Ellenberg829c6082011-06-03 21:18:13 +02001357 unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_IRQ1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001358 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 }
Lars Ellenberg829c6082011-06-03 21:18:13 +02001363 kunmap_atomic(paddr, KM_IRQ1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001364}
1365
Lars Ellenberg829c6082011-06-03 21:18:13 +02001366/* Same thing as drbd_bm_set_bits,
1367 * but more efficient for a large bit range.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001368 * 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 */
Lars Ellenberg829c6082011-06-03 21:18:13 +02001381 struct drbd_bitmap *b = mdev->bitmap;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001382 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. */
Lars Ellenberg829c6082011-06-03 21:18:13 +02001392 spin_lock_irq(&b->bm_lock);
1393 __bm_change_bits_to(mdev, s, e, 1);
1394 spin_unlock_irq(&b->bm_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001395 return;
1396 }
1397
1398 /* difference is large enough that we can trust sl and el */
1399
Lars Ellenberg829c6082011-06-03 21:18:13 +02001400 spin_lock_irq(&b->bm_lock);
1401
Philipp Reisnerb411b362009-09-25 16:07:19 -07001402 /* bits filling the current long */
1403 if (sl)
Lars Ellenberg829c6082011-06-03 21:18:13 +02001404 __bm_change_bits_to(mdev, s, sl-1, 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001405
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);
Lars Ellenberg8ccee202011-06-06 11:31:42 +02001417 spin_unlock_irq(&b->bm_lock);
1418 cond_resched();
Philipp Reisnerb411b362009-09-25 16:07:19 -07001419 first_word = 0;
Lars Ellenberg8ccee202011-06-06 11:31:42 +02001420 spin_lock_irq(&b->bm_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001421 }
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)
Lars Ellenberg829c6082011-06-03 21:18:13 +02001433 __bm_change_bits_to(mdev, el, e, 1);
1434 spin_unlock_irq(&b->bm_lock);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001435}
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
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001451 if (!expect(b))
1452 return 0;
1453 if (!expect(b->bm_pages))
1454 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001455
1456 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001457 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001458 bm_print_lock_info(mdev);
1459 if (bitnr < b->bm_bits) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001460 p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
Linus Torvalds7e599e62011-03-28 07:42:58 -07001461 i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001462 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;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001479 unsigned long *p_addr = NULL;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001480 unsigned long bitnr;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001481 unsigned int page_nr = -1U;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001482 int c = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001483
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) */
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001488 if (!expect(b))
1489 return 1;
1490 if (!expect(b->bm_pages))
1491 return 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001492
1493 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001494 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001495 bm_print_lock_info(mdev);
1496 for (bitnr = s; bitnr <= e; bitnr++) {
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001497 unsigned int idx = bm_bit_to_page_idx(b, bitnr);
1498 if (page_nr != idx) {
1499 page_nr = idx;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001500 if (p_addr)
1501 bm_unmap(p_addr);
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001502 p_addr = bm_map_pidx(b, idx);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001503 }
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001504 if (expect(bitnr < b->bm_bits))
Linus Torvalds7e599e62011-03-28 07:42:58 -07001505 c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001506 else
1507 dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001508 }
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
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001537 if (!expect(b))
1538 return 0;
1539 if (!expect(b->bm_pages))
1540 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001541
1542 spin_lock_irqsave(&b->bm_lock, flags);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001543 if (BM_DONT_TEST & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001544 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;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001551 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001552 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
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001563/* Set all bits covered by the AL-extent al_enr.
1564 * Returns number of bits changed. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001565unsigned 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;
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001570 unsigned long s, e;
1571 int count, i, do_now;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001572 if (!expect(b))
1573 return 0;
1574 if (!expect(b->bm_pages))
1575 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001576
1577 spin_lock_irq(&b->bm_lock);
Lars Ellenberg20ceb2b2011-01-21 10:56:44 +01001578 if (BM_DONT_SET & b->bm_flags)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001579 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;
Lars Ellenberg19f843a2010-12-15 08:59:11 +01001590 p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
Philipp Reisnerb411b362009-09-25 16:07:19 -07001591 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 {
Lars Ellenberg4b0715f2010-12-14 15:13:04 +01001602 dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001603 }
1604 weight = b->bm_set - weight;
1605 spin_unlock_irq(&b->bm_lock);
1606 return weight;
1607}