drbd_bitmap.c 44.7 KB
Newer Older
P
Philipp Reisner 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/*
   drbd_bitmap.c

   This file is part of DRBD by Philipp Reisner and Lars Ellenberg.

   Copyright (C) 2004-2008, LINBIT Information Technologies GmbH.
   Copyright (C) 2004-2008, Philipp Reisner <philipp.reisner@linbit.com>.
   Copyright (C) 2004-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.

   drbd is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   drbd is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with drbd; see the file COPYING.  If not, write to
   the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/bitops.h>
#include <linux/vmalloc.h>
#include <linux/string.h>
#include <linux/drbd.h>
29
#include <linux/slab.h>
P
Philipp Reisner 已提交
30
#include <asm/kmap_types.h>
31

P
Philipp Reisner 已提交
32 33
#include "drbd_int.h"

34

P
Philipp Reisner 已提交
35 36 37 38 39 40
/* OPAQUE outside this file!
 * interface defined in drbd_int.h

 * convention:
 * function name drbd_bm_... => used elsewhere, "public".
 * function name      bm_... => internal to implementation, "private".
41 42
 */

P
Philipp Reisner 已提交
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
/*
 * LIMITATIONS:
 * We want to support >= peta byte of backend storage, while for now still using
 * a granularity of one bit per 4KiB of storage.
 * 1 << 50		bytes backend storage (1 PiB)
 * 1 << (50 - 12)	bits needed
 *	38 --> we need u64 to index and count bits
 * 1 << (38 - 3)	bitmap bytes needed
 *	35 --> we still need u64 to index and count bytes
 *			(that's 32 GiB of bitmap for 1 PiB storage)
 * 1 << (35 - 2)	32bit longs needed
 *	33 --> we'd even need u64 to index and count 32bit long words.
 * 1 << (35 - 3)	64bit longs needed
 *	32 --> we could get away with a 32bit unsigned int to index and count
 *	64bit long words, but I rather stay with unsigned long for now.
 *	We probably should neither count nor point to bytes or long words
 *	directly, but either by bitnumber, or by page index and offset.
 * 1 << (35 - 12)
 *	22 --> we need that much 4KiB pages of bitmap.
 *	1 << (22 + 3) --> on a 64bit arch,
 *	we need 32 MiB to store the array of page pointers.
 *
 * Because I'm lazy, and because the resulting patch was too large, too ugly
 * and still incomplete, on 32bit we still "only" support 16 TiB (minus some),
 * (1 << 32) bits * 4k storage.
 *

 * bitmap storage and IO:
 *	Bitmap is stored little endian on disk, and is kept little endian in
 *	core memory. Currently we still hold the full bitmap in core as long
 *	as we are "attached" to a local disk, which at 32 GiB for 1PiB storage
 *	seems excessive.
 *
B
Bart Van Assche 已提交
77
 *	We plan to reduce the amount of in-core bitmap pages by paging them in
78 79 80
 *	and out against their on-disk location as necessary, but need to make
 *	sure we don't cause too much meta data IO, and must not deadlock in
 *	tight memory situations. This needs some more work.
P
Philipp Reisner 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
 */

/*
 * NOTE
 *  Access to the *bm_pages is protected by bm_lock.
 *  It is safe to read the other members within the lock.
 *
 *  drbd_bm_set_bits is called from bio_endio callbacks,
 *  We may be called with irq already disabled,
 *  so we need spin_lock_irqsave().
 *  And we need the kmap_atomic.
 */
struct drbd_bitmap {
	struct page **bm_pages;
	spinlock_t bm_lock;
96 97 98

	/* see LIMITATIONS: above */

P
Philipp Reisner 已提交
99 100 101 102 103
	unsigned long bm_set;       /* nr of set bits; THINK maybe atomic_t? */
	unsigned long bm_bits;
	size_t   bm_words;
	size_t   bm_number_of_pages;
	sector_t bm_dev_capacity;
104
	struct mutex bm_change; /* serializes resize operations */
P
Philipp Reisner 已提交
105

106
	wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */
P
Philipp Reisner 已提交
107

108
	enum bm_flag bm_flags;
P
Philipp Reisner 已提交
109 110 111 112 113 114 115 116 117 118 119 120 121

	/* debugging aid, in case we are still racy somewhere */
	char          *bm_why;
	struct task_struct *bm_task;
};

#define bm_print_lock_info(m) __bm_print_lock_info(m, __func__)
static void __bm_print_lock_info(struct drbd_conf *mdev, const char *func)
{
	struct drbd_bitmap *b = mdev->bitmap;
	if (!__ratelimit(&drbd_ratelimit_state))
		return;
	dev_err(DEV, "FIXME %s in %s, bitmap locked for '%s' by %s\n",
122 123 124
	    current == mdev->tconn->receiver.task ? "receiver" :
	    current == mdev->tconn->asender.task  ? "asender"  :
	    current == mdev->tconn->worker.task   ? "worker"   : current->comm,
P
Philipp Reisner 已提交
125
	    func, b->bm_why ?: "?",
126 127 128
	    b->bm_task == mdev->tconn->receiver.task ? "receiver" :
	    b->bm_task == mdev->tconn->asender.task  ? "asender"  :
	    b->bm_task == mdev->tconn->worker.task   ? "worker"   : "?");
P
Philipp Reisner 已提交
129 130
}

131
void drbd_bm_lock(struct drbd_conf *mdev, char *why, enum bm_flag flags)
P
Philipp Reisner 已提交
132 133 134 135 136 137 138 139 140
{
	struct drbd_bitmap *b = mdev->bitmap;
	int trylock_failed;

	if (!b) {
		dev_err(DEV, "FIXME no bitmap in drbd_bm_lock!?\n");
		return;
	}

141
	trylock_failed = !mutex_trylock(&b->bm_change);
P
Philipp Reisner 已提交
142 143 144

	if (trylock_failed) {
		dev_warn(DEV, "%s going to '%s' but bitmap already locked for '%s' by %s\n",
145 146 147
		    current == mdev->tconn->receiver.task ? "receiver" :
		    current == mdev->tconn->asender.task  ? "asender"  :
		    current == mdev->tconn->worker.task   ? "worker"   : current->comm,
P
Philipp Reisner 已提交
148
		    why, b->bm_why ?: "?",
149 150 151
		    b->bm_task == mdev->tconn->receiver.task ? "receiver" :
		    b->bm_task == mdev->tconn->asender.task  ? "asender"  :
		    b->bm_task == mdev->tconn->worker.task   ? "worker"   : "?");
152
		mutex_lock(&b->bm_change);
P
Philipp Reisner 已提交
153
	}
154
	if (BM_LOCKED_MASK & b->bm_flags)
P
Philipp Reisner 已提交
155
		dev_err(DEV, "FIXME bitmap already locked in bm_lock\n");
156
	b->bm_flags |= flags & BM_LOCKED_MASK;
P
Philipp Reisner 已提交
157 158 159 160 161 162 163 164 165 166 167 168 169

	b->bm_why  = why;
	b->bm_task = current;
}

void drbd_bm_unlock(struct drbd_conf *mdev)
{
	struct drbd_bitmap *b = mdev->bitmap;
	if (!b) {
		dev_err(DEV, "FIXME no bitmap in drbd_bm_unlock!?\n");
		return;
	}

170
	if (!(BM_LOCKED_MASK & mdev->bitmap->bm_flags))
P
Philipp Reisner 已提交
171 172
		dev_err(DEV, "FIXME bitmap not locked in bm_unlock\n");

173
	b->bm_flags &= ~BM_LOCKED_MASK;
P
Philipp Reisner 已提交
174 175
	b->bm_why  = NULL;
	b->bm_task = NULL;
176
	mutex_unlock(&b->bm_change);
P
Philipp Reisner 已提交
177 178
}

179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
/* we store some "meta" info about our pages in page->private */
/* at a granularity of 4k storage per bitmap bit:
 * one peta byte storage: 1<<50 byte, 1<<38 * 4k storage blocks
 *  1<<38 bits,
 *  1<<23 4k bitmap pages.
 * Use 24 bits as page index, covers 2 peta byte storage
 * at a granularity of 4k per bit.
 * Used to report the failed page idx on io error from the endio handlers.
 */
