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/drbd.h>
29 #include "drbd_wrappers.h"
31 /* We maintain a trivial check sum in our on disk activity log.
32 * With that we can ensure correct operation even when the storage
33 * device might do a partial (last) sector write while loosing power.
35 struct __packed al_transaction {
40 u32 extent; } updates[1 + AL_EXTENTS_PT];
44 struct update_odbm_work {
49 struct update_al_work {
51 struct lc_element *al_ext;
52 struct completion event;
54 /* if old_enr != LC_FREE, write corresponding bitmap sector, too */
58 struct drbd_atodb_wait {
60 struct completion io_done;
61 struct drbd_conf *mdev;
66 int w_al_write_transaction(struct drbd_conf *, struct drbd_work *, int);
68 static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
69 struct drbd_backing_dev *bdev,
70 struct page *page, sector_t sector,
74 struct drbd_md_io md_io;
78 init_completion(&md_io.event);
81 if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
85 bio = bio_alloc(GFP_NOIO, 1);
86 bio->bi_bdev = bdev->md_bdev;
87 bio->bi_sector = sector;
88 ok = (bio_add_page(bio, page, size, 0) == size);
91 bio->bi_private = &md_io;
92 bio->bi_end_io = drbd_md_io_complete;
95 if (FAULT_ACTIVE(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
99 wait_for_completion(&md_io.event);
100 ok = bio_flagged(bio, BIO_UPTODATE) && md_io.error == 0;
107 int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
108 sector_t sector, int rw)
110 int logical_block_size, mask, ok;
112 struct page *iop = mdev->md_io_page;
114 D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
116 BUG_ON(!bdev->md_bdev);
118 logical_block_size = bdev_logical_block_size(bdev->md_bdev);
119 if (logical_block_size == 0)
120 logical_block_size = MD_SECTOR_SIZE;
122 /* in case logical_block_size != 512 [ s390 only? ] */
123 if (logical_block_size != MD_SECTOR_SIZE) {
124 mask = (logical_block_size / MD_SECTOR_SIZE) - 1;
125 D_ASSERT(mask == 1 || mask == 3 || mask == 7);
126 D_ASSERT(logical_block_size == (mask+1) * MD_SECTOR_SIZE);
127 offset = sector & mask;
128 sector = sector & ~mask;
129 iop = mdev->md_io_tmpp;
132 /* these are GFP_KERNEL pages, pre-allocated
133 * on device initialization */
134 void *p = page_address(mdev->md_io_page);
135 void *hp = page_address(mdev->md_io_tmpp);
137 ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector,
138 READ, logical_block_size);
141 dev_err(DEV, "drbd_md_sync_page_io(,%llus,"
142 "READ [logical_block_size!=512]) failed!\n",
143 (unsigned long long)sector);
147 memcpy(hp + offset*MD_SECTOR_SIZE, p, MD_SECTOR_SIZE);
151 if (sector < drbd_md_first_sector(bdev) ||
152 sector > drbd_md_last_sector(bdev))
153 dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
154 current->comm, current->pid, __func__,
155 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
157 ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, logical_block_size);
159 dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
160 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
164 if (logical_block_size != MD_SECTOR_SIZE && !(rw & WRITE)) {
165 void *p = page_address(mdev->md_io_page);
166 void *hp = page_address(mdev->md_io_tmpp);
168 memcpy(p, hp + offset*MD_SECTOR_SIZE, MD_SECTOR_SIZE);
174 static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
176 struct lc_element *al_ext;
177 struct lc_element *tmp;
178 unsigned long al_flags = 0;
181 spin_lock_irq(&mdev->al_lock);
182 tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
183 if (unlikely(tmp != NULL)) {
184 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
185 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
186 wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
187 spin_unlock_irq(&mdev->al_lock);
189 wake_up(&mdev->al_wait);
193 al_ext = lc_get(mdev->act_log, enr);
194 al_flags = mdev->act_log->flags;
195 spin_unlock_irq(&mdev->al_lock);
199 if (al_flags & LC_STARVING)
200 dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n");
201 if (al_flags & LC_DIRTY)
202 dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n");
209 void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
211 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
212 struct lc_element *al_ext;
213 struct update_al_work al_work;
215 D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
217 wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
219 if (al_ext->lc_number != enr) {
220 /* drbd_al_write_transaction(mdev,al_ext,enr);
221 * recurses into generic_make_request(), which
222 * disallows recursion, bios being serialized on the
223 * current->bio_tail list now.
224 * we have to delegate updates to the activity log
225 * to the worker thread. */
226 init_completion(&al_work.event);
227 al_work.al_ext = al_ext;
229 al_work.old_enr = al_ext->lc_number;
230 al_work.w.cb = w_al_write_transaction;
231 drbd_queue_work_front(&mdev->data.work, &al_work.w);
232 wait_for_completion(&al_work.event);
236 spin_lock_irq(&mdev->al_lock);
237 lc_changed(mdev->act_log, al_ext);
238 spin_unlock_irq(&mdev->al_lock);
239 wake_up(&mdev->al_wait);
243 void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
245 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
246 struct lc_element *extent;
249 spin_lock_irqsave(&mdev->al_lock, flags);
251 extent = lc_find(mdev->act_log, enr);
254 spin_unlock_irqrestore(&mdev->al_lock, flags);
255 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
259 if (lc_put(mdev->act_log, extent) == 0)
260 wake_up(&mdev->al_wait);
262 spin_unlock_irqrestore(&mdev->al_lock, flags);
266 w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused)
268 struct update_al_work *aw = container_of(w, struct update_al_work, w);
269 struct lc_element *updated = aw->al_ext;
270 const unsigned int new_enr = aw->enr;
271 const unsigned int evicted = aw->old_enr;
272 struct al_transaction *buffer;
275 unsigned int extent_nr;
278 if (!get_ldev(mdev)) {
280 "disk is %s, cannot start al transaction (-%d +%d)\n",
281 drbd_disk_str(mdev->state.disk), evicted, new_enr);
282 complete(&((struct update_al_work *)w)->event);
285 /* do we have to do a bitmap write, first?
286 * TODO reduce maximum latency:
287 * submit both bios, then wait for both,
288 * instead of doing two synchronous sector writes.
289 * For now, we must not write the transaction,
290 * if we cannot write out the bitmap of the evicted extent. */
291 if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
292 drbd_bm_write_sect(mdev, evicted/AL_EXT_PER_BM_SECT);
294 /* The bitmap write may have failed, causing a state change. */
295 if (mdev->state.disk < D_INCONSISTENT) {
297 "disk is %s, cannot write al transaction (-%d +%d)\n",
298 drbd_disk_str(mdev->state.disk), evicted, new_enr);
299 complete(&((struct update_al_work *)w)->event);
304 mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
305 buffer = (struct al_transaction *)page_address(mdev->md_io_page);
307 buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
308 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
310 n = lc_index_of(mdev->act_log, updated);
312 buffer->updates[0].pos = cpu_to_be32(n);
313 buffer->updates[0].extent = cpu_to_be32(new_enr);
317 mx = min_t(int, AL_EXTENTS_PT,
318 mdev->act_log->nr_elements - mdev->al_tr_cycle);
319 for (i = 0; i < mx; i++) {
320 unsigned idx = mdev->al_tr_cycle + i;
321 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
322 buffer->updates[i+1].pos = cpu_to_be32(idx);
323 buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
324 xor_sum ^= extent_nr;
326 for (; i < AL_EXTENTS_PT; i++) {
327 buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
328 buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
331 mdev->al_tr_cycle += AL_EXTENTS_PT;
332 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
333 mdev->al_tr_cycle = 0;
335 buffer->xor_sum = cpu_to_be32(xor_sum);
337 sector = mdev->ldev->md.md_offset
338 + mdev->ldev->md.al_offset + mdev->al_tr_pos;
340 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
341 drbd_chk_io_error(mdev, 1, TRUE);
343 if (++mdev->al_tr_pos >
344 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
347 D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
348 mdev->al_tr_number++;
350 mutex_unlock(&mdev->md_io_mutex);
352 complete(&((struct update_al_work *)w)->event);
359 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
360 * @mdev: DRBD device.
361 * @bdev: Block device to read form.
362 * @b: pointer to an al_transaction.
363 * @index: On disk slot of the transaction to read.
365 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
367 static int drbd_al_read_tr(struct drbd_conf *mdev,
368 struct drbd_backing_dev *bdev,
369 struct al_transaction *b,
376 sector = bdev->md.md_offset + bdev->md.al_offset + index;
378 /* Dont process error normally,
379 * as this is done before disk is attached! */
380 if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
383 rv = (be32_to_cpu(b->magic) == DRBD_MAGIC);
385 for (i = 0; i < AL_EXTENTS_PT + 1; i++)
386 xor_sum ^= be32_to_cpu(b->updates[i].extent);
387 rv &= (xor_sum == be32_to_cpu(b->xor_sum));
393 * drbd_al_read_log() - Restores the activity log from its on disk representation.
394 * @mdev: DRBD device.
395 * @bdev: Block device to read form.
397 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
399 int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
401 struct al_transaction *buffer;
405 int active_extents = 0;
406 int transactions = 0;
414 mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
416 /* lock out all other meta data io for now,
417 * and make sure the page is mapped.
419 mutex_lock(&mdev->md_io_mutex);
420 buffer = page_address(mdev->md_io_page);
422 /* Find the valid transaction in the log */
423 for (i = 0; i <= mx; i++) {
424 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
428 mutex_unlock(&mdev->md_io_mutex);
431 cnr = be32_to_cpu(buffer->tr_number);
433 if (++found_valid == 1) {
440 if ((int)cnr - (int)from_tnr < 0) {
441 D_ASSERT(from_tnr - cnr + i - from == mx+1);
445 if ((int)cnr - (int)to_tnr > 0) {
446 D_ASSERT(cnr - to_tnr == i - to);
453 dev_warn(DEV, "No usable activity log found.\n");
454 mutex_unlock(&mdev->md_io_mutex);
458 /* Read the valid transactions.
459 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
463 unsigned int extent_nr;
466 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
467 ERR_IF(rv == 0) goto cancel;
469 mutex_unlock(&mdev->md_io_mutex);
473 trn = be32_to_cpu(buffer->tr_number);
475 spin_lock_irq(&mdev->al_lock);
477 /* This loop runs backwards because in the cyclic
478 elements there might be an old version of the
479 updated element (in slot 0). So the element in slot 0
480 can overwrite old versions. */
481 for (j = AL_EXTENTS_PT; j >= 0; j--) {
482 pos = be32_to_cpu(buffer->updates[j].pos);
483 extent_nr = be32_to_cpu(buffer->updates[j].extent);
485 if (extent_nr == LC_FREE)
488 lc_set(mdev->act_log, extent_nr, pos);
491 spin_unlock_irq(&mdev->al_lock);
503 mdev->al_tr_number = to_tnr+1;
504 mdev->al_tr_pos = to;
505 if (++mdev->al_tr_pos >
506 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
509 /* ok, we are done with it */
510 mutex_unlock(&mdev->md_io_mutex);
512 dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
513 transactions, active_extents);
518 static void atodb_endio(struct bio *bio, int error)
520 struct drbd_atodb_wait *wc = bio->bi_private;
521 struct drbd_conf *mdev = wc->mdev;
523 int uptodate = bio_flagged(bio, BIO_UPTODATE);
525 /* strange behavior of some lower level drivers...
526 * fail the request by clearing the uptodate flag,
527 * but do not return any error?! */
528 if (!error && !uptodate)
531 drbd_chk_io_error(mdev, error, TRUE);
532 if (error && wc->error == 0)
535 if (atomic_dec_and_test(&wc->count))
536 complete(&wc->io_done);
538 page = bio->bi_io_vec[0].bv_page;
546 #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
548 /* activity log to on disk bitmap -- prepare bio unless that sector
549 * is already covered by previously prepared bios */
550 static int atodb_prepare_unless_covered(struct drbd_conf *mdev,
553 struct drbd_atodb_wait *wc) __must_hold(local)
557 sector_t on_disk_sector;
558 unsigned int page_offset = PAGE_SIZE;
563 /* We always write aligned, full 4k blocks,
564 * so we can ignore the logical_block_size (for now) */
566 on_disk_sector = enr + mdev->ldev->md.md_offset
567 + mdev->ldev->md.bm_offset;
569 D_ASSERT(!(on_disk_sector & 7U));
571 /* Check if that enr is already covered by an already created bio.
572 * Caution, bios[] is not NULL terminated,
573 * but only initialized to all NULL.
574 * For completely scattered activity log,
575 * the last invocation iterates over all bios,
576 * and finds the last NULL entry.
578 while ((bio = bios[i])) {
579 if (bio->bi_sector == on_disk_sector)
583 /* bios[i] == NULL, the next not yet used slot */
585 /* GFP_KERNEL, we are not in the write-out path */
586 bio = bio_alloc(GFP_KERNEL, 1);
591 const struct bio_vec *prev_bv = bios[i-1]->bi_io_vec;
592 page_offset = prev_bv->bv_offset + prev_bv->bv_len;
593 page = prev_bv->bv_page;
595 if (page_offset == PAGE_SIZE) {
596 page = alloc_page(__GFP_HIGHMEM);
605 drbd_bm_get_lel(mdev, offset,
606 min_t(size_t, S2W(8), drbd_bm_words(mdev) - offset),
607 kmap(page) + page_offset);
610 bio->bi_private = wc;
611 bio->bi_end_io = atodb_endio;
612 bio->bi_bdev = mdev->ldev->md_bdev;
613 bio->bi_sector = on_disk_sector;
615 if (bio_add_page(bio, page, 4096, page_offset) != 4096)
618 atomic_inc(&wc->count);
619 /* we already know that we may do this...
620 * get_ldev_if_state(mdev,D_ATTACHING);
621 * just get the extra reference, so that the local_cnt reflects
622 * the number of pending IO requests DRBD at its backing device.
624 atomic_inc(&mdev->local_cnt);
639 * drbd_al_to_on_disk_bm() - * Writes bitmap parts covered by active AL extents
640 * @mdev: DRBD device.
642 * Called when we detach (unconfigure) local storage,
643 * or when we go from R_PRIMARY to R_SECONDARY role.
645 void drbd_al_to_on_disk_bm(struct drbd_conf *mdev)
650 struct drbd_atodb_wait wc;
652 ERR_IF (!get_ldev_if_state(mdev, D_ATTACHING))
653 return; /* sorry, I don't have any act_log etc... */
655 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
657 nr_elements = mdev->act_log->nr_elements;
659 /* GFP_KERNEL, we are not in anyone's write-out path */
660 bios = kzalloc(sizeof(struct bio *) * nr_elements, GFP_KERNEL);
662 goto submit_one_by_one;
664 atomic_set(&wc.count, 0);
665 init_completion(&wc.io_done);
669 for (i = 0; i < nr_elements; i++) {
670 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
673 /* next statement also does atomic_inc wc.count and local_cnt */
674 if (atodb_prepare_unless_covered(mdev, bios,
675 enr/AL_EXT_PER_BM_SECT,
677 goto free_bios_submit_one_by_one;
680 /* unnecessary optimization? */
681 lc_unlock(mdev->act_log);
682 wake_up(&mdev->al_wait);
684 /* all prepared, submit them */
685 for (i = 0; i < nr_elements; i++) {
688 if (FAULT_ACTIVE(mdev, DRBD_FAULT_MD_WR)) {
689 bios[i]->bi_rw = WRITE;
690 bio_endio(bios[i], -EIO);
692 submit_bio(WRITE, bios[i]);
696 /* always (try to) flush bitmap to stable storage */
699 /* In case we did not submit a single IO do not wait for
700 * them to complete. ( Because we would wait forever here. )
702 * In case we had IOs and they are already complete, there
703 * is not point in waiting anyways.
704 * Therefore this if () ... */
705 if (atomic_read(&wc.count))
706 wait_for_completion(&wc.io_done);
713 free_bios_submit_one_by_one:
714 /* free everything by calling the endio callback directly. */
715 for (i = 0; i < nr_elements && bios[i]; i++)
716 bio_endio(bios[i], 0);
721 dev_warn(DEV, "Using the slow drbd_al_to_on_disk_bm()\n");
723 for (i = 0; i < mdev->act_log->nr_elements; i++) {
724 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
727 /* Really slow: if we have al-extents 16..19 active,
728 * sector 4 will be written four times! Synchronous! */
729 drbd_bm_write_sect(mdev, enr/AL_EXT_PER_BM_SECT);
732 lc_unlock(mdev->act_log);
733 wake_up(&mdev->al_wait);
738 * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
739 * @mdev: DRBD device.
741 void drbd_al_apply_to_bm(struct drbd_conf *mdev)
744 unsigned long add = 0;
748 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
750 for (i = 0; i < mdev->act_log->nr_elements; i++) {
751 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
754 tmp = drbd_bm_ALe_set_all(mdev, enr);
755 dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
759 lc_unlock(mdev->act_log);
760 wake_up(&mdev->al_wait);
762 dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
763 ppsize(ppb, Bit2KB(add)));
766 static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
770 spin_lock_irq(&mdev->al_lock);
771 rv = (al_ext->refcnt == 0);
773 lc_del(mdev->act_log, al_ext);
774 spin_unlock_irq(&mdev->al_lock);
780 * drbd_al_shrink() - Removes all active extents form the activity log
781 * @mdev: DRBD device.
783 * Removes all active extents form the activity log, waiting until
784 * the reference count of each entry dropped to 0 first, of course.
786 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
788 void drbd_al_shrink(struct drbd_conf *mdev)
790 struct lc_element *al_ext;
793 D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
795 for (i = 0; i < mdev->act_log->nr_elements; i++) {
796 al_ext = lc_element_by_index(mdev->act_log, i);
797 if (al_ext->lc_number == LC_FREE)
799 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
802 wake_up(&mdev->al_wait);
805 static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
807 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
809 if (!get_ldev(mdev)) {
810 if (__ratelimit(&drbd_ratelimit_state))
811 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
816 drbd_bm_write_sect(mdev, udw->enr);
821 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
822 switch (mdev->state.conn) {
823 case C_SYNC_SOURCE: case C_SYNC_TARGET:
824 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
825 drbd_resync_finished(mdev);
831 drbd_bcast_sync_progress(mdev);
837 /* ATTENTION. The AL's extents are 4MB each, while the extents in the
838 * resync LRU-cache are 16MB each.
839 * The caller of this function has to hold an get_ldev() reference.
841 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
843 static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
844 int count, int success)
846 struct lc_element *e;
847 struct update_odbm_work *udw;
851 D_ASSERT(atomic_read(&mdev->local_cnt));
853 /* I simply assume that a sector/size pair never crosses
854 * a 16 MB extent border. (Currently this is true...) */
855 enr = BM_SECT_TO_EXT(sector);
857 e = lc_get(mdev->resync, enr);
859 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
860 if (ext->lce.lc_number == enr) {
862 ext->rs_left -= count;
864 ext->rs_failed += count;
865 if (ext->rs_left < ext->rs_failed) {
866 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
867 "rs_failed=%d count=%d\n",
868 (unsigned long long)sector,
869 ext->lce.lc_number, ext->rs_left,
870 ext->rs_failed, count);
873 lc_put(mdev->resync, &ext->lce);
874 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
878 /* Normally this element should be in the cache,
879 * since drbd_rs_begin_io() pulled it already in.
881 * But maybe an application write finished, and we set
882 * something outside the resync lru_cache in sync.
884 int rs_left = drbd_bm_e_weight(mdev, enr);
885 if (ext->flags != 0) {
886 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
888 ext->lce.lc_number, ext->rs_left,
889 ext->flags, enr, rs_left);
892 if (ext->rs_failed) {
893 dev_warn(DEV, "Kicking resync_lru element enr=%u "
894 "out with rs_failed=%d\n",
895 ext->lce.lc_number, ext->rs_failed);
896 set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
898 ext->rs_left = rs_left;
899 ext->rs_failed = success ? 0 : count;
900 lc_changed(mdev->resync, &ext->lce);
902 lc_put(mdev->resync, &ext->lce);
903 /* no race, we are within the al_lock! */
905 if (ext->rs_left == ext->rs_failed) {
908 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
910 udw->enr = ext->lce.lc_number;
911 udw->w.cb = w_update_odbm;
912 drbd_queue_work_front(&mdev->data.work, &udw->w);
914 dev_warn(DEV, "Could not kmalloc an udw\n");
915 set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
919 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
921 mdev->resync->nr_elements,
922 mdev->resync->flags);
926 void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
928 unsigned long now = jiffies;
929 unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
930 int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
931 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
932 if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
933 mdev->state.conn != C_PAUSED_SYNC_T &&
934 mdev->state.conn != C_PAUSED_SYNC_S) {
935 mdev->rs_mark_time[next] = now;
936 mdev->rs_mark_left[next] = still_to_go;
937 mdev->rs_last_mark = next;
942 /* clear the bit corresponding to the piece of storage in question:
943 * size byte of data starting from sector. Only clear a bits of the affected
944 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
946 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
949 void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
950 const char *file, const unsigned int line)
952 /* Is called from worker and receiver context _only_ */
953 unsigned long sbnr, ebnr, lbnr;
954 unsigned long count = 0;
955 sector_t esector, nr_sectors;
959 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
960 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
961 (unsigned long long)sector, size);
964 nr_sectors = drbd_get_capacity(mdev->this_bdev);
965 esector = sector + (size >> 9) - 1;
967 ERR_IF(sector >= nr_sectors) return;
968 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
970 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
972 /* we clear it (in sync).
973 * round up start sector, round down end sector. we make sure we only
974 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
975 if (unlikely(esector < BM_SECT_PER_BIT-1))
977 if (unlikely(esector == (nr_sectors-1)))
980 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
981 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
987 * ok, (capacity & 7) != 0 sometimes, but who cares...
988 * we count rs_{total,left} in bits, not sectors.
990 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
991 if (count && get_ldev(mdev)) {
992 drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
993 spin_lock_irqsave(&mdev->al_lock, flags);
994 drbd_try_clear_on_disk_bm(mdev, sector, count, TRUE);
995 spin_unlock_irqrestore(&mdev->al_lock, flags);
997 /* just wake_up unconditional now, various lc_chaged(),
998 * lc_put() in drbd_try_clear_on_disk_bm(). */
1003 wake_up(&mdev->al_wait);
1007 * this is intended to set one request worth of data out of sync.
1008 * affects at least 1 bit,
1009 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
1011 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
1012 * so this can be _any_ process.
1014 int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
1015 const char *file, const unsigned int line)
1017 unsigned long sbnr, ebnr, lbnr, flags;
1018 sector_t esector, nr_sectors;
1019 unsigned int enr, count = 0;
1020 struct lc_element *e;
1022 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
1023 dev_err(DEV, "sector: %llus, size: %d\n",
1024 (unsigned long long)sector, size);
1028 if (!get_ldev(mdev))
1029 return 0; /* no disk, no metadata, no bitmap to set bits in */
1031 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1032 esector = sector + (size >> 9) - 1;
1034 ERR_IF(sector >= nr_sectors)
1036 ERR_IF(esector >= nr_sectors)
1037 esector = (nr_sectors-1);
1039 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1041 /* we set it out of sync,
1042 * we do not need to round anything here */
1043 sbnr = BM_SECT_TO_BIT(sector);
1044 ebnr = BM_SECT_TO_BIT(esector);
1046 /* ok, (capacity & 7) != 0 sometimes, but who cares...
1047 * we count rs_{total,left} in bits, not sectors. */
1048 spin_lock_irqsave(&mdev->al_lock, flags);
1049 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
1051 enr = BM_SECT_TO_EXT(sector);
1052 e = lc_find(mdev->resync, enr);
1054 lc_entry(e, struct bm_extent, lce)->rs_left += count;
1055 spin_unlock_irqrestore(&mdev->al_lock, flags);
1064 struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
1066 struct lc_element *e;
1067 struct bm_extent *bm_ext;
1069 unsigned long rs_flags;
1071 spin_lock_irq(&mdev->al_lock);
1072 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
1073 spin_unlock_irq(&mdev->al_lock);
1076 e = lc_get(mdev->resync, enr);
1077 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1079 if (bm_ext->lce.lc_number != enr) {
1080 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1081 bm_ext->rs_failed = 0;
1082 lc_changed(mdev->resync, &bm_ext->lce);
1085 if (bm_ext->lce.refcnt == 1)
1086 mdev->resync_locked++;
1087 set_bit(BME_NO_WRITES, &bm_ext->flags);
1089 rs_flags = mdev->resync->flags;
1090 spin_unlock_irq(&mdev->al_lock);
1092 wake_up(&mdev->al_wait);
1095 if (rs_flags & LC_STARVING)
1096 dev_warn(DEV, "Have to wait for element"
1097 " (resync LRU too small?)\n");
1098 BUG_ON(rs_flags & LC_DIRTY);
1104 static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1106 struct lc_element *al_ext;
1109 spin_lock_irq(&mdev->al_lock);
1110 if (unlikely(enr == mdev->act_log->new_number))
1113 al_ext = lc_find(mdev->act_log, enr);
1119 spin_unlock_irq(&mdev->al_lock);
1123 dev_info(DEV, "Delaying sync read until app's write is done\n");
1130 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1131 * @mdev: DRBD device.
1132 * @sector: The sector number.
1134 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
1136 int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1138 unsigned int enr = BM_SECT_TO_EXT(sector);
1139 struct bm_extent *bm_ext;
1141 int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
1142 200 times -> 20 seconds. */
1145 sig = wait_event_interruptible(mdev->al_wait,
1146 (bm_ext = _bme_get(mdev, enr)));
1150 if (test_bit(BME_LOCKED, &bm_ext->flags))
1153 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1154 sig = wait_event_interruptible(mdev->al_wait,
1155 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
1156 test_bit(BME_PRIORITY, &bm_ext->flags));
1158 if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
1159 spin_lock_irq(&mdev->al_lock);
1160 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1161 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
1162 mdev->resync_locked--;
1163 wake_up(&mdev->al_wait);
1165 spin_unlock_irq(&mdev->al_lock);
1168 if (schedule_timeout_interruptible(HZ/10))
1170 if (sa && --sa == 0)
1171 dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
1172 "Resync stalled?\n");
1176 set_bit(BME_LOCKED, &bm_ext->flags);
1181 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1182 * @mdev: DRBD device.
1183 * @sector: The sector number.
1185 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1186 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1187 * if there is still application IO going on in this area.
1189 int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1191 unsigned int enr = BM_SECT_TO_EXT(sector);
1192 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1193 struct lc_element *e;
1194 struct bm_extent *bm_ext;
1197 spin_lock_irq(&mdev->al_lock);
1198 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1199 /* in case you have very heavy scattered io, it may
1200 * stall the syncer undefined if we give up the ref count
1201 * when we try again and requeue.
1203 * if we don't give up the refcount, but the next time
1204 * we are scheduled this extent has been "synced" by new
1205 * application writes, we'd miss the lc_put on the
1206 * extent we keep the refcount on.
1207 * so we remembered which extent we had to try again, and
1208 * if the next requested one is something else, we do
1209 * the lc_put here...
1210 * we also have to wake_up
1212 e = lc_find(mdev->resync, mdev->resync_wenr);
1213 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1215 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1216 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1217 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1218 mdev->resync_wenr = LC_FREE;
1219 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1220 mdev->resync_locked--;
1221 wake_up(&mdev->al_wait);
1223 dev_alert(DEV, "LOGIC BUG\n");
1227 e = lc_try_get(mdev->resync, enr);
1228 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1230 if (test_bit(BME_LOCKED, &bm_ext->flags))
1232 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1233 mdev->resync_locked++;
1235 /* we did set the BME_NO_WRITES,
1236 * but then could not set BME_LOCKED,
1237 * so we tried again.
1238 * drop the extra reference. */
1239 bm_ext->lce.refcnt--;
1240 D_ASSERT(bm_ext->lce.refcnt > 0);
1244 /* do we rather want to try later? */
1245 if (mdev->resync_locked > mdev->resync->nr_elements-3)
1247 /* Do or do not. There is no try. -- Yoda */
1248 e = lc_get(mdev->resync, enr);
1249 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1251 const unsigned long rs_flags = mdev->resync->flags;
1252 if (rs_flags & LC_STARVING)
1253 dev_warn(DEV, "Have to wait for element"
1254 " (resync LRU too small?)\n");
1255 BUG_ON(rs_flags & LC_DIRTY);
1258 if (bm_ext->lce.lc_number != enr) {
1259 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1260 bm_ext->rs_failed = 0;
1261 lc_changed(mdev->resync, &bm_ext->lce);
1262 wake_up(&mdev->al_wait);
1263 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1265 set_bit(BME_NO_WRITES, &bm_ext->flags);
1266 D_ASSERT(bm_ext->lce.refcnt == 1);
1267 mdev->resync_locked++;
1271 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1272 if (unlikely(al_enr+i == mdev->act_log->new_number))
1274 if (lc_is_used(mdev->act_log, al_enr+i))
1277 set_bit(BME_LOCKED, &bm_ext->flags);
1279 mdev->resync_wenr = LC_FREE;
1280 spin_unlock_irq(&mdev->al_lock);
1285 mdev->resync_wenr = enr;
1286 spin_unlock_irq(&mdev->al_lock);
1290 void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1292 unsigned int enr = BM_SECT_TO_EXT(sector);
1293 struct lc_element *e;
1294 struct bm_extent *bm_ext;
1295 unsigned long flags;
1297 spin_lock_irqsave(&mdev->al_lock, flags);
1298 e = lc_find(mdev->resync, enr);
1299 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1301 spin_unlock_irqrestore(&mdev->al_lock, flags);
1302 if (__ratelimit(&drbd_ratelimit_state))
1303 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1307 if (bm_ext->lce.refcnt == 0) {
1308 spin_unlock_irqrestore(&mdev->al_lock, flags);
1309 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1310 "but refcnt is 0!?\n",
1311 (unsigned long long)sector, enr);
1315 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1316 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
1317 mdev->resync_locked--;
1318 wake_up(&mdev->al_wait);
1321 spin_unlock_irqrestore(&mdev->al_lock, flags);
1325 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1326 * @mdev: DRBD device.
1328 void drbd_rs_cancel_all(struct drbd_conf *mdev)
1330 spin_lock_irq(&mdev->al_lock);
1332 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1333 lc_reset(mdev->resync);
1336 mdev->resync_locked = 0;
1337 mdev->resync_wenr = LC_FREE;
1338 spin_unlock_irq(&mdev->al_lock);
1339 wake_up(&mdev->al_wait);
1343 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1344 * @mdev: DRBD device.
1346 * Returns 0 upon success, -EAGAIN if at least one reference count was
1349 int drbd_rs_del_all(struct drbd_conf *mdev)
1351 struct lc_element *e;
1352 struct bm_extent *bm_ext;
1355 spin_lock_irq(&mdev->al_lock);
1357 if (get_ldev_if_state(mdev, D_FAILED)) {
1358 /* ok, ->resync is there. */
1359 for (i = 0; i < mdev->resync->nr_elements; i++) {
1360 e = lc_element_by_index(mdev->resync, i);
1361 bm_ext = lc_entry(e, struct bm_extent, lce);
1362 if (bm_ext->lce.lc_number == LC_FREE)
1364 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1365 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1366 " got 'synced' by application io\n",
1368 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1369 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1370 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1371 mdev->resync_wenr = LC_FREE;
1372 lc_put(mdev->resync, &bm_ext->lce);
1374 if (bm_ext->lce.refcnt != 0) {
1375 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1376 "refcnt=%d\n", bm_ext->lce.refcnt);
1378 spin_unlock_irq(&mdev->al_lock);
1381 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1382 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1383 lc_del(mdev->resync, &bm_ext->lce);
1385 D_ASSERT(mdev->resync->used == 0);
1388 spin_unlock_irq(&mdev->al_lock);
1394 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1395 * @mdev: DRBD device.
1396 * @sector: The sector number.
1397 * @size: Size of failed IO operation, in byte.
1399 void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1401 /* Is called from worker and receiver context _only_ */
1402 unsigned long sbnr, ebnr, lbnr;
1403 unsigned long count;
1404 sector_t esector, nr_sectors;
1407 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
1408 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1409 (unsigned long long)sector, size);
1412 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1413 esector = sector + (size >> 9) - 1;
1415 ERR_IF(sector >= nr_sectors) return;
1416 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
1418 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1421 * round up start sector, round down end sector. we make sure we only
1422 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1423 if (unlikely(esector < BM_SECT_PER_BIT-1))
1425 if (unlikely(esector == (nr_sectors-1)))
1428 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1429 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1435 * ok, (capacity & 7) != 0 sometimes, but who cares...
1436 * we count rs_{total,left} in bits, not sectors.
1438 spin_lock_irq(&mdev->al_lock);
1439 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1441 mdev->rs_failed += count;
1443 if (get_ldev(mdev)) {
1444 drbd_try_clear_on_disk_bm(mdev, sector, count, FALSE);
1448 /* just wake_up unconditional now, various lc_chaged(),
1449 * lc_put() in drbd_try_clear_on_disk_bm(). */
1452 spin_unlock_irq(&mdev->al_lock);
1454 wake_up(&mdev->al_wait);