2022-11-22 13:43:01 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2009-09-25 23:07:19 +00:00
|
|
|
/*
|
|
|
|
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>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2014-03-27 13:10:55 +00:00
|
|
|
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
|
|
|
|
2015-03-20 15:15:24 +00:00
|
|
|
#include <linux/bitmap.h>
|
2009-09-25 23:07:19 +00:00
|
|
|
#include <linux/vmalloc.h>
|
|
|
|
#include <linux/string.h>
|
|
|
|
#include <linux/drbd.h>
|
include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit slab.h inclusion from percpu.h
percpu.h is included by sched.h and module.h and thus ends up being
included when building most .c files. percpu.h includes slab.h which
in turn includes gfp.h making everything defined by the two files
universally available and complicating inclusion dependencies.
percpu.h -> slab.h dependency is about to be removed. Prepare for
this change by updating users of gfp and slab facilities include those
headers directly instead of assuming availability. As this conversion
needs to touch large number of source files, the following script is
used as the basis of conversion.
http://userweb.kernel.org/~tj/misc/slabh-sweep.py
The script does the followings.
* Scan files for gfp and slab usages and update includes such that
only the necessary includes are there. ie. if only gfp is used,
gfp.h, if slab is used, slab.h.
* When the script inserts a new include, it looks at the include
blocks and try to put the new include such that its order conforms
to its surrounding. It's put in the include block which contains
core kernel includes, in the same order that the rest are ordered -
alphabetical, Christmas tree, rev-Xmas-tree or at the end if there
doesn't seem to be any matching order.
* If the script can't find a place to put a new include (mostly
because the file doesn't have fitting include block), it prints out
an error message indicating which .h file needs to be added to the
file.
The conversion was done in the following steps.
1. The initial automatic conversion of all .c files updated slightly
over 4000 files, deleting around 700 includes and adding ~480 gfp.h
and ~3000 slab.h inclusions. The script emitted errors for ~400
files.
2. Each error was manually checked. Some didn't need the inclusion,
some needed manual addition while adding it to implementation .h or
embedding .c file was more appropriate for others. This step added
inclusions to around 150 files.
3. The script was run again and the output was compared to the edits
from #2 to make sure no file was left behind.
4. Several build tests were done and a couple of problems were fixed.
e.g. lib/decompress_*.c used malloc/free() wrappers around slab
APIs requiring slab.h to be added manually.
5. The script was run on all .h files but without automatically
editing them as sprinkling gfp.h and slab.h inclusions around .h
files could easily lead to inclusion dependency hell. Most gfp.h
inclusion directives were ignored as stuff from gfp.h was usually
wildly available and often used in preprocessor macros. Each
slab.h inclusion directive was examined and added manually as
necessary.
6. percpu.h was updated not to include slab.h.
7. Build test were done on the following configurations and failures
were fixed. CONFIG_GCOV_KERNEL was turned off for all tests (as my
distributed build env didn't work with gcov compiles) and a few
more options had to be turned off depending on archs to make things
build (like ipr on powerpc/64 which failed due to missing writeq).
* x86 and x86_64 UP and SMP allmodconfig and a custom test config.
* powerpc and powerpc64 SMP allmodconfig
* sparc and sparc64 SMP allmodconfig
* ia64 SMP allmodconfig
* s390 SMP allmodconfig
* alpha SMP allmodconfig
* um on x86_64 SMP allmodconfig
8. percpu.h modifications were reverted so that it could be applied as
a separate patch and serve as bisection point.
Given the fact that I had only a couple of failures from tests on step
6, I'm fairly confident about the coverage of this conversion patch.
If there is a breakage, it's likely to be something in one of the arch
headers which should be easily discoverable easily on most builds of
the specific arch.
Signed-off-by: Tejun Heo <tj@kernel.org>
Guess-its-ok-by: Christoph Lameter <cl@linux-foundation.org>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Lee Schermerhorn <Lee.Schermerhorn@hp.com>
2010-03-24 08:04:11 +00:00
|
|
|
#include <linux/slab.h>
|
2015-08-28 07:27:17 +00:00
|
|
|
#include <linux/highmem.h>
|
2011-03-17 14:02:51 +00:00
|
|
|
|
2009-09-25 23:07:19 +00:00
|
|
|
#include "drbd_int.h"
|
|
|
|
|
2010-12-15 07:59:09 +00:00
|
|
|
|
2009-09-25 23:07:19 +00:00
|
|
|
/* 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".
|
2010-12-14 14:13:04 +00:00
|
|
|
*/
|
|
|
|
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2010-12-14 14:13:04 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*
|
2011-05-21 16:32:29 +00:00
|
|
|
* We plan to reduce the amount of in-core bitmap pages by paging them in
|
2010-12-14 14:13:04 +00:00
|
|
|
* 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.
|
2009-09-25 23:07:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2010-12-14 14:13:04 +00:00
|
|
|
|
2016-06-13 22:26:38 +00:00
|
|
|
/* exclusively to be used by __al_write_transaction(),
|
|
|
|
* drbd_bm_mark_for_writeout() and
|
|
|
|
* and drbd_bm_write_hinted() -> bm_rw() called from there.
|
|
|
|
*/
|
|
|
|
unsigned int n_bitmap_hints;
|
|
|
|
unsigned int al_bitmap_hints[AL_UPDATES_PER_TRANSACTION];
|
|
|
|
|
2010-12-14 14:13:04 +00:00
|
|
|
/* see LIMITATIONS: above */
|
|
|
|
|
2009-09-25 23:07:19 +00:00
|
|
|
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;
|
2010-01-29 20:39:07 +00:00
|
|
|
struct mutex bm_change; /* serializes resize operations */
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2010-12-15 07:59:11 +00:00
|
|
|
wait_queue_head_t bm_io_wait; /* used to serialize IO of single pages */
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2011-01-21 09:56:44 +00:00
|
|
|
enum bm_flag bm_flags;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
/* 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__)
|
2011-07-03 11:26:43 +00:00
|
|
|
static void __bm_print_lock_info(struct drbd_device *device, const char *func)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2022-12-01 11:03:49 +00:00
|
|
|
if (!drbd_ratelimit())
|
2009-09-25 23:07:19 +00:00
|
|
|
return;
|
2011-08-10 13:05:02 +00:00
|
|
|
drbd_err(device, "FIXME %s[%d] in %s, bitmap locked for '%s' by %s[%d]\n",
|
|
|
|
current->comm, task_pid_nr(current),
|
|
|
|
func, b->bm_why ?: "?",
|
|
|
|
b->bm_task->comm, task_pid_nr(b->bm_task));
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
void drbd_bm_lock(struct drbd_device *device, char *why, enum bm_flag flags)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
int trylock_failed;
|
|
|
|
|
|
|
|
if (!b) {
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "FIXME no bitmap in drbd_bm_lock!?\n");
|
2009-09-25 23:07:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-29 20:39:07 +00:00
|
|
|
trylock_failed = !mutex_trylock(&b->bm_change);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
if (trylock_failed) {
|
2011-08-10 13:05:02 +00:00
|
|
|
drbd_warn(device, "%s[%d] going to '%s' but bitmap already locked for '%s' by %s[%d]\n",
|
|
|
|
current->comm, task_pid_nr(current),
|
|
|
|
why, b->bm_why ?: "?",
|
|
|
|
b->bm_task->comm, task_pid_nr(b->bm_task));
|
2010-01-29 20:39:07 +00:00
|
|
|
mutex_lock(&b->bm_change);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
2011-01-21 09:56:44 +00:00
|
|
|
if (BM_LOCKED_MASK & b->bm_flags)
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "FIXME bitmap already locked in bm_lock\n");
|
2011-01-21 09:56:44 +00:00
|
|
|
b->bm_flags |= flags & BM_LOCKED_MASK;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
b->bm_why = why;
|
|
|
|
b->bm_task = current;
|
|
|
|
}
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
void drbd_bm_unlock(struct drbd_device *device)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
if (!b) {
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "FIXME no bitmap in drbd_bm_unlock!?\n");
|
2009-09-25 23:07:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
if (!(BM_LOCKED_MASK & device->bitmap->bm_flags))
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "FIXME bitmap not locked in bm_unlock\n");
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2011-01-21 09:56:44 +00:00
|
|
|
b->bm_flags &= ~BM_LOCKED_MASK;
|
2009-09-25 23:07:19 +00:00
|
|
|
b->bm_why = NULL;
|
|
|
|
b->bm_task = NULL;
|
2010-01-29 20:39:07 +00:00
|
|
|
mutex_unlock(&b->bm_change);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 07:59:11 +00:00
|
|
|
/* 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
|
2011-02-21 12:21:00 +00:00
|
|
|
/* pages marked with this "HINT" will be considered for writeout
|
|
|
|
* on activity log transactions */
|
|
|
|
#define BM_PAGE_HINT_WRITEOUT 27
|
2010-12-15 07:59:11 +00:00
|
|
|
|
2011-05-21 16:32:29 +00:00
|
|
|
/* store_page_idx uses non-atomic assignment. It is only used directly after
|
2010-12-15 07:59:11 +00:00
|
|
|
* 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)
|
2010-12-15 07:59:09 +00:00
|
|
|
{
|
2010-12-15 07:59:11 +00:00
|
|
|
BUG_ON(0 != (idx & ~BM_PAGE_IDX_MASK));
|
2012-03-16 07:19:33 +00:00
|
|
|
set_page_private(page, idx);
|
2010-12-15 07:59:09 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 07:59:11 +00:00
|
|
|
static unsigned long bm_page_to_idx(struct page *page)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2010-12-15 07:59:11 +00:00
|
|
|
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.
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
static void bm_page_lock_io(struct drbd_device *device, int page_nr)
|
2010-12-15 07:59:11 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2010-12-15 07:59:11 +00:00
|
|
|
void *addr = &page_private(b->bm_pages[page_nr]);
|
|
|
|
wait_event(b->bm_io_wait, !test_and_set_bit(BM_PAGE_IO_LOCK, addr));
|
|
|
|
}
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
static void bm_page_unlock_io(struct drbd_device *device, int page_nr)
|
2010-12-15 07:59:11 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2010-12-15 07:59:11 +00:00
|
|
|
void *addr = &page_private(b->bm_pages[page_nr]);
|
2011-02-21 12:20:55 +00:00
|
|
|
clear_bit_unlock(BM_PAGE_IO_LOCK, addr);
|
2011-07-03 11:26:43 +00:00
|
|
|
wake_up(&device->bitmap->bm_io_wait);
|
2010-12-15 07:59:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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));
|
|
|
|
}
|
|
|
|
|
2016-06-13 22:26:38 +00:00
|
|
|
void drbd_bm_reset_al_hints(struct drbd_device *device)
|
|
|
|
{
|
|
|
|
device->bitmap->n_bitmap_hints = 0;
|
|
|
|
}
|
|
|
|
|
2011-02-21 12:21:00 +00:00
|
|
|
/**
|
|
|
|
* drbd_bm_mark_for_writeout() - mark a page with a "hint" to be considered for writeout
|
2011-07-03 11:26:43 +00:00
|
|
|
* @device: DRBD device.
|
2011-02-21 12:21:00 +00:00
|
|
|
* @page_nr: the bitmap page to mark with the "hint" flag
|
|
|
|
*
|
|
|
|
* From within an activity log transaction, we mark a few pages with these
|
|
|
|
* hints, then call drbd_bm_write_hinted(), which will only write out changed
|
|
|
|
* pages which are flagged with this mark.
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
void drbd_bm_mark_for_writeout(struct drbd_device *device, int page_nr)
|
2011-02-21 12:21:00 +00:00
|
|
|
{
|
2016-06-13 22:26:38 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2011-02-21 12:21:00 +00:00
|
|
|
struct page *page;
|
2011-07-03 11:26:43 +00:00
|
|
|
if (page_nr >= device->bitmap->bm_number_of_pages) {
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_warn(device, "BAD: page_nr: %u, number_of_pages: %u\n",
|
2011-07-03 11:26:43 +00:00
|
|
|
page_nr, (int)device->bitmap->bm_number_of_pages);
|
2011-02-21 12:21:00 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-07-03 11:26:43 +00:00
|
|
|
page = device->bitmap->bm_pages[page_nr];
|
2016-06-13 22:26:38 +00:00
|
|
|
BUG_ON(b->n_bitmap_hints >= ARRAY_SIZE(b->al_bitmap_hints));
|
|
|
|
if (!test_and_set_bit(BM_PAGE_HINT_WRITEOUT, &page_private(page)))
|
|
|
|
b->al_bitmap_hints[b->n_bitmap_hints++] = page_nr;
|
2011-02-21 12:21:00 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 07:59:11 +00:00
|
|
|
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;
|
|
|
|
}
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2010-12-15 07:59:11 +00:00
|
|
|
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)
|
|
|
|
{
|
2009-09-25 23:07:19 +00:00
|
|
|
/* page_nr = (word*sizeof(long)) >> PAGE_SHIFT; */
|
2010-12-15 07:59:11 +00:00
|
|
|
unsigned int page_nr = long_nr >> (PAGE_SHIFT - LN2_BPL + 3);
|
2009-09-25 23:07:19 +00:00
|
|
|
BUG_ON(page_nr >= b->bm_number_of_pages);
|
2010-12-15 07:59:11 +00:00
|
|
|
return page_nr;
|
|
|
|
}
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2010-12-15 07:59:11 +00:00
|
|
|
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;
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-25 15:51:58 +00:00
|
|
|
static unsigned long *__bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
|
2010-12-15 07:59:09 +00:00
|
|
|
{
|
|
|
|
struct page *page = b->bm_pages[idx];
|
2011-11-25 15:14:18 +00:00
|
|
|
return (unsigned long *) kmap_atomic(page);
|
2010-12-15 07:59:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long *bm_map_pidx(struct drbd_bitmap *b, unsigned int idx)
|
|
|
|
{
|
2011-11-25 15:14:18 +00:00
|
|
|
return __bm_map_pidx(b, idx);
|
2010-12-15 07:59:09 +00:00
|
|
|
}
|
|
|
|
|
2011-11-25 15:14:18 +00:00
|
|
|
static void __bm_unmap(unsigned long *p_addr)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-11-25 15:14:18 +00:00
|
|
|
kunmap_atomic(p_addr);
|
2009-09-25 23:07:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void bm_unmap(unsigned long *p_addr)
|
|
|
|
{
|
2011-11-25 15:14:18 +00:00
|
|
|
return __bm_unmap(p_addr);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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))
|
2011-05-21 16:32:29 +00:00
|
|
|
hm, well, Philipp thinks gcc might not optimize the % into & (... - 1)
|
2009-09-25 23:07:19 +00:00
|
|
|
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
|
2011-07-03 11:26:43 +00:00
|
|
|
* struct drbd_device*, but for the debug macros I like to have the device around
|
2009-09-25 23:07:19 +00:00
|
|
|
* to be able to report device specific.
|
|
|
|
*/
|
|
|
|
|
2010-12-15 07:59:11 +00:00
|
|
|
|
2009-09-25 23:07:19 +00:00
|
|
|
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]) {
|
2014-03-27 13:10:55 +00:00
|
|
|
pr_alert("bm_free_pages tried to free a NULL pointer; i=%lu n=%lu\n",
|
|
|
|
i, number);
|
2009-09-25 23:07:19 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
__free_page(pages[i]);
|
|
|
|
pages[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-22 23:11:02 +00:00
|
|
|
static inline void bm_vk_free(void *ptr)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2016-01-22 23:11:02 +00:00
|
|
|
kvfree(ptr);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* "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;
|
2016-01-22 23:11:02 +00:00
|
|
|
unsigned int i, bytes;
|
2009-09-25 23:07:19 +00:00
|
|
|
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.
|
2012-09-20 12:05:39 +00:00
|
|
|
* GFP_NOIO, as this is called while drbd IO is "suspended",
|
|
|
|
* and during resize or attach on diskless Primary,
|
|
|
|
* we must not block on IO to ourselves.
|
2012-09-26 12:18:51 +00:00
|
|
|
* Context is receiver thread or dmsetup. */
|
2009-09-25 23:07:19 +00:00
|
|
|
bytes = sizeof(struct page *)*want;
|
2013-06-19 19:15:53 +00:00
|
|
|
new_pages = kzalloc(bytes, GFP_NOIO | __GFP_NOWARN);
|
2009-09-25 23:07:19 +00:00
|
|
|
if (!new_pages) {
|
2020-06-02 04:51:40 +00:00
|
|
|
new_pages = __vmalloc(bytes, GFP_NOIO | __GFP_ZERO);
|
2009-09-25 23:07:19 +00:00
|
|
|
if (!new_pages)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (want >= have) {
|
|
|
|
for (i = 0; i < have; i++)
|
|
|
|
new_pages[i] = old_pages[i];
|
|
|
|
for (; i < want; i++) {
|
2012-09-20 12:05:39 +00:00
|
|
|
page = alloc_page(GFP_NOIO | __GFP_HIGHMEM);
|
2009-09-25 23:07:19 +00:00
|
|
|
if (!page) {
|
|
|
|
bm_free_pages(new_pages + have, i - have);
|
2016-01-22 23:11:02 +00:00
|
|
|
bm_vk_free(new_pages);
|
2009-09-25 23:07:19 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-12-15 07:59:11 +00:00
|
|
|
/* we want to know which page it is
|
|
|
|
* from the endio handlers */
|
|
|
|
bm_store_page_idx(page, i);
|
2009-09-25 23:07:19 +00:00
|
|
|
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);
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
return new_pages;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2016-06-13 22:26:35 +00:00
|
|
|
* allocates the drbd_bitmap and stores it in device->bitmap.
|
2009-09-25 23:07:19 +00:00
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_init(struct drbd_device *device)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
WARN_ON(b != NULL);
|
|
|
|
b = kzalloc(sizeof(struct drbd_bitmap), GFP_KERNEL);
|
|
|
|
if (!b)
|
|
|
|
return -ENOMEM;
|
|
|
|
spin_lock_init(&b->bm_lock);
|
2010-01-29 20:39:07 +00:00
|
|
|
mutex_init(&b->bm_change);
|
2009-09-25 23:07:19 +00:00
|
|
|
init_waitqueue_head(&b->bm_io_wait);
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
device->bitmap = b;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
sector_t drbd_bm_capacity(struct drbd_device *device)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, device->bitmap))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 0;
|
2011-07-03 11:26:43 +00:00
|
|
|
return device->bitmap->bm_dev_capacity;
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* called on driver unload. TODO: call when a device is destroyed.
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
void drbd_bm_cleanup(struct drbd_device *device)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, device->bitmap))
|
2010-12-15 18:31:20 +00:00
|
|
|
return;
|
2011-07-03 11:26:43 +00:00
|
|
|
bm_free_pages(device->bitmap->bm_pages, device->bitmap->bm_number_of_pages);
|
2016-01-22 23:11:02 +00:00
|
|
|
bm_vk_free(device->bitmap->bm_pages);
|
2011-07-03 11:26:43 +00:00
|
|
|
kfree(device->bitmap);
|
|
|
|
device->bitmap = NULL;
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* since (b->bm_bits % BITS_PER_LONG) != 0,
|
|
|
|
* this masks out the remaining bits.
|
|
|
|
* Returns the number of bits cleared.
|
|
|
|
*/
|
2015-03-20 14:47:22 +00:00
|
|
|
#ifndef BITS_PER_PAGE
|
2010-12-15 07:59:09 +00:00
|
|
|
#define BITS_PER_PAGE (1UL << (PAGE_SHIFT + 3))
|
|
|
|
#define BITS_PER_PAGE_MASK (BITS_PER_PAGE - 1)
|
2015-03-20 14:47:22 +00:00
|
|
|
#else
|
|
|
|
# if BITS_PER_PAGE != (1UL << (PAGE_SHIFT + 3))
|
|
|
|
# error "ambiguous BITS_PER_PAGE"
|
|
|
|
# endif
|
|
|
|
#endif
|
2010-12-15 07:59:09 +00:00
|
|
|
#define BITS_PER_LONG_MASK (BITS_PER_LONG - 1)
|
2009-09-25 23:07:19 +00:00
|
|
|
static int bm_clear_surplus(struct drbd_bitmap *b)
|
|
|
|
{
|
2010-12-15 07:59:09 +00:00
|
|
|
unsigned long mask;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long *p_addr, *bm;
|
2010-12-15 07:59:09 +00:00
|
|
|
int tmp;
|
|
|
|
int cleared = 0;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2010-12-15 07:59:09 +00:00
|
|
|
/* 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);
|
|
|
|
|
2010-12-15 23:32:38 +00:00
|
|
|
p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
|
2010-12-15 07:59:09 +00:00
|
|
|
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. */
|
2009-09-25 23:07:19 +00:00
|
|
|
cleared = hweight_long(*bm & ~mask);
|
|
|
|
*bm &= mask;
|
2010-12-15 07:59:09 +00:00
|
|
|
bm++;
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 07:59:09 +00:00
|
|
|
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 */
|
2009-09-25 23:07:19 +00:00
|
|
|
cleared += hweight_long(*bm);
|
|
|
|
*bm = 0;
|
|
|
|
}
|
|
|
|
bm_unmap(p_addr);
|
|
|
|
return cleared;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void bm_set_surplus(struct drbd_bitmap *b)
|
|
|
|
{
|
2010-12-15 07:59:09 +00:00
|
|
|
unsigned long mask;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long *p_addr, *bm;
|
2010-12-15 07:59:09 +00:00
|
|
|
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);
|
|
|
|
|
2010-12-15 23:32:38 +00:00
|
|
|
p_addr = bm_map_pidx(b, b->bm_number_of_pages - 1);
|
2010-12-15 07:59:09 +00:00
|
|
|
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. */
|
2009-09-25 23:07:19 +00:00
|
|
|
*bm |= ~mask;
|
2010-12-15 07:59:09 +00:00
|
|
|
bm++;
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2010-12-15 07:59:09 +00:00
|
|
|
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;
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
bm_unmap(p_addr);
|
|
|
|
}
|
|
|
|
|
2010-12-14 14:13:04 +00:00
|
|
|
/* you better not modify the bitmap while this is running,
|
|
|
|
* or its results will be stale */
|
2010-12-15 07:59:09 +00:00
|
|
|
static unsigned long bm_count_bits(struct drbd_bitmap *b)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2010-12-14 14:13:04 +00:00
|
|
|
unsigned long *p_addr;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long bits = 0;
|
2010-12-14 14:13:04 +00:00
|
|
|
unsigned long mask = (1UL << (b->bm_bits & BITS_PER_LONG_MASK)) -1;
|
2015-03-20 15:15:24 +00:00
|
|
|
int idx, last_word;
|
2010-12-14 14:13:04 +00:00
|
|
|
|
|
|
|
/* all but last page */
|
2010-12-15 23:32:38 +00:00
|
|
|
for (idx = 0; idx < b->bm_number_of_pages - 1; idx++) {
|
2011-11-25 15:14:18 +00:00
|
|
|
p_addr = __bm_map_pidx(b, idx);
|
2015-03-20 15:15:24 +00:00
|
|
|
bits += bitmap_weight(p_addr, BITS_PER_PAGE);
|
2011-11-25 15:14:18 +00:00
|
|
|
__bm_unmap(p_addr);
|
2009-09-25 23:07:19 +00:00
|
|
|
cond_resched();
|
|
|
|
}
|
2010-12-14 14:13:04 +00:00
|
|
|
/* last (or only) page */
|
|
|
|
last_word = ((b->bm_bits - 1) & BITS_PER_PAGE_MASK) >> LN2_BPL;
|
2011-11-25 15:51:58 +00:00
|
|
|
p_addr = __bm_map_pidx(b, idx);
|
2015-03-20 15:15:24 +00:00
|
|
|
bits += bitmap_weight(p_addr, last_word * BITS_PER_LONG);
|
2010-12-14 14:13:04 +00:00
|
|
|
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;
|
2011-11-25 15:51:58 +00:00
|
|
|
__bm_unmap(p_addr);
|
2009-09-25 23:07:19 +00:00
|
|
|
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;
|
2010-12-15 07:59:11 +00:00
|
|
|
unsigned int idx;
|
2009-09-25 23:07:19 +00:00
|
|
|
size_t do_now, end;
|
|
|
|
|
|
|
|
end = offset + len;
|
|
|
|
|
|
|
|
if (end > b->bm_words) {
|
2014-03-27 13:10:55 +00:00
|
|
|
pr_alert("bm_memset end > bm_words\n");
|
2009-09-25 23:07:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (offset < end) {
|
|
|
|
do_now = min_t(size_t, ALIGN(offset + 1, LWPP), end) - offset;
|
2010-12-15 07:59:11 +00:00
|
|
|
idx = bm_word_to_page_idx(b, offset);
|
|
|
|
p_addr = bm_map_pidx(b, idx);
|
2009-09-25 23:07:19 +00:00
|
|
|
bm = p_addr + MLPP(offset);
|
|
|
|
if (bm+do_now > p_addr + LWPP) {
|
2014-03-27 13:10:55 +00:00
|
|
|
pr_alert("BUG BUG BUG! p_addr:%p bm:%p do_now:%d\n",
|
2009-09-25 23:07:19 +00:00
|
|
|
p_addr, bm, (int)do_now);
|
2010-12-15 23:37:57 +00:00
|
|
|
} else
|
|
|
|
memset(bm, c, do_now * sizeof(long));
|
2009-09-25 23:07:19 +00:00
|
|
|
bm_unmap(p_addr);
|
2010-12-15 07:59:11 +00:00
|
|
|
bm_set_page_need_writeout(b->bm_pages[idx]);
|
2009-09-25 23:07:19 +00:00
|
|
|
offset += do_now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-19 17:16:43 +00:00
|
|
|
/* For the layout, see comment above drbd_md_set_sector_offsets(). */
|
|
|
|
static u64 drbd_md_on_disk_bits(struct drbd_backing_dev *ldev)
|
|
|
|
{
|
|
|
|
u64 bitmap_sectors;
|
|
|
|
if (ldev->md.al_offset == 8)
|
|
|
|
bitmap_sectors = ldev->md.md_size_sect - ldev->md.bm_offset;
|
|
|
|
else
|
|
|
|
bitmap_sectors = ldev->md.al_offset - ldev->md.bm_offset;
|
|
|
|
return bitmap_sectors << (9 + 3);
|
|
|
|
}
|
|
|
|
|
2009-09-25 23:07:19 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_resize(struct drbd_device *device, sector_t capacity, int set_new_bits)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2010-12-15 23:32:38 +00:00
|
|
|
unsigned long bits, words, owords, obits;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long want, have, onpages; /* number of pages */
|
|
|
|
struct page **npages, **opages = NULL;
|
2016-06-13 22:26:35 +00:00
|
|
|
int err = 0;
|
|
|
|
bool growing;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return -ENOMEM;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
drbd_bm_lock(device, "resize", BM_LOCKED_MASK);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_info(device, "drbd_bm_resize called with capacity == %llu\n",
|
2009-09-25 23:07:19 +00:00
|
|
|
(unsigned long long)capacity);
|
|
|
|
|
|
|
|
if (capacity == b->bm_dev_capacity)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
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);
|
2016-01-22 23:11:02 +00:00
|
|
|
bm_vk_free(opages);
|
2009-09-25 23:07:19 +00:00
|
|
|
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;
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
if (get_ldev(device)) {
|
|
|
|
u64 bits_on_disk = drbd_md_on_disk_bits(device->ldev);
|
|
|
|
put_ldev(device);
|
2010-12-14 14:13:04 +00:00
|
|
|
if (bits > bits_on_disk) {
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_info(device, "bits = %lu\n", bits);
|
|
|
|
drbd_info(device, "bits_on_disk = %llu\n", bits_on_disk);
|
2010-12-14 14:13:04 +00:00
|
|
|
err = -ENOSPC;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 19:07:12 +00:00
|
|
|
want = PFN_UP(words*sizeof(long));
|
2009-09-25 23:07:19 +00:00
|
|
|
have = b->bm_number_of_pages;
|
|
|
|
if (want == have) {
|
2011-06-27 14:23:33 +00:00
|
|
|
D_ASSERT(device, b->bm_pages != NULL);
|
2009-09-25 23:07:19 +00:00
|
|
|
npages = b->bm_pages;
|
|
|
|
} else {
|
2011-07-03 11:26:43 +00:00
|
|
|
if (drbd_insert_fault(device, DRBD_FAULT_BM_ALLOC))
|
2009-09-25 23:07:19 +00:00
|
|
|
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;
|
2010-04-28 12:46:57 +00:00
|
|
|
if (opages && growing && set_new_bits)
|
2009-09-25 23:07:19 +00:00
|
|
|
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) {
|
2010-03-24 15:23:03 +00:00
|
|
|
if (set_new_bits) {
|
|
|
|
bm_memset(b, owords, 0xff, words-owords);
|
|
|
|
b->bm_set += bits - obits;
|
|
|
|
} else
|
|
|
|
bm_memset(b, owords, 0x00, words-owords);
|
|
|
|
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2016-01-22 23:11:02 +00:00
|
|
|
bm_vk_free(opages);
|
2009-09-25 23:07:19 +00:00
|
|
|
if (!growing)
|
|
|
|
b->bm_set = bm_count_bits(b);
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_info(device, "resync bitmap: bits=%lu words=%lu pages=%lu\n", bits, words, want);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
out:
|
2011-07-03 11:26:43 +00:00
|
|
|
drbd_bm_unlock(device);
|
2009-09-25 23:07:19 +00:00
|
|
|
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 ?
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
unsigned long _drbd_bm_total_weight(struct drbd_device *device)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long s;
|
|
|
|
unsigned long flags;
|
|
|
|
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 0;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b->bm_pages))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 0;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&b->bm_lock, flags);
|
|
|
|
s = b->bm_set;
|
|
|
|
spin_unlock_irqrestore(&b->bm_lock, flags);
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
unsigned long drbd_bm_total_weight(struct drbd_device *device)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
|
|
|
unsigned long s;
|
|
|
|
/* if I don't have a disk, I don't know about out-of-sync status */
|
2011-07-03 11:26:43 +00:00
|
|
|
if (!get_ldev_if_state(device, D_NEGOTIATING))
|
2009-09-25 23:07:19 +00:00
|
|
|
return 0;
|
2011-07-03 11:26:43 +00:00
|
|
|
s = _drbd_bm_total_weight(device);
|
|
|
|
put_ldev(device);
|
2009-09-25 23:07:19 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
size_t drbd_bm_words(struct drbd_device *device)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 0;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b->bm_pages))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 0;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
return b->bm_words;
|
|
|
|
}
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
unsigned long drbd_bm_bits(struct drbd_device *device)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 0;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
void drbd_bm_merge_lel(struct drbd_device *device, size_t offset, size_t number,
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long *buffer)
|
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long *p_addr, *bm;
|
|
|
|
unsigned long word, bits;
|
2010-12-15 07:59:11 +00:00
|
|
|
unsigned int idx;
|
2009-09-25 23:07:19 +00:00
|
|
|
size_t end, do_now;
|
|
|
|
|
|
|
|
end = offset + number;
|
|
|
|
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b->bm_pages))
|
2010-12-15 18:31:20 +00:00
|
|
|
return;
|
2009-09-25 23:07:19 +00:00
|
|
|
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;
|
2010-12-15 07:59:11 +00:00
|
|
|
idx = bm_word_to_page_idx(b, offset);
|
|
|
|
p_addr = bm_map_pidx(b, idx);
|
2009-09-25 23:07:19 +00:00
|
|
|
bm = p_addr + MLPP(offset);
|
|
|
|
offset += do_now;
|
|
|
|
while (do_now--) {
|
|
|
|
bits = hweight_long(*bm);
|
2010-12-15 07:59:09 +00:00
|
|
|
word = *bm | *buffer++;
|
2009-09-25 23:07:19 +00:00
|
|
|
*bm++ = word;
|
|
|
|
b->bm_set += hweight_long(word) - bits;
|
|
|
|
}
|
|
|
|
bm_unmap(p_addr);
|
2010-12-15 07:59:11 +00:00
|
|
|
bm_set_page_need_writeout(b->bm_pages[idx]);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
/* 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.
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
void drbd_bm_get_lel(struct drbd_device *device, size_t offset, size_t number,
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long *buffer)
|
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long *p_addr, *bm;
|
|
|
|
size_t end, do_now;
|
|
|
|
|
|
|
|
end = offset + number;
|
|
|
|
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b->bm_pages))
|
2010-12-15 18:31:20 +00:00
|
|
|
return;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
spin_lock_irq(&b->bm_lock);
|
|
|
|
if ((offset >= b->bm_words) ||
|
|
|
|
(end > b->bm_words) ||
|
|
|
|
(number <= 0))
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "offset=%lu number=%lu bm_words=%lu\n",
|
2009-09-25 23:07:19 +00:00
|
|
|
(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;
|
2010-12-15 07:59:11 +00:00
|
|
|
p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, offset));
|
2009-09-25 23:07:19 +00:00
|
|
|
bm = p_addr + MLPP(offset);
|
|
|
|
offset += do_now;
|
|
|
|
while (do_now--)
|
2010-12-15 07:59:09 +00:00
|
|
|
*buffer++ = *bm++;
|
2009-09-25 23:07:19 +00:00
|
|
|
bm_unmap(p_addr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spin_unlock_irq(&b->bm_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set all bits in the bitmap */
|
2011-07-03 11:26:43 +00:00
|
|
|
void drbd_bm_set_all(struct drbd_device *device)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b->bm_pages))
|
2010-12-15 18:31:20 +00:00
|
|
|
return;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
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 */
|
2011-07-03 11:26:43 +00:00
|
|
|
void drbd_bm_clear_all(struct drbd_device *device)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b->bm_pages))
|
2010-12-15 18:31:20 +00:00
|
|
|
return;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
spin_lock_irq(&b->bm_lock);
|
|
|
|
bm_memset(b, 0, 0, b->bm_words);
|
|
|
|
b->bm_set = 0;
|
|
|
|
spin_unlock_irq(&b->bm_lock);
|
|
|
|
}
|
|
|
|
|
2014-05-05 22:44:59 +00:00
|
|
|
static void drbd_bm_aio_ctx_destroy(struct kref *kref)
|
2011-06-27 12:56:52 +00:00
|
|
|
{
|
2014-05-05 22:44:59 +00:00
|
|
|
struct drbd_bm_aio_ctx *ctx = container_of(kref, struct drbd_bm_aio_ctx, kref);
|
|
|
|
unsigned long flags;
|
2011-06-27 12:56:52 +00:00
|
|
|
|
2014-05-05 22:44:59 +00:00
|
|
|
spin_lock_irqsave(&ctx->device->resource->req_lock, flags);
|
|
|
|
list_del(&ctx->list);
|
|
|
|
spin_unlock_irqrestore(&ctx->device->resource->req_lock, flags);
|
2011-07-03 11:26:43 +00:00
|
|
|
put_ldev(ctx->device);
|
2011-06-27 12:56:52 +00:00
|
|
|
kfree(ctx);
|
|
|
|
}
|
|
|
|
|
2010-12-15 07:59:11 +00:00
|
|
|
/* bv_page may be a copy, or may be the original */
|
2015-07-20 13:29:37 +00:00
|
|
|
static void drbd_bm_endio(struct bio *bio)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2014-05-05 22:44:59 +00:00
|
|
|
struct drbd_bm_aio_ctx *ctx = bio->bi_private;
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_device *device = ctx->device;
|
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2017-12-18 12:22:04 +00:00
|
|
|
unsigned int idx = bm_page_to_idx(bio_first_page_all(bio));
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2010-12-17 22:58:41 +00:00
|
|
|
if ((ctx->flags & BM_AIO_COPY_PAGES) == 0 &&
|
|
|
|
!bm_test_page_unchanged(b->bm_pages[idx]))
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_warn(device, "bitmap page idx %u changed during IO!\n", idx);
|
2010-12-15 07:59:11 +00:00
|
|
|
|
2017-06-03 07:38:06 +00:00
|
|
|
if (bio->bi_status) {
|
2010-12-15 07:59:11 +00:00
|
|
|
/* ctx error will hold the completed-last non-zero error code,
|
|
|
|
* in case error codes differ. */
|
2017-06-03 07:38:06 +00:00
|
|
|
ctx->error = blk_status_to_errno(bio->bi_status);
|
2010-12-15 07:59:11 +00:00
|
|
|
bm_set_page_io_err(b->bm_pages[idx]);
|
|
|
|
/* Not identical to on disk version of it.
|
|
|
|
* Is BM_PAGE_IO_ERROR enough? */
|
2022-12-01 11:03:49 +00:00
|
|
|
if (drbd_ratelimit())
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "IO ERROR %d on bitmap page idx %u\n",
|
2017-06-03 07:38:06 +00:00
|
|
|
bio->bi_status, idx);
|
2010-12-15 07:59:11 +00:00
|
|
|
} else {
|
|
|
|
bm_clear_page_io_err(b->bm_pages[idx]);
|
2011-07-03 15:53:52 +00:00
|
|
|
dynamic_drbd_dbg(device, "bitmap page idx %u completed\n", idx);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
2010-12-15 07:59:11 +00:00
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
bm_page_unlock_io(device, idx);
|
2010-12-15 07:59:11 +00:00
|
|
|
|
|
|
|
if (ctx->flags & BM_AIO_COPY_PAGES)
|
2018-05-20 22:25:48 +00:00
|
|
|
mempool_free(bio->bi_io_vec[0].bv_page, &drbd_md_io_page_pool);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
bio_put(bio);
|
2010-12-15 07:59:11 +00:00
|
|
|
|
2011-06-27 12:56:52 +00:00
|
|
|
if (atomic_dec_and_test(&ctx->in_flight)) {
|
2011-06-27 13:29:16 +00:00
|
|
|
ctx->done = 1;
|
2011-07-03 11:26:43 +00:00
|
|
|
wake_up(&device->misc_wait);
|
2014-05-05 22:44:59 +00:00
|
|
|
kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy);
|
2011-06-27 12:56:52 +00:00
|
|
|
}
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2022-06-22 20:49:32 +00:00
|
|
|
/* For the layout, see comment above drbd_md_set_sector_offsets(). */
|
|
|
|
static inline sector_t drbd_md_last_bitmap_sector(struct drbd_backing_dev *bdev)
|
|
|
|
{
|
|
|
|
switch (bdev->md.meta_dev_idx) {
|
|
|
|
case DRBD_MD_INDEX_INTERNAL:
|
|
|
|
case DRBD_MD_INDEX_FLEX_INT:
|
|
|
|
return bdev->md.md_offset + bdev->md.al_offset -1;
|
|
|
|
case DRBD_MD_INDEX_FLEX_EXT:
|
|
|
|
default:
|
|
|
|
return bdev->md.md_offset + bdev->md.md_size_sect -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-05 22:44:59 +00:00
|
|
|
static void bm_page_io_async(struct drbd_bm_aio_ctx *ctx, int page_nr) __must_hold(local)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_device *device = ctx->device;
|
2022-07-14 18:06:39 +00:00
|
|
|
enum req_op op = ctx->flags & BM_AIO_READ ? REQ_OP_READ : REQ_OP_WRITE;
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2022-06-22 20:49:32 +00:00
|
|
|
struct bio *bio;
|
2010-12-15 07:59:11 +00:00
|
|
|
struct page *page;
|
2022-06-22 20:49:32 +00:00
|
|
|
sector_t last_bm_sect;
|
|
|
|
sector_t first_bm_sect;
|
|
|
|
sector_t on_disk_sector;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned int len;
|
2010-12-15 07:59:11 +00:00
|
|
|
|
2022-06-22 20:49:32 +00:00
|
|
|
first_bm_sect = device->ldev->md.md_offset + device->ldev->md.bm_offset;
|
|
|
|
on_disk_sector = first_bm_sect + (((sector_t)page_nr) << (PAGE_SHIFT-SECTOR_SHIFT));
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
/* this might happen with very small
|
2010-12-15 07:59:11 +00:00
|
|
|
* flexible external meta data device,
|
|
|
|
* or with PAGE_SIZE > 4k */
|
2022-06-22 20:49:32 +00:00
|
|
|
last_bm_sect = drbd_md_last_bitmap_sector(device->ldev);
|
|
|
|
if (first_bm_sect <= on_disk_sector && last_bm_sect >= on_disk_sector) {
|
|
|
|
sector_t len_sect = last_bm_sect - on_disk_sector + 1;
|
|
|
|
if (len_sect < PAGE_SIZE/SECTOR_SIZE)
|
|
|
|
len = (unsigned int)len_sect*SECTOR_SIZE;
|
|
|
|
else
|
|
|
|
len = PAGE_SIZE;
|
|
|
|
} else {
|
2022-12-01 11:03:49 +00:00
|
|
|
if (drbd_ratelimit()) {
|
2022-06-22 20:49:32 +00:00
|
|
|
drbd_err(device, "Invalid offset during on-disk bitmap access: "
|
|
|
|
"page idx %u, sector %llu\n", page_nr, on_disk_sector);
|
|
|
|
}
|
|
|
|
ctx->error = -EIO;
|
|
|
|
bm_set_page_io_err(b->bm_pages[page_nr]);
|
|
|
|
if (atomic_dec_and_test(&ctx->in_flight)) {
|
|
|
|
ctx->done = 1;
|
|
|
|
wake_up(&device->misc_wait);
|
|
|
|
kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2010-12-15 07:59:11 +00:00
|
|
|
/* serialize IO on this page */
|
2011-07-03 11:26:43 +00:00
|
|
|
bm_page_lock_io(device, page_nr);
|
2010-12-15 07:59:11 +00:00
|
|
|
/* 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) {
|
2018-05-20 22:25:48 +00:00
|
|
|
page = mempool_alloc(&drbd_md_io_page_pool,
|
2018-05-09 07:54:08 +00:00
|
|
|
GFP_NOIO | __GFP_HIGHMEM);
|
2012-11-09 00:12:31 +00:00
|
|
|
copy_highpage(page, b->bm_pages[page_nr]);
|
2010-12-15 07:59:11 +00:00
|
|
|
bm_store_page_idx(page, page_nr);
|
|
|
|
} else
|
|
|
|
page = b->bm_pages[page_nr];
|
2022-06-22 20:49:32 +00:00
|
|
|
bio = bio_alloc_bioset(device->ldev->md_bdev, 1, op, GFP_NOIO,
|
|
|
|
&drbd_md_io_bio_set);
|
2013-10-11 22:44:27 +00:00
|
|
|
bio->bi_iter.bi_sector = on_disk_sector;
|
2011-02-23 14:38:47 +00:00
|
|
|
/* bio_add_page of a single page to an empty bio will always succeed,
|
|
|
|
* according to api. Do we want to assert that? */
|
2010-12-15 07:59:11 +00:00
|
|
|
bio_add_page(bio, page, len, 0);
|
|
|
|
bio->bi_private = ctx;
|
2014-09-11 12:29:06 +00:00
|
|
|
bio->bi_end_io = drbd_bm_endio;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2016-06-05 19:32:06 +00:00
|
|
|
if (drbd_insert_fault(device, (op == REQ_OP_WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) {
|
2015-07-20 13:29:37 +00:00
|
|
|
bio_io_error(bio);
|
2009-09-25 23:07:19 +00:00
|
|
|
} else {
|
2016-06-05 19:31:41 +00:00
|
|
|
submit_bio(bio);
|
2011-06-14 12:18:23 +00:00
|
|
|
/* this should not count as user activity and cause the
|
|
|
|
* resync to throttle -- see drbd_rs_should_slow_down(). */
|
2011-07-03 11:26:43 +00:00
|
|
|
atomic_add(len >> 9, &device->rs_sect_ev);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bm_rw: read/write the whole bitmap from/to its on disk location.
|
|
|
|
*/
|
2014-05-05 22:44:59 +00:00
|
|
|
static int bm_rw(struct drbd_device *device, const unsigned int flags, unsigned lazy_writeout_upper_idx) __must_hold(local)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2014-05-05 22:44:59 +00:00
|
|
|
struct drbd_bm_aio_ctx *ctx;
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2016-06-13 22:26:38 +00:00
|
|
|
unsigned int num_pages, i, count = 0;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long now;
|
|
|
|
char ppb[10];
|
|
|
|
int err = 0;
|
|
|
|
|
2010-12-15 07:59:11 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2011-06-27 12:56:52 +00:00
|
|
|
|
2014-05-05 22:44:59 +00:00
|
|
|
ctx = kmalloc(sizeof(struct drbd_bm_aio_ctx), GFP_NOIO);
|
2011-06-27 12:56:52 +00:00
|
|
|
if (!ctx)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-05-05 22:44:59 +00:00
|
|
|
*ctx = (struct drbd_bm_aio_ctx) {
|
2011-07-03 11:26:43 +00:00
|
|
|
.device = device,
|
2014-05-05 22:44:59 +00:00
|
|
|
.start_jif = jiffies,
|
2011-06-27 12:56:52 +00:00
|
|
|
.in_flight = ATOMIC_INIT(1),
|
2011-06-27 13:29:16 +00:00
|
|
|
.done = 0,
|
2012-04-25 21:06:45 +00:00
|
|
|
.flags = flags,
|
2011-06-27 12:56:52 +00:00
|
|
|
.error = 0,
|
2016-11-14 16:12:23 +00:00
|
|
|
.kref = KREF_INIT(2),
|
2011-06-27 12:56:52 +00:00
|
|
|
};
|
|
|
|
|
2014-05-05 22:44:59 +00:00
|
|
|
if (!get_ldev_if_state(device, D_ATTACHING)) { /* put is in drbd_bm_aio_ctx_destroy() */
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "ASSERT FAILED: get_ldev_if_state() == 1 in bm_rw()\n");
|
2011-06-27 13:29:16 +00:00
|
|
|
kfree(ctx);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
drbd: device->ldev is not guaranteed on an D_ATTACHING disk
Some parts of the code assumed that get_ldev_if_state(device, D_ATTACHING)
is sufficient to access the ldev member of the device object. That was
wrong. ldev may not be there or might be freed at any time if the device
has a disk state of D_ATTACHING.
bm_rw()
Documented that drbd_bm_read() is only called from drbd_adm_attach.
drbd_bm_write() is only called when a reference is held, and it is
documented that a caller has to hold a reference before calling
drbd_bm_write()
drbd_bm_write_page()
Use get_ldev() instead of get_ldev_if_state(device, D_ATTACHING)
drbd_bmio_set_n_write()
No longer use get_ldev_if_state(device, D_ATTACHING). All callers
hold a reference to ldev now.
drbd_bmio_clear_n_write()
All callers where holding a reference of ldev anyways. Remove the
misleading get_ldev_if_state(device, D_ATTACHING)
drbd_reconsider_max_bio_size()
Removed the get_ldev_if_state(device, D_ATTACHING). All callers
now pass a struct drbd_backing_dev* when they have a proper
reference, or a NULL pointer.
Before this fix, the receiver could trigger a NULL pointer
deref when in drbd_reconsider_max_bio_size()
drbd_bump_write_ordering()
Used get_ldev_if_state(device, D_ATTACHING) with the wrong assumption.
Remove it, and allow the caller to pass in a struct drbd_backing_dev*
when the caller knows that accessing this bdev is safe.
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
2013-11-22 12:22:13 +00:00
|
|
|
/* Here D_ATTACHING is sufficient since drbd_bm_read() is called only from
|
|
|
|
drbd_adm_attach(), after device->ldev was assigned. */
|
2011-06-27 13:29:16 +00:00
|
|
|
|
2014-05-05 22:44:59 +00:00
|
|
|
if (0 == (ctx->flags & ~BM_AIO_READ))
|
2011-01-21 09:56:44 +00:00
|
|
|
WARN_ON(!(BM_LOCKED_MASK & b->bm_flags));
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2014-05-05 22:44:59 +00:00
|
|
|
spin_lock_irq(&device->resource->req_lock);
|
|
|
|
list_add_tail(&ctx->list, &device->pending_bitmap_io);
|
|
|
|
spin_unlock_irq(&device->resource->req_lock);
|
|
|
|
|
2010-12-15 23:32:38 +00:00
|
|
|
num_pages = b->bm_number_of_pages;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
now = jiffies;
|
|
|
|
|
|
|
|
/* let the layers below us try to merge these bios... */
|
2012-08-14 09:46:59 +00:00
|
|
|
|
2016-06-13 22:26:38 +00:00
|
|
|
if (flags & BM_AIO_READ) {
|
|
|
|
for (i = 0; i < num_pages; i++) {
|
|
|
|
atomic_inc(&ctx->in_flight);
|
|
|
|
bm_page_io_async(ctx, i);
|
|
|
|
++count;
|
|
|
|
cond_resched();
|
|
|
|
}
|
|
|
|
} else if (flags & BM_AIO_WRITE_HINTED) {
|
|
|
|
/* ASSERT: BM_AIO_WRITE_ALL_PAGES is not set. */
|
|
|
|
unsigned int hint;
|
|
|
|
for (hint = 0; hint < b->n_bitmap_hints; hint++) {
|
|
|
|
i = b->al_bitmap_hints[hint];
|
|
|
|
if (i >= num_pages) /* == -1U: no hint here. */
|
|
|
|
continue;
|
|
|
|
/* Several AL-extents may point to the same page. */
|
|
|
|
if (!test_and_clear_bit(BM_PAGE_HINT_WRITEOUT,
|
|
|
|
&page_private(b->bm_pages[i])))
|
|
|
|
continue;
|
|
|
|
/* Has it even changed? */
|
|
|
|
if (bm_test_page_unchanged(b->bm_pages[i]))
|
|
|
|
continue;
|
|
|
|
atomic_inc(&ctx->in_flight);
|
|
|
|
bm_page_io_async(ctx, i);
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < num_pages; i++) {
|
|
|
|
/* ignore completely unchanged pages */
|
|
|
|
if (lazy_writeout_upper_idx && i == lazy_writeout_upper_idx)
|
|
|
|
break;
|
2014-05-05 22:44:59 +00:00
|
|
|
if (!(flags & BM_AIO_WRITE_ALL_PAGES) &&
|
2012-08-08 19:19:09 +00:00
|
|
|
bm_test_page_unchanged(b->bm_pages[i])) {
|
2011-07-03 15:53:52 +00:00
|
|
|
dynamic_drbd_dbg(device, "skipped bm write for idx %u\n", i);
|
2010-12-15 07:59:11 +00:00
|
|
|
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])) {
|
2011-07-03 15:53:52 +00:00
|
|
|
dynamic_drbd_dbg(device, "skipped bm lazy write for idx %u\n", i);
|
2010-12-15 07:59:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-06-13 22:26:38 +00:00
|
|
|
atomic_inc(&ctx->in_flight);
|
|
|
|
bm_page_io_async(ctx, i);
|
|
|
|
++count;
|
|
|
|
cond_resched();
|
2010-12-15 07:59:11 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-25 23:07:19 +00:00
|
|
|
|
drbd: fix potential access of on-stack wait_queue_head_t after return
I run into something declaring itself as "spinlock deadlock",
BUG: spinlock lockup on CPU#1, kjournald/27816, ffff88000ad6bca0
Pid: 27816, comm: kjournald Tainted: G W 2.6.34.6 #2
Call Trace:
<IRQ> [<ffffffff811ba0aa>] do_raw_spin_lock+0x11e/0x14d
[<ffffffff81340fde>] _raw_spin_lock_irqsave+0x6a/0x81
[<ffffffff8103b694>] ? __wake_up+0x22/0x50
[<ffffffff8103b694>] __wake_up+0x22/0x50
[<ffffffffa07ff661>] bm_async_io_complete+0x258/0x299 [drbd]
but the call traces do not fit at all,
all other cpus are cpu_idle.
I think it may be this race:
drbd_bm_write_page
wait_queue_head_t io_wait;
atomic_t in_flight;
bm_async_io
submit_bio
bm_async_io_complete
if (atomic_dec_and_test(in_flight))
wait_event(io_wait,
atomic_read(in_flight) == 0)
return
wake_up(io_wait)
The wake_up now accesses the wait_queue_head_t spinlock, which is no
longer valid, since the stack frame of drbd_bm_write_page has been
clobbered now.
Fix this by using struct completion, which does both the condition test
as well as the wake_up inside its spinlock, so this race cannot happen.
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
2010-12-19 10:29:55 +00:00
|
|
|
/*
|
2014-09-11 12:29:06 +00:00
|
|
|
* We initialize ctx->in_flight to one to make sure drbd_bm_endio
|
2011-06-27 13:29:16 +00:00
|
|
|
* will not set ctx->done early, and decrement / test it here. If there
|
drbd: fix potential access of on-stack wait_queue_head_t after return
I run into something declaring itself as "spinlock deadlock",
BUG: spinlock lockup on CPU#1, kjournald/27816, ffff88000ad6bca0
Pid: 27816, comm: kjournald Tainted: G W 2.6.34.6 #2
Call Trace:
<IRQ> [<ffffffff811ba0aa>] do_raw_spin_lock+0x11e/0x14d
[<ffffffff81340fde>] _raw_spin_lock_irqsave+0x6a/0x81
[<ffffffff8103b694>] ? __wake_up+0x22/0x50
[<ffffffff8103b694>] __wake_up+0x22/0x50
[<ffffffffa07ff661>] bm_async_io_complete+0x258/0x299 [drbd]
but the call traces do not fit at all,
all other cpus are cpu_idle.
I think it may be this race:
drbd_bm_write_page
wait_queue_head_t io_wait;
atomic_t in_flight;
bm_async_io
submit_bio
bm_async_io_complete
if (atomic_dec_and_test(in_flight))
wait_event(io_wait,
atomic_read(in_flight) == 0)
return
wake_up(io_wait)
The wake_up now accesses the wait_queue_head_t spinlock, which is no
longer valid, since the stack frame of drbd_bm_write_page has been
clobbered now.
Fix this by using struct completion, which does both the condition test
as well as the wake_up inside its spinlock, so this race cannot happen.
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
2010-12-19 10:29:55 +00:00
|
|
|
* are still some bios in flight, we need to wait for them here.
|
2011-06-27 13:29:16 +00:00
|
|
|
* If all IO is done already (or nothing had been submitted), there is
|
|
|
|
* no need to wait. Still, we need to put the kref associated with the
|
|
|
|
* "in_flight reached zero, all done" event.
|
drbd: fix potential access of on-stack wait_queue_head_t after return
I run into something declaring itself as "spinlock deadlock",
BUG: spinlock lockup on CPU#1, kjournald/27816, ffff88000ad6bca0
Pid: 27816, comm: kjournald Tainted: G W 2.6.34.6 #2
Call Trace:
<IRQ> [<ffffffff811ba0aa>] do_raw_spin_lock+0x11e/0x14d
[<ffffffff81340fde>] _raw_spin_lock_irqsave+0x6a/0x81
[<ffffffff8103b694>] ? __wake_up+0x22/0x50
[<ffffffff8103b694>] __wake_up+0x22/0x50
[<ffffffffa07ff661>] bm_async_io_complete+0x258/0x299 [drbd]
but the call traces do not fit at all,
all other cpus are cpu_idle.
I think it may be this race:
drbd_bm_write_page
wait_queue_head_t io_wait;
atomic_t in_flight;
bm_async_io
submit_bio
bm_async_io_complete
if (atomic_dec_and_test(in_flight))
wait_event(io_wait,
atomic_read(in_flight) == 0)
return
wake_up(io_wait)
The wake_up now accesses the wait_queue_head_t spinlock, which is no
longer valid, since the stack frame of drbd_bm_write_page has been
clobbered now.
Fix this by using struct completion, which does both the condition test
as well as the wake_up inside its spinlock, so this race cannot happen.
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
2010-12-19 10:29:55 +00:00
|
|
|
*/
|
2011-06-27 12:56:52 +00:00
|
|
|
if (!atomic_dec_and_test(&ctx->in_flight))
|
2011-07-03 11:26:43 +00:00
|
|
|
wait_until_done_or_force_detached(device, device->ldev, &ctx->done);
|
2011-06-27 13:29:16 +00:00
|
|
|
else
|
2014-05-05 22:44:59 +00:00
|
|
|
kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy);
|
2011-06-27 12:56:52 +00:00
|
|
|
|
2011-02-21 14:10:23 +00:00
|
|
|
/* summary for global bitmap IO */
|
2016-06-13 22:26:37 +00:00
|
|
|
if (flags == 0) {
|
|
|
|
unsigned int ms = jiffies_to_msecs(jiffies - now);
|
|
|
|
if (ms > 5) {
|
|
|
|
drbd_info(device, "bitmap %s of %u pages took %u ms\n",
|
|
|
|
(flags & BM_AIO_READ) ? "READ" : "WRITE",
|
|
|
|
count, ms);
|
|
|
|
}
|
|
|
|
}
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2011-06-27 12:56:52 +00:00
|
|
|
if (ctx->error) {
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_alert(device, "we had at least one MD IO ERROR during bitmap IO\n");
|
2011-07-03 11:26:43 +00:00
|
|
|
drbd_chk_io_error(device, 1, DRBD_META_IO_ERROR);
|
2011-06-27 12:56:52 +00:00
|
|
|
err = -EIO; /* ctx->error ? */
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2011-06-27 13:29:16 +00:00
|
|
|
if (atomic_read(&ctx->in_flight))
|
2012-09-27 11:03:45 +00:00
|
|
|
err = -EIO; /* Disk timeout/force-detach during IO... */
|
2011-06-27 13:29:16 +00:00
|
|
|
|
2009-09-25 23:07:19 +00:00
|
|
|
now = jiffies;
|
2014-05-05 22:44:59 +00:00
|
|
|
if (flags & BM_AIO_READ) {
|
2010-12-15 07:59:09 +00:00
|
|
|
b->bm_set = bm_count_bits(b);
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_info(device, "recounting of set bits took additional %lu jiffies\n",
|
2009-09-25 23:07:19 +00:00
|
|
|
jiffies - now);
|
|
|
|
}
|
|
|
|
now = b->bm_set;
|
|
|
|
|
2014-05-05 22:44:59 +00:00
|
|
|
if ((flags & ~BM_AIO_READ) == 0)
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_info(device, "%s (%lu bits) marked out-of-sync by on disk bit-map.\n",
|
2011-02-21 14:10:23 +00:00
|
|
|
ppsize(ppb, now << (BM_BLOCK_SHIFT-10)), now);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2014-05-05 22:44:59 +00:00
|
|
|
kref_put(&ctx->kref, &drbd_bm_aio_ctx_destroy);
|
2009-09-25 23:07:19 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drbd_bm_read() - Read the whole bitmap from its on disk location.
|
2011-07-03 11:26:43 +00:00
|
|
|
* @device: DRBD device.
|
2009-09-25 23:07:19 +00:00
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_read(struct drbd_device *device) __must_hold(local)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2014-05-05 22:44:59 +00:00
|
|
|
return bm_rw(device, BM_AIO_READ, 0);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* drbd_bm_write() - Write the whole bitmap to its on disk location.
|
2011-07-03 11:26:43 +00:00
|
|
|
* @device: DRBD device.
|
2010-12-15 07:59:11 +00:00
|
|
|
*
|
|
|
|
* Will only write pages that have changed since last IO.
|
2009-09-25 23:07:19 +00:00
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_write(struct drbd_device *device) __must_hold(local)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2014-05-05 22:44:59 +00:00
|
|
|
return bm_rw(device, 0, 0);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2012-08-08 19:19:09 +00:00
|
|
|
/**
|
|
|
|
* drbd_bm_write_all() - Write the whole bitmap to its on disk location.
|
2011-07-03 11:26:43 +00:00
|
|
|
* @device: DRBD device.
|
2012-08-08 19:19:09 +00:00
|
|
|
*
|
|
|
|
* Will write all pages.
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_write_all(struct drbd_device *device) __must_hold(local)
|
2012-08-08 19:19:09 +00:00
|
|
|
{
|
2014-05-05 22:44:59 +00:00
|
|
|
return bm_rw(device, BM_AIO_WRITE_ALL_PAGES, 0);
|
2012-08-08 19:19:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 10:39:48 +00:00
|
|
|
/**
|
|
|
|
* drbd_bm_write_lazy() - Write bitmap pages 0 to @upper_idx-1, if they have changed.
|
|
|
|
* @device: DRBD device.
|
|
|
|
* @upper_idx: 0: write all changed pages; +ve: page index to stop scanning for changed pages
|
|
|
|
*/
|
|
|
|
int drbd_bm_write_lazy(struct drbd_device *device, unsigned upper_idx) __must_hold(local)
|
|
|
|
{
|
|
|
|
return bm_rw(device, BM_AIO_COPY_PAGES, upper_idx);
|
|
|
|
}
|
|
|
|
|
2012-04-25 21:06:45 +00:00
|
|
|
/**
|
|
|
|
* drbd_bm_write_copy_pages() - Write the whole bitmap to its on disk location.
|
2011-07-03 11:26:43 +00:00
|
|
|
* @device: DRBD device.
|
2012-04-25 21:06:45 +00:00
|
|
|
*
|
|
|
|
* Will only write pages that have changed since last IO.
|
|
|
|
* In contrast to drbd_bm_write(), this will copy the bitmap pages
|
|
|
|
* to temporary writeout pages. It is intended to trigger a full write-out
|
|
|
|
* while still allowing the bitmap to change, for example if a resync or online
|
|
|
|
* verify is aborted due to a failed peer disk, while local IO continues, or
|
|
|
|
* pending resync acks are still being processed.
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_write_copy_pages(struct drbd_device *device) __must_hold(local)
|
2012-04-25 21:06:45 +00:00
|
|
|
{
|
2014-05-05 22:44:59 +00:00
|
|
|
return bm_rw(device, BM_AIO_COPY_PAGES, 0);
|
2010-12-15 07:59:11 +00:00
|
|
|
}
|
|
|
|
|
2011-02-21 12:21:00 +00:00
|
|
|
/**
|
|
|
|
* drbd_bm_write_hinted() - Write bitmap pages with "hint" marks, if they have changed.
|
2011-07-03 11:26:43 +00:00
|
|
|
* @device: DRBD device.
|
2011-02-21 12:21:00 +00:00
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_write_hinted(struct drbd_device *device) __must_hold(local)
|
2011-02-21 12:21:00 +00:00
|
|
|
{
|
2014-05-05 22:44:59 +00:00
|
|
|
return bm_rw(device, BM_AIO_WRITE_HINTED | BM_AIO_COPY_PAGES, 0);
|
2011-02-21 12:21:00 +00:00
|
|
|
}
|
2010-12-15 07:59:11 +00:00
|
|
|
|
2009-09-25 23:07:19 +00:00
|
|
|
/* NOTE
|
|
|
|
* find_first_bit returns int, we return unsigned long.
|
2010-12-14 14:13:04 +00:00
|
|
|
* 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.
|
2009-09-25 23:07:19 +00:00
|
|
|
*
|
|
|
|
* this returns a bit number, NOT a sector!
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
static unsigned long __bm_find_next(struct drbd_device *device, unsigned long bm_fo,
|
2011-11-25 15:14:18 +00:00
|
|
|
const int find_zero_bit)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long *p_addr;
|
2010-12-14 14:13:04 +00:00
|
|
|
unsigned long bit_offset;
|
|
|
|
unsigned i;
|
|
|
|
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
if (bm_fo > b->bm_bits) {
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "bm_fo=%lu bm_bits=%lu\n", bm_fo, b->bm_bits);
|
2010-12-14 14:13:04 +00:00
|
|
|
bm_fo = DRBD_END_OF_BITMAP;
|
2009-09-25 23:07:19 +00:00
|
|
|
} else {
|
|
|
|
while (bm_fo < b->bm_bits) {
|
2010-12-15 07:59:11 +00:00
|
|
|
/* bit offset of the first bit in the page */
|
2010-12-14 14:13:04 +00:00
|
|
|
bit_offset = bm_fo & ~BITS_PER_PAGE_MASK;
|
2011-11-25 15:14:18 +00:00
|
|
|
p_addr = __bm_map_pidx(b, bm_bit_to_page_idx(b, bm_fo));
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
if (find_zero_bit)
|
2011-03-28 14:42:58 +00:00
|
|
|
i = find_next_zero_bit_le(p_addr,
|
2010-12-14 14:13:04 +00:00
|
|
|
PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
|
2009-09-25 23:07:19 +00:00
|
|
|
else
|
2011-03-28 14:42:58 +00:00
|
|
|
i = find_next_bit_le(p_addr,
|
2010-12-14 14:13:04 +00:00
|
|
|
PAGE_SIZE*8, bm_fo & BITS_PER_PAGE_MASK);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2011-11-25 15:14:18 +00:00
|
|
|
__bm_unmap(p_addr);
|
2009-09-25 23:07:19 +00:00
|
|
|
if (i < PAGE_SIZE*8) {
|
2010-12-14 14:13:04 +00:00
|
|
|
bm_fo = bit_offset + i;
|
|
|
|
if (bm_fo >= b->bm_bits)
|
2009-09-25 23:07:19 +00:00
|
|
|
break;
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
bm_fo = bit_offset + PAGE_SIZE*8;
|
|
|
|
}
|
2010-12-14 14:13:04 +00:00
|
|
|
bm_fo = DRBD_END_OF_BITMAP;
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
found:
|
2010-12-14 14:13:04 +00:00
|
|
|
return bm_fo;
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
static unsigned long bm_find_next(struct drbd_device *device,
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long bm_fo, const int find_zero_bit)
|
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2010-12-14 14:13:04 +00:00
|
|
|
unsigned long i = DRBD_END_OF_BITMAP;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return i;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b->bm_pages))
|
2010-12-15 18:31:20 +00:00
|
|
|
return i;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
spin_lock_irq(&b->bm_lock);
|
2011-01-21 09:56:44 +00:00
|
|
|
if (BM_DONT_TEST & b->bm_flags)
|
2011-07-03 11:26:43 +00:00
|
|
|
bm_print_lock_info(device);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
i = __bm_find_next(device, bm_fo, find_zero_bit);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
spin_unlock_irq(&b->bm_lock);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
unsigned long drbd_bm_find_next(struct drbd_device *device, unsigned long bm_fo)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
return bm_find_next(device, bm_fo, 0);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* not yet needed for anything. */
|
2011-07-03 11:26:43 +00:00
|
|
|
unsigned long drbd_bm_find_next_zero(struct drbd_device *device, unsigned long bm_fo)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
return bm_find_next(device, bm_fo, 1);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* does not spin_lock_irqsave.
|
|
|
|
* you must take drbd_bm_lock() first */
|
2011-07-03 11:26:43 +00:00
|
|
|
unsigned long _drbd_bm_find_next(struct drbd_device *device, unsigned long bm_fo)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
/* WARN_ON(!(BM_DONT_SET & device->b->bm_flags)); */
|
|
|
|
return __bm_find_next(device, bm_fo, 0);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
unsigned long _drbd_bm_find_next_zero(struct drbd_device *device, unsigned long bm_fo)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
/* WARN_ON(!(BM_DONT_SET & device->b->bm_flags)); */
|
|
|
|
return __bm_find_next(device, bm_fo, 1);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
2011-07-03 11:26:43 +00:00
|
|
|
static int __bm_change_bits_to(struct drbd_device *device, const unsigned long s,
|
drbd: add missing spinlock to bitmap receive
During bitmap exchange, when using the RLE bitmap compression scheme,
we have a code path that can set the whole bitmap at once.
To avoid holding spin_lock_irq() for too long, we used to lock out other
bitmap modifications during bitmap exchange by other means, and then,
knowing we have exclusive access to the bitmap, modify it without
the spinlock, and with IRQs enabled.
Since we now allow local IO to continue, potentially setting additional
bits during the bitmap receive phase, this is no longer true, and we get
uncoordinated updates of bitmap members, causing bm_set to no longer
accurately reflect the total number of set bits.
To actually see this, you'd need to have a large bitmap, use RLE bitmap
compression, and have busy IO during sync handshake and bitmap exchange.
Fix this by taking the spin_lock_irq() in this code path as well, but
calling cond_resched_lock() after each page worth of bits processed.
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
2011-06-03 19:18:13 +00:00
|
|
|
unsigned long e, int val)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long *p_addr = NULL;
|
|
|
|
unsigned long bitnr;
|
2010-12-15 07:59:11 +00:00
|
|
|
unsigned int last_page_nr = -1U;
|
2009-09-25 23:07:19 +00:00
|
|
|
int c = 0;
|
2010-12-15 07:59:11 +00:00
|
|
|
int changed_total = 0;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
if (e >= b->bm_bits) {
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "ASSERT FAILED: bit_s=%lu bit_e=%lu bm_bits=%lu\n",
|
2009-09-25 23:07:19 +00:00
|
|
|
s, e, b->bm_bits);
|
|
|
|
e = b->bm_bits ? b->bm_bits -1 : 0;
|
|
|
|
}
|
|
|
|
for (bitnr = s; bitnr <= e; bitnr++) {
|
2010-12-15 07:59:11 +00:00
|
|
|
unsigned int page_nr = bm_bit_to_page_idx(b, bitnr);
|
2009-09-25 23:07:19 +00:00
|
|
|
if (page_nr != last_page_nr) {
|
|
|
|
if (p_addr)
|
2011-11-25 15:14:18 +00:00
|
|
|
__bm_unmap(p_addr);
|
2010-12-15 07:59:11 +00:00
|
|
|
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;
|
2011-11-25 15:14:18 +00:00
|
|
|
p_addr = __bm_map_pidx(b, page_nr);
|
2009-09-25 23:07:19 +00:00
|
|
|
last_page_nr = page_nr;
|
|
|
|
}
|
|
|
|
if (val)
|
2011-03-28 14:42:58 +00:00
|
|
|
c += (0 == __test_and_set_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
|
2009-09-25 23:07:19 +00:00
|
|
|
else
|
2011-03-28 14:42:58 +00:00
|
|
|
c -= (0 != __test_and_clear_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr));
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
if (p_addr)
|
2011-11-25 15:14:18 +00:00
|
|
|
__bm_unmap(p_addr);
|
2010-12-15 07:59:11 +00:00
|
|
|
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;
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
2011-07-03 11:26:43 +00:00
|
|
|
static int bm_change_bits_to(struct drbd_device *device, const unsigned long s,
|
2009-09-25 23:07:19 +00:00
|
|
|
const unsigned long e, int val)
|
|
|
|
{
|
|
|
|
unsigned long flags;
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
int c = 0;
|
|
|
|
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 1;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b->bm_pages))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 0;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&b->bm_lock, flags);
|
2011-01-21 09:56:44 +00:00
|
|
|
if ((val ? BM_DONT_SET : BM_DONT_CLEAR) & b->bm_flags)
|
2011-07-03 11:26:43 +00:00
|
|
|
bm_print_lock_info(device);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
2011-07-03 11:26:43 +00:00
|
|
|
c = __bm_change_bits_to(device, s, e, val);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
spin_unlock_irqrestore(&b->bm_lock, flags);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns number of bits changed 0 -> 1 */
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_set_bits(struct drbd_device *device, const unsigned long s, const unsigned long e)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
return bm_change_bits_to(device, s, e, 1);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* returns number of bits changed 1 -> 0 */
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_clear_bits(struct drbd_device *device, const unsigned long s, const unsigned long e)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
return -bm_change_bits_to(device, s, e, 0);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2011-09-26 07:58:04 +00:00
|
|
|
int changed = 0;
|
2011-11-25 15:14:18 +00:00
|
|
|
unsigned long *paddr = kmap_atomic(b->bm_pages[page_nr]);
|
2015-03-20 15:15:24 +00:00
|
|
|
|
|
|
|
/* I think it is more cache line friendly to hweight_long then set to ~0UL,
|
|
|
|
* than to first bitmap_weight() all words, then bitmap_fill() all words */
|
2009-09-25 23:07:19 +00:00
|
|
|
for (i = first_word; i < last_word; i++) {
|
|
|
|
bits = hweight_long(paddr[i]);
|
|
|
|
paddr[i] = ~0UL;
|
2011-09-26 07:58:04 +00:00
|
|
|
changed += BITS_PER_LONG - bits;
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
2011-11-25 15:14:18 +00:00
|
|
|
kunmap_atomic(paddr);
|
2011-09-26 07:58:04 +00:00
|
|
|
if (changed) {
|
|
|
|
/* We only need lazy writeout, the information is still in the
|
|
|
|
* remote bitmap as well, and is reconstructed during the next
|
|
|
|
* bitmap exchange, if lost locally due to a crash. */
|
|
|
|
bm_set_page_lazy_writeout(b->bm_pages[page_nr]);
|
|
|
|
b->bm_set += changed;
|
|
|
|
}
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
drbd: add missing spinlock to bitmap receive
During bitmap exchange, when using the RLE bitmap compression scheme,
we have a code path that can set the whole bitmap at once.
To avoid holding spin_lock_irq() for too long, we used to lock out other
bitmap modifications during bitmap exchange by other means, and then,
knowing we have exclusive access to the bitmap, modify it without
the spinlock, and with IRQs enabled.
Since we now allow local IO to continue, potentially setting additional
bits during the bitmap receive phase, this is no longer true, and we get
uncoordinated updates of bitmap members, causing bm_set to no longer
accurately reflect the total number of set bits.
To actually see this, you'd need to have a large bitmap, use RLE bitmap
compression, and have busy IO during sync handshake and bitmap exchange.
Fix this by taking the spin_lock_irq() in this code path as well, but
calling cond_resched_lock() after each page worth of bits processed.
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
2011-06-03 19:18:13 +00:00
|
|
|
/* Same thing as drbd_bm_set_bits,
|
|
|
|
* but more efficient for a large bit range.
|
2009-09-25 23:07:19 +00:00
|
|
|
* You must first drbd_bm_lock().
|
|
|
|
* Can be called to set the whole bitmap in one go.
|
|
|
|
* Sets bits from s to e _inclusive_. */
|
2011-07-03 11:26:43 +00:00
|
|
|
void _drbd_bm_set_bits(struct drbd_device *device, const unsigned long s, const unsigned long e)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
|
|
|
/* 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.
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
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. */
|
drbd: add missing spinlock to bitmap receive
During bitmap exchange, when using the RLE bitmap compression scheme,
we have a code path that can set the whole bitmap at once.
To avoid holding spin_lock_irq() for too long, we used to lock out other
bitmap modifications during bitmap exchange by other means, and then,
knowing we have exclusive access to the bitmap, modify it without
the spinlock, and with IRQs enabled.
Since we now allow local IO to continue, potentially setting additional
bits during the bitmap receive phase, this is no longer true, and we get
uncoordinated updates of bitmap members, causing bm_set to no longer
accurately reflect the total number of set bits.
To actually see this, you'd need to have a large bitmap, use RLE bitmap
compression, and have busy IO during sync handshake and bitmap exchange.
Fix this by taking the spin_lock_irq() in this code path as well, but
calling cond_resched_lock() after each page worth of bits processed.
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
2011-06-03 19:18:13 +00:00
|
|
|
spin_lock_irq(&b->bm_lock);
|
2011-07-03 11:26:43 +00:00
|
|
|
__bm_change_bits_to(device, s, e, 1);
|
drbd: add missing spinlock to bitmap receive
During bitmap exchange, when using the RLE bitmap compression scheme,
we have a code path that can set the whole bitmap at once.
To avoid holding spin_lock_irq() for too long, we used to lock out other
bitmap modifications during bitmap exchange by other means, and then,
knowing we have exclusive access to the bitmap, modify it without
the spinlock, and with IRQs enabled.
Since we now allow local IO to continue, potentially setting additional
bits during the bitmap receive phase, this is no longer true, and we get
uncoordinated updates of bitmap members, causing bm_set to no longer
accurately reflect the total number of set bits.
To actually see this, you'd need to have a large bitmap, use RLE bitmap
compression, and have busy IO during sync handshake and bitmap exchange.
Fix this by taking the spin_lock_irq() in this code path as well, but
calling cond_resched_lock() after each page worth of bits processed.
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
2011-06-03 19:18:13 +00:00
|
|
|
spin_unlock_irq(&b->bm_lock);
|
2009-09-25 23:07:19 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* difference is large enough that we can trust sl and el */
|
|
|
|
|
drbd: add missing spinlock to bitmap receive
During bitmap exchange, when using the RLE bitmap compression scheme,
we have a code path that can set the whole bitmap at once.
To avoid holding spin_lock_irq() for too long, we used to lock out other
bitmap modifications during bitmap exchange by other means, and then,
knowing we have exclusive access to the bitmap, modify it without
the spinlock, and with IRQs enabled.
Since we now allow local IO to continue, potentially setting additional
bits during the bitmap receive phase, this is no longer true, and we get
uncoordinated updates of bitmap members, causing bm_set to no longer
accurately reflect the total number of set bits.
To actually see this, you'd need to have a large bitmap, use RLE bitmap
compression, and have busy IO during sync handshake and bitmap exchange.
Fix this by taking the spin_lock_irq() in this code path as well, but
calling cond_resched_lock() after each page worth of bits processed.
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
2011-06-03 19:18:13 +00:00
|
|
|
spin_lock_irq(&b->bm_lock);
|
|
|
|
|
2009-09-25 23:07:19 +00:00
|
|
|
/* bits filling the current long */
|
|
|
|
if (sl)
|
2011-07-03 11:26:43 +00:00
|
|
|
__bm_change_bits_to(device, s, sl-1, 1);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
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++) {
|
2011-07-03 11:26:43 +00:00
|
|
|
bm_set_full_words_within_one_page(device->bitmap, page_nr, first_word, last_word);
|
2011-06-06 09:31:42 +00:00
|
|
|
spin_unlock_irq(&b->bm_lock);
|
|
|
|
cond_resched();
|
2009-09-25 23:07:19 +00:00
|
|
|
first_word = 0;
|
2011-06-06 09:31:42 +00:00
|
|
|
spin_lock_irq(&b->bm_lock);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
/* last page (respectively only page, for first page == last page) */
|
|
|
|
last_word = MLPP(el >> LN2_BPL);
|
drbd: fix access of unallocated pages and kernel panic
BUG: unable to handle kernel NULL pointer dereference at (null)
...
[<d1e17561>] ? _drbd_bm_set_bits+0x151/0x240 [drbd]
[<d1e236f8>] ? receive_bitmap+0x4f8/0xbc0 [drbd]
This fixes an off-by-one error in the receive_bitmap() path,
if run-length encoded bitmap transfer is enabled.
If the bitmap is an exact multiple of PAGE_SIZE, which means the visible
capacity of the drbd device is an exact multiple of 128 MiB (for 4k page
size), and bitmap compression (use-rle) is enabled (which became default
with 8.4), and the very last bit is dirty and reported in an rle
comressed bitmap packet, we ended up trying to kmap_atomic a page pointer
that does not exist (bitmap->bm_pages[last index + 1]).
bug introduced by:
Date: Fri Jul 24 15:33:24 2009 +0200
set bits: optimize for complete last word, fix off-by-one-word corner case
made effective by:
Date: Thu Dec 16 00:32:38 2010 +0100
drbd: get rid of unused debug code
Long time ago, we had paranoia code in the bitmap that allocated one
extra word, assigned a magic value, and checked on every occasion that
the magic value was still unchanged.
That debug code is unused, the extra long word complicates code a bit.
Get rid of it.
No-one triggered this bug in the last few years, because a large subset
of our userbase is unaffected:
* typically the last few blocks of a device are not modified
frequently, and remain unset
* use-rle was disabled by default in drbd < 8.4
* those with slightly "odd" device sizes, or
* drbd internal meta data (which will skew the device size slightly,
thus makes it harder to have a bug relevant device size)
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
2012-06-08 11:18:51 +00:00
|
|
|
|
|
|
|
/* consider bitmap->bm_bits = 32768, bitmap->bm_number_of_pages = 1. (or multiples).
|
|
|
|
* ==> e = 32767, el = 32768, last_page = 2,
|
|
|
|
* and now last_word = 0.
|
|
|
|
* We do not want to touch last_page in this case,
|
|
|
|
* as we did not allocate it, it is not present in bitmap->bm_pages.
|
|
|
|
*/
|
|
|
|
if (last_word)
|
2011-07-03 11:26:43 +00:00
|
|
|
bm_set_full_words_within_one_page(device->bitmap, last_page, first_word, last_word);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
/* 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)
|
2011-07-03 11:26:43 +00:00
|
|
|
__bm_change_bits_to(device, el, e, 1);
|
drbd: add missing spinlock to bitmap receive
During bitmap exchange, when using the RLE bitmap compression scheme,
we have a code path that can set the whole bitmap at once.
To avoid holding spin_lock_irq() for too long, we used to lock out other
bitmap modifications during bitmap exchange by other means, and then,
knowing we have exclusive access to the bitmap, modify it without
the spinlock, and with IRQs enabled.
Since we now allow local IO to continue, potentially setting additional
bits during the bitmap receive phase, this is no longer true, and we get
uncoordinated updates of bitmap members, causing bm_set to no longer
accurately reflect the total number of set bits.
To actually see this, you'd need to have a large bitmap, use RLE bitmap
compression, and have busy IO during sync handshake and bitmap exchange.
Fix this by taking the spin_lock_irq() in this code path as well, but
calling cond_resched_lock() after each page worth of bits processed.
Signed-off-by: Philipp Reisner <philipp.reisner@linbit.com>
Signed-off-by: Lars Ellenberg <lars.ellenberg@linbit.com>
2011-06-03 19:18:13 +00:00
|
|
|
spin_unlock_irq(&b->bm_lock);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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!
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_test_bit(struct drbd_device *device, const unsigned long bitnr)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long *p_addr;
|
|
|
|
int i;
|
|
|
|
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 0;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b->bm_pages))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 0;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&b->bm_lock, flags);
|
2011-01-21 09:56:44 +00:00
|
|
|
if (BM_DONT_TEST & b->bm_flags)
|
2011-07-03 11:26:43 +00:00
|
|
|
bm_print_lock_info(device);
|
2009-09-25 23:07:19 +00:00
|
|
|
if (bitnr < b->bm_bits) {
|
2010-12-15 07:59:11 +00:00
|
|
|
p_addr = bm_map_pidx(b, bm_bit_to_page_idx(b, bitnr));
|
2011-03-28 14:42:58 +00:00
|
|
|
i = test_bit_le(bitnr & BITS_PER_PAGE_MASK, p_addr) ? 1 : 0;
|
2009-09-25 23:07:19 +00:00
|
|
|
bm_unmap(p_addr);
|
|
|
|
} else if (bitnr == b->bm_bits) {
|
|
|
|
i = -1;
|
|
|
|
} else { /* (bitnr > b->bm_bits) */
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "bitnr=%lu > bm_bits=%lu\n", bitnr, b->bm_bits);
|
2009-09-25 23:07:19 +00:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock_irqrestore(&b->bm_lock, flags);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* returns number of bits set in the range [s, e] */
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_count_bits(struct drbd_device *device, const unsigned long s, const unsigned long e)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
|
|
|
unsigned long flags;
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2010-12-15 07:59:11 +00:00
|
|
|
unsigned long *p_addr = NULL;
|
2009-09-25 23:07:19 +00:00
|
|
|
unsigned long bitnr;
|
2010-12-15 07:59:11 +00:00
|
|
|
unsigned int page_nr = -1U;
|
2009-09-25 23:07:19 +00:00
|
|
|
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) */
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 1;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b->bm_pages))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 1;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&b->bm_lock, flags);
|
2011-01-21 09:56:44 +00:00
|
|
|
if (BM_DONT_TEST & b->bm_flags)
|
2011-07-03 11:26:43 +00:00
|
|
|
bm_print_lock_info(device);
|
2009-09-25 23:07:19 +00:00
|
|
|
for (bitnr = s; bitnr <= e; bitnr++) {
|
2010-12-15 07:59:11 +00:00
|
|
|
unsigned int idx = bm_bit_to_page_idx(b, bitnr);
|
|
|
|
if (page_nr != idx) {
|
|
|
|
page_nr = idx;
|
2009-09-25 23:07:19 +00:00
|
|
|
if (p_addr)
|
|
|
|
bm_unmap(p_addr);
|
2010-12-15 07:59:11 +00:00
|
|
|
p_addr = bm_map_pidx(b, idx);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
2022-12-01 11:03:50 +00:00
|
|
|
if (expect(device, bitnr < b->bm_bits))
|
2011-03-28 14:42:58 +00:00
|
|
|
c += (0 != test_bit_le(bitnr - (page_nr << (PAGE_SHIFT+3)), p_addr));
|
2010-12-15 18:31:20 +00:00
|
|
|
else
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "bitnr=%lu bm_bits=%lu\n", bitnr, b->bm_bits);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
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...
|
|
|
|
*
|
|
|
|
*/
|
2011-07-03 11:26:43 +00:00
|
|
|
int drbd_bm_e_weight(struct drbd_device *device, unsigned long enr)
|
2009-09-25 23:07:19 +00:00
|
|
|
{
|
2011-07-03 11:26:43 +00:00
|
|
|
struct drbd_bitmap *b = device->bitmap;
|
2009-09-25 23:07:19 +00:00
|
|
|
int count, s, e;
|
|
|
|
unsigned long flags;
|
|
|
|
unsigned long *p_addr, *bm;
|
|
|
|
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 0;
|
2022-12-01 11:03:50 +00:00
|
|
|
if (!expect(device, b->bm_pages))
|
2010-12-15 18:31:20 +00:00
|
|
|
return 0;
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
spin_lock_irqsave(&b->bm_lock, flags);
|
2011-01-21 09:56:44 +00:00
|
|
|
if (BM_DONT_TEST & b->bm_flags)
|
2011-07-03 11:26:43 +00:00
|
|
|
bm_print_lock_info(device);
|
2009-09-25 23:07:19 +00:00
|
|
|
|
|
|
|
s = S2W(enr);
|
|
|
|
e = min((size_t)S2W(enr+1), b->bm_words);
|
|
|
|
count = 0;
|
|
|
|
if (s < b->bm_words) {
|
|
|
|
int n = e-s;
|
2010-12-15 07:59:11 +00:00
|
|
|
p_addr = bm_map_pidx(b, bm_word_to_page_idx(b, s));
|
2009-09-25 23:07:19 +00:00
|
|
|
bm = p_addr + MLPP(s);
|
2015-03-20 15:15:24 +00:00
|
|
|
count += bitmap_weight(bm, n * BITS_PER_LONG);
|
2009-09-25 23:07:19 +00:00
|
|
|
bm_unmap(p_addr);
|
|
|
|
} else {
|
2011-07-03 15:53:52 +00:00
|
|
|
drbd_err(device, "start offset (%d) too large in drbd_bm_e_weight\n", s);
|
2009-09-25 23:07:19 +00:00
|
|
|
}
|
|
|
|
spin_unlock_irqrestore(&b->bm_lock, flags);
|
|
|
|
return count;
|
|
|
|
}
|