#define BM_PAGE_IDX_MASK	((1UL<<24)-1)
/* this page is currently read in, or written back */
#define BM_PAGE_IO_LOCK		31
/* if there has been an IO error for this page */
#define BM_PAGE_IO_ERROR	30
/* this is to be able to intelligently skip disk IO,
 * set if bits have been set since last IO. */
#define BM_PAGE_NEED_WRITEOUT	29
/* to mark for lazy writeout once syncer cleared all clearable bits,
 * we if bits have been cleared since last IO. */
#define BM_PAGE_LAZY_WRITEOUT	28

B
Bart Van Assche 已提交
200
/* store_page_idx uses non-atomic assignment. It is only used directly after
201 202 203 204 205
 * allocating the page.  All other bm_set_page_* and bm_clear_page_* need to
 * use atomic bit manipulation, as set_out_of_sync (and therefore bitmap
 * changes) may happen from various contexts, and wait_on_bit/wake_up_bit
 * requires it all to be atomic as well. */
static void bm_store_page_idx(struct page *page, unsigned long idx)
206
{
207 208
	BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
	page_private(page) |= idx;
209 210
}

211
static unsigned long bm_page_to_idx(struct page *page)
P
Philipp Reisner 已提交
212
{
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	return page_private(page) & BM_PAGE_IDX_MASK;
}

/* As is very unlikely that the same page is under IO from more than one
 * context, we can get away with a bit per page and one wait queue per bitmap.
 */
static void bm_page_lock_io(struct drbd_conf *mdev, int page_nr)
{
	struct drbd_bitmap *b = mdev->bitmap;
	void *addr = &page_private(b->bm_pages[page_nr]);
	wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
}

static void bm_page_unlock_io(struct drbd_conf *mdev, int page_nr)
{
	struct drbd_bitmap *b = mdev->bitmap;
	void *addr = &page_private(b->bm_pages[page_nr]);
	clear_bit(BM_PAGE_IO_LOCK, addr);
	smp_mb__after_clear_bit();
	wake_up(&mdev->bitmap->bm_io_wait);
}

/* set _before_ submit_io, so it may be reset due to being changed
 * while this page is in flight... will get submitted later again */
static void bm_set_page_unchanged(struct page *page)
{
	/* use cmpxchg? */
	clear_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
	clear_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
}

static void bm_set_page_need_writeout(struct page *page)
{
	set_bit(BM_PAGE_NEED_WRITEOUT, &page_private(page));
}

static int bm_test_page_unchanged(struct page *page)
{
	volatile const unsigned long *addr = &page_private(page);
	return (*addr & ((1UL<<BM_PAGE_NEED_WRITEOUT)|(1UL<<BM_PAGE_LAZY_WRITEOUT))) == 0;
}
P
Philipp Reisner 已提交
254

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
static void bm_set_page_io_err(struct page *page)
{
	set_bit(BM_PAGE_IO_ERROR, &page_private(page));
}

static void bm_clear_page_io_err(struct page *page)
{
	clear_bit(BM_PAGE_IO_ERROR, &page_private(page));
}

static void bm_set_page_lazy_writeout(struct page *page)
{
	set_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
}

static int bm_test_page_lazy_writeout(struct page *page)
{
	return test_bit(BM_PAGE_LAZY_WRITEOUT, &page_private(page));
}

/* on a 32bit box, this would allow for exactly (2<<38) bits. */
static unsigned int bm_word_to_page_idx(struct drbd_bitmap *b, unsigned long long_nr)
{
P
Philipp Reisner 已提交
278
	/* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
279
	unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
P
Philipp Reisner 已提交
280
	BUG_ON(page_nr >= b->bm_number_of_pages);
281 282
	return page_nr;
}
P
Philipp Reisner 已提交
283

284 285 286 287 288 289
static unsigned int bm_bit_to_page_idx(struct drbd_bitmap *b, u64 bitnr)
{
	/* page_nr = (bitnr/8) >> PAGE_SHIFT; */
	unsigned int page_nr = bitnr >> (PAGE_SHIFT + 3);
	BUG_ON(page_nr >= b->bm_number_of_pages);
	return page_nr;
P
Philipp Reisner 已提交
290 291
}

292 293 294 295 296 297 298 299 300 301 302
static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx, const enum km_type km)
{
	struct page *page = b->bm_pages[idx];
	return (unsigned long *) kmap_atomic(page, km);
}

static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
{
	return __bm_map_pidx(b, idx, KM_IRQ1);
}

P
Philipp Reisner 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
static void __bm_unmap(unsigned long *p_addr, const enum km_type km)
{
	kunmap_atomic(p_addr, km);
};

static void bm_unmap(unsigned long *p_addr)
{
	return __bm_unmap(p_addr, KM_IRQ1);
}

/* long word offset of _bitmap_ sector */
#define S2W(s)	((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
/* word offset from start of bitmap to word number _in_page_
 * modulo longs per page
#define MLPP(X) ((X) % (PAGE_SIZE/sizeof(long))
B
Bart Van Assche 已提交
318
 hm, well, Philipp thinks gcc might not optimize the % into & (... - 1)
P
Philipp Reisner 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331
 so do it explicitly:
 */
#define MLPP(X) ((X) & ((PAGE_SIZE/sizeof(long))-1))

/* Long words per page */
#define LWPP (PAGE_SIZE/sizeof(long))

/*
 * actually most functions herein should take a struct drbd_bitmap*, not a
 * struct drbd_conf*, but for the debug macros I like to have the mdev around
 * to be able to report device specific.
 */

332

P
Philipp Reisner 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
static void bm_free_pages(struct page **pages, unsigned long number)
{
	unsigned long i;
	if (!pages)
		return;

	for (i = 0; i < number; i++) {
		if (!pages[i]) {
			printk(KERN_ALERT "drbd: bm_free_pages tried to free "
					  "a NULL pointer; i=%lu n=%lu\n",
					  i, number);
			continue;
		}
		__free_page(pages[i]);
		pages[i] = NULL;
	}
}

static void bm_vk_free(void *ptr, int v)
{
	if (v)
		vfree(ptr);
	else
		kfree(ptr);
}

/*
 * "have" and "want" are NUMBER OF PAGES.
 */
static struct page **bm_realloc_pages(struct drbd_bitmap *b, unsigned long want)
{
	struct page **old_pages = b->bm_pages;
	struct page **new_pages, *page;
	unsigned int i, bytes, vmalloced = 0;
	unsigned long have = b->bm_number_of_pages;

	BUG_ON(have == 0 && old_pages != NULL);
	BUG_ON(have != 0 && old_pages == NULL);

	if (have == want)
		return old_pages;

	/* Trying kmalloc first, falling back to vmalloc.
	 * GFP_KERNEL is ok, as this is done when a lower level disk is
	 * "attached" to the drbd.  Context is receiver thread or cqueue
	 * thread.  As we have no disk yet, we are not in the IO path,
	 * not even the IO path of the peer. */
	bytes = sizeof(struct page *)*want;
	new_pages = kmalloc(bytes, GFP_KERNEL);
	if (!new_pages) {
		new_pages = vmalloc(bytes);
		if (!new_pages)
			return NULL;
		vmalloced = 1;
	}

	memset(new_pages, 0, bytes);
	if (want >= have) {
		for (i = 0; i < have; i++)
			new_pages[i] = old_pages[i];
		for (; i < want; i++) {
			page = alloc_page(GFP_HIGHUSER);
			if (!page) {
				bm_free_pages(new_pages + have, i - have);
				bm_vk_free(new_pages, vmalloced);
				return NULL;
			}
400 401 402
			/* we want to know which page it is
			 * from the endio handlers */
			bm_store_page_idx(page, i);
P
Philipp Reisner 已提交
403 404 405 406 407 408 409 410 411 412 413
			new_pages[i] = page;
		}
	} else {
		for (i = 0; i < want; i++)
			new_pages[i] = old_pages[i];
		/* NOT HERE, we are outside the spinlock!
		bm_free_pages(old_pages + want, have - want);
		*/
	}

	if (vmalloced)
414
		b->bm_flags |= BM_P_VMALLOCED;
P
Philipp Reisner 已提交
415
	else
416
		b->bm_flags &= ~BM_P_VMALLOCED;
P
Philipp Reisner 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432

	return new_pages;
}

