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"
34 /* all fields on disc in big endian */
35 struct __packed al_transaction_on_disk {
36 /* don't we all like magic */
39 /* to identify the most recent transaction block
40 * in the on disk ring buffer */
43 /* checksum on the full 4k block, with this field set to 0. */
46 /* type of transaction, special transaction types like:
47 * purge-all, set-all-idle, set-all-active, ... to-be-defined */
48 __be16 transaction_type;
50 /* we currently allow only a few thousand extents,
51 * so 16bit will be enough for the slot number. */
53 /* how many updates in this transaction */
56 /* maximum slot number, "al-extents" in drbd.conf speak.
57 * Having this in each transaction should make reconfiguration
58 * of that parameter easier. */
61 /* slot number the context starts with */
62 __be16 context_start_slot_nr;
64 /* Some reserved bytes. Expected usage is a 64bit counter of
65 * sectors-written since device creation, and other data generation tag
69 /* --- 36 byte used --- */
71 /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes
72 * in one transaction, then use the remaining byte in the 4k block for
73 * context information. "Flexible" number of updates per transaction
74 * does not help, as we have to account for the case when all update
75 * slots are used anyways, so it would only complicate code without
78 __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION];
80 /* but the extent number is 32bit, which at an extent size of 4 MiB
81 * allows to cover device sizes of up to 2**54 Byte (16 PiB) */
82 __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION];
84 /* --- 420 bytes used (36 + 64*6) --- */
86 /* 4096 - 420 = 3676 = 919 * 4 */
87 __be32 context[AL_CONTEXT_PER_TRANSACTION];
90 struct update_odbm_work {
95 struct update_al_work {
97 struct completion event;
101 struct drbd_atodb_wait {
103 struct completion io_done;
104 struct drbd_conf *mdev;
109 static int w_al_write_transaction(struct drbd_work *, int);
111 static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
112 struct drbd_backing_dev *bdev,
113 struct page *page, sector_t sector,
117 struct drbd_md_io md_io;
121 init_completion(&md_io.event);
124 if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
125 rw |= REQ_FUA | REQ_FLUSH;
128 bio = bio_alloc_drbd(GFP_NOIO);
129 bio->bi_bdev = bdev->md_bdev;
130 bio->bi_sector = sector;
131 ok = (bio_add_page(bio, page, size, 0) == size);
134 bio->bi_private = &md_io;
135 bio->bi_end_io = drbd_md_io_complete;
138 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
139 bio_endio(bio, -EIO);
142 wait_for_completion(&md_io.event);
143 ok = bio_flagged(bio, BIO_UPTODATE) && md_io.error == 0;
150 int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
151 sector_t sector, int rw)
154 struct page *iop = mdev->md_io_page;
156 D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
158 BUG_ON(!bdev->md_bdev);
160 dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s)\n",
161 current->comm, current->pid, __func__,
162 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
164 if (sector < drbd_md_first_sector(bdev) ||
165 sector + 7 > drbd_md_last_sector(bdev))
166 dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
167 current->comm, current->pid, __func__,
168 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
170 ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, MD_BLOCK_SIZE);
172 dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
173 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
180 static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
182 struct lc_element *al_ext;
183 struct lc_element *tmp;
186 spin_lock_irq(&mdev->al_lock);
187 tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
188 if (unlikely(tmp != NULL)) {
189 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
190 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
191 wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
192 spin_unlock_irq(&mdev->al_lock);
194 wake_up(&mdev->al_wait);
198 al_ext = lc_get(mdev->act_log, enr);
199 spin_unlock_irq(&mdev->al_lock);
203 void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
205 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
206 struct lc_element *al_ext;
207 struct update_al_work al_work;
209 D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
211 wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
213 if (al_ext->lc_number != enr) {
214 /* drbd_al_write_transaction(mdev,al_ext,enr);
215 * recurses into generic_make_request(), which
216 * disallows recursion, bios being serialized on the
217 * current->bio_tail list now.
218 * we have to delegate updates to the activity log
219 * to the worker thread. */
221 /* Serialize multiple transactions.
222 * This uses test_and_set_bit, memory barrier is implicit.
223 * Optimization potential:
224 * first check for transaction number > old transaction number,
225 * so not all waiters have to lock/unlock. */
226 wait_event(mdev->al_wait, lc_try_lock_for_transaction(mdev->act_log));
228 /* Double check: it may have been committed by someone else,
229 * while we have been waiting for the lock. */
230 if (al_ext->lc_number != enr) {
231 init_completion(&al_work.event);
232 al_work.w.cb = w_al_write_transaction;
233 al_work.w.mdev = mdev;
234 drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w);
235 wait_for_completion(&al_work.event);
239 spin_lock_irq(&mdev->al_lock);
242 we need an "lc_cancel" here;
244 lc_committed(mdev->act_log);
245 spin_unlock_irq(&mdev->al_lock);
247 lc_unlock(mdev->act_log);
248 wake_up(&mdev->al_wait);
252 void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
254 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
255 struct lc_element *extent;
258 spin_lock_irqsave(&mdev->al_lock, flags);
260 extent = lc_find(mdev->act_log, enr);
263 spin_unlock_irqrestore(&mdev->al_lock, flags);
264 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
268 if (lc_put(mdev->act_log, extent) == 0)
269 wake_up(&mdev->al_wait);
271 spin_unlock_irqrestore(&mdev->al_lock, flags);
274 #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
275 /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
276 * are still coupled, or assume too much about their relation.
277 * Code below will not work if this is violated.
278 * Will be cleaned up with some followup patch.
283 static unsigned int al_extent_to_bm_page(unsigned int al_enr)
288 /* al extent number to bit */
289 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
292 static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
297 /* al extent number to bit */
298 (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
302 w_al_write_transaction(struct drbd_work *w, int unused)
304 struct update_al_work *aw = container_of(w, struct update_al_work, w);
305 struct drbd_conf *mdev = w->mdev;
306 struct al_transaction_on_disk *buffer;
307 struct lc_element *e;
313 if (!get_ldev(mdev)) {
314 dev_err(DEV, "disk is %s, cannot start al transaction\n",
315 drbd_disk_str(mdev->state.disk));
317 complete(&((struct update_al_work *)w)->event);
321 /* The bitmap write may have failed, causing a state change. */
322 if (mdev->state.disk < D_INCONSISTENT) {
324 "disk is %s, cannot write al transaction\n",
325 drbd_disk_str(mdev->state.disk));
327 complete(&((struct update_al_work *)w)->event);
332 mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
333 buffer = page_address(mdev->md_io_page);
335 memset(buffer, 0, sizeof(*buffer));
336 buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
337 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
341 /* Even though no one can start to change this list
342 * once we set the LC_LOCKED -- from drbd_al_begin_io(),
343 * lc_try_lock_for_transaction() --, someone may still
344 * be in the process of changing it. */
345 spin_lock_irq(&mdev->al_lock);
346 list_for_each_entry(e, &mdev->act_log->to_be_changed, list) {
347 if (i == AL_UPDATES_PER_TRANSACTION) {
351 buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
352 buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
353 if (e->lc_number != LC_FREE)
354 drbd_bm_mark_for_writeout(mdev,
355 al_extent_to_bm_page(e->lc_number));
358 spin_unlock_irq(&mdev->al_lock);
359 BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
361 buffer->n_updates = cpu_to_be16(i);
362 for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
363 buffer->update_slot_nr[i] = cpu_to_be16(-1);
364 buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
367 buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements);
368 buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle);
370 mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
371 mdev->act_log->nr_elements - mdev->al_tr_cycle);
372 for (i = 0; i < mx; i++) {
373 unsigned idx = mdev->al_tr_cycle + i;
374 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
375 buffer->context[i] = cpu_to_be32(extent_nr);
377 for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
378 buffer->context[i] = cpu_to_be32(LC_FREE);
380 mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
381 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
382 mdev->al_tr_cycle = 0;
384 sector = mdev->ldev->md.md_offset
385 + mdev->ldev->md.al_offset
386 + mdev->al_tr_pos * (MD_BLOCK_SIZE>>9);
388 crc = crc32c(0, buffer, 4096);
389 buffer->crc32c = cpu_to_be32(crc);
391 if (drbd_bm_write_hinted(mdev))
393 /* drbd_chk_io_error done already */
394 else if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
396 drbd_chk_io_error(mdev, 1, true);
398 /* advance ringbuffer position and transaction counter */
399 mdev->al_tr_pos = (mdev->al_tr_pos + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
400 mdev->al_tr_number++;
403 mutex_unlock(&mdev->md_io_mutex);
404 complete(&((struct update_al_work *)w)->event);
411 * reading of the activity log,
412 * and potentially dirtying of the affected bitmap regions,
413 * should be done from userland only.
414 * DRBD would simply always attach with an empty activity log,
415 * and refuse to attach to something that looks like a crashed primary.
419 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
420 * @mdev: DRBD device.
421 * @bdev: Block device to read form.
422 * @b: pointer to an al_transaction.
423 * @index: On disk slot of the transaction to read.
425 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
427 static int drbd_al_read_tr(struct drbd_conf *mdev,
428 struct drbd_backing_dev *bdev,
431 struct al_transaction_on_disk *b = page_address(mdev->md_io_page);
435 sector = bdev->md.md_offset
437 + index * (MD_BLOCK_SIZE>>9);
439 /* Dont process error normally,
440 * as this is done before disk is attached! */
441 if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
444 if (!expect(b->magic == cpu_to_be32(DRBD_AL_MAGIC)))
447 if (!expect(be16_to_cpu(b->n_updates) <= AL_UPDATES_PER_TRANSACTION))
450 if (!expect(be16_to_cpu(b->context_size) <= DRBD_AL_EXTENTS_MAX))
453 if (!expect(be16_to_cpu(b->context_start_slot_nr) < DRBD_AL_EXTENTS_MAX))
456 crc = be32_to_cpu(b->crc32c);
458 if (!expect(crc == crc32c(0, b, 4096)))
465 * drbd_al_read_log() - Restores the activity log from its on disk representation.
466 * @mdev: DRBD device.
467 * @bdev: Block device to read form.
469 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
471 int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
473 struct al_transaction_on_disk *b;
477 int active_extents = 0;
478 int transactions = 0;
486 /* Note that this is expected to be called with a newly created,
487 * clean and all unused activity log of the "expected size".
490 /* lock out all other meta data io for now,
491 * and make sure the page is mapped.
493 mutex_lock(&mdev->md_io_mutex);
494 b = page_address(mdev->md_io_page);
496 /* Always use the full ringbuffer space for now.
497 * possible optimization: read in all of it,
498 * then scan the in-memory pages. */
500 mx = (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
502 /* Find the valid transaction in the log */
503 for (i = 0; i < mx; i++) {
504 rv = drbd_al_read_tr(mdev, bdev, i);
505 /* invalid data in that block */
511 mutex_unlock(&mdev->md_io_mutex);
515 cnr = be32_to_cpu(b->tr_number);
516 if (++found_valid == 1) {
524 D_ASSERT(cnr != to_tnr);
525 D_ASSERT(cnr != from_tnr);
526 if ((int)cnr - (int)from_tnr < 0) {
527 D_ASSERT(from_tnr - cnr + i - from == mx);
531 if ((int)cnr - (int)to_tnr > 0) {
532 D_ASSERT(cnr - to_tnr == i - to);
539 dev_warn(DEV, "No usable activity log found.\n");
540 mutex_unlock(&mdev->md_io_mutex);
544 /* Read the valid transactions.
545 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
548 struct lc_element *e;
549 unsigned j, n, slot, extent_nr;
551 rv = drbd_al_read_tr(mdev, bdev, i);
552 if (!expect(rv != 0))
555 mutex_unlock(&mdev->md_io_mutex);
559 /* deal with different transaction types.
560 * not yet implemented */
561 if (!expect(b->transaction_type == 0))
564 /* on the fly re-create/resize activity log?
565 * will be a special transaction type flag. */
566 if (!expect(be16_to_cpu(b->context_size) == mdev->act_log->nr_elements))
568 if (!expect(be16_to_cpu(b->context_start_slot_nr) < mdev->act_log->nr_elements))
571 /* We are the only user of the activity log right now,
572 * don't actually need to take that lock. */
573 spin_lock_irq(&mdev->al_lock);
575 /* first, apply the context, ... */
576 for (j = 0, slot = be16_to_cpu(b->context_start_slot_nr);
577 j < AL_CONTEXT_PER_TRANSACTION &&
578 slot < mdev->act_log->nr_elements; j++, slot++) {
579 extent_nr = be32_to_cpu(b->context[j]);
580 e = lc_element_by_index(mdev->act_log, slot);
581 if (e->lc_number != extent_nr) {
582 if (extent_nr != LC_FREE)
587 lc_set(mdev->act_log, extent_nr, slot);
590 /* ... then apply the updates,
591 * which override the context information.
592 * drbd_al_read_tr already did the rangecheck
593 * on n <= AL_UPDATES_PER_TRANSACTION */
594 n = be16_to_cpu(b->n_updates);
595 for (j = 0; j < n; j++) {
596 slot = be16_to_cpu(b->update_slot_nr[j]);
597 extent_nr = be32_to_cpu(b->update_extent_nr[j]);
598 if (!expect(slot < mdev->act_log->nr_elements))
600 e = lc_element_by_index(mdev->act_log, slot);
601 if (e->lc_number != extent_nr) {
602 if (extent_nr != LC_FREE)
607 lc_set(mdev->act_log, extent_nr, slot);
609 spin_unlock_irq(&mdev->al_lock);
621 mdev->al_tr_number = to_tnr+1;
622 mdev->al_tr_pos = (to + 1) % (MD_AL_SECTORS*512/MD_BLOCK_SIZE);
624 /* ok, we are done with it */
625 mutex_unlock(&mdev->md_io_mutex);
627 dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
628 transactions, active_extents);
634 * drbd_al_apply_to_bm() - Sets the bitmap to dirty(1) where covered by active AL extents
635 * @mdev: DRBD device.
637 void drbd_al_apply_to_bm(struct drbd_conf *mdev)
640 unsigned long add = 0;
644 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
646 for (i = 0; i < mdev->act_log->nr_elements; i++) {
647 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
650 tmp = drbd_bm_ALe_set_all(mdev, enr);
651 dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
655 lc_unlock(mdev->act_log);
656 wake_up(&mdev->al_wait);
658 dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
659 ppsize(ppb, Bit2KB(add)));
662 static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
666 spin_lock_irq(&mdev->al_lock);
667 rv = (al_ext->refcnt == 0);
669 lc_del(mdev->act_log, al_ext);
670 spin_unlock_irq(&mdev->al_lock);
676 * drbd_al_shrink() - Removes all active extents form the activity log
677 * @mdev: DRBD device.
679 * Removes all active extents form the activity log, waiting until
680 * the reference count of each entry dropped to 0 first, of course.
682 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
684 void drbd_al_shrink(struct drbd_conf *mdev)
686 struct lc_element *al_ext;
689 D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags));
691 for (i = 0; i < mdev->act_log->nr_elements; i++) {
692 al_ext = lc_element_by_index(mdev->act_log, i);
693 if (al_ext->lc_number == LC_FREE)
695 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
698 wake_up(&mdev->al_wait);
701 static int w_update_odbm(struct drbd_work *w, int unused)
703 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
704 struct drbd_conf *mdev = w->mdev;
705 struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
707 if (!get_ldev(mdev)) {
708 if (__ratelimit(&drbd_ratelimit_state))
709 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
714 drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
719 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
720 switch (mdev->state.conn) {
721 case C_SYNC_SOURCE: case C_SYNC_TARGET:
722 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
723 drbd_resync_finished(mdev);
729 drbd_bcast_event(mdev, &sib);
735 /* ATTENTION. The AL's extents are 4MB each, while the extents in the
736 * resync LRU-cache are 16MB each.
737 * The caller of this function has to hold an get_ldev() reference.
739 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
741 static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
742 int count, int success)
744 struct lc_element *e;
745 struct update_odbm_work *udw;
749 D_ASSERT(atomic_read(&mdev->local_cnt));
751 /* I simply assume that a sector/size pair never crosses
752 * a 16 MB extent border. (Currently this is true...) */
753 enr = BM_SECT_TO_EXT(sector);
755 e = lc_get(mdev->resync, enr);
757 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
758 if (ext->lce.lc_number == enr) {
760 ext->rs_left -= count;
762 ext->rs_failed += count;
763 if (ext->rs_left < ext->rs_failed) {
764 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
765 "rs_failed=%d count=%d\n",
766 (unsigned long long)sector,
767 ext->lce.lc_number, ext->rs_left,
768 ext->rs_failed, count);
771 lc_put(mdev->resync, &ext->lce);
772 conn_request_state(mdev->tconn, NS(conn, C_DISCONNECTING), CS_HARD);
776 /* Normally this element should be in the cache,
777 * since drbd_rs_begin_io() pulled it already in.
779 * But maybe an application write finished, and we set
780 * something outside the resync lru_cache in sync.
782 int rs_left = drbd_bm_e_weight(mdev, enr);
783 if (ext->flags != 0) {
784 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
786 ext->lce.lc_number, ext->rs_left,
787 ext->flags, enr, rs_left);
790 if (ext->rs_failed) {
791 dev_warn(DEV, "Kicking resync_lru element enr=%u "
792 "out with rs_failed=%d\n",
793 ext->lce.lc_number, ext->rs_failed);
795 ext->rs_left = rs_left;
796 ext->rs_failed = success ? 0 : count;
797 /* we don't keep a persistent log of the resync lru,
798 * we can commit any change right away. */
799 lc_committed(mdev->resync);
801 lc_put(mdev->resync, &ext->lce);
802 /* no race, we are within the al_lock! */
804 if (ext->rs_left == ext->rs_failed) {
807 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
809 udw->enr = ext->lce.lc_number;
810 udw->w.cb = w_update_odbm;
812 drbd_queue_work_front(&mdev->tconn->data.work, &udw->w);
814 dev_warn(DEV, "Could not kmalloc an udw\n");
818 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
820 mdev->resync->nr_elements,
821 mdev->resync->flags);
825 void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
827 unsigned long now = jiffies;
828 unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
829 int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
830 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
831 if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
832 mdev->state.conn != C_PAUSED_SYNC_T &&
833 mdev->state.conn != C_PAUSED_SYNC_S) {
834 mdev->rs_mark_time[next] = now;
835 mdev->rs_mark_left[next] = still_to_go;
836 mdev->rs_last_mark = next;
841 /* clear the bit corresponding to the piece of storage in question:
842 * size byte of data starting from sector. Only clear a bits of the affected
843 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
845 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
848 void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
849 const char *file, const unsigned int line)
851 /* Is called from worker and receiver context _only_ */
852 unsigned long sbnr, ebnr, lbnr;
853 unsigned long count = 0;
854 sector_t esector, nr_sectors;
858 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
859 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
860 (unsigned long long)sector, size);
863 nr_sectors = drbd_get_capacity(mdev->this_bdev);
864 esector = sector + (size >> 9) - 1;
866 if (!expect(sector < nr_sectors))
868 if (!expect(esector < nr_sectors))
869 esector = nr_sectors - 1;
871 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
873 /* we clear it (in sync).
874 * round up start sector, round down end sector. we make sure we only
875 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
876 if (unlikely(esector < BM_SECT_PER_BIT-1))
878 if (unlikely(esector == (nr_sectors-1)))
881 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
882 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
888 * ok, (capacity & 7) != 0 sometimes, but who cares...
889 * we count rs_{total,left} in bits, not sectors.
891 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
892 if (count && get_ldev(mdev)) {
893 drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
894 spin_lock_irqsave(&mdev->al_lock, flags);
895 drbd_try_clear_on_disk_bm(mdev, sector, count, true);
896 spin_unlock_irqrestore(&mdev->al_lock, flags);
898 /* just wake_up unconditional now, various lc_chaged(),
899 * lc_put() in drbd_try_clear_on_disk_bm(). */
904 wake_up(&mdev->al_wait);
908 * this is intended to set one request worth of data out of sync.
909 * affects at least 1 bit,
910 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
912 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
913 * so this can be _any_ process.
915 int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
916 const char *file, const unsigned int line)
918 unsigned long sbnr, ebnr, lbnr, flags;
919 sector_t esector, nr_sectors;
920 unsigned int enr, count = 0;
921 struct lc_element *e;
923 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
924 dev_err(DEV, "sector: %llus, size: %d\n",
925 (unsigned long long)sector, size);
930 return 0; /* no disk, no metadata, no bitmap to set bits in */
932 nr_sectors = drbd_get_capacity(mdev->this_bdev);
933 esector = sector + (size >> 9) - 1;
935 if (!expect(sector < nr_sectors))
937 if (!expect(esector < nr_sectors))
938 esector = nr_sectors - 1;
940 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
942 /* we set it out of sync,
943 * we do not need to round anything here */
944 sbnr = BM_SECT_TO_BIT(sector);
945 ebnr = BM_SECT_TO_BIT(esector);
947 /* ok, (capacity & 7) != 0 sometimes, but who cares...
948 * we count rs_{total,left} in bits, not sectors. */
949 spin_lock_irqsave(&mdev->al_lock, flags);
950 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
952 enr = BM_SECT_TO_EXT(sector);
953 e = lc_find(mdev->resync, enr);
955 lc_entry(e, struct bm_extent, lce)->rs_left += count;
956 spin_unlock_irqrestore(&mdev->al_lock, flags);
965 struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
967 struct lc_element *e;
968 struct bm_extent *bm_ext;
970 unsigned long rs_flags;
972 spin_lock_irq(&mdev->al_lock);
973 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
974 spin_unlock_irq(&mdev->al_lock);
977 e = lc_get(mdev->resync, enr);
978 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
980 if (bm_ext->lce.lc_number != enr) {
981 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
982 bm_ext->rs_failed = 0;
983 lc_committed(mdev->resync);
986 if (bm_ext->lce.refcnt == 1)
987 mdev->resync_locked++;
988 set_bit(BME_NO_WRITES, &bm_ext->flags);
990 rs_flags = mdev->resync->flags;
991 spin_unlock_irq(&mdev->al_lock);
993 wake_up(&mdev->al_wait);
996 if (rs_flags & LC_STARVING)
997 dev_warn(DEV, "Have to wait for element"
998 " (resync LRU too small?)\n");
999 BUG_ON(rs_flags & LC_LOCKED);
1005 static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1009 spin_lock_irq(&mdev->al_lock);
1010 rv = lc_is_used(mdev->act_log, enr);
1011 spin_unlock_irq(&mdev->al_lock);
1017 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1018 * @mdev: DRBD device.
1019 * @sector: The sector number.
1021 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
1023 int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1025 unsigned int enr = BM_SECT_TO_EXT(sector);
1026 struct bm_extent *bm_ext;
1028 int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
1029 200 times -> 20 seconds. */
1032 sig = wait_event_interruptible(mdev->al_wait,
1033 (bm_ext = _bme_get(mdev, enr)));
1037 if (test_bit(BME_LOCKED, &bm_ext->flags))
1040 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1041 sig = wait_event_interruptible(mdev->al_wait,
1042 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
1043 test_bit(BME_PRIORITY, &bm_ext->flags));
1045 if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
1046 spin_lock_irq(&mdev->al_lock);
1047 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1048 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
1049 mdev->resync_locked--;
1050 wake_up(&mdev->al_wait);
1052 spin_unlock_irq(&mdev->al_lock);
1055 if (schedule_timeout_interruptible(HZ/10))
1057 if (sa && --sa == 0)
1058 dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
1059 "Resync stalled?\n");
1063 set_bit(BME_LOCKED, &bm_ext->flags);
1068 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1069 * @mdev: DRBD device.
1070 * @sector: The sector number.
1072 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1073 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1074 * if there is still application IO going on in this area.
1076 int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1078 unsigned int enr = BM_SECT_TO_EXT(sector);
1079 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1080 struct lc_element *e;
1081 struct bm_extent *bm_ext;
1084 spin_lock_irq(&mdev->al_lock);
1085 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1086 /* in case you have very heavy scattered io, it may
1087 * stall the syncer undefined if we give up the ref count
1088 * when we try again and requeue.
1090 * if we don't give up the refcount, but the next time
1091 * we are scheduled this extent has been "synced" by new
1092 * application writes, we'd miss the lc_put on the
1093 * extent we keep the refcount on.
1094 * so we remembered which extent we had to try again, and
1095 * if the next requested one is something else, we do
1096 * the lc_put here...
1097 * we also have to wake_up
1099 e = lc_find(mdev->resync, mdev->resync_wenr);
1100 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1102 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1103 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1104 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1105 mdev->resync_wenr = LC_FREE;
1106 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1107 mdev->resync_locked--;
1108 wake_up(&mdev->al_wait);
1110 dev_alert(DEV, "LOGIC BUG\n");
1114 e = lc_try_get(mdev->resync, enr);
1115 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1117 if (test_bit(BME_LOCKED, &bm_ext->flags))
1119 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1120 mdev->resync_locked++;
1122 /* we did set the BME_NO_WRITES,
1123 * but then could not set BME_LOCKED,
1124 * so we tried again.
1125 * drop the extra reference. */
1126 bm_ext->lce.refcnt--;
1127 D_ASSERT(bm_ext->lce.refcnt > 0);
1131 /* do we rather want to try later? */
1132 if (mdev->resync_locked > mdev->resync->nr_elements-3)
1134 /* Do or do not. There is no try. -- Yoda */
1135 e = lc_get(mdev->resync, enr);
1136 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1138 const unsigned long rs_flags = mdev->resync->flags;
1139 if (rs_flags & LC_STARVING)
1140 dev_warn(DEV, "Have to wait for element"
1141 " (resync LRU too small?)\n");
1142 BUG_ON(rs_flags & LC_LOCKED);
1145 if (bm_ext->lce.lc_number != enr) {
1146 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1147 bm_ext->rs_failed = 0;
1148 lc_committed(mdev->resync);
1149 wake_up(&mdev->al_wait);
1150 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1152 set_bit(BME_NO_WRITES, &bm_ext->flags);
1153 D_ASSERT(bm_ext->lce.refcnt == 1);
1154 mdev->resync_locked++;
1158 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1159 if (lc_is_used(mdev->act_log, al_enr+i))
1162 set_bit(BME_LOCKED, &bm_ext->flags);
1164 mdev->resync_wenr = LC_FREE;
1165 spin_unlock_irq(&mdev->al_lock);
1170 mdev->resync_wenr = enr;
1171 spin_unlock_irq(&mdev->al_lock);
1175 void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1177 unsigned int enr = BM_SECT_TO_EXT(sector);
1178 struct lc_element *e;
1179 struct bm_extent *bm_ext;
1180 unsigned long flags;
1182 spin_lock_irqsave(&mdev->al_lock, flags);
1183 e = lc_find(mdev->resync, enr);
1184 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1186 spin_unlock_irqrestore(&mdev->al_lock, flags);
1187 if (__ratelimit(&drbd_ratelimit_state))
1188 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1192 if (bm_ext->lce.refcnt == 0) {
1193 spin_unlock_irqrestore(&mdev->al_lock, flags);
1194 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1195 "but refcnt is 0!?\n",
1196 (unsigned long long)sector, enr);
1200 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1201 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
1202 mdev->resync_locked--;
1203 wake_up(&mdev->al_wait);
1206 spin_unlock_irqrestore(&mdev->al_lock, flags);
1210 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1211 * @mdev: DRBD device.
1213 void drbd_rs_cancel_all(struct drbd_conf *mdev)
1215 spin_lock_irq(&mdev->al_lock);
1217 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1218 lc_reset(mdev->resync);
1221 mdev->resync_locked = 0;
1222 mdev->resync_wenr = LC_FREE;
1223 spin_unlock_irq(&mdev->al_lock);
1224 wake_up(&mdev->al_wait);
1228 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1229 * @mdev: DRBD device.
1231 * Returns 0 upon success, -EAGAIN if at least one reference count was
1234 int drbd_rs_del_all(struct drbd_conf *mdev)
1236 struct lc_element *e;
1237 struct bm_extent *bm_ext;
1240 spin_lock_irq(&mdev->al_lock);
1242 if (get_ldev_if_state(mdev, D_FAILED)) {
1243 /* ok, ->resync is there. */
1244 for (i = 0; i < mdev->resync->nr_elements; i++) {
1245 e = lc_element_by_index(mdev->resync, i);
1246 bm_ext = lc_entry(e, struct bm_extent, lce);
1247 if (bm_ext->lce.lc_number == LC_FREE)
1249 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1250 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1251 " got 'synced' by application io\n",
1253 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1254 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1255 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1256 mdev->resync_wenr = LC_FREE;
1257 lc_put(mdev->resync, &bm_ext->lce);
1259 if (bm_ext->lce.refcnt != 0) {
1260 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1261 "refcnt=%d\n", bm_ext->lce.refcnt);
1263 spin_unlock_irq(&mdev->al_lock);
1266 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1267 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1268 lc_del(mdev->resync, &bm_ext->lce);
1270 D_ASSERT(mdev->resync->used == 0);
1273 spin_unlock_irq(&mdev->al_lock);
1279 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1280 * @mdev: DRBD device.
1281 * @sector: The sector number.
1282 * @size: Size of failed IO operation, in byte.
1284 void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1286 /* Is called from worker and receiver context _only_ */
1287 unsigned long sbnr, ebnr, lbnr;
1288 unsigned long count;
1289 sector_t esector, nr_sectors;
1292 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
1293 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1294 (unsigned long long)sector, size);
1297 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1298 esector = sector + (size >> 9) - 1;
1300 if (!expect(sector < nr_sectors))
1302 if (!expect(esector < nr_sectors))
1303 esector = nr_sectors - 1;
1305 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1308 * round up start sector, round down end sector. we make sure we only
1309 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1310 if (unlikely(esector < BM_SECT_PER_BIT-1))
1312 if (unlikely(esector == (nr_sectors-1)))
1315 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1316 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1322 * ok, (capacity & 7) != 0 sometimes, but who cares...
1323 * we count rs_{total,left} in bits, not sectors.
1325 spin_lock_irq(&mdev->al_lock);
1326 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1328 mdev->rs_failed += count;
1330 if (get_ldev(mdev)) {
1331 drbd_try_clear_on_disk_bm(mdev, sector, count, false);
1335 /* just wake_up unconditional now, various lc_chaged(),
1336 * lc_put() in drbd_try_clear_on_disk_bm(). */
1339 spin_unlock_irq(&mdev->al_lock);
1341 wake_up(&mdev->al_wait);