4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
6 Copyright (C) 2003-2008, LINBIT Information Technologies GmbH.
7 Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>.
8 Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
10 drbd is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2, or (at your option)
15 drbd is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with drbd; see the file COPYING. If not, write to
22 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include <linux/slab.h>
27 #include <linux/crc32c.h>
28 #include <linux/drbd.h>
29 #include <linux/drbd_limits.h>
30 #include <linux/dynamic_debug.h>
32 #include "drbd_wrappers.h"
35 enum al_transaction_types {
37 AL_TR_INITIALIZED = 0xffff
39 /* all fields on disc in big endian */
40 struct __packed al_transaction_on_disk {
41 /* don't we all like magic */
44 /* to identify the most recent transaction block
45 * in the on disk ring buffer */
48 /* checksum on the full 4k block, with this field set to 0. */
51 /* type of transaction, special transaction types like:
52 * purge-all, set-all-idle, set-all-active, ... to-be-defined
53 * see also enum al_transaction_types */
54 __be16 transaction_type;
56 /* we currently allow only a few thousand extents,
57 * so 16bit will be enough for the slot number. */
59 /* how many updates in this transaction */
62 /* maximum slot number, "al-extents" in drbd.conf speak.
63 * Having this in each transaction should make reconfiguration
64 * of that parameter easier. */
67 /* slot number the context starts with */
68 __be16 context_start_slot_nr;
70 /* Some reserved bytes. Expected usage is a 64bit counter of
71 * sectors-written since device creation, and other data generation tag
75 /* --- 36 byte used --- */
77 /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes
78 * in one transaction, then use the remaining byte in the 4k block for
79 * context information. "Flexible" number of updates per transaction
80 * does not help, as we have to account for the case when all update
81 * slots are used anyways, so it would only complicate code without
84 __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION];
86 /* but the extent number is 32bit, which at an extent size of 4 MiB
87 * allows to cover device sizes of up to 2**54 Byte (16 PiB) */
88 __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION];
90 /* --- 420 bytes used (36 + 64*6) --- */
92 /* 4096 - 420 = 3676 = 919 * 4 */
93 __be32 context[AL_CONTEXT_PER_TRANSACTION];
96 struct update_odbm_work {
101 struct update_al_work {
103 struct completion event;
107 struct drbd_atodb_wait {
109 struct completion io_done;
110 struct drbd_conf *mdev;
115 static int w_al_write_transaction(struct drbd_work *, int);
117 static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
118 struct drbd_backing_dev *bdev,
119 struct page *page, sector_t sector,
123 struct drbd_md_io md_io;
127 init_completion(&md_io.event);
130 if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
131 rw |= REQ_FUA | REQ_FLUSH;
134 bio = bio_alloc_drbd(GFP_NOIO);
135 bio->bi_bdev = bdev->md_bdev;
136 bio->bi_sector = sector;
138 if (bio_add_page(bio, page, size, 0) != size)
140 bio->bi_private = &md_io;
141 bio->bi_end_io = drbd_md_io_complete;
144 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
145 bio_endio(bio, -EIO);
148 wait_for_completion(&md_io.event);
149 if (bio_flagged(bio, BIO_UPTODATE))
157 int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
158 sector_t sector, int rw)
161 struct page *iop = mdev->md_io_page;
163 D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
165 BUG_ON(!bdev->md_bdev);
167 dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s)\n",
168 current->comm, current->pid, __func__,
169 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
171 if (sector < drbd_md_first_sector(bdev) ||
172 sector + 7 > drbd_md_last_sector(bdev))
173 dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
174 current->comm, current->pid, __func__,
175 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
177 err = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, MD_BLOCK_SIZE);
179 dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
180 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
185 static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
187 struct lc_element *al_ext;
188 struct lc_element *tmp;
191 spin_lock_irq(&mdev->al_lock);
192 tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
193 if (unlikely(tmp != NULL)) {
194 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
195 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
196 wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
197 spin_unlock_irq(&mdev->al_lock);
199 wake_up(&mdev->al_wait);
203 al_ext = lc_get(mdev->act_log, enr);
204 spin_unlock_irq(&mdev->al_lock);
208 void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
210 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
211 struct lc_element *al_ext;
212 struct update_al_work al_work;
214 D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
216 wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
218 if (al_ext->lc_number != enr) {
219 /* drbd_al_write_transaction(mdev,al_ext,enr);
220 * recurses into generic_make_request(), which
221 * disallows recursion, bios being serialized on the
222 * current->bio_tail list now.
223 * we have to delegate updates to the activity log
224 * to the worker thread. */
226 /* Serialize multiple transactions.
227 * This uses test_and_set_bit, memory barrier is implicit.
228 * Optimization potential:
229 * first check for transaction number > old transaction number,
230 * so not all waiters have to lock/unlock. */
231 wait_event(mdev->al_wait, lc_try_lock_for_transaction(mdev->act_log));
233 /* Double check: it may have been committed by someone else,
234 * while we have been waiting for the lock. */
235 if (al_ext->lc_number != enr) {
236 init_completion(&al_work.event);
237 al_work.w.cb = w_al_write_transaction;
238 al_work.w.mdev = mdev;
239 drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w);
240 wait_for_completion(&al_work.event);
244 spin_lock_irq(&mdev->al_lock);
247 we need an "lc_cancel" here;
249 lc_committed(mdev->act_log);
250 spin_unlock_irq(&mdev->al_lock);
252 lc_unlock(mdev->act_log);
253 wake_up(&mdev->al_wait);
257 void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
259 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
260 struct lc_element *extent;
263 spin_lock_irqsave(&mdev->al_lock, flags);
265 extent = lc_find(mdev->act_log, enr);
268 spin_unlock_irqrestore(&mdev->al_lock, flags);
269 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
273 if (lc_put(mdev->act_log, extent) == 0)
274 wake_up(&mdev->al_wait);
276 spin_unlock_irqrestore(&mdev->al_lock, flags);
279 #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
280 /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
281 * are still coupled, or assume too much about their relation.
282 * Code below will not work if this is violated.
283 * Will be cleaned up with some followup patch.
288 static unsigned int al_extent_to_bm_page(unsigned int al_enr)
293 /* al extent number to bit */
294 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
297 static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
302 /* al extent number to bit */
303 (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
307 w_al_write_transaction(struct drbd_work *w, int unused)
309 struct update_al_work *aw = container_of(w, struct update_al_work, w);
310 struct drbd_conf *mdev = w->mdev;
311 struct al_transaction_on_disk *buffer;
312 struct lc_element *e;
318 if (!get_ldev(mdev)) {
319 dev_err(DEV, "disk is %s, cannot start al transaction\n",
320 drbd_disk_str(mdev->state.disk));
322 complete(&((struct update_al_work *)w)->event);
326 /* The bitmap write may have failed, causing a state change. */
327 if (mdev->state.disk < D_INCONSISTENT) {
329 "disk is %s, cannot write al transaction\n",
330 drbd_disk_str(mdev->state.disk));
332 complete(&((struct update_al_work *)w)->event);
337 mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
338 buffer = page_address(mdev->md_io_page);
340 memset(buffer, 0, sizeof(*buffer));
341 buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
342 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
346 /* Even though no one can start to change this list
347 * once we set the LC_LOCKED -- from drbd_al_begin_io(),
348 * lc_try_lock_for_transaction() --, someone may still
349 * be in the process of changing it. */
350 spin_lock_irq(&mdev->al_lock);
351 list_for_each_entry(e, &mdev->act_log->to_be_changed, list) {
352 if (i == AL_UPDATES_PER_TRANSACTION) {
356 buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
357 buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
358 if (e->lc_number != LC_FREE)
359 drbd_bm_mark_for_writeout(mdev,
360 al_extent_to_bm_page(e->lc_number));
363 spin_unlock_irq(&mdev->al_lock);
364 BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
366 buffer->n_updates = cpu_to_be16(i);
367 for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
368 buffer->update_slot_nr[i] = cpu_to_be16(-1);
369 buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
372 buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements);
373 buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle);
375 mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
376 mdev->act_log->nr_elements - mdev->al_tr_cycle);
377 for (i = 0; i < mx; i++) {
378 unsigned idx = mdev->al_tr_cycle + i;
379 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
380 buffer->context[i] = cpu_to_be32(extent_nr);
382 for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
383 buffer->context[i] = cpu_to_be32(LC_FREE);
385 mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
386 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
387 mdev->al_tr_cycle = 0;
389 sector = mdev->ldev->md.md_offset
390 + mdev->ldev->md.al_offset
391 + mdev->al_tr_pos * (MD_BLOCK_SIZE>>9);
393 crc = crc32c(0, buffer, 4096);
394 buffer->crc32c = cpu_to_be32(crc);
396 if (drbd_bm_write_hinted(mdev))
398 /* drbd_chk_io_error done already */
399 else if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
401 drbd_chk_io_error(mdev, 1, true);
403 /* advance ringbuffer position and transaction counter */
404 mdev->al_tr_pos = (mdev->al_tr_pos + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
405 mdev->al_tr_number++;
408 mutex_unlock(&mdev->md_io_mutex);
409 complete(&((struct update_al_work *)w)->event);
416 * reading of the activity log,
417 * and potentially dirtying of the affected bitmap regions,
418 * should be done from userland only.
419 * DRBD would simply always attach with an empty activity log,
420 * and refuse to attach to something that looks like a crashed primary.
424 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
425 * @mdev: DRBD device.
426 * @bdev: Block device to read form.
427 * @b: pointer to an al_transaction.
428 * @index: On disk slot of the transaction to read.
430 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
432 static int drbd_al_read_tr(struct drbd_conf *mdev,
433 struct drbd_backing_dev *bdev,
436 struct al_transaction_on_disk *b = page_address(mdev->md_io_page);
440 sector = bdev->md.md_offset
442 + index * (MD_BLOCK_SIZE>>9);
444 /* Dont process error normally,
445 * as this is done before disk is attached! */
446 if (drbd_md_sync_page_io(mdev, bdev, sector, READ))
449 if (!expect(b->magic == cpu_to_be32(DRBD_AL_MAGIC)))
452 if (!expect(be16_to_cpu(b->n_updates) <= AL_UPDATES_PER_TRANSACTION))
455 if (!expect(be16_to_cpu(b->context_size) <= DRBD_AL_EXTENTS_MAX))
458 if (!expect(be16_to_cpu(b->context_start_slot_nr) < DRBD_AL_EXTENTS_MAX))
461 crc = be32_to_cpu(b->crc32c);
463 if (!expect(crc == crc32c(0, b, 4096)))
470 * drbd_al_read_log() - Restores the activity log from its on disk representation.
471 * @mdev: DRBD device.
472 * @bdev: Block device to read form.
474 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
476 int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
478 struct al_transaction_on_disk *b;
482 int active_extents = 0;
483 int transactions = 0;
485 int found_initialized = 0;
492 /* Note that this is expected to be called with a newly created,
493 * clean and all unused activity log of the "expected size".
496 /* lock out all other meta data io for now,
497 * and make sure the page is mapped.
499 mutex_lock(&mdev->md_io_mutex);
500 b = page_address(mdev->md_io_page);
502 /* Always use the full ringbuffer space for now.
503 * possible optimization: read in all of it,
504 * then scan the in-memory pages. */
506 mx = (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
508 /* Find the valid transaction in the log */
509 for (i = 0; i < mx; i++) {
510 rv = drbd_al_read_tr(mdev, bdev, i);
511 /* invalid data in that block */
514 if (be16_to_cpu(b->transaction_type) == AL_TR_INITIALIZED) {
521 mutex_unlock(&mdev->md_io_mutex);
525 cnr = be32_to_cpu(b->tr_number);
526 if (++found_valid == 1) {
534 D_ASSERT(cnr != to_tnr);
535 D_ASSERT(cnr != from_tnr);
536 if ((int)cnr - (int)from_tnr < 0) {
537 D_ASSERT(from_tnr - cnr + i - from == mx);
541 if ((int)cnr - (int)to_tnr > 0) {
542 D_ASSERT(cnr - to_tnr == i - to);
549 if (found_initialized != mx)
550 dev_warn(DEV, "No usable activity log found.\n");
551 mutex_unlock(&mdev->md_io_mutex);
555 /* Read the valid transactions.
556 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
559 struct lc_element *e;
560 unsigned j, n, slot, extent_nr;
562 rv = drbd_al_read_tr(mdev, bdev, i);
563 if (!expect(rv != 0))
566 mutex_unlock(&mdev->md_io_mutex);
570 /* deal with different transaction types.
571 * not yet implemented */
572 if (!expect(b->transaction_type == 0))
575 /* on the fly re-create/resize activity log?
576 * will be a special transaction type flag. */
577 if (!expect(be16_to_cpu(b->context_size) == mdev->act_log->nr_elements))
579 if (!expect(be16_to_cpu(b->context_start_slot_nr) < mdev->act_log->nr_elements))
582 /* We are the only user of the activity log right now,
583 * don't actually need to take that lock. */
584 spin_lock_irq(&mdev->al_lock);
586 /* first, apply the context, ... */
587 for (j = 0, slot = be16_to_cpu(b->context_start_slot_nr);
588 j < AL_CONTEXT_PER_TRANSACTION &&
589 slot < mdev->act_log->nr_elements; j++, slot++) {
590 extent_nr = be32_to_cpu(b->context[j]);
591 e = lc_element_by_index(mdev->act_log, slot);
592 if (e->lc_number != extent_nr) {
593 if (extent_nr != LC_FREE)
598 lc_set(mdev->act_log, extent_nr, slot);
601 /* ... then apply the updates,
602 * which override the context information.
603 * drbd_al_read_tr already did the rangecheck
604 * on n <= AL_UPDATES_PER_TRANSACTION */
605 n = be16_to_cpu(b->n_updates);
606 for (j = 0; j < n; j++) {
607 slot = be16_to_cpu(b->update_slot_nr[j]);
608 extent_nr = be32_to_cpu(b->update_extent_nr[j]);
609 if (!expect(slot < mdev->act_log->nr_elements))
611 e = lc_element_by_index(mdev->act_log, slot);
612 if (e->lc_number != extent_nr) {
613 if (extent_nr != LC_FREE)
618 lc_set(mdev->act_log, extent_nr, slot);
620 spin_unlock_irq(&mdev->al_lock);
632 mdev->al_tr_number = to_tnr+1;
633 mdev->al_tr_pos = (to + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
635 /* ok, we are done with it */
636 mutex_unlock(&mdev->md_io_mutex);
638 dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
639 transactions, active_extents);
645 * drbd_al_apply_to_bm() - Sets the bitmap to dirty(1) where covered by active AL extents
646 * @mdev: DRBD device.
648 void drbd_al_apply_to_bm(struct drbd_conf *mdev)
651 unsigned long add = 0;
655 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
657 for (i = 0; i < mdev->act_log->nr_elements; i++) {
658 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
661 tmp = drbd_bm_ALe_set_all(mdev, enr);
662 dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
666 lc_unlock(mdev->act_log);
667 wake_up(&mdev->al_wait);
669 dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
670 ppsize(ppb, Bit2KB(add)));
673 static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
677 spin_lock_irq(&mdev->al_lock);
678 rv = (al_ext->refcnt == 0);
680 lc_del(mdev->act_log, al_ext);
681 spin_unlock_irq(&mdev->al_lock);
687 * drbd_al_shrink() - Removes all active extents form the activity log
688 * @mdev: DRBD device.
690 * Removes all active extents form the activity log, waiting until
691 * the reference count of each entry dropped to 0 first, of course.
693 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
695 void drbd_al_shrink(struct drbd_conf *mdev)
697 struct lc_element *al_ext;
700 D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags));
702 for (i = 0; i < mdev->act_log->nr_elements; i++) {
703 al_ext = lc_element_by_index(mdev->act_log, i);
704 if (al_ext->lc_number == LC_FREE)
706 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
709 wake_up(&mdev->al_wait);
712 static int w_update_odbm(struct drbd_work *w, int unused)
714 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
715 struct drbd_conf *mdev = w->mdev;
716 struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
718 if (!get_ldev(mdev)) {
719 if (__ratelimit(&drbd_ratelimit_state))
720 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
725 drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
730 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
731 switch (mdev->state.conn) {
732 case C_SYNC_SOURCE: case C_SYNC_TARGET:
733 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
734 drbd_resync_finished(mdev);
740 drbd_bcast_event(mdev, &sib);
746 /* ATTENTION. The AL's extents are 4MB each, while the extents in the
747 * resync LRU-cache are 16MB each.
748 * The caller of this function has to hold an get_ldev() reference.
750 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
752 static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
753 int count, int success)
755 struct lc_element *e;
756 struct update_odbm_work *udw;
760 D_ASSERT(atomic_read(&mdev->local_cnt));
762 /* I simply assume that a sector/size pair never crosses
763 * a 16 MB extent border. (Currently this is true...) */
764 enr = BM_SECT_TO_EXT(sector);
766 e = lc_get(mdev->resync, enr);
768 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
769 if (ext->lce.lc_number == enr) {
771 ext->rs_left -= count;
773 ext->rs_failed += count;
774 if (ext->rs_left < ext->rs_failed) {
775 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
776 "rs_failed=%d count=%d\n",
777 (unsigned long long)sector,
778 ext->lce.lc_number, ext->rs_left,
779 ext->rs_failed, count);
782 lc_put(mdev->resync, &ext->lce);
783 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
787 /* Normally this element should be in the cache,
788 * since drbd_rs_begin_io() pulled it already in.
790 * But maybe an application write finished, and we set
791 * something outside the resync lru_cache in sync.
793 int rs_left = drbd_bm_e_weight(mdev, enr);
794 if (ext->flags != 0) {
795 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
797 ext->lce.lc_number, ext->rs_left,
798 ext->flags, enr, rs_left);
801 if (ext->rs_failed) {
802 dev_warn(DEV, "Kicking resync_lru element enr=%u "
803 "out with rs_failed=%d\n",
804 ext->lce.lc_number, ext->rs_failed);
806 ext->rs_left = rs_left;
807 ext->rs_failed = success ? 0 : count;
808 /* we don't keep a persistent log of the resync lru,
809 * we can commit any change right away. */
810 lc_committed(mdev->resync);
812 lc_put(mdev->resync, &ext->lce);
813 /* no race, we are within the al_lock! */
815 if (ext->rs_left == ext->rs_failed) {
818 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
820 udw->enr = ext->lce.lc_number;
821 udw->w.cb = w_update_odbm;
823 drbd_queue_work_front(&mdev->tconn->data.work, &udw->w);
825 dev_warn(DEV, "Could not kmalloc an udw\n");
829 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
831 mdev->resync->nr_elements,
832 mdev->resync->flags);
836 void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
838 unsigned long now = jiffies;
839 unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
840 int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
841 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
842 if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
843 mdev->state.conn != C_PAUSED_SYNC_T &&
844 mdev->state.conn != C_PAUSED_SYNC_S) {
845 mdev->rs_mark_time[next] = now;
846 mdev->rs_mark_left[next] = still_to_go;
847 mdev->rs_last_mark = next;
852 /* clear the bit corresponding to the piece of storage in question:
853 * size byte of data starting from sector. Only clear a bits of the affected
854 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
856 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
859 void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
860 const char *file, const unsigned int line)
862 /* Is called from worker and receiver context _only_ */
863 unsigned long sbnr, ebnr, lbnr;
864 unsigned long count = 0;
865 sector_t esector, nr_sectors;
869 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
870 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
871 (unsigned long long)sector, size);
874 nr_sectors = drbd_get_capacity(mdev->this_bdev);
875 esector = sector + (size >> 9) - 1;
877 if (!expect(sector < nr_sectors))
879 if (!expect(esector < nr_sectors))
880 esector = nr_sectors - 1;
882 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
884 /* we clear it (in sync).
885 * round up start sector, round down end sector. we make sure we only
886 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
887 if (unlikely(esector < BM_SECT_PER_BIT-1))
889 if (unlikely(esector == (nr_sectors-1)))
892 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
893 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
899 * ok, (capacity & 7) != 0 sometimes, but who cares...
900 * we count rs_{total,left} in bits, not sectors.
902 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
903 if (count && get_ldev(mdev)) {
904 drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
905 spin_lock_irqsave(&mdev->al_lock, flags);
906 drbd_try_clear_on_disk_bm(mdev, sector, count, true);
907 spin_unlock_irqrestore(&mdev->al_lock, flags);
909 /* just wake_up unconditional now, various lc_chaged(),
910 * lc_put() in drbd_try_clear_on_disk_bm(). */
915 wake_up(&mdev->al_wait);
919 * this is intended to set one request worth of data out of sync.
920 * affects at least 1 bit,
921 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
923 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
924 * so this can be _any_ process.
926 int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
927 const char *file, const unsigned int line)
929 unsigned long sbnr, ebnr, lbnr, flags;
930 sector_t esector, nr_sectors;
931 unsigned int enr, count = 0;
932 struct lc_element *e;
934 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
935 dev_err(DEV, "sector: %llus, size: %d\n",
936 (unsigned long long)sector, size);
941 return 0; /* no disk, no metadata, no bitmap to set bits in */
943 nr_sectors = drbd_get_capacity(mdev->this_bdev);
944 esector = sector + (size >> 9) - 1;
946 if (!expect(sector < nr_sectors))
948 if (!expect(esector < nr_sectors))
949 esector = nr_sectors - 1;
951 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
953 /* we set it out of sync,
954 * we do not need to round anything here */
955 sbnr = BM_SECT_TO_BIT(sector);
956 ebnr = BM_SECT_TO_BIT(esector);
958 /* ok, (capacity & 7) != 0 sometimes, but who cares...
959 * we count rs_{total,left} in bits, not sectors. */
960 spin_lock_irqsave(&mdev->al_lock, flags);
961 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
963 enr = BM_SECT_TO_EXT(sector);
964 e = lc_find(mdev->resync, enr);
966 lc_entry(e, struct bm_extent, lce)->rs_left += count;
967 spin_unlock_irqrestore(&mdev->al_lock, flags);
976 struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
978 struct lc_element *e;
979 struct bm_extent *bm_ext;
981 unsigned long rs_flags;
983 spin_lock_irq(&mdev->al_lock);
984 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
985 spin_unlock_irq(&mdev->al_lock);
988 e = lc_get(mdev->resync, enr);
989 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
991 if (bm_ext->lce.lc_number != enr) {
992 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
993 bm_ext->rs_failed = 0;
994 lc_committed(mdev->resync);
997 if (bm_ext->lce.refcnt == 1)
998 mdev->resync_locked++;
999 set_bit(BME_NO_WRITES, &bm_ext->flags);
1001 rs_flags = mdev->resync->flags;
1002 spin_unlock_irq(&mdev->al_lock);
1004 wake_up(&mdev->al_wait);
1007 if (rs_flags & LC_STARVING)
1008 dev_warn(DEV, "Have to wait for element"
1009 " (resync LRU too small?)\n");
1010 BUG_ON(rs_flags & LC_LOCKED);
1016 static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1020 spin_lock_irq(&mdev->al_lock);
1021 rv = lc_is_used(mdev->act_log, enr);
1022 spin_unlock_irq(&mdev->al_lock);
1028 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1029 * @mdev: DRBD device.
1030 * @sector: The sector number.
1032 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
1034 int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1036 unsigned int enr = BM_SECT_TO_EXT(sector);
1037 struct bm_extent *bm_ext;
1039 int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
1040 200 times -> 20 seconds. */
1043 sig = wait_event_interruptible(mdev->al_wait,
1044 (bm_ext = _bme_get(mdev, enr)));
1048 if (test_bit(BME_LOCKED, &bm_ext->flags))
1051 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1052 sig = wait_event_interruptible(mdev->al_wait,
1053 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
1054 test_bit(BME_PRIORITY, &bm_ext->flags));
1056 if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
1057 spin_lock_irq(&mdev->al_lock);
1058 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1059 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
1060 mdev->resync_locked--;
1061 wake_up(&mdev->al_wait);
1063 spin_unlock_irq(&mdev->al_lock);
1066 if (schedule_timeout_interruptible(HZ/10))
1068 if (sa && --sa == 0)
1069 dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
1070 "Resync stalled?\n");
1074 set_bit(BME_LOCKED, &bm_ext->flags);
1079 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1080 * @mdev: DRBD device.
1081 * @sector: The sector number.
1083 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1084 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1085 * if there is still application IO going on in this area.
1087 int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1089 unsigned int enr = BM_SECT_TO_EXT(sector);
1090 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1091 struct lc_element *e;
1092 struct bm_extent *bm_ext;
1095 spin_lock_irq(&mdev->al_lock);
1096 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1097 /* in case you have very heavy scattered io, it may
1098 * stall the syncer undefined if we give up the ref count
1099 * when we try again and requeue.
1101 * if we don't give up the refcount, but the next time
1102 * we are scheduled this extent has been "synced" by new
1103 * application writes, we'd miss the lc_put on the
1104 * extent we keep the refcount on.
1105 * so we remembered which extent we had to try again, and
1106 * if the next requested one is something else, we do
1107 * the lc_put here...
1108 * we also have to wake_up
1110 e = lc_find(mdev->resync, mdev->resync_wenr);
1111 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1113 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1114 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1115 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1116 mdev->resync_wenr = LC_FREE;
1117 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1118 mdev->resync_locked--;
1119 wake_up(&mdev->al_wait);
1121 dev_alert(DEV, "LOGIC BUG\n");
1125 e = lc_try_get(mdev->resync, enr);
1126 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1128 if (test_bit(BME_LOCKED, &bm_ext->flags))
1130 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1131 mdev->resync_locked++;
1133 /* we did set the BME_NO_WRITES,
1134 * but then could not set BME_LOCKED,
1135 * so we tried again.
1136 * drop the extra reference. */
1137 bm_ext->lce.refcnt--;
1138 D_ASSERT(bm_ext->lce.refcnt > 0);
1142 /* do we rather want to try later? */
1143 if (mdev->resync_locked > mdev->resync->nr_elements-3)
1145 /* Do or do not. There is no try. -- Yoda */
1146 e = lc_get(mdev->resync, enr);
1147 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1149 const unsigned long rs_flags = mdev->resync->flags;
1150 if (rs_flags & LC_STARVING)
1151 dev_warn(DEV, "Have to wait for element"
1152 " (resync LRU too small?)\n");
1153 BUG_ON(rs_flags & LC_LOCKED);
1156 if (bm_ext->lce.lc_number != enr) {
1157 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1158 bm_ext->rs_failed = 0;
1159 lc_committed(mdev->resync);
1160 wake_up(&mdev->al_wait);
1161 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1163 set_bit(BME_NO_WRITES, &bm_ext->flags);
1164 D_ASSERT(bm_ext->lce.refcnt == 1);
1165 mdev->resync_locked++;
1169 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1170 if (lc_is_used(mdev->act_log, al_enr+i))
1173 set_bit(BME_LOCKED, &bm_ext->flags);
1175 mdev->resync_wenr = LC_FREE;
1176 spin_unlock_irq(&mdev->al_lock);
1181 mdev->resync_wenr = enr;
1182 spin_unlock_irq(&mdev->al_lock);
1186 void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1188 unsigned int enr = BM_SECT_TO_EXT(sector);
1189 struct lc_element *e;
1190 struct bm_extent *bm_ext;
1191 unsigned long flags;
1193 spin_lock_irqsave(&mdev->al_lock, flags);
1194 e = lc_find(mdev->resync, enr);
1195 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1197 spin_unlock_irqrestore(&mdev->al_lock, flags);
1198 if (__ratelimit(&drbd_ratelimit_state))
1199 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1203 if (bm_ext->lce.refcnt == 0) {
1204 spin_unlock_irqrestore(&mdev->al_lock, flags);
1205 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1206 "but refcnt is 0!?\n",
1207 (unsigned long long)sector, enr);
1211 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1212 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
1213 mdev->resync_locked--;
1214 wake_up(&mdev->al_wait);
1217 spin_unlock_irqrestore(&mdev->al_lock, flags);
1221 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1222 * @mdev: DRBD device.
1224 void drbd_rs_cancel_all(struct drbd_conf *mdev)
1226 spin_lock_irq(&mdev->al_lock);
1228 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1229 lc_reset(mdev->resync);
1232 mdev->resync_locked = 0;
1233 mdev->resync_wenr = LC_FREE;
1234 spin_unlock_irq(&mdev->al_lock);
1235 wake_up(&mdev->al_wait);
1239 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1240 * @mdev: DRBD device.
1242 * Returns 0 upon success, -EAGAIN if at least one reference count was
1245 int drbd_rs_del_all(struct drbd_conf *mdev)
1247 struct lc_element *e;
1248 struct bm_extent *bm_ext;
1251 spin_lock_irq(&mdev->al_lock);
1253 if (get_ldev_if_state(mdev, D_FAILED)) {
1254 /* ok, ->resync is there. */
1255 for (i = 0; i < mdev->resync->nr_elements; i++) {
1256 e = lc_element_by_index(mdev->resync, i);
1257 bm_ext = lc_entry(e, struct bm_extent, lce);
1258 if (bm_ext->lce.lc_number == LC_FREE)
1260 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1261 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1262 " got 'synced' by application io\n",
1264 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1265 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1266 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1267 mdev->resync_wenr = LC_FREE;
1268 lc_put(mdev->resync, &bm_ext->lce);
1270 if (bm_ext->lce.refcnt != 0) {
1271 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1272 "refcnt=%d\n", bm_ext->lce.refcnt);
1274 spin_unlock_irq(&mdev->al_lock);
1277 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1278 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1279 lc_del(mdev->resync, &bm_ext->lce);
1281 D_ASSERT(mdev->resync->used == 0);
1284 spin_unlock_irq(&mdev->al_lock);
1290 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1291 * @mdev: DRBD device.
1292 * @sector: The sector number.
1293 * @size: Size of failed IO operation, in byte.
1295 void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1297 /* Is called from worker and receiver context _only_ */
1298 unsigned long sbnr, ebnr, lbnr;
1299 unsigned long count;
1300 sector_t esector, nr_sectors;
1303 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
1304 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1305 (unsigned long long)sector, size);
1308 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1309 esector = sector + (size >> 9) - 1;
1311 if (!expect(sector < nr_sectors))
1313 if (!expect(esector < nr_sectors))
1314 esector = nr_sectors - 1;
1316 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1319 * round up start sector, round down end sector. we make sure we only
1320 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1321 if (unlikely(esector < BM_SECT_PER_BIT-1))
1323 if (unlikely(esector == (nr_sectors-1)))
1326 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1327 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1333 * ok, (capacity & 7) != 0 sometimes, but who cares...
1334 * we count rs_{total,left} in bits, not sectors.
1336 spin_lock_irq(&mdev->al_lock);
1337 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1339 mdev->rs_failed += count;
1341 if (get_ldev(mdev)) {
1342 drbd_try_clear_on_disk_bm(mdev, sector, count, false);
1346 /* just wake_up unconditional now, various lc_chaged(),
1347 * lc_put() in drbd_try_clear_on_disk_bm(). */
1350 spin_unlock_irq(&mdev->al_lock);
1352 wake_up(&mdev->al_wait);