/*
 * called on driver init only. TODO call when a device is created.
 * allocates the drbd_bitmap, and stores it in mdev->bitmap.
 */
int drbd_bm_init(struct drbd_conf *mdev)
{
	struct drbd_bitmap *b = mdev->bitmap;
	WARN_ON(b != NULL);
	b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
	if (!b)
		return -ENOMEM;
	spin_lock_init(&b->bm_lock);
433
	mutex_init(&b->bm_change);
P
Philipp Reisner 已提交
434 435 436 437 438 439 440 441 442
	init_waitqueue_head(&b->bm_io_wait);

	mdev->bitmap = b;

	return 0;
}

sector_t drbd_bm_capacity(struct drbd_conf *mdev)
{
443 444
	if (!expect(mdev->bitmap))
		return 0;
P
Philipp Reisner 已提交
445 446 447 448 449 450 451
	return mdev->bitmap->bm_dev_capacity;
}

/* called on driver unload. TODO: call when a device is destroyed.
 */
void drbd_bm_cleanup(struct drbd_conf *mdev)
{
452 453
	if (!expect(mdev->bitmap))
		return;
P
Philipp Reisner 已提交
454
	bm_free_pages(mdev->bitmap->bm_pages, mdev->bitmap->bm_number_of_pages);
455
	bm_vk_free(mdev->bitmap->bm_pages, (BM_P_VMALLOCED & mdev->bitmap->bm_flags));
P
Philipp Reisner 已提交
456 457 458 459 460 461 462 463 464
	kfree(mdev->bitmap);
	mdev->bitmap = NULL;
}

/*
 * since (b->bm_bits % BITS_PER_LONG) != 0,
 * this masks out the remaining bits.
 * Returns the number of bits cleared.
 */
465 466 467
#define BITS_PER_PAGE		(1UL << (PAGE_SHIFT + 3))
#define BITS_PER_PAGE_MASK	(BITS_PER_PAGE - 1)
#define BITS_PER_LONG_MASK	(BITS_PER_LONG - 1)
P
Philipp Reisner 已提交
468 469
static int bm_clear_surplus(struct drbd_bitmap *b)
{
470
	unsigned long mask;
P
Philipp Reisner 已提交
471
	unsigned long *p_addr, *bm;
472 473
	int tmp;
	int cleared = 0;
P
Philipp Reisner 已提交
474

475 476 477 478 479 480 481 482
	/* number of bits modulo bits per page */
	tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
	/* mask the used bits of the word containing the last bit */
	mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
	/* bitmap is always stored little endian,
	 * on disk and in core memory alike */
	mask = cpu_to_lel(mask);

483
	p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
484 485 486 487 488 489
	bm = p_addr + (tmp/BITS_PER_LONG);
	if (mask) {
		/* If mask != 0, we are not exactly aligned, so bm now points
		 * to the long containing the last bit.
		 * If mask == 0, bm already points to the word immediately
		 * after the last (long word aligned) bit. */
P
Philipp Reisner 已提交
490 491
		cleared = hweight_long(*bm & ~mask);
		*bm &= mask;
492
		bm++;
P
Philipp Reisner 已提交
493 494
	}

495 496 497
	if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
		/* on a 32bit arch, we may need to zero out
		 * a padding long to align with a 64bit remote */
P
Philipp Reisner 已提交
498 499 500 501 502 503 504 505 506
		cleared += hweight_long(*bm);
		*bm = 0;
	}
	bm_unmap(p_addr);
	return cleared;
}

static void bm_set_surplus(struct drbd_bitmap *b)
{
507
	unsigned long mask;
P
Philipp Reisner 已提交
508
	unsigned long *p_addr, *bm;
509 510 511 512 513 514 515 516 517 518
	int tmp;

	/* number of bits modulo bits per page */
	tmp = (b->bm_bits & BITS_PER_PAGE_MASK);
	/* mask the used bits of the word containing the last bit */
	mask = (1UL << (tmp & BITS_PER_LONG_MASK)) -1;
	/* bitmap is always stored little endian,
	 * on disk and in core memory alike */
	mask = cpu_to_lel(mask);

519
	p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
520 521 522 523 524 525
	bm = p_addr + (tmp/BITS_PER_LONG);
	if (mask) {
		/* If mask != 0, we are not exactly aligned, so bm now points
		 * to the long containing the last bit.
		 * If mask == 0, bm already points to the word immediately
		 * after the last (long word aligned) bit. */
P
Philipp Reisner 已提交
526
		*bm |= ~mask;
527
		bm++;
P
Philipp Reisner 已提交
528 529
	}

530 531 532 533
	if (BITS_PER_LONG == 32 && ((bm - p_addr) & 1) == 1) {
		/* on a 32bit arch, we may need to zero out
		 * a padding long to align with a 64bit remote */
		*bm = ~0UL;
P
Philipp Reisner 已提交
534 535 536 537
	}
	bm_unmap(p_addr);
}

538 539
/* you better not modify the bitmap while this is running,
 * or its results will be stale */
540
static unsigned long bm_count_bits(struct drbd_bitmap *b)
P
Philipp Reisner 已提交
541
{
542
	unsigned long *p_addr;
P
Philipp Reisner 已提交
543
	unsigned long bits = 0;
544
	unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1;
545
	int idx, i, last_word;
546 547

	/* all but last page */
548
	for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) {
549 550 551
		p_addr = __bm_map_pidx(b, idx, KM_USER0);
		for (i = 0; i < LWPP; i++)
			bits += hweight_long(p_addr[i]);
552
		__bm_unmap(p_addr, KM_USER0);
P
Philipp Reisner 已提交
553 554
		cond_resched();
	}
555 556 557 558 559 560 561 562 563 564 565
	/* last (or only) page */
	last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL;
	p_addr = __bm_map_pidx(b, idx, KM_USER0);
	for (i = 0; i < last_word; i++)
		bits += hweight_long(p_addr[i]);
	p_addr[last_word] &= cpu_to_lel(mask);
	bits += hweight_long(p_addr[last_word]);
	/* 32bit arch, may have an unused padding long */
	if (BITS_PER_LONG == 32 && (last_word & 1) == 0)
		p_addr[last_word+1] = 0;
	__bm_unmap(p_addr, KM_USER0);
P
Philipp Reisner 已提交
566 567 568 569 570 571 572
	return bits;
}

/* offset and len in long words.*/
static void bm_memset(struct drbd_bitmap *b, size_t offset, int c, size_t len)
{
	unsigned long *p_addr, *bm;
573
	unsigned int idx;
P
Philipp Reisner 已提交
574 575 576 577 578 579 580 581 582 583 584
	size_t do_now, end;

	end = offset + len;

	if (end > b->bm_words) {
		printk(KERN_ALERT "drbd: bm_memset end > bm_words\n");
		return;
	}

	while (offset < end) {
		do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
585 586
		idx = bm_word_to_page_idx(b, offset);
		p_addr = bm_map_pidx(b, idx);
P
Philipp Reisner 已提交
587 588 589 590
		bm = p_addr + MLPP(offset);
		if (bm+do_now > p_addr + LWPP) {
			printk(KERN_ALERT "drbd: BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
			       p_addr, bm, (int)do_now);
591 592
		} else
			memset(bm, c, do_now * sizeof(long));
P
Philipp Reisner 已提交
593
		bm_unmap(p_addr);
594
		bm_set_page_need_writeout(b->bm_pages[idx]);
P
Philipp Reisner 已提交
595 596 597 598 599 600 601 602 603 604 605 606
		offset += do_now;
	}
}

/*
 * make sure the bitmap has enough room for the attached storage,
 * if necessary, resize.
 * called whenever we may have changed the device size.
 * returns -ENOMEM if we could not allocate enough memory, 0 on success.
 * In case this is actually a resize, we copy the old bitmap into the new one.
 * Otherwise, the bitmap is initialized to all bits set.
 */
