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, struct drbd_interval *i)
210 /* for bios crossing activity log extent boundaries,
211 * we may need to activate two extents in one go */
212 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
213 unsigned last = (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
218 D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
220 for (enr = first; enr <= last; enr++)
221 wait_event(mdev->al_wait, _al_get(mdev, enr) != NULL);
223 /* Serialize multiple transactions.
224 * This uses test_and_set_bit, memory barrier is implicit.
226 wait_event(mdev->al_wait,
227 mdev->act_log->pending_changes == 0 ||
228 (locked = lc_try_lock_for_transaction(mdev->act_log)));
231 /* drbd_al_write_transaction(mdev,al_ext,enr);
232 * recurses into generic_make_request(), which
233 * disallows recursion, bios being serialized on the
234 * current->bio_tail list now.
235 * we have to delegate updates to the activity log
236 * to the worker thread. */
238 /* Double check: it may have been committed by someone else,
239 * while we have been waiting for the lock. */
240 if (mdev->act_log->pending_changes) {
241 struct update_al_work al_work;
242 init_completion(&al_work.event);
243 al_work.w.cb = w_al_write_transaction;
244 al_work.w.mdev = mdev;
245 drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w);
246 wait_for_completion(&al_work.event);
250 spin_lock_irq(&mdev->al_lock);
253 we need an "lc_cancel" here;
255 lc_committed(mdev->act_log);
256 spin_unlock_irq(&mdev->al_lock);
258 lc_unlock(mdev->act_log);
259 wake_up(&mdev->al_wait);
263 void drbd_al_complete_io(struct drbd_conf *mdev, struct drbd_interval *i)
265 /* for bios crossing activity log extent boundaries,
266 * we may need to activate two extents in one go */
267 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
268 unsigned last = (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
270 struct lc_element *extent;
274 spin_lock_irqsave(&mdev->al_lock, flags);
276 for (enr = first; enr <= last; enr++) {
277 extent = lc_find(mdev->act_log, enr);
279 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
282 if (lc_put(mdev->act_log, extent) == 0)
285 spin_unlock_irqrestore(&mdev->al_lock, flags);
286 wake_up(&mdev->al_wait);
289 #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
290 /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
291 * are still coupled, or assume too much about their relation.
292 * Code below will not work if this is violated.
293 * Will be cleaned up with some followup patch.
298 static unsigned int al_extent_to_bm_page(unsigned int al_enr)
303 /* al extent number to bit */
304 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
307 static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
312 /* resync extent number to bit */
313 (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
317 w_al_write_transaction(struct drbd_work *w, int unused)
319 struct update_al_work *aw = container_of(w, struct update_al_work, w);
320 struct drbd_conf *mdev = w->mdev;
321 struct al_transaction_on_disk *buffer;
322 struct lc_element *e;
328 if (!get_ldev(mdev)) {
329 dev_err(DEV, "disk is %s, cannot start al transaction\n",
330 drbd_disk_str(mdev->state.disk));
332 complete(&((struct update_al_work *)w)->event);
336 /* The bitmap write may have failed, causing a state change. */
337 if (mdev->state.disk < D_INCONSISTENT) {
339 "disk is %s, cannot write al transaction\n",
340 drbd_disk_str(mdev->state.disk));
342 complete(&((struct update_al_work *)w)->event);
347 mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
348 buffer = page_address(mdev->md_io_page);
350 memset(buffer, 0, sizeof(*buffer));
351 buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
352 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
356 /* Even though no one can start to change this list
357 * once we set the LC_LOCKED -- from drbd_al_begin_io(),
358 * lc_try_lock_for_transaction() --, someone may still
359 * be in the process of changing it. */
360 spin_lock_irq(&mdev->al_lock);
361 list_for_each_entry(e, &mdev->act_log->to_be_changed, list) {
362 if (i == AL_UPDATES_PER_TRANSACTION) {
366 buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
367 buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
368 if (e->lc_number != LC_FREE)
369 drbd_bm_mark_for_writeout(mdev,
370 al_extent_to_bm_page(e->lc_number));
373 spin_unlock_irq(&mdev->al_lock);
374 BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
376 buffer->n_updates = cpu_to_be16(i);
377 for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
378 buffer->update_slot_nr[i] = cpu_to_be16(-1);
379 buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
382 buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements);
383 buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle);
385 mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
386 mdev->act_log->nr_elements - mdev->al_tr_cycle);
387 for (i = 0; i < mx; i++) {
388 unsigned idx = mdev->al_tr_cycle + i;
389 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
390 buffer->context[i] = cpu_to_be32(extent_nr);
392 for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
393 buffer->context[i] = cpu_to_be32(LC_FREE);
395 mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
396 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
397 mdev->al_tr_cycle = 0;
399 sector = mdev->ldev->md.md_offset
400 + mdev->ldev->md.al_offset
401 + mdev->al_tr_pos * (MD_BLOCK_SIZE>>9);
403 crc = crc32c(0, buffer, 4096);
404 buffer->crc32c = cpu_to_be32(crc);
406 if (drbd_bm_write_hinted(mdev))
408 /* drbd_chk_io_error done already */
409 else if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
411 drbd_chk_io_error(mdev, 1, true);
413 /* advance ringbuffer position and transaction counter */
414 mdev->al_tr_pos = (mdev->al_tr_pos + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
415 mdev->al_tr_number++;
418 mutex_unlock(&mdev->md_io_mutex);
419 complete(&((struct update_al_work *)w)->event);
426 * reading of the activity log,
427 * and potentially dirtying of the affected bitmap regions,
428 * should be done from userland only.
429 * DRBD would simply always attach with an empty activity log,
430 * and refuse to attach to something that looks like a crashed primary.
434 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
435 * @mdev: DRBD device.
436 * @bdev: Block device to read form.
437 * @b: pointer to an al_transaction.
438 * @index: On disk slot of the transaction to read.
440 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
442 static int drbd_al_read_tr(struct drbd_conf *mdev,
443 struct drbd_backing_dev *bdev,
446 struct al_transaction_on_disk *b = page_address(mdev->md_io_page);
450 sector = bdev->md.md_offset
452 + index * (MD_BLOCK_SIZE>>9);
454 /* Dont process error normally,
455 * as this is done before disk is attached! */
456 if (drbd_md_sync_page_io(mdev, bdev, sector, READ))
459 if (!expect(b->magic == cpu_to_be32(DRBD_AL_MAGIC)))
462 if (!expect(be16_to_cpu(b->n_updates) <= AL_UPDATES_PER_TRANSACTION))
465 if (!expect(be16_to_cpu(b->context_size) <= DRBD_AL_EXTENTS_MAX))
468 if (!expect(be16_to_cpu(b->context_start_slot_nr) < DRBD_AL_EXTENTS_MAX))
471 crc = be32_to_cpu(b->crc32c);
473 if (!expect(crc == crc32c(0, b, 4096)))
480 * drbd_al_read_log() - Restores the activity log from its on disk representation.
481 * @mdev: DRBD device.
482 * @bdev: Block device to read form.
484 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
486 int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
488 struct al_transaction_on_disk *b;
492 int active_extents = 0;
493 int transactions = 0;
495 int found_initialized = 0;
502 /* Note that this is expected to be called with a newly created,
503 * clean and all unused activity log of the "expected size".
506 /* lock out all other meta data io for now,
507 * and make sure the page is mapped.
509 mutex_lock(&mdev->md_io_mutex);
510 b = page_address(mdev->md_io_page);
512 /* Always use the full ringbuffer space for now.
513 * possible optimization: read in all of it,
514 * then scan the in-memory pages. */
516 mx = (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
518 /* Find the valid transaction in the log */
519 for (i = 0; i < mx; i++) {
520 rv = drbd_al_read_tr(mdev, bdev, i);
521 /* invalid data in that block */
524 if (be16_to_cpu(b->transaction_type) == AL_TR_INITIALIZED) {
531 mutex_unlock(&mdev->md_io_mutex);
535 cnr = be32_to_cpu(b->tr_number);
536 if (++found_valid == 1) {
544 D_ASSERT(cnr != to_tnr);
545 D_ASSERT(cnr != from_tnr);
546 if ((int)cnr - (int)from_tnr < 0) {
547 D_ASSERT(from_tnr - cnr + i - from == mx);
551 if ((int)cnr - (int)to_tnr > 0) {
552 D_ASSERT(cnr - to_tnr == i - to);
559 if (found_initialized != mx)
560 dev_warn(DEV, "No usable activity log found.\n");
561 mutex_unlock(&mdev->md_io_mutex);
565 /* Read the valid transactions.
566 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
569 struct lc_element *e;
570 unsigned j, n, slot, extent_nr;
572 rv = drbd_al_read_tr(mdev, bdev, i);
573 if (!expect(rv != 0))
576 mutex_unlock(&mdev->md_io_mutex);
580 /* deal with different transaction types.
581 * not yet implemented */
582 if (!expect(b->transaction_type == 0))
585 /* on the fly re-create/resize activity log?
586 * will be a special transaction type flag. */
587 if (!expect(be16_to_cpu(b->context_size) == mdev->act_log->nr_elements))
589 if (!expect(be16_to_cpu(b->context_start_slot_nr) < mdev->act_log->nr_elements))
592 /* We are the only user of the activity log right now,
593 * don't actually need to take that lock. */
594 spin_lock_irq(&mdev->al_lock);
596 /* first, apply the context, ... */
597 for (j = 0, slot = be16_to_cpu(b->context_start_slot_nr);
598 j < AL_CONTEXT_PER_TRANSACTION &&
599 slot < mdev->act_log->nr_elements; j++, slot++) {
600 extent_nr = be32_to_cpu(b->context[j]);
601 e = lc_element_by_index(mdev->act_log, slot);
602 if (e->lc_number != extent_nr) {
603 if (extent_nr != LC_FREE)
608 lc_set(mdev->act_log, extent_nr, slot);
611 /* ... then apply the updates,
612 * which override the context information.
613 * drbd_al_read_tr already did the rangecheck
614 * on n <= AL_UPDATES_PER_TRANSACTION */
615 n = be16_to_cpu(b->n_updates);
616 for (j = 0; j < n; j++) {
617 slot = be16_to_cpu(b->update_slot_nr[j]);
618 extent_nr = be32_to_cpu(b->update_extent_nr[j]);
619 if (!expect(slot < mdev->act_log->nr_elements))
621 e = lc_element_by_index(mdev->act_log, slot);
622 if (e->lc_number != extent_nr) {
623 if (extent_nr != LC_FREE)
628 lc_set(mdev->act_log, extent_nr, slot);
630 spin_unlock_irq(&mdev->al_lock);
642 mdev->al_tr_number = to_tnr+1;
643 mdev->al_tr_pos = (to + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
645 /* ok, we are done with it */
646 mutex_unlock(&mdev->md_io_mutex);
648 dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
649 transactions, active_extents);
655 * drbd_al_apply_to_bm() - Sets the bitmap to dirty(1) where covered by active AL extents
656 * @mdev: DRBD device.
658 void drbd_al_apply_to_bm(struct drbd_conf *mdev)
661 unsigned long add = 0;
665 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
667 for (i = 0; i < mdev->act_log->nr_elements; i++) {
668 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
671 tmp = drbd_bm_ALe_set_all(mdev, enr);
672 dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
676 lc_unlock(mdev->act_log);
677 wake_up(&mdev->al_wait);
679 dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
680 ppsize(ppb, Bit2KB(add)));
683 static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
687 spin_lock_irq(&mdev->al_lock);
688 rv = (al_ext->refcnt == 0);
690 lc_del(mdev->act_log, al_ext);
691 spin_unlock_irq(&mdev->al_lock);
697 * drbd_al_shrink() - Removes all active extents form the activity log
698 * @mdev: DRBD device.
700 * Removes all active extents form the activity log, waiting until
701 * the reference count of each entry dropped to 0 first, of course.
703 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
705 void drbd_al_shrink(struct drbd_conf *mdev)
707 struct lc_element *al_ext;
710 D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags));
712 for (i = 0; i < mdev->act_log->nr_elements; i++) {
713 al_ext = lc_element_by_index(mdev->act_log, i);
714 if (al_ext->lc_number == LC_FREE)
716 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
719 wake_up(&mdev->al_wait);
722 static int w_update_odbm(struct drbd_work *w, int unused)
724 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
725 struct drbd_conf *mdev = w->mdev;
726 struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
728 if (!get_ldev(mdev)) {
729 if (__ratelimit(&drbd_ratelimit_state))
730 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
735 drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
740 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
741 switch (mdev->state.conn) {
742 case C_SYNC_SOURCE: case C_SYNC_TARGET:
743 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
744 drbd_resync_finished(mdev);
750 drbd_bcast_event(mdev, &sib);
756 /* ATTENTION. The AL's extents are 4MB each, while the extents in the
757 * resync LRU-cache are 16MB each.
758 * The caller of this function has to hold an get_ldev() reference.
760 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
762 static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
763 int count, int success)
765 struct lc_element *e;
766 struct update_odbm_work *udw;
770 D_ASSERT(atomic_read(&mdev->local_cnt));
772 /* I simply assume that a sector/size pair never crosses
773 * a 16 MB extent border. (Currently this is true...) */
774 enr = BM_SECT_TO_EXT(sector);
776 e = lc_get(mdev->resync, enr);
778 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
779 if (ext->lce.lc_number == enr) {
781 ext->rs_left -= count;
783 ext->rs_failed += count;
784 if (ext->rs_left < ext->rs_failed) {
785 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
786 "rs_failed=%d count=%d\n",
787 (unsigned long long)sector,
788 ext->lce.lc_number, ext->rs_left,
789 ext->rs_failed, count);
792 lc_put(mdev->resync, &ext->lce);
793 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
797 /* Normally this element should be in the cache,
798 * since drbd_rs_begin_io() pulled it already in.
800 * But maybe an application write finished, and we set
801 * something outside the resync lru_cache in sync.
803 int rs_left = drbd_bm_e_weight(mdev, enr);
804 if (ext->flags != 0) {
805 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
807 ext->lce.lc_number, ext->rs_left,
808 ext->flags, enr, rs_left);
811 if (ext->rs_failed) {
812 dev_warn(DEV, "Kicking resync_lru element enr=%u "
813 "out with rs_failed=%d\n",
814 ext->lce.lc_number, ext->rs_failed);
816 ext->rs_left = rs_left;
817 ext->rs_failed = success ? 0 : count;
818 /* we don't keep a persistent log of the resync lru,
819 * we can commit any change right away. */
820 lc_committed(mdev->resync);
822 lc_put(mdev->resync, &ext->lce);
823 /* no race, we are within the al_lock! */
825 if (ext->rs_left == ext->rs_failed) {
828 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
830 udw->enr = ext->lce.lc_number;
831 udw->w.cb = w_update_odbm;
833 drbd_queue_work_front(&mdev->tconn->data.work, &udw->w);
835 dev_warn(DEV, "Could not kmalloc an udw\n");
839 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
841 mdev->resync->nr_elements,
842 mdev->resync->flags);
846 void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
848 unsigned long now = jiffies;
849 unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
850 int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
851 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
852 if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
853 mdev->state.conn != C_PAUSED_SYNC_T &&
854 mdev->state.conn != C_PAUSED_SYNC_S) {
855 mdev->rs_mark_time[next] = now;
856 mdev->rs_mark_left[next] = still_to_go;
857 mdev->rs_last_mark = next;
862 /* clear the bit corresponding to the piece of storage in question:
863 * size byte of data starting from sector. Only clear a bits of the affected
864 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
866 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
869 void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
870 const char *file, const unsigned int line)
872 /* Is called from worker and receiver context _only_ */
873 unsigned long sbnr, ebnr, lbnr;
874 unsigned long count = 0;
875 sector_t esector, nr_sectors;
879 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
880 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
881 (unsigned long long)sector, size);
884 nr_sectors = drbd_get_capacity(mdev->this_bdev);
885 esector = sector + (size >> 9) - 1;
887 if (!expect(sector < nr_sectors))
889 if (!expect(esector < nr_sectors))
890 esector = nr_sectors - 1;
892 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
894 /* we clear it (in sync).
895 * round up start sector, round down end sector. we make sure we only
896 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
897 if (unlikely(esector < BM_SECT_PER_BIT-1))
899 if (unlikely(esector == (nr_sectors-1)))
902 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
903 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
909 * ok, (capacity & 7) != 0 sometimes, but who cares...
910 * we count rs_{total,left} in bits, not sectors.
912 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
913 if (count && get_ldev(mdev)) {
914 drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
915 spin_lock_irqsave(&mdev->al_lock, flags);
916 drbd_try_clear_on_disk_bm(mdev, sector, count, true);
917 spin_unlock_irqrestore(&mdev->al_lock, flags);
919 /* just wake_up unconditional now, various lc_chaged(),
920 * lc_put() in drbd_try_clear_on_disk_bm(). */
925 wake_up(&mdev->al_wait);
929 * this is intended to set one request worth of data out of sync.
930 * affects at least 1 bit,
931 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
933 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
934 * so this can be _any_ process.
936 int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
937 const char *file, const unsigned int line)
939 unsigned long sbnr, ebnr, lbnr, flags;
940 sector_t esector, nr_sectors;
941 unsigned int enr, count = 0;
942 struct lc_element *e;
944 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
945 dev_err(DEV, "sector: %llus, size: %d\n",
946 (unsigned long long)sector, size);
951 return 0; /* no disk, no metadata, no bitmap to set bits in */
953 nr_sectors = drbd_get_capacity(mdev->this_bdev);
954 esector = sector + (size >> 9) - 1;
956 if (!expect(sector < nr_sectors))
958 if (!expect(esector < nr_sectors))
959 esector = nr_sectors - 1;
961 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
963 /* we set it out of sync,
964 * we do not need to round anything here */
965 sbnr = BM_SECT_TO_BIT(sector);
966 ebnr = BM_SECT_TO_BIT(esector);
968 /* ok, (capacity & 7) != 0 sometimes, but who cares...
969 * we count rs_{total,left} in bits, not sectors. */
970 spin_lock_irqsave(&mdev->al_lock, flags);
971 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
973 enr = BM_SECT_TO_EXT(sector);
974 e = lc_find(mdev->resync, enr);
976 lc_entry(e, struct bm_extent, lce)->rs_left += count;
977 spin_unlock_irqrestore(&mdev->al_lock, flags);
986 struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
988 struct lc_element *e;
989 struct bm_extent *bm_ext;
991 unsigned long rs_flags;
993 spin_lock_irq(&mdev->al_lock);
994 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
995 spin_unlock_irq(&mdev->al_lock);
998 e = lc_get(mdev->resync, enr);
999 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1001 if (bm_ext->lce.lc_number != enr) {
1002 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1003 bm_ext->rs_failed = 0;
1004 lc_committed(mdev->resync);
1007 if (bm_ext->lce.refcnt == 1)
1008 mdev->resync_locked++;
1009 set_bit(BME_NO_WRITES, &bm_ext->flags);
1011 rs_flags = mdev->resync->flags;
1012 spin_unlock_irq(&mdev->al_lock);
1014 wake_up(&mdev->al_wait);
1017 if (rs_flags & LC_STARVING)
1018 dev_warn(DEV, "Have to wait for element"
1019 " (resync LRU too small?)\n");
1020 BUG_ON(rs_flags & LC_LOCKED);
1026 static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1030 spin_lock_irq(&mdev->al_lock);
1031 rv = lc_is_used(mdev->act_log, enr);
1032 spin_unlock_irq(&mdev->al_lock);
1038 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1039 * @mdev: DRBD device.
1040 * @sector: The sector number.
1042 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
1044 int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1046 unsigned int enr = BM_SECT_TO_EXT(sector);
1047 struct bm_extent *bm_ext;
1049 int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
1050 200 times -> 20 seconds. */
1053 sig = wait_event_interruptible(mdev->al_wait,
1054 (bm_ext = _bme_get(mdev, enr)));
1058 if (test_bit(BME_LOCKED, &bm_ext->flags))
1061 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1062 sig = wait_event_interruptible(mdev->al_wait,
1063 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
1064 test_bit(BME_PRIORITY, &bm_ext->flags));
1066 if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
1067 spin_lock_irq(&mdev->al_lock);
1068 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1069 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
1070 mdev->resync_locked--;
1071 wake_up(&mdev->al_wait);
1073 spin_unlock_irq(&mdev->al_lock);
1076 if (schedule_timeout_interruptible(HZ/10))
1078 if (sa && --sa == 0)
1079 dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
1080 "Resync stalled?\n");
1084 set_bit(BME_LOCKED, &bm_ext->flags);
1089 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1090 * @mdev: DRBD device.
1091 * @sector: The sector number.
1093 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1094 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1095 * if there is still application IO going on in this area.
1097 int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1099 unsigned int enr = BM_SECT_TO_EXT(sector);
1100 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1101 struct lc_element *e;
1102 struct bm_extent *bm_ext;
1105 spin_lock_irq(&mdev->al_lock);
1106 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1107 /* in case you have very heavy scattered io, it may
1108 * stall the syncer undefined if we give up the ref count
1109 * when we try again and requeue.
1111 * if we don't give up the refcount, but the next time
1112 * we are scheduled this extent has been "synced" by new
1113 * application writes, we'd miss the lc_put on the
1114 * extent we keep the refcount on.
1115 * so we remembered which extent we had to try again, and
1116 * if the next requested one is something else, we do
1117 * the lc_put here...
1118 * we also have to wake_up
1120 e = lc_find(mdev->resync, mdev->resync_wenr);
1121 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1123 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1124 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1125 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1126 mdev->resync_wenr = LC_FREE;
1127 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1128 mdev->resync_locked--;
1129 wake_up(&mdev->al_wait);
1131 dev_alert(DEV, "LOGIC BUG\n");
1135 e = lc_try_get(mdev->resync, enr);
1136 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1138 if (test_bit(BME_LOCKED, &bm_ext->flags))
1140 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1141 mdev->resync_locked++;
1143 /* we did set the BME_NO_WRITES,
1144 * but then could not set BME_LOCKED,
1145 * so we tried again.
1146 * drop the extra reference. */
1147 bm_ext->lce.refcnt--;
1148 D_ASSERT(bm_ext->lce.refcnt > 0);
1152 /* do we rather want to try later? */
1153 if (mdev->resync_locked > mdev->resync->nr_elements-3)
1155 /* Do or do not. There is no try. -- Yoda */
1156 e = lc_get(mdev->resync, enr);
1157 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1159 const unsigned long rs_flags = mdev->resync->flags;
1160 if (rs_flags & LC_STARVING)
1161 dev_warn(DEV, "Have to wait for element"
1162 " (resync LRU too small?)\n");
1163 BUG_ON(rs_flags & LC_LOCKED);
1166 if (bm_ext->lce.lc_number != enr) {
1167 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1168 bm_ext->rs_failed = 0;
1169 lc_committed(mdev->resync);
1170 wake_up(&mdev->al_wait);
1171 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1173 set_bit(BME_NO_WRITES, &bm_ext->flags);
1174 D_ASSERT(bm_ext->lce.refcnt == 1);
1175 mdev->resync_locked++;
1179 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1180 if (lc_is_used(mdev->act_log, al_enr+i))
1183 set_bit(BME_LOCKED, &bm_ext->flags);
1185 mdev->resync_wenr = LC_FREE;
1186 spin_unlock_irq(&mdev->al_lock);
1191 mdev->resync_wenr = enr;
1192 spin_unlock_irq(&mdev->al_lock);
1196 void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1198 unsigned int enr = BM_SECT_TO_EXT(sector);
1199 struct lc_element *e;
1200 struct bm_extent *bm_ext;
1201 unsigned long flags;
1203 spin_lock_irqsave(&mdev->al_lock, flags);
1204 e = lc_find(mdev->resync, enr);
1205 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1207 spin_unlock_irqrestore(&mdev->al_lock, flags);
1208 if (__ratelimit(&drbd_ratelimit_state))
1209 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1213 if (bm_ext->lce.refcnt == 0) {
1214 spin_unlock_irqrestore(&mdev->al_lock, flags);
1215 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1216 "but refcnt is 0!?\n",
1217 (unsigned long long)sector, enr);
1221 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1222 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
1223 mdev->resync_locked--;
1224 wake_up(&mdev->al_wait);
1227 spin_unlock_irqrestore(&mdev->al_lock, flags);
1231 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1232 * @mdev: DRBD device.
1234 void drbd_rs_cancel_all(struct drbd_conf *mdev)
1236 spin_lock_irq(&mdev->al_lock);
1238 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1239 lc_reset(mdev->resync);
1242 mdev->resync_locked = 0;
1243 mdev->resync_wenr = LC_FREE;
1244 spin_unlock_irq(&mdev->al_lock);
1245 wake_up(&mdev->al_wait);
1249 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1250 * @mdev: DRBD device.
1252 * Returns 0 upon success, -EAGAIN if at least one reference count was
1255 int drbd_rs_del_all(struct drbd_conf *mdev)
1257 struct lc_element *e;
1258 struct bm_extent *bm_ext;
1261 spin_lock_irq(&mdev->al_lock);
1263 if (get_ldev_if_state(mdev, D_FAILED)) {
1264 /* ok, ->resync is there. */
1265 for (i = 0; i < mdev->resync->nr_elements; i++) {
1266 e = lc_element_by_index(mdev->resync, i);
1267 bm_ext = lc_entry(e, struct bm_extent, lce);
1268 if (bm_ext->lce.lc_number == LC_FREE)
1270 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1271 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1272 " got 'synced' by application io\n",
1274 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1275 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1276 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1277 mdev->resync_wenr = LC_FREE;
1278 lc_put(mdev->resync, &bm_ext->lce);
1280 if (bm_ext->lce.refcnt != 0) {
1281 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1282 "refcnt=%d\n", bm_ext->lce.refcnt);
1284 spin_unlock_irq(&mdev->al_lock);
1287 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1288 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1289 lc_del(mdev->resync, &bm_ext->lce);
1291 D_ASSERT(mdev->resync->used == 0);
1294 spin_unlock_irq(&mdev->al_lock);
1300 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1301 * @mdev: DRBD device.
1302 * @sector: The sector number.
1303 * @size: Size of failed IO operation, in byte.
1305 void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1307 /* Is called from worker and receiver context _only_ */
1308 unsigned long sbnr, ebnr, lbnr;
1309 unsigned long count;
1310 sector_t esector, nr_sectors;
1313 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
1314 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1315 (unsigned long long)sector, size);
1318 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1319 esector = sector + (size >> 9) - 1;
1321 if (!expect(sector < nr_sectors))
1323 if (!expect(esector < nr_sectors))
1324 esector = nr_sectors - 1;
1326 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1329 * round up start sector, round down end sector. we make sure we only
1330 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1331 if (unlikely(esector < BM_SECT_PER_BIT-1))
1333 if (unlikely(esector == (nr_sectors-1)))
1336 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1337 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1343 * ok, (capacity & 7) != 0 sometimes, but who cares...
1344 * we count rs_{total,left} in bits, not sectors.
1346 spin_lock_irq(&mdev->al_lock);
1347 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1349 mdev->rs_failed += count;
1351 if (get_ldev(mdev)) {
1352 drbd_try_clear_on_disk_bm(mdev, sector, count, false);
1356 /* just wake_up unconditional now, various lc_chaged(),
1357 * lc_put() in drbd_try_clear_on_disk_bm(). */
1360 spin_unlock_irq(&mdev->al_lock);
1362 wake_up(&mdev->al_wait);