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;
180 spin_lock_irq(&mdev->al_lock);
181 tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
182 if (unlikely(tmp != NULL)) {
183 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
184 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
185 set_bit(BME_PRIORITY, &bm_ext->flags);
186 spin_unlock_irq(&mdev->al_lock);
190 al_ext = lc_get(mdev->act_log, enr);
191 al_flags = mdev->act_log->flags;
192 spin_unlock_irq(&mdev->al_lock);
196 if (al_flags & LC_STARVING)
197 dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n");
198 if (al_flags & LC_DIRTY)
199 dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n");
206 void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
208 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
209 struct lc_element *al_ext;
210 struct update_al_work al_work;
212 D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
214 wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
216 if (al_ext->lc_number != enr) {
217 /* drbd_al_write_transaction(mdev,al_ext,enr);
218 * recurses into generic_make_request(), which
219 * disallows recursion, bios being serialized on the
220 * current->bio_tail list now.
221 * we have to delegate updates to the activity log
222 * to the worker thread. */
223 init_completion(&al_work.event);
224 al_work.al_ext = al_ext;
226 al_work.old_enr = al_ext->lc_number;
227 al_work.w.cb = w_al_write_transaction;
228 drbd_queue_work_front(&mdev->data.work, &al_work.w);
229 wait_for_completion(&al_work.event);
233 spin_lock_irq(&mdev->al_lock);
234 lc_changed(mdev->act_log, al_ext);
235 spin_unlock_irq(&mdev->al_lock);
236 wake_up(&mdev->al_wait);
240 void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
242 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
243 struct lc_element *extent;
246 spin_lock_irqsave(&mdev->al_lock, flags);
248 extent = lc_find(mdev->act_log, enr);
251 spin_unlock_irqrestore(&mdev->al_lock, flags);
252 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
256 if (lc_put(mdev->act_log, extent) == 0)
257 wake_up(&mdev->al_wait);
259 spin_unlock_irqrestore(&mdev->al_lock, flags);
263 w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused)
265 struct update_al_work *aw = container_of(w, struct update_al_work, w);
266 struct lc_element *updated = aw->al_ext;
267 const unsigned int new_enr = aw->enr;
268 const unsigned int evicted = aw->old_enr;
269 struct al_transaction *buffer;
272 unsigned int extent_nr;
275 if (!get_ldev(mdev)) {
277 "disk is %s, cannot start al transaction (-%d +%d)\n",
278 drbd_disk_str(mdev->state.disk), evicted, new_enr);
279 complete(&((struct update_al_work *)w)->event);
282 /* do we have to do a bitmap write, first?
283 * TODO reduce maximum latency:
284 * submit both bios, then wait for both,
285 * instead of doing two synchronous sector writes.
286 * For now, we must not write the transaction,
287 * if we cannot write out the bitmap of the evicted extent. */
288 if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
289 drbd_bm_write_sect(mdev, evicted/AL_EXT_PER_BM_SECT);
291 /* The bitmap write may have failed, causing a state change. */
292 if (mdev->state.disk < D_INCONSISTENT) {
294 "disk is %s, cannot write al transaction (-%d +%d)\n",
295 drbd_disk_str(mdev->state.disk), evicted, new_enr);
296 complete(&((struct update_al_work *)w)->event);
301 mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
302 buffer = (struct al_transaction *)page_address(mdev->md_io_page);
304 buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
305 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
307 n = lc_index_of(mdev->act_log, updated);
309 buffer->updates[0].pos = cpu_to_be32(n);
310 buffer->updates[0].extent = cpu_to_be32(new_enr);
314 mx = min_t(int, AL_EXTENTS_PT,
315 mdev->act_log->nr_elements - mdev->al_tr_cycle);
316 for (i = 0; i < mx; i++) {
317 unsigned idx = mdev->al_tr_cycle + i;
318 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
319 buffer->updates[i+1].pos = cpu_to_be32(idx);
320 buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
321 xor_sum ^= extent_nr;
323 for (; i < AL_EXTENTS_PT; i++) {
324 buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
325 buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
328 mdev->al_tr_cycle += AL_EXTENTS_PT;
329 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
330 mdev->al_tr_cycle = 0;
332 buffer->xor_sum = cpu_to_be32(xor_sum);
334 sector = mdev->ldev->md.md_offset
335 + mdev->ldev->md.al_offset + mdev->al_tr_pos;
337 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
338 drbd_chk_io_error(mdev, 1, TRUE);
340 if (++mdev->al_tr_pos >
341 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
344 D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
345 mdev->al_tr_number++;
347 mutex_unlock(&mdev->md_io_mutex);
349 complete(&((struct update_al_work *)w)->event);
356 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
357 * @mdev: DRBD device.
358 * @bdev: Block device to read form.
359 * @b: pointer to an al_transaction.
360 * @index: On disk slot of the transaction to read.
362 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
364 static int drbd_al_read_tr(struct drbd_conf *mdev,
365 struct drbd_backing_dev *bdev,
366 struct al_transaction *b,
373 sector = bdev->md.md_offset + bdev->md.al_offset + index;
375 /* Dont process error normally,
376 * as this is done before disk is attached! */
377 if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
380 rv = (be32_to_cpu(b->magic) == DRBD_MAGIC);
382 for (i = 0; i < AL_EXTENTS_PT + 1; i++)
383 xor_sum ^= be32_to_cpu(b->updates[i].extent);
384 rv &= (xor_sum == be32_to_cpu(b->xor_sum));
390 * drbd_al_read_log() - Restores the activity log from its on disk representation.
391 * @mdev: DRBD device.
392 * @bdev: Block device to read form.
394 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
396 int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
398 struct al_transaction *buffer;
402 int active_extents = 0;
403 int transactions = 0;
411 mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
413 /* lock out all other meta data io for now,
414 * and make sure the page is mapped.
416 mutex_lock(&mdev->md_io_mutex);
417 buffer = page_address(mdev->md_io_page);
419 /* Find the valid transaction in the log */
420 for (i = 0; i <= mx; i++) {
421 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
425 mutex_unlock(&mdev->md_io_mutex);
428 cnr = be32_to_cpu(buffer->tr_number);
430 if (++found_valid == 1) {
437 if ((int)cnr - (int)from_tnr < 0) {
438 D_ASSERT(from_tnr - cnr + i - from == mx+1);
442 if ((int)cnr - (int)to_tnr > 0) {
443 D_ASSERT(cnr - to_tnr == i - to);
450 dev_warn(DEV, "No usable activity log found.\n");
451 mutex_unlock(&mdev->md_io_mutex);
455 /* Read the valid transactions.
456 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
460 unsigned int extent_nr;
463 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
464 ERR_IF(rv == 0) goto cancel;
466 mutex_unlock(&mdev->md_io_mutex);
470 trn = be32_to_cpu(buffer->tr_number);
472 spin_lock_irq(&mdev->al_lock);
474 /* This loop runs backwards because in the cyclic
475 elements there might be an old version of the
476 updated element (in slot 0). So the element in slot 0
477 can overwrite old versions. */
478 for (j = AL_EXTENTS_PT; j >= 0; j--) {
479 pos = be32_to_cpu(buffer->updates[j].pos);
480 extent_nr = be32_to_cpu(buffer->updates[j].extent);
482 if (extent_nr == LC_FREE)
485 lc_set(mdev->act_log, extent_nr, pos);
488 spin_unlock_irq(&mdev->al_lock);
500 mdev->al_tr_number = to_tnr+1;
501 mdev->al_tr_pos = to;
502 if (++mdev->al_tr_pos >
503 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
506 /* ok, we are done with it */
507 mutex_unlock(&mdev->md_io_mutex);
509 dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
510 transactions, active_extents);
515 static void atodb_endio(struct bio *bio, int error)
517 struct drbd_atodb_wait *wc = bio->bi_private;
518 struct drbd_conf *mdev = wc->mdev;
520 int uptodate = bio_flagged(bio, BIO_UPTODATE);
522 /* strange behavior of some lower level drivers...
523 * fail the request by clearing the uptodate flag,
524 * but do not return any error?! */
525 if (!error && !uptodate)
528 drbd_chk_io_error(mdev, error, TRUE);
529 if (error && wc->error == 0)
532 if (atomic_dec_and_test(&wc->count))
533 complete(&wc->io_done);
535 page = bio->bi_io_vec[0].bv_page;
543 #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
545 /* activity log to on disk bitmap -- prepare bio unless that sector
546 * is already covered by previously prepared bios */
547 static int atodb_prepare_unless_covered(struct drbd_conf *mdev,
550 struct drbd_atodb_wait *wc) __must_hold(local)
554 sector_t on_disk_sector;
555 unsigned int page_offset = PAGE_SIZE;
560 /* We always write aligned, full 4k blocks,
561 * so we can ignore the logical_block_size (for now) */
563 on_disk_sector = enr + mdev->ldev->md.md_offset
564 + mdev->ldev->md.bm_offset;
566 D_ASSERT(!(on_disk_sector & 7U));
568 /* Check if that enr is already covered by an already created bio.
569 * Caution, bios[] is not NULL terminated,
570 * but only initialized to all NULL.
571 * For completely scattered activity log,
572 * the last invocation iterates over all bios,
573 * and finds the last NULL entry.
575 while ((bio = bios[i])) {
576 if (bio->bi_sector == on_disk_sector)
580 /* bios[i] == NULL, the next not yet used slot */
582 /* GFP_KERNEL, we are not in the write-out path */
583 bio = bio_alloc(GFP_KERNEL, 1);
588 const struct bio_vec *prev_bv = bios[i-1]->bi_io_vec;
589 page_offset = prev_bv->bv_offset + prev_bv->bv_len;
590 page = prev_bv->bv_page;
592 if (page_offset == PAGE_SIZE) {
593 page = alloc_page(__GFP_HIGHMEM);
602 drbd_bm_get_lel(mdev, offset,
603 min_t(size_t, S2W(8), drbd_bm_words(mdev) - offset),
604 kmap(page) + page_offset);
607 bio->bi_private = wc;
608 bio->bi_end_io = atodb_endio;
609 bio->bi_bdev = mdev->ldev->md_bdev;
610 bio->bi_sector = on_disk_sector;
612 if (bio_add_page(bio, page, 4096, page_offset) != 4096)
615 atomic_inc(&wc->count);
616 /* we already know that we may do this...
617 * get_ldev_if_state(mdev,D_ATTACHING);
618 * just get the extra reference, so that the local_cnt reflects
619 * the number of pending IO requests DRBD at its backing device.
621 atomic_inc(&mdev->local_cnt);
636 * drbd_al_to_on_disk_bm() - * Writes bitmap parts covered by active AL extents
637 * @mdev: DRBD device.
639 * Called when we detach (unconfigure) local storage,
640 * or when we go from R_PRIMARY to R_SECONDARY role.
642 void drbd_al_to_on_disk_bm(struct drbd_conf *mdev)
647 struct drbd_atodb_wait wc;
649 ERR_IF (!get_ldev_if_state(mdev, D_ATTACHING))
650 return; /* sorry, I don't have any act_log etc... */
652 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
654 nr_elements = mdev->act_log->nr_elements;
656 /* GFP_KERNEL, we are not in anyone's write-out path */
657 bios = kzalloc(sizeof(struct bio *) * nr_elements, GFP_KERNEL);
659 goto submit_one_by_one;
661 atomic_set(&wc.count, 0);
662 init_completion(&wc.io_done);
666 for (i = 0; i < nr_elements; i++) {
667 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
670 /* next statement also does atomic_inc wc.count and local_cnt */
671 if (atodb_prepare_unless_covered(mdev, bios,
672 enr/AL_EXT_PER_BM_SECT,
674 goto free_bios_submit_one_by_one;
677 /* unnecessary optimization? */
678 lc_unlock(mdev->act_log);
679 wake_up(&mdev->al_wait);
681 /* all prepared, submit them */
682 for (i = 0; i < nr_elements; i++) {
685 if (FAULT_ACTIVE(mdev, DRBD_FAULT_MD_WR)) {
686 bios[i]->bi_rw = WRITE;
687 bio_endio(bios[i], -EIO);
689 submit_bio(WRITE, bios[i]);
693 /* always (try to) flush bitmap to stable storage */
696 /* In case we did not submit a single IO do not wait for
697 * them to complete. ( Because we would wait forever here. )
699 * In case we had IOs and they are already complete, there
700 * is not point in waiting anyways.
701 * Therefore this if () ... */
702 if (atomic_read(&wc.count))
703 wait_for_completion(&wc.io_done);
710 free_bios_submit_one_by_one:
711 /* free everything by calling the endio callback directly. */
712 for (i = 0; i < nr_elements && bios[i]; i++)
713 bio_endio(bios[i], 0);
718 dev_warn(DEV, "Using the slow drbd_al_to_on_disk_bm()\n");
720 for (i = 0; i < mdev->act_log->nr_elements; i++) {
721 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
724 /* Really slow: if we have al-extents 16..19 active,
725 * sector 4 will be written four times! Synchronous! */
726 drbd_bm_write_sect(mdev, enr/AL_EXT_PER_BM_SECT);
729 lc_unlock(mdev->act_log);
730 wake_up(&mdev->al_wait);
735 * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
736 * @mdev: DRBD device.
738 void drbd_al_apply_to_bm(struct drbd_conf *mdev)
741 unsigned long add = 0;
745 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
747 for (i = 0; i < mdev->act_log->nr_elements; i++) {
748 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
751 tmp = drbd_bm_ALe_set_all(mdev, enr);
752 dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
756 lc_unlock(mdev->act_log);
757 wake_up(&mdev->al_wait);
759 dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
760 ppsize(ppb, Bit2KB(add)));
763 static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
767 spin_lock_irq(&mdev->al_lock);
768 rv = (al_ext->refcnt == 0);
770 lc_del(mdev->act_log, al_ext);
771 spin_unlock_irq(&mdev->al_lock);
777 * drbd_al_shrink() - Removes all active extents form the activity log
778 * @mdev: DRBD device.
780 * Removes all active extents form the activity log, waiting until
781 * the reference count of each entry dropped to 0 first, of course.
783 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
785 void drbd_al_shrink(struct drbd_conf *mdev)
787 struct lc_element *al_ext;
790 D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
792 for (i = 0; i < mdev->act_log->nr_elements; i++) {
793 al_ext = lc_element_by_index(mdev->act_log, i);
794 if (al_ext->lc_number == LC_FREE)
796 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
799 wake_up(&mdev->al_wait);
802 static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
804 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
806 if (!get_ldev(mdev)) {
807 if (__ratelimit(&drbd_ratelimit_state))
808 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
813 drbd_bm_write_sect(mdev, udw->enr);
818 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
819 switch (mdev->state.conn) {
820 case C_SYNC_SOURCE: case C_SYNC_TARGET:
821 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
822 drbd_resync_finished(mdev);
828 drbd_bcast_sync_progress(mdev);
834 /* ATTENTION. The AL's extents are 4MB each, while the extents in the
835 * resync LRU-cache are 16MB each.
836 * The caller of this function has to hold an get_ldev() reference.
838 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
840 static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
841 int count, int success)
843 struct lc_element *e;
844 struct update_odbm_work *udw;
848 D_ASSERT(atomic_read(&mdev->local_cnt));
850 /* I simply assume that a sector/size pair never crosses
851 * a 16 MB extent border. (Currently this is true...) */
852 enr = BM_SECT_TO_EXT(sector);
854 e = lc_get(mdev->resync, enr);
856 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
857 if (ext->lce.lc_number == enr) {
859 ext->rs_left -= count;
861 ext->rs_failed += count;
862 if (ext->rs_left < ext->rs_failed) {
863 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
864 "rs_failed=%d count=%d\n",
865 (unsigned long long)sector,
866 ext->lce.lc_number, ext->rs_left,
867 ext->rs_failed, count);
870 lc_put(mdev->resync, &ext->lce);
871 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
875 /* Normally this element should be in the cache,
876 * since drbd_rs_begin_io() pulled it already in.
878 * But maybe an application write finished, and we set
879 * something outside the resync lru_cache in sync.
881 int rs_left = drbd_bm_e_weight(mdev, enr);
882 if (ext->flags != 0) {
883 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
885 ext->lce.lc_number, ext->rs_left,
886 ext->flags, enr, rs_left);
889 if (ext->rs_failed) {
890 dev_warn(DEV, "Kicking resync_lru element enr=%u "
891 "out with rs_failed=%d\n",
892 ext->lce.lc_number, ext->rs_failed);
893 set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
895 ext->rs_left = rs_left;
896 ext->rs_failed = success ? 0 : count;
897 lc_changed(mdev->resync, &ext->lce);
899 lc_put(mdev->resync, &ext->lce);
900 /* no race, we are within the al_lock! */
902 if (ext->rs_left == ext->rs_failed) {
905 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
907 udw->enr = ext->lce.lc_number;
908 udw->w.cb = w_update_odbm;
909 drbd_queue_work_front(&mdev->data.work, &udw->w);
911 dev_warn(DEV, "Could not kmalloc an udw\n");
912 set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
916 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
918 mdev->resync->nr_elements,
919 mdev->resync->flags);
923 void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
925 unsigned long now = jiffies;
926 unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
927 int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
928 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
929 if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
930 mdev->state.conn != C_PAUSED_SYNC_T &&
931 mdev->state.conn != C_PAUSED_SYNC_S) {
932 mdev->rs_mark_time[next] = now;
933 mdev->rs_mark_left[next] = still_to_go;
934 mdev->rs_last_mark = next;
939 /* clear the bit corresponding to the piece of storage in question:
940 * size byte of data starting from sector. Only clear a bits of the affected
941 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
943 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
946 void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
947 const char *file, const unsigned int line)
949 /* Is called from worker and receiver context _only_ */
950 unsigned long sbnr, ebnr, lbnr;
951 unsigned long count = 0;
952 sector_t esector, nr_sectors;
956 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
957 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
958 (unsigned long long)sector, size);
961 nr_sectors = drbd_get_capacity(mdev->this_bdev);
962 esector = sector + (size >> 9) - 1;
964 ERR_IF(sector >= nr_sectors) return;
965 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
967 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
969 /* we clear it (in sync).
970 * round up start sector, round down end sector. we make sure we only
971 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
972 if (unlikely(esector < BM_SECT_PER_BIT-1))
974 if (unlikely(esector == (nr_sectors-1)))
977 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
978 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
984 * ok, (capacity & 7) != 0 sometimes, but who cares...
985 * we count rs_{total,left} in bits, not sectors.
987 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
988 if (count && get_ldev(mdev)) {
989 drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
990 spin_lock_irqsave(&mdev->al_lock, flags);
991 drbd_try_clear_on_disk_bm(mdev, sector, count, TRUE);
992 spin_unlock_irqrestore(&mdev->al_lock, flags);
994 /* just wake_up unconditional now, various lc_chaged(),
995 * lc_put() in drbd_try_clear_on_disk_bm(). */
1000 wake_up(&mdev->al_wait);
1004 * this is intended to set one request worth of data out of sync.
1005 * affects at least 1 bit,
1006 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
1008 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
1009 * so this can be _any_ process.
1011 int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
1012 const char *file, const unsigned int line)
1014 unsigned long sbnr, ebnr, lbnr, flags;
1015 sector_t esector, nr_sectors;
1016 unsigned int enr, count = 0;
1017 struct lc_element *e;
1019 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
1020 dev_err(DEV, "sector: %llus, size: %d\n",
1021 (unsigned long long)sector, size);
1025 if (!get_ldev(mdev))
1026 return 0; /* no disk, no metadata, no bitmap to set bits in */
1028 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1029 esector = sector + (size >> 9) - 1;
1031 ERR_IF(sector >= nr_sectors)
1033 ERR_IF(esector >= nr_sectors)
1034 esector = (nr_sectors-1);
1036 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1038 /* we set it out of sync,
1039 * we do not need to round anything here */
1040 sbnr = BM_SECT_TO_BIT(sector);
1041 ebnr = BM_SECT_TO_BIT(esector);
1043 /* ok, (capacity & 7) != 0 sometimes, but who cares...
1044 * we count rs_{total,left} in bits, not sectors. */
1045 spin_lock_irqsave(&mdev->al_lock, flags);
1046 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
1048 enr = BM_SECT_TO_EXT(sector);
1049 e = lc_find(mdev->resync, enr);
1051 lc_entry(e, struct bm_extent, lce)->rs_left += count;
1052 spin_unlock_irqrestore(&mdev->al_lock, flags);
1061 struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
1063 struct lc_element *e;
1064 struct bm_extent *bm_ext;
1066 unsigned long rs_flags;
1068 spin_lock_irq(&mdev->al_lock);
1069 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
1070 spin_unlock_irq(&mdev->al_lock);
1073 e = lc_get(mdev->resync, enr);
1074 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1076 if (bm_ext->lce.lc_number != enr) {
1077 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1078 bm_ext->rs_failed = 0;
1079 lc_changed(mdev->resync, &bm_ext->lce);
1082 if (bm_ext->lce.refcnt == 1)
1083 mdev->resync_locked++;
1084 set_bit(BME_NO_WRITES, &bm_ext->flags);
1086 rs_flags = mdev->resync->flags;
1087 spin_unlock_irq(&mdev->al_lock);
1089 wake_up(&mdev->al_wait);
1092 if (rs_flags & LC_STARVING)
1093 dev_warn(DEV, "Have to wait for element"
1094 " (resync LRU too small?)\n");
1095 BUG_ON(rs_flags & LC_DIRTY);
1101 static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1103 struct lc_element *al_ext;
1106 spin_lock_irq(&mdev->al_lock);
1107 if (unlikely(enr == mdev->act_log->new_number))
1110 al_ext = lc_find(mdev->act_log, enr);
1116 spin_unlock_irq(&mdev->al_lock);
1120 dev_info(DEV, "Delaying sync read until app's write is done\n");
1127 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1128 * @mdev: DRBD device.
1129 * @sector: The sector number.
1131 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
1133 int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1135 unsigned int enr = BM_SECT_TO_EXT(sector);
1136 struct bm_extent *bm_ext;
1139 sig = wait_event_interruptible(mdev->al_wait,
1140 (bm_ext = _bme_get(mdev, enr)));
1144 if (test_bit(BME_LOCKED, &bm_ext->flags))
1147 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1148 sig = wait_event_interruptible(mdev->al_wait,
1149 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i));
1151 spin_lock_irq(&mdev->al_lock);
1152 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1153 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1154 mdev->resync_locked--;
1155 wake_up(&mdev->al_wait);
1157 spin_unlock_irq(&mdev->al_lock);
1161 set_bit(BME_LOCKED, &bm_ext->flags);
1166 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1167 * @mdev: DRBD device.
1168 * @sector: The sector number.
1170 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1171 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1172 * if there is still application IO going on in this area.
1174 int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1176 unsigned int enr = BM_SECT_TO_EXT(sector);
1177 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1178 struct lc_element *e;
1179 struct bm_extent *bm_ext;
1182 spin_lock_irq(&mdev->al_lock);
1183 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1184 /* in case you have very heavy scattered io, it may
1185 * stall the syncer undefined if we give up the ref count
1186 * when we try again and requeue.
1188 * if we don't give up the refcount, but the next time
1189 * we are scheduled this extent has been "synced" by new
1190 * application writes, we'd miss the lc_put on the
1191 * extent we keep the refcount on.
1192 * so we remembered which extent we had to try again, and
1193 * if the next requested one is something else, we do
1194 * the lc_put here...
1195 * we also have to wake_up
1197 e = lc_find(mdev->resync, mdev->resync_wenr);
1198 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1200 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1201 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1202 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1203 mdev->resync_wenr = LC_FREE;
1204 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1205 mdev->resync_locked--;
1206 wake_up(&mdev->al_wait);
1208 dev_alert(DEV, "LOGIC BUG\n");
1212 e = lc_try_get(mdev->resync, enr);
1213 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1215 if (test_bit(BME_LOCKED, &bm_ext->flags))
1217 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1218 mdev->resync_locked++;
1220 /* we did set the BME_NO_WRITES,
1221 * but then could not set BME_LOCKED,
1222 * so we tried again.
1223 * drop the extra reference. */
1224 bm_ext->lce.refcnt--;
1225 D_ASSERT(bm_ext->lce.refcnt > 0);
1229 /* do we rather want to try later? */
1230 if (mdev->resync_locked > mdev->resync->nr_elements-3)
1232 /* Do or do not. There is no try. -- Yoda */
1233 e = lc_get(mdev->resync, enr);
1234 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1236 const unsigned long rs_flags = mdev->resync->flags;
1237 if (rs_flags & LC_STARVING)
1238 dev_warn(DEV, "Have to wait for element"
1239 " (resync LRU too small?)\n");
1240 BUG_ON(rs_flags & LC_DIRTY);
1243 if (bm_ext->lce.lc_number != enr) {
1244 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1245 bm_ext->rs_failed = 0;
1246 lc_changed(mdev->resync, &bm_ext->lce);
1247 wake_up(&mdev->al_wait);
1248 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1250 set_bit(BME_NO_WRITES, &bm_ext->flags);
1251 D_ASSERT(bm_ext->lce.refcnt == 1);
1252 mdev->resync_locked++;
1256 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1257 if (unlikely(al_enr+i == mdev->act_log->new_number))
1259 if (lc_is_used(mdev->act_log, al_enr+i))
1262 set_bit(BME_LOCKED, &bm_ext->flags);
1264 mdev->resync_wenr = LC_FREE;
1265 spin_unlock_irq(&mdev->al_lock);
1270 mdev->resync_wenr = enr;
1271 spin_unlock_irq(&mdev->al_lock);
1275 void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1277 unsigned int enr = BM_SECT_TO_EXT(sector);
1278 struct lc_element *e;
1279 struct bm_extent *bm_ext;
1280 unsigned long flags;
1282 spin_lock_irqsave(&mdev->al_lock, flags);
1283 e = lc_find(mdev->resync, enr);
1284 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1286 spin_unlock_irqrestore(&mdev->al_lock, flags);
1287 if (__ratelimit(&drbd_ratelimit_state))
1288 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1292 if (bm_ext->lce.refcnt == 0) {
1293 spin_unlock_irqrestore(&mdev->al_lock, flags);
1294 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1295 "but refcnt is 0!?\n",
1296 (unsigned long long)sector, enr);
1300 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1301 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
1302 mdev->resync_locked--;
1303 wake_up(&mdev->al_wait);
1306 spin_unlock_irqrestore(&mdev->al_lock, flags);
1310 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1311 * @mdev: DRBD device.
1313 void drbd_rs_cancel_all(struct drbd_conf *mdev)
1315 spin_lock_irq(&mdev->al_lock);
1317 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1318 lc_reset(mdev->resync);
1321 mdev->resync_locked = 0;
1322 mdev->resync_wenr = LC_FREE;
1323 spin_unlock_irq(&mdev->al_lock);
1324 wake_up(&mdev->al_wait);
1328 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1329 * @mdev: DRBD device.
1331 * Returns 0 upon success, -EAGAIN if at least one reference count was
1334 int drbd_rs_del_all(struct drbd_conf *mdev)
1336 struct lc_element *e;
1337 struct bm_extent *bm_ext;
1340 spin_lock_irq(&mdev->al_lock);
1342 if (get_ldev_if_state(mdev, D_FAILED)) {
1343 /* ok, ->resync is there. */
1344 for (i = 0; i < mdev->resync->nr_elements; i++) {
1345 e = lc_element_by_index(mdev->resync, i);
1346 bm_ext = lc_entry(e, struct bm_extent, lce);
1347 if (bm_ext->lce.lc_number == LC_FREE)
1349 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1350 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1351 " got 'synced' by application io\n",
1353 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1354 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1355 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1356 mdev->resync_wenr = LC_FREE;
1357 lc_put(mdev->resync, &bm_ext->lce);
1359 if (bm_ext->lce.refcnt != 0) {
1360 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1361 "refcnt=%d\n", bm_ext->lce.refcnt);
1363 spin_unlock_irq(&mdev->al_lock);
1366 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1367 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1368 lc_del(mdev->resync, &bm_ext->lce);
1370 D_ASSERT(mdev->resync->used == 0);
1373 spin_unlock_irq(&mdev->al_lock);
1379 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1380 * @mdev: DRBD device.
1381 * @sector: The sector number.
1382 * @size: Size of failed IO operation, in byte.
1384 void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1386 /* Is called from worker and receiver context _only_ */
1387 unsigned long sbnr, ebnr, lbnr;
1388 unsigned long count;
1389 sector_t esector, nr_sectors;
1392 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
1393 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1394 (unsigned long long)sector, size);
1397 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1398 esector = sector + (size >> 9) - 1;
1400 ERR_IF(sector >= nr_sectors) return;
1401 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
1403 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1406 * round up start sector, round down end sector. we make sure we only
1407 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1408 if (unlikely(esector < BM_SECT_PER_BIT-1))
1410 if (unlikely(esector == (nr_sectors-1)))
1413 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1414 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1420 * ok, (capacity & 7) != 0 sometimes, but who cares...
1421 * we count rs_{total,left} in bits, not sectors.
1423 spin_lock_irq(&mdev->al_lock);
1424 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1426 mdev->rs_failed += count;
1428 if (get_ldev(mdev)) {
1429 drbd_try_clear_on_disk_bm(mdev, sector, count, FALSE);
1433 /* just wake_up unconditional now, various lc_chaged(),
1434 * lc_put() in drbd_try_clear_on_disk_bm(). */
1437 spin_unlock_irq(&mdev->al_lock);
1439 wake_up(&mdev->al_wait);