607
int drbd_bm_resize(struct drbd_conf *mdev, sector_t capacity, int set_new_bits)
P
Philipp Reisner 已提交
608 609
{
	struct drbd_bitmap *b = mdev->bitmap;
610
	unsigned long bits, words, owords, obits;
P
Philipp Reisner 已提交
611 612 613 614 615
	unsigned long want, have, onpages; /* number of pages */
	struct page **npages, **opages = NULL;
	int err = 0, growing;
	int opages_vmalloced;

616 617
	if (!expect(b))
		return -ENOMEM;
P
Philipp Reisner 已提交
618

619
	drbd_bm_lock(mdev, "resize", BM_LOCKED_MASK);
P
Philipp Reisner 已提交
620 621 622 623 624 625 626

	dev_info(DEV, "drbd_bm_resize called with capacity == %llu\n",
			(unsigned long long)capacity);

	if (capacity == b->bm_dev_capacity)
		goto out;

627
	opages_vmalloced = (BM_P_VMALLOCED & b->bm_flags);
P
Philipp Reisner 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654

	if (capacity == 0) {
		spin_lock_irq(&b->bm_lock);
		opages = b->bm_pages;
		onpages = b->bm_number_of_pages;
		owords = b->bm_words;
		b->bm_pages = NULL;
		b->bm_number_of_pages =
		b->bm_set   =
		b->bm_bits  =
		b->bm_words =
		b->bm_dev_capacity = 0;
		spin_unlock_irq(&b->bm_lock);
		bm_free_pages(opages, onpages);
		bm_vk_free(opages, opages_vmalloced);
		goto out;
	}
	bits  = BM_SECT_TO_BIT(ALIGN(capacity, BM_SECT_PER_BIT));

	/* if we would use
	   words = ALIGN(bits,BITS_PER_LONG) >> LN2_BPL;
	   a 32bit host could present the wrong number of words
	   to a 64bit host.
	*/
	words = ALIGN(bits, 64) >> LN2_BPL;

	if (get_ldev(mdev)) {
655
		u64 bits_on_disk = ((u64)mdev->ldev->md.md_size_sect-MD_BM_OFFSET) << 12;
P
Philipp Reisner 已提交
656
		put_ldev(mdev);
657 658 659 660 661 662
		if (bits > bits_on_disk) {
			dev_info(DEV, "bits = %lu\n", bits);
			dev_info(DEV, "bits_on_disk = %llu\n", bits_on_disk);
			err = -ENOSPC;
			goto out;
		}
P
Philipp Reisner 已提交
663 664
	}

665
	want = ALIGN(words*sizeof(long), PAGE_SIZE) >> PAGE_SHIFT;
P
Philipp Reisner 已提交
666 667 668 669 670
	have = b->bm_number_of_pages;
	if (want == have) {
		D_ASSERT(b->bm_pages != NULL);
		npages = b->bm_pages;
	} else {
671
		if (drbd_insert_fault(mdev, DRBD_FAULT_BM_ALLOC))
P
Philipp Reisner 已提交
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
			npages = NULL;
		else
			npages = bm_realloc_pages(b, want);
	}

	if (!npages) {
		err = -ENOMEM;
		goto out;
	}

	spin_lock_irq(&b->bm_lock);
	opages = b->bm_pages;
	owords = b->bm_words;
	obits  = b->bm_bits;

	growing = bits > obits;
688
	if (opages && growing && set_new_bits)
P
Philipp Reisner 已提交
689 690 691 692 693 694 695 696 697
		bm_set_surplus(b);

	b->bm_pages = npages;
	b->bm_number_of_pages = want;
	b->bm_bits  = bits;
	b->bm_words = words;
	b->bm_dev_capacity = capacity;

	if (growing) {
698 699 700 701 702 703
		if (set_new_bits) {
			bm_memset(b, owords, 0xff, words-owords);
			b->bm_set += bits - obits;
		} else
			bm_memset(b, owords, 0x00, words-owords);

P
Philipp Reisner 已提交
704 705 706 707 708 709 710 711 712 713 714 715 716 717
	}

	if (want < have) {
		/* implicit: (opages != NULL) && (opages != npages) */
		bm_free_pages(opages + want, have - want);
	}

	(void)bm_clear_surplus(b);

	spin_unlock_irq(&b->bm_lock);
	if (opages != npages)
		bm_vk_free(opages, opages_vmalloced);
	if (!growing)
		b->bm_set = bm_count_bits(b);
718
	dev_info(DEV, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);
P
Philipp Reisner 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732

 out:
	drbd_bm_unlock(mdev);
	return err;
}

/* inherently racy:
 * if not protected by other means, return value may be out of date when
 * leaving this function...
 * we still need to lock it, since it is important that this returns
 * bm_set == 0 precisely.
 *
 * maybe bm_set should be atomic_t ?
 */
733
unsigned long _drbd_bm_total_weight(struct drbd_conf *mdev)
P
Philipp Reisner 已提交
734 735 736 737 738
{
	struct drbd_bitmap *b = mdev->bitmap;
	unsigned long s;
	unsigned long flags;

739 740 741 742
	if (!expect(b))
		return 0;
	if (!expect(b->bm_pages))
		return 0;
P
Philipp Reisner 已提交
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

	spin_lock_irqsave(&b->bm_lock, flags);
	s = b->bm_set;
	spin_unlock_irqrestore(&b->bm_lock, flags);

	return s;
}

unsigned long drbd_bm_total_weight(struct drbd_conf *mdev)
{
	unsigned long s;
	/* if I don't have a disk, I don't know about out-of-sync status */
	if (!get_ldev_if_state(mdev, D_NEGOTIATING))
		return 0;
	s = _drbd_bm_total_weight(mdev);
	put_ldev(mdev);
	return s;
}

size_t drbd_bm_words(struct drbd_conf *mdev)
{
	struct drbd_bitmap *b = mdev->bitmap;
765 766 767 768
	if (!expect(b))
		return 0;
	if (!expect(b->bm_pages))
		return 0;
P
Philipp Reisner 已提交
769 770 771 772 773 774 775

	return b->bm_words;
}

unsigned long drbd_bm_bits(struct drbd_conf *mdev)
{
	struct drbd_bitmap *b = mdev->bitmap;
776 777
	if (!expect(b))
		return 0;
P
Philipp Reisner 已提交
778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

	return b->bm_bits;
}

/* merge number words from buffer into the bitmap starting at offset.
 * buffer[i] is expected to be little endian unsigned long.
 * bitmap must be locked by drbd_bm_lock.
 * currently only used from receive_bitmap.
 */
void drbd_bm_merge_lel(struct drbd_conf *mdev, size_t offset, size_t number,
			unsigned long *buffer)
{
	struct drbd_bitmap *b = mdev->bitmap;
	unsigned long *p_addr, *bm;
	unsigned long word, bits;
793
	unsigned int idx;
P
Philipp Reisner 已提交
794 795 796 797
	size_t end, do_now;

	end = offset + number;

798 799 800 801
	if (!expect(b))
		return;
	if (!expect(b->bm_pages))
		return;
P
Philipp Reisner 已提交
802 803 804 805 806 807 808 809
	if (number == 0)
		return;
	WARN_ON(offset >= b->bm_words);
	WARN_ON(end    >  b->bm_words);

	spin_lock_irq(&b->bm_lock);
	while (offset < end) {
		do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
810 811
		idx = bm_word_to_page_idx(b, offset);
		p_addr = bm_map_pidx(b, idx);
P
Philipp Reisner 已提交
812 813 814 815
		bm = p_addr + MLPP(offset);
		offset += do_now;
		while (do_now--) {
			bits = hweight_long(*bm);
816
			word = *bm | *buffer++;
P
Philipp Reisner 已提交
817 818 819 820
			*bm++ = word;
			b->bm_set += hweight_long(word) - bits;
		}
		bm_unmap(p_addr);
821
		bm_set_page_need_writeout(b->bm_pages[idx]);
P
Philipp Reisner 已提交
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844
	}
	/* with 32bit <-> 64bit cross-platform connect
	 * this is only correct for current usage,
	 * where we _know_ that we are 64 bit aligned,
	 * and know that this function is used in this way, too...
	 */
	if (end == b->bm_words)
		b->bm_set -= bm_clear_surplus(b);
	spin_unlock_irq(&b->bm_lock);
}

/* copy number words from the bitmap starting at offset into the buffer.
 * buffer[i] will be little endian unsigned long.
 */
void drbd_bm_get_lel(struct drbd_conf *mdev, size_t offset, size_t number,
		     unsigned long *buffer)
{
	struct drbd_bitmap *b = mdev->bitmap;
	unsigned long *p_addr, *bm;
	size_t end, do_now;

	end = offset + number;

845 846 847 848
	if (!expect(b))
		return;
	if (!expect(b->bm_pages))
		return;
P
Philipp Reisner 已提交
849 850 851 852 853 854 855 856 857 858 859 860

	spin_lock_irq(&b->bm_lock);
	if ((offset >= b->bm_words) ||
	    (end    >  b->bm_words) ||
	    (number <= 0))
		dev_err(DEV, "offset=%lu number=%lu bm_words=%lu\n",
			(unsigned long)	offset,
			(unsigned long)	number,
			(unsigned long) b->bm_words);
	else {
		while (offset < end) {
			do_now = min_t(size_t, ALIGN(offset+1, LWPP), end) - offset;
861
			p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
P
Philipp Reisner 已提交
862 863 864
			bm = p_addr + MLPP(offset);
			offset += do_now;
			while (do_now--)
865
				*buffer++ = *bm++;
P
Philipp Reisner 已提交
866 867 868 869 870 871 872 873 874 875
			bm_unmap(p_addr);
		}
	}
	spin_unlock_irq(&b->bm_lock);
}

/* set all bits in the bitmap */
void drbd_bm_set_all(struct drbd_conf *mdev)
{
	struct drbd_bitmap *b = mdev->bitmap;
876 877 878 879
	if (!expect(b))
		return;
	if (!expect(b->bm_pages))
		return;
P
Philipp Reisner 已提交
880 881 882 883 884 885 886 887 888 889 890 891

	spin_lock_irq(&b->bm_lock);
	bm_memset(b, 0, 0xff, b->bm_words);
	(void)bm_clear_surplus(b);
	b->bm_set = b->bm_bits;
	spin_unlock_irq(&b->bm_lock);
}

/* clear all bits in the bitmap */
void drbd_bm_clear_all(struct drbd_conf *mdev)
{
	struct drbd_bitmap *b = mdev->bitmap;
892 893 894 895
	if (!expect(b))
		return;
	if (!expect(b->bm_pages))
		return;
P
Philipp Reisner 已提交
896 897 898 899 900 901 902

	spin_lock_irq(&b->bm_lock);
	bm_memset(b, 0, 0, b->bm_words);
	b->bm_set = 0;
	spin_unlock_irq(&b->bm_lock);
}

903 904 905
struct bm_aio_ctx {
	struct drbd_conf *mdev;
	atomic_t in_flight;
906
	struct completion done;
907 908 909 910 911 912
	unsigned flags;
#define BM_AIO_COPY_PAGES	1
	int error;
};

/* bv_page may be a copy, or may be the original */
P
Philipp Reisner 已提交
913 914
static void bm_async_io_complete(struct bio *bio, int error)
{
915 916 917 918
	struct bm_aio_ctx *ctx = bio->bi_private;
	struct drbd_conf *mdev = ctx->mdev;
	struct drbd_bitmap *b = mdev->bitmap;
	unsigned int idx = bm_page_to_idx(bio->bi_io_vec[0].bv_page);
P
Philipp Reisner 已提交
919 920 921 922 923 924 925 926 927 928
	int uptodate = bio_flagged(bio, BIO_UPTODATE);


	/* strange behavior of some lower level drivers...
	 * fail the request by clearing the uptodate flag,
	 * but do not return any error?!
	 * do we want to WARN() on this? */
	if (!error && !uptodate)
		error = -EIO;

929 930 931
	if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 &&
	    !bm_test_page_unchanged(b->bm_pages[idx]))
		dev_warn(DEV, "bitmap page idx %u changed during IO!\n", idx);
932

P
Philipp Reisner 已提交
933
	if (error) {
934 935 936 937 938 939 940 941 942 943 944 945
		/* ctx error will hold the completed-last non-zero error code,
		 * in case error codes differ. */
		ctx->error = error;
		bm_set_page_io_err(b->bm_pages[idx]);
		/* Not identical to on disk version of it.
		 * Is BM_PAGE_IO_ERROR enough? */
		if (__ratelimit(&drbd_ratelimit_state))
			dev_err(DEV, "IO ERROR %d on bitmap page idx %u\n",
					error, idx);
	} else {
		bm_clear_page_io_err(b->bm_pages[idx]);
		dynamic_dev_dbg(DEV, "bitmap page idx %u completed\n", idx);
P
Philipp Reisner 已提交
946
	}
947 948 949 950 951 952

	bm_page_unlock_io(mdev, idx);

	/* FIXME give back to page pool */
	if (ctx->flags & BM_AIO_COPY_PAGES)
		put_page(bio->bi_io_vec[0].bv_page);
P
Philipp Reisner 已提交
953 954

	bio_put(bio);
955 956

	if (atomic_dec_and_test(&ctx->in_flight))
957
		complete(&ctx->done);
P
Philipp Reisner 已提交
958 959
}

960
static void bm_page_io_async(struct bm_aio_ctx *ctx, int page_nr, int rw) __must_hold(local)
P
Philipp Reisner 已提交
961 962 963
{
	/* we are process context. we always get a bio */
	struct bio *bio = bio_alloc(GFP_KERNEL, 1);
964 965 966
	struct drbd_conf *mdev = ctx->mdev;
	struct drbd_bitmap *b = mdev->bitmap;
	struct page *page;
P
Philipp Reisner 已提交
967
	unsigned int len;
968

P
Philipp Reisner 已提交
969 970 971 972 973
	sector_t on_disk_sector =
		mdev->ldev->md.md_offset + mdev->ldev->md.bm_offset;
	on_disk_sector += ((sector_t)page_nr) << (PAGE_SHIFT-9);

	/* this might happen with very small
974 975
	 * flexible external meta data device,
	 * or with PAGE_SIZE > 4k */
P
Philipp Reisner 已提交
976 977 978
	len = min_t(unsigned int, PAGE_SIZE,
		(drbd_md_last_sector(mdev->ldev) - on_disk_sector + 1)<<9);

979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
	/* serialize IO on this page */
	bm_page_lock_io(mdev, page_nr);
	/* before memcpy and submit,
	 * so it can be redirtied any time */
	bm_set_page_unchanged(b->bm_pages[page_nr]);

	if (ctx->flags & BM_AIO_COPY_PAGES) {
		/* FIXME alloc_page is good enough for now, but actually needs
		 * to use pre-allocated page pool */
		void *src, *dest;
		page = alloc_page(__GFP_HIGHMEM|__GFP_WAIT);
		dest = kmap_atomic(page, KM_USER0);
		src = kmap_atomic(b->bm_pages[page_nr], KM_USER1);
		memcpy(dest, src, PAGE_SIZE);
		kunmap_atomic(src, KM_USER1);
		kunmap_atomic(dest, KM_USER0);
		bm_store_page_idx(page, page_nr);
	} else
		page = b->bm_pages[page_nr];

P
Philipp Reisner 已提交
999 1000
	bio->bi_bdev = mdev->ldev->md_bdev;
	bio->bi_sector = on_disk_sector;
1001 1002
	bio_add_page(bio, page, len, 0);
	bio->bi_private = ctx;
P
Philipp Reisner 已提交
1003 1004
	bio->bi_end_io = bm_async_io_complete;

1005
	if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
P
Philipp Reisner 已提交
1006 1007 1008 1009
		bio->bi_rw |= rw;
		bio_endio(bio, -EIO);
	} else {
		submit_bio(rw, bio);
1010 1011 1012
		/* this should not count as user activity and cause the
		 * resync to throttle -- see drbd_rs_should_slow_down(). */
		atomic_add(len >> 9, &mdev->rs_sect_ev);
P
Philipp Reisner 已提交
1013 1014 1015 1016 1017 1018
	}
}

/*
 * bm_rw: read/write the whole bitmap from/to its on disk location.
 */
1019
static int bm_rw(struct drbd_conf *mdev, int rw, unsigned lazy_writeout_upper_idx) __must_hold(local)
P
Philipp Reisner 已提交
1020
{
1021 1022 1023 1024 1025 1026
	struct bm_aio_ctx ctx = {
		.mdev = mdev,
		.in_flight = ATOMIC_INIT(1),
		.done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
		.flags = lazy_writeout_upper_idx ? BM_AIO_COPY_PAGES : 0,
	};
P
Philipp Reisner 已提交
1027
	struct drbd_bitmap *b = mdev->bitmap;
1028
	int num_pages, i, count = 0;
P
Philipp Reisner 已提交
1029 1030 1031 1032
	unsigned long now;
	char ppb[10];
	int err = 0;

1033 1034 1035 1036 1037 1038 1039 1040 1041
	/*
	 * We are protected against bitmap disappearing/resizing by holding an
	 * ldev reference (caller must have called get_ldev()).
	 * For read/write, we are protected against changes to the bitmap by
	 * the bitmap lock (see drbd_bitmap_io).
	 * For lazy writeout, we don't care for ongoing changes to the bitmap,
	 * as we submit copies of pages anyways.
	 */
	if (!ctx.flags)
1042
		WARN_ON(!(BM_LOCKED_MASK & b->bm_flags));
P
Philipp Reisner 已提交
1043

1044
	num_pages = b->bm_number_of_pages;
P
Philipp Reisner 已提交
1045 1046 1047 1048

	now = jiffies;

	/* let the layers below us try to merge these bios... */
1049
	for (i = 0; i < num_pages; i++) {
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
		/* ignore completely unchanged pages */
		if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
			break;
		if (rw & WRITE) {
			if (bm_test_page_unchanged(b->bm_pages[i])) {
				dynamic_dev_dbg(DEV, "skipped bm write for idx %u\n", i);
				continue;
			}
			/* during lazy writeout,
			 * ignore those pages not marked for lazy writeout. */
			if (lazy_writeout_upper_idx &&
			    !bm_test_page_lazy_writeout(b->bm_pages[i])) {
				dynamic_dev_dbg(DEV, "skipped bm lazy write for idx %u\n", i);
				continue;
			}
		}
		atomic_inc(&ctx.in_flight);
		bm_page_io_async(&ctx, i, rw);
		++count;
		cond_resched();
	}
P
Philipp Reisner 已提交
1071

1072 1073 1074 1075 1076 1077 1078
	/*
	 * We initialize ctx.in_flight to one to make sure bm_async_io_complete
	 * will not complete() early, and decrement / test it here.  If there
	 * are still some bios in flight, we need to wait for them here.
	 */
	if (!atomic_dec_and_test(&ctx.in_flight))
		wait_for_completion(&ctx.done);
1079 1080 1081
	dev_info(DEV, "bitmap %s of %u pages took %lu jiffies\n",
			rw == WRITE ? "WRITE" : "READ",
			count, jiffies - now);
P
Philipp Reisner 已提交
1082

1083
	if (ctx.error) {
P
Philipp Reisner 已提交
1084
		dev_alert(DEV, "we had at least one MD IO ERROR during bitmap IO\n");
1085
		drbd_chk_io_error(mdev, 1, true);
1086
		err = -EIO; /* ctx.error ? */
P
Philipp Reisner 已提交
1087 1088 1089 1090 1091 1092
	}

	now = jiffies;
	if (rw == WRITE) {
		drbd_md_flush(mdev);
	} else /* rw == READ */ {
1093
		b->bm_set = bm_count_bits(b);
P
Philipp Reisner 已提交
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
		dev_info(DEV, "recounting of set bits took additional %lu jiffies\n",
		     jiffies - now);
	}
	now = b->bm_set;

	dev_info(DEV, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
	     ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);

	return err;
}

/**
 * drbd_bm_read() - Read the whole bitmap from its on disk location.
 * @mdev:	DRBD device.
 */
int drbd_bm_read(struct drbd_conf *mdev) __must_hold(local)
{
1111
	return bm_rw(mdev, READ, 0);
P
Philipp Reisner 已提交
1112 1113 1114 1115 1116
}

/**
 * drbd_bm_write() - Write the whole bitmap to its on disk location.
 * @mdev:	DRBD device.
1117 1118
 *
 * Will only write pages that have changed since last IO.
P
Philipp Reisner 已提交
1119 1120 1121
 */
int drbd_bm_write(struct drbd_conf *mdev) __must_hold(local)
{
1122
	return bm_rw(mdev, WRITE, 0);
P
Philipp Reisner 已提交
1123 1124 1125
}

/**
1126
 * drbd_bm_lazy_write_out() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
P
Philipp Reisner 已提交
1127
 * @mdev:	DRBD device.
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
 * @upper_idx:	0: write all changed pages; +ve: page index to stop scanning for changed pages
 */
int drbd_bm_write_lazy(struct drbd_conf *mdev, unsigned upper_idx) __must_hold(local)
{
	return bm_rw(mdev, WRITE, upper_idx);
}


/**
 * drbd_bm_write_page: Writes a PAGE_SIZE aligned piece of bitmap
 * @mdev:	DRBD device.
 * @idx:	bitmap page index
P
Philipp Reisner 已提交
1140
 *
1141 1142
 * We don't want to special case on logical_block_size of the backend device,
 * so we submit PAGE_SIZE aligned pieces.
1143
 * Note that on "most" systems, PAGE_SIZE is 4k.
1144 1145 1146
 *
 * In case this becomes an issue on systems with larger PAGE_SIZE,
 * we may want to change this again to write 4k aligned 4k pieces.
P
Philipp Reisner 已提交
1147
 */
1148
int drbd_bm_write_page(struct drbd_conf *mdev, unsigned int idx) __must_hold(local)
P
Philipp Reisner 已提交
1149
{
1150 1151 1152 1153 1154 1155
	struct bm_aio_ctx ctx = {
		.mdev = mdev,
		.in_flight = ATOMIC_INIT(1),
		.done = COMPLETION_INITIALIZER_ONSTACK(ctx.done),
		.flags = BM_AIO_COPY_PAGES,
	};
P
Philipp Reisner 已提交
1156

1157
	if (bm_test_page_unchanged(mdev->bitmap->bm_pages[idx])) {
1158
		dynamic_dev_dbg(DEV, "skipped bm page write for idx %u\n", idx);
1159
		return 0;
P
Philipp Reisner 已提交
1160
	}
1161 1162

	bm_page_io_async(&ctx, idx, WRITE_SYNC);
1163
	wait_for_completion(&ctx.done);
1164 1165 1166 1167 1168 1169

	if (ctx.error)
		drbd_chk_io_error(mdev, 1, true);
		/* that should force detach, so the in memory bitmap will be
		 * gone in a moment as well. */

P
Philipp Reisner 已提交
1170
	mdev->bm_writ_cnt++;
1171
	return ctx.error;
P
Philipp Reisner 已提交
1172 1173 1174 1175
}

/* NOTE
 * find_first_bit returns int, we return unsigned long.
1176 1177 1178
 * For this to work on 32bit arch with bitnumbers > (1<<32),
 * we'd need to return u64, and get a whole lot of other places
 * fixed where we still use unsigned long.
P
Philipp Reisner 已提交
1179 1180 1181 1182 1183 1184 1185 1186
 *
 * this returns a bit number, NOT a sector!
 */
static unsigned long __bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo,
	const int find_zero_bit, const enum km_type km)
{
	struct drbd_bitmap *b = mdev->bitmap;
	unsigned long *p_addr;
1187 1188 1189
	unsigned long bit_offset;
	unsigned i;

P
Philipp Reisner 已提交
1190 1191 1192

	if (bm_fo > b->bm_bits) {
		dev_err(DEV, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
1193
		bm_fo = DRBD_END_OF_BITMAP;
P
Philipp Reisner 已提交
1194 1195
	} else {
		while (bm_fo < b->bm_bits) {
1196
			/* bit offset of the first bit in the page */
1197
			bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
1198
			p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo), km);
P
Philipp Reisner 已提交
1199 1200

			if (find_zero_bit)
L
Linus Torvalds 已提交
1201
				i = find_next_zero_bit_le(p_addr,
1202
						PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
P
Philipp Reisner 已提交
1203
			else
L
Linus Torvalds 已提交
1204
				i = find_next_bit_le(p_addr,
1205
						PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
P
Philipp Reisner 已提交
1206 1207 1208

			__bm_unmap(p_addr, km);
			if (i < PAGE_SIZE*8) {
1209 1210
				bm_fo = bit_offset + i;
				if (bm_fo >= b->bm_bits)
P
Philipp Reisner 已提交
1211 1212 1213 1214 1215
					break;
				goto found;
			}
			bm_fo = bit_offset + PAGE_SIZE*8;
		}
1216
		bm_fo = DRBD_END_OF_BITMAP;
P
Philipp Reisner 已提交
1217 1218
	}
 found:
1219
	return bm_fo;
P
Philipp Reisner 已提交
1220 1221 1222 1223 1224 1225
}

static unsigned long bm_find_next(struct drbd_conf *mdev,
	unsigned long bm_fo, const int find_zero_bit)
{
	struct drbd_bitmap *b = mdev->bitmap;
1226
	unsigned long i = DRBD_END_OF_BITMAP;
P
Philipp Reisner 已提交
1227

1228 1229 1230 1231
	if (!expect(b))
		return i;
	if (!expect(b->bm_pages))
		return i;
P
Philipp Reisner 已提交
1232 1233

	spin_lock_irq(&b->bm_lock);
1234
	if (BM_DONT_TEST & b->bm_flags)
P
Philipp Reisner 已提交
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
		bm_print_lock_info(mdev);

	i = __bm_find_next(mdev, bm_fo, find_zero_bit, KM_IRQ1);

	spin_unlock_irq(&b->bm_lock);
	return i;
}

unsigned long drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
{
	return bm_find_next(mdev, bm_fo, 0);
}

#if 0
/* not yet needed for anything. */
unsigned long drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
{
	return bm_find_next(mdev, bm_fo, 1);
}
#endif

/* does not spin_lock_irqsave.
 * you must take drbd_bm_lock() first */
unsigned long _drbd_bm_find_next(struct drbd_conf *mdev, unsigned long bm_fo)
{
1260
	/* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
P
Philipp Reisner 已提交
1261 1262 1263 1264 1265
	return __bm_find_next(mdev, bm_fo, 0, KM_USER1);
}

unsigned long _drbd_bm_find_next_zero(struct drbd_conf *mdev, unsigned long bm_fo)
{
1266
	/* WARN_ON(!(BM_DONT_SET & mdev->b->bm_flags)); */
P
Philipp Reisner 已提交
1267 1268 1269 1270 1271 1272 1273 1274 1275
	return __bm_find_next(mdev, bm_fo, 1, KM_USER1);
}

/* returns number of bits actually changed.
 * for val != 0, we change 0 -> 1, return code positive
 * for val == 0, we change 1 -> 0, return code negative
 * wants bitnr, not sector.
 * expected to be called for only a few bits (e - s about BITS_PER_LONG).
 * Must hold bitmap lock already. */
1276
static int __bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
1277
	unsigned long e, int val)
P
Philipp Reisner 已提交
1278 1279 1280 1281
{
	struct drbd_bitmap *b = mdev->bitmap;
	unsigned long *p_addr = NULL;
	unsigned long bitnr;
1282
	unsigned int last_page_nr = -1U;
P
Philipp Reisner 已提交
1283
	int c = 0;
1284
	int changed_total = 0;
P
Philipp Reisner 已提交
1285 1286 1287 1288 1289 1290 1291

	if (e >= b->bm_bits) {
		dev_err(DEV, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
				s, e, b->bm_bits);
		e = b->bm_bits ? b->bm_bits -1 : 0;
	}
	for (bitnr = s; bitnr <= e; bitnr++) {
1292
		unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
P
Philipp Reisner 已提交
1293 1294
		if (page_nr != last_page_nr) {
			if (p_addr)
1295
				__bm_unmap(p_addr, KM_IRQ1);
1296 1297 1298 1299 1300 1301
			if (c < 0)
				bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
			else if (c > 0)
				bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
			changed_total += c;
			c = 0;
1302
			p_addr = __bm_map_pidx(b, page_nr, KM_IRQ1);
P
Philipp Reisner 已提交
1303 1304 1305
			last_page_nr = page_nr;
		}
		if (val)
L
Linus Torvalds 已提交
1306
			c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
P
Philipp Reisner 已提交
1307
		else
L
Linus Torvalds 已提交
1308
			c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
P
Philipp Reisner 已提交
1309 1310
	}
	if (p_addr)
1311
		__bm_unmap(p_addr, KM_IRQ1);
1312 1313 1314 1315 1316 1317 1318
	if (c < 0)
		bm_set_page_lazy_writeout(b->bm_pages[last_page_nr]);
	else if (c > 0)
		bm_set_page_need_writeout(b->bm_pages[last_page_nr]);
	changed_total += c;
	b->bm_set += changed_total;
	return changed_total;
P
Philipp Reisner 已提交
1319 1320 1321 1322 1323 1324
}

/* returns number of bits actually changed.
 * for val != 0, we change 0 -> 1, return code positive
 * for val == 0, we change 1 -> 0, return code negative
 * wants bitnr, not sector */
1325
static int bm_change_bits_to(struct drbd_conf *mdev, const unsigned long s,
P
Philipp Reisner 已提交
1326 1327 1328 1329 1330 1331
	const unsigned long e, int val)
{
	unsigned long flags;
	struct drbd_bitmap *b = mdev->bitmap;
	int c = 0;

1332 1333 1334 1335
	if (!expect(b))
		return 1;
	if (!expect(b->bm_pages))
		return 0;
P
Philipp Reisner 已提交
1336 1337

	spin_lock_irqsave(&b->bm_lock, flags);
1338
	if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags)
P
Philipp Reisner 已提交
1339 1340
		bm_print_lock_info(mdev);

1341
	c = __bm_change_bits_to(mdev, s, e, val);
P
Philipp Reisner 已提交
1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365

	spin_unlock_irqrestore(&b->bm_lock, flags);
	return c;
}

/* returns number of bits changed 0 -> 1 */
int drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
{
	return bm_change_bits_to(mdev, s, e, 1);
}

/* returns number of bits changed 1 -> 0 */
int drbd_bm_clear_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
{
	return -bm_change_bits_to(mdev, s, e, 0);
}

/* sets all bits in full words,
 * from first_word up to, but not including, last_word */
static inline void bm_set_full_words_within_one_page(struct drbd_bitmap *b,
		int page_nr, int first_word, int last_word)
{
	int i;
	int bits;
1366
	unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr], KM_IRQ1);
P
Philipp Reisner 已提交
1367 1368 1369 1370 1371
	for (i = first_word; i < last_word; i++) {
		bits = hweight_long(paddr[i]);
		paddr[i] = ~0UL;
		b->bm_set += BITS_PER_LONG - bits;
	}
1372
	kunmap_atomic(paddr, KM_IRQ1);
P
Philipp Reisner 已提交
1373 1374
}

1375 1376
/* Same thing as drbd_bm_set_bits,
 * but more efficient for a large bit range.
P
Philipp Reisner 已提交
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389
 * You must first drbd_bm_lock().
 * Can be called to set the whole bitmap in one go.
 * Sets bits from s to e _inclusive_. */
void _drbd_bm_set_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
{
	/* First set_bit from the first bit (s)
	 * up to the next long boundary (sl),
	 * then assign full words up to the last long boundary (el),
	 * then set_bit up to and including the last bit (e).
	 *
	 * Do not use memset, because we must account for changes,
	 * so we need to loop over the words with hweight() anyways.
	 */
1390
	struct drbd_bitmap *b = mdev->bitmap;
P
Philipp Reisner 已提交
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
	unsigned long sl = ALIGN(s,BITS_PER_LONG);
	unsigned long el = (e+1) & ~((unsigned long)BITS_PER_LONG-1);
	int first_page;
	int last_page;
	int page_nr;
	int first_word;
	int last_word;

	if (e - s <= 3*BITS_PER_LONG) {
		/* don't bother; el and sl may even be wrong. */
1401 1402 1403
		spin_lock_irq(&b->bm_lock);
		__bm_change_bits_to(mdev, s, e, 1);
		spin_unlock_irq(&b->bm_lock);
P
Philipp Reisner 已提交
1404 1405 1406 1407 1408
		return;
	}

	/* difference is large enough that we can trust sl and el */

1409 1410
	spin_lock_irq(&b->bm_lock);

P
Philipp Reisner 已提交
1411 1412
	/* bits filling the current long */
	if (sl)
1413
		__bm_change_bits_to(mdev, s, sl-1, 1);
P
Philipp Reisner 已提交
1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425

	first_page = sl >> (3 + PAGE_SHIFT);
	last_page = el >> (3 + PAGE_SHIFT);

	/* MLPP: modulo longs per page */
	/* LWPP: long words per page */
	first_word = MLPP(sl >> LN2_BPL);
	last_word = LWPP;

	/* first and full pages, unless first page == last page */
	for (page_nr = first_page; page_nr < last_page; page_nr++) {
		bm_set_full_words_within_one_page(mdev->bitmap, page_nr, first_word, last_word);
1426 1427
		spin_unlock_irq(&b->bm_lock);
		cond_resched();
P
Philipp Reisner 已提交
1428
		first_word = 0;
1429
		spin_lock_irq(&b->bm_lock);
P
Philipp Reisner 已提交
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441
	}

	/* last page (respectively only page, for first page == last page) */
	last_word = MLPP(el >> LN2_BPL);
	bm_set_full_words_within_one_page(mdev->bitmap, last_page, first_word, last_word);

	/* possibly trailing bits.
	 * example: (e & 63) == 63, el will be e+1.
	 * if that even was the very last bit,
	 * it would trigger an assert in __bm_change_bits_to()
	 */
	if (el <= e)
1442 1443
		__bm_change_bits_to(mdev, el, e, 1);
	spin_unlock_irq(&b->bm_lock);
P
Philipp Reisner 已提交
1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
}

/* returns bit state
 * wants bitnr, NOT sector.
 * inherently racy... area needs to be locked by means of {al,rs}_lru
 *  1 ... bit set
 *  0 ... bit not set
 * -1 ... first out of bounds access, stop testing for bits!
 */
int drbd_bm_test_bit(struct drbd_conf *mdev, const unsigned long bitnr)
{
	unsigned long flags;
	struct drbd_bitmap *b = mdev->bitmap;
	unsigned long *p_addr;
	int i;

1460 1461 1462 1463
	if (!expect(b))
		return 0;
	if (!expect(b->bm_pages))
		return 0;
P
Philipp Reisner 已提交
1464 1465

	spin_lock_irqsave(&b->bm_lock, flags);
1466
	if (BM_DONT_TEST & b->bm_flags)
P
Philipp Reisner 已提交
1467 1468
		bm_print_lock_info(mdev);
	if (bitnr < b->bm_bits) {
1469
		p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
L
Linus Torvalds 已提交
1470
		i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
P
Philipp Reisner 已提交
1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
		bm_unmap(p_addr);
	} else if (bitnr == b->bm_bits) {
		i = -1;
	} else { /* (bitnr > b->bm_bits) */
		dev_err(DEV, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
		i = 0;
	}

	spin_unlock_irqrestore(&b->bm_lock, flags);
	return i;
}

/* returns number of bits set in the range [s, e] */
int drbd_bm_count_bits(struct drbd_conf *mdev, const unsigned long s, const unsigned long e)
{
	unsigned long flags;
	struct drbd_bitmap *b = mdev->bitmap;
1488
	unsigned long *p_addr = NULL;
P
Philipp Reisner 已提交
1489
	unsigned long bitnr;
1490
	unsigned int page_nr = -1U;
P
Philipp Reisner 已提交
1491 1492 1493 1494 1495 1496
	int c = 0;

	/* If this is called without a bitmap, that is a bug.  But just to be
	 * robust in case we screwed up elsewhere, in that case pretend there
	 * was one dirty bit in the requested area, so we won't try to do a
	 * local read there (no bitmap probably implies no disk) */
1497 1498 1499 1500
	if (!expect(b))
		return 1;
	if (!expect(b->bm_pages))
		return 1;
P
Philipp Reisner 已提交
1501 1502

	spin_lock_irqsave(&b->bm_lock, flags);
1503
	if (BM_DONT_TEST & b->bm_flags)
P
Philipp Reisner 已提交
1504 1505
		bm_print_lock_info(mdev);
	for (bitnr = s; bitnr <= e; bitnr++) {
1506 1507 1508
		unsigned int idx = bm_bit_to_page_idx(b, bitnr);
		if (page_nr != idx) {
			page_nr = idx;
P
Philipp Reisner 已提交
1509 1510
			if (p_addr)
				bm_unmap(p_addr);
1511
			p_addr = bm_map_pidx(b, idx);
P
Philipp Reisner 已提交
1512
		}
1513
		if (expect(bitnr < b->bm_bits))
L
Linus Torvalds 已提交
1514
			c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
1515 1516
		else
			dev_err(DEV, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
P
Philipp Reisner 已提交
1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545
	}
	if (p_addr)
		bm_unmap(p_addr);
	spin_unlock_irqrestore(&b->bm_lock, flags);
	return c;
}


/* inherently racy...
 * return value may be already out-of-date when this function returns.
 * but the general usage is that this is only use during a cstate when bits are
 * only cleared, not set, and typically only care for the case when the return
 * value is zero, or we already "locked" this "bitmap extent" by other means.
 *
 * enr is bm-extent number, since we chose to name one sector (512 bytes)
 * worth of the bitmap a "bitmap extent".
 *
 * TODO
 * I think since we use it like a reference count, we should use the real
 * reference count of some bitmap extent element from some lru instead...
 *
 */
int drbd_bm_e_weight(struct drbd_conf *mdev, unsigned long enr)
{
	struct drbd_bitmap *b = mdev->bitmap;
	int count, s, e;
	unsigned long flags;
	unsigned long *p_addr, *bm;

1546 1547 1548 1549
	if (!expect(b))
		return 0;
	if (!expect(b->bm_pages))
		return 0;
P
Philipp Reisner 已提交
1550 1551

	spin_lock_irqsave(&b->bm_lock, flags);
1552
	if (BM_DONT_TEST & b->bm_flags)
P
Philipp Reisner 已提交
1553 1554 1555 1556 1557 1558 1559
		bm_print_lock_info(mdev);

	s = S2W(enr);
	e = min((size_t)S2W(enr+1), b->bm_words);
	count = 0;
	if (s < b->bm_words) {
		int n = e-s;
1560
		p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
P
Philipp Reisner 已提交
1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
		bm = p_addr + MLPP(s);
		while (n--)
			count += hweight_long(*bm++);
		bm_unmap(p_addr);
	} else {
		dev_err(DEV, "start offset (%d) too large in drbd_bm_e_weight\n", s);
	}
	spin_unlock_irqrestore(&b->bm_lock, flags);
	return count;
}

1572 1573
/* Set all bits covered by the AL-extent al_enr.
 * Returns number of bits changed. */
P
Philipp Reisner 已提交
1574 1575 1576 1577 1578
unsigned long drbd_bm_ALe_set_all(struct drbd_conf *mdev, unsigned long al_enr)
{
	struct drbd_bitmap *b = mdev->bitmap;
	unsigned long *p_addr, *bm;
	unsigned long weight;
1579 1580
	unsigned long s, e;
	int count, i, do_now;
1581 1582 1583 1584
	if (!expect(b))
		return 0;
	if (!expect(b->bm_pages))
		return 0;
P
Philipp Reisner 已提交
1585 1586

	spin_lock_irq(&b->bm_lock);
1587
	if (BM_DONT_SET & b->bm_flags)
P
Philipp Reisner 已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
		bm_print_lock_info(mdev);
	weight = b->bm_set;

	s = al_enr * BM_WORDS_PER_AL_EXT;
	e = min_t(size_t, s + BM_WORDS_PER_AL_EXT, b->bm_words);
	/* assert that s and e are on the same page */
	D_ASSERT((e-1) >> (PAGE_SHIFT - LN2_BPL + 3)
	      ==  s    >> (PAGE_SHIFT - LN2_BPL + 3));
	count = 0;
	if (s < b->bm_words) {
		i = do_now = e-s;
1599
		p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
P
Philipp Reisner 已提交
1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
		bm = p_addr + MLPP(s);
		while (i--) {
			count += hweight_long(*bm);
			*bm = -1UL;
			bm++;
		}
		bm_unmap(p_addr);
		b->bm_set += do_now*BITS_PER_LONG - count;
		if (e == b->bm_words)
			b->bm_set -= bm_clear_surplus(b);
	} else {
1611
		dev_err(DEV, "start offset (%lu) too large in drbd_bm_ALe_set_all\n", s);
P
Philipp Reisner 已提交
1612 1613 1614 1615 1616
	}
	weight = b->bm_set - weight;
	spin_unlock_irq(&b->bm_lock);
	return weight;
}