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_tracing.h"
30 #include "drbd_wrappers.h"
32 /* We maintain a trivial check sum in our on disk activity log.
33 * With that we can ensure correct operation even when the storage
34 * device might do a partial (last) sector write while loosing power.
36 struct __packed al_transaction {
41 u32 extent; } updates[1 + AL_EXTENTS_PT];
45 struct update_odbm_work {
50 struct update_al_work {
52 struct lc_element *al_ext;
53 struct completion event;
55 /* if old_enr != LC_FREE, write corresponding bitmap sector, too */
59 struct drbd_atodb_wait {
61 struct completion io_done;
62 struct drbd_conf *mdev;
67 int w_al_write_transaction(struct drbd_conf *, struct drbd_work *, int);
69 /* The actual tracepoint needs to have constant number of known arguments...
71 void trace_drbd_resync(struct drbd_conf *mdev, int level, const char *fmt, ...)
76 trace__drbd_resync(mdev, level, fmt, ap);
80 static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
81 struct drbd_backing_dev *bdev,
82 struct page *page, sector_t sector,
86 struct drbd_md_io md_io;
90 init_completion(&md_io.event);
93 if ((rw & WRITE) && !test_bit(MD_NO_BARRIER, &mdev->flags))
94 rw |= (1 << BIO_RW_BARRIER);
95 rw |= ((1<<BIO_RW_UNPLUG) | (1<<BIO_RW_SYNCIO));
98 bio = bio_alloc(GFP_NOIO, 1);
99 bio->bi_bdev = bdev->md_bdev;
100 bio->bi_sector = sector;
101 ok = (bio_add_page(bio, page, size, 0) == size);
104 bio->bi_private = &md_io;
105 bio->bi_end_io = drbd_md_io_complete;
108 trace_drbd_bio(mdev, "Md", bio, 0, NULL);
110 if (FAULT_ACTIVE(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
111 bio_endio(bio, -EIO);
114 wait_for_completion(&md_io.event);
115 ok = bio_flagged(bio, BIO_UPTODATE) && md_io.error == 0;
117 /* check for unsupported barrier op.
118 * would rather check on EOPNOTSUPP, but that is not reliable.
119 * don't try again for ANY return value != 0 */
120 if (unlikely(bio_rw_flagged(bio, BIO_RW_BARRIER) && !ok)) {
121 /* Try again with no barrier */
122 dev_warn(DEV, "Barriers not supported on meta data device - disabling\n");
123 set_bit(MD_NO_BARRIER, &mdev->flags);
124 rw &= ~(1 << BIO_RW_BARRIER);
133 int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
134 sector_t sector, int rw)
136 int logical_block_size, mask, ok;
138 struct page *iop = mdev->md_io_page;
140 D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
142 BUG_ON(!bdev->md_bdev);
144 logical_block_size = bdev_logical_block_size(bdev->md_bdev);
145 if (logical_block_size == 0)
146 logical_block_size = MD_SECTOR_SIZE;
148 /* in case logical_block_size != 512 [ s390 only? ] */
149 if (logical_block_size != MD_SECTOR_SIZE) {
150 mask = (logical_block_size / MD_SECTOR_SIZE) - 1;
151 D_ASSERT(mask == 1 || mask == 3 || mask == 7);
152 D_ASSERT(logical_block_size == (mask+1) * MD_SECTOR_SIZE);
153 offset = sector & mask;
154 sector = sector & ~mask;
155 iop = mdev->md_io_tmpp;
158 /* these are GFP_KERNEL pages, pre-allocated
159 * on device initialization */
160 void *p = page_address(mdev->md_io_page);
161 void *hp = page_address(mdev->md_io_tmpp);
163 ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector,
164 READ, logical_block_size);
167 dev_err(DEV, "drbd_md_sync_page_io(,%llus,"
168 "READ [logical_block_size!=512]) failed!\n",
169 (unsigned long long)sector);
173 memcpy(hp + offset*MD_SECTOR_SIZE, p, MD_SECTOR_SIZE);
177 if (sector < drbd_md_first_sector(bdev) ||
178 sector > drbd_md_last_sector(bdev))
179 dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
180 current->comm, current->pid, __func__,
181 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
183 ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, logical_block_size);
185 dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
186 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
190 if (logical_block_size != MD_SECTOR_SIZE && !(rw & WRITE)) {
191 void *p = page_address(mdev->md_io_page);
192 void *hp = page_address(mdev->md_io_tmpp);
194 memcpy(p, hp + offset*MD_SECTOR_SIZE, MD_SECTOR_SIZE);
200 static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
202 struct lc_element *al_ext;
203 struct lc_element *tmp;
204 unsigned long al_flags = 0;
206 spin_lock_irq(&mdev->al_lock);
207 tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
208 if (unlikely(tmp != NULL)) {
209 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
210 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
211 spin_unlock_irq(&mdev->al_lock);
215 al_ext = lc_get(mdev->act_log, enr);
216 al_flags = mdev->act_log->flags;
217 spin_unlock_irq(&mdev->al_lock);
221 if (al_flags & LC_STARVING)
222 dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n");
223 if (al_flags & LC_DIRTY)
224 dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n");
231 void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
233 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
234 struct lc_element *al_ext;
235 struct update_al_work al_work;
237 D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
239 trace_drbd_actlog(mdev, sector, "al_begin_io");
241 wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
243 if (al_ext->lc_number != enr) {
244 /* drbd_al_write_transaction(mdev,al_ext,enr);
245 * recurses into generic_make_request(), which
246 * disallows recursion, bios being serialized on the
247 * current->bio_tail list now.
248 * we have to delegate updates to the activity log
249 * to the worker thread. */
250 init_completion(&al_work.event);
251 al_work.al_ext = al_ext;
253 al_work.old_enr = al_ext->lc_number;
254 al_work.w.cb = w_al_write_transaction;
255 drbd_queue_work_front(&mdev->data.work, &al_work.w);
256 wait_for_completion(&al_work.event);
260 spin_lock_irq(&mdev->al_lock);
261 lc_changed(mdev->act_log, al_ext);
262 spin_unlock_irq(&mdev->al_lock);
263 wake_up(&mdev->al_wait);
267 void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
269 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
270 struct lc_element *extent;
273 trace_drbd_actlog(mdev, sector, "al_complete_io");
275 spin_lock_irqsave(&mdev->al_lock, flags);
277 extent = lc_find(mdev->act_log, enr);
280 spin_unlock_irqrestore(&mdev->al_lock, flags);
281 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
285 if (lc_put(mdev->act_log, extent) == 0)
286 wake_up(&mdev->al_wait);
288 spin_unlock_irqrestore(&mdev->al_lock, flags);
292 w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused)
294 struct update_al_work *aw = container_of(w, struct update_al_work, w);
295 struct lc_element *updated = aw->al_ext;
296 const unsigned int new_enr = aw->enr;
297 const unsigned int evicted = aw->old_enr;
298 struct al_transaction *buffer;
301 unsigned int extent_nr;
304 if (!get_ldev(mdev)) {
305 dev_err(DEV, "get_ldev() failed in w_al_write_transaction\n");
306 complete(&((struct update_al_work *)w)->event);
309 /* do we have to do a bitmap write, first?
310 * TODO reduce maximum latency:
311 * submit both bios, then wait for both,
312 * instead of doing two synchronous sector writes. */
313 if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
314 drbd_bm_write_sect(mdev, evicted/AL_EXT_PER_BM_SECT);
316 mutex_lock(&mdev->md_io_mutex); /* protects md_io_page, al_tr_cycle, ... */
317 buffer = (struct al_transaction *)page_address(mdev->md_io_page);
319 buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
320 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
322 n = lc_index_of(mdev->act_log, updated);
324 buffer->updates[0].pos = cpu_to_be32(n);
325 buffer->updates[0].extent = cpu_to_be32(new_enr);
329 mx = min_t(int, AL_EXTENTS_PT,
330 mdev->act_log->nr_elements - mdev->al_tr_cycle);
331 for (i = 0; i < mx; i++) {
332 unsigned idx = mdev->al_tr_cycle + i;
333 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
334 buffer->updates[i+1].pos = cpu_to_be32(idx);
335 buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
336 xor_sum ^= extent_nr;
338 for (; i < AL_EXTENTS_PT; i++) {
339 buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
340 buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
343 mdev->al_tr_cycle += AL_EXTENTS_PT;
344 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
345 mdev->al_tr_cycle = 0;
347 buffer->xor_sum = cpu_to_be32(xor_sum);
349 sector = mdev->ldev->md.md_offset
350 + mdev->ldev->md.al_offset + mdev->al_tr_pos;
352 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
353 drbd_chk_io_error(mdev, 1, TRUE);
355 if (++mdev->al_tr_pos >
356 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
359 D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
360 mdev->al_tr_number++;
362 mutex_unlock(&mdev->md_io_mutex);
364 complete(&((struct update_al_work *)w)->event);
371 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
372 * @mdev: DRBD device.
373 * @bdev: Block device to read form.
374 * @b: pointer to an al_transaction.
375 * @index: On disk slot of the transaction to read.
377 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
379 static int drbd_al_read_tr(struct drbd_conf *mdev,
380 struct drbd_backing_dev *bdev,
381 struct al_transaction *b,
388 sector = bdev->md.md_offset + bdev->md.al_offset + index;
390 /* Dont process error normally,
391 * as this is done before disk is attached! */
392 if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
395 rv = (be32_to_cpu(b->magic) == DRBD_MAGIC);
397 for (i = 0; i < AL_EXTENTS_PT + 1; i++)
398 xor_sum ^= be32_to_cpu(b->updates[i].extent);
399 rv &= (xor_sum == be32_to_cpu(b->xor_sum));
405 * drbd_al_read_log() - Restores the activity log from its on disk representation.
406 * @mdev: DRBD device.
407 * @bdev: Block device to read form.
409 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
411 int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
413 struct al_transaction *buffer;
417 int active_extents = 0;
418 int transactions = 0;
426 mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
428 /* lock out all other meta data io for now,
429 * and make sure the page is mapped.
431 mutex_lock(&mdev->md_io_mutex);
432 buffer = page_address(mdev->md_io_page);
434 /* Find the valid transaction in the log */
435 for (i = 0; i <= mx; i++) {
436 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
440 mutex_unlock(&mdev->md_io_mutex);
443 cnr = be32_to_cpu(buffer->tr_number);
445 if (++found_valid == 1) {
452 if ((int)cnr - (int)from_tnr < 0) {
453 D_ASSERT(from_tnr - cnr + i - from == mx+1);
457 if ((int)cnr - (int)to_tnr > 0) {
458 D_ASSERT(cnr - to_tnr == i - to);
465 dev_warn(DEV, "No usable activity log found.\n");
466 mutex_unlock(&mdev->md_io_mutex);
470 /* Read the valid transactions.
471 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
475 unsigned int extent_nr;
478 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
479 ERR_IF(rv == 0) goto cancel;
481 mutex_unlock(&mdev->md_io_mutex);
485 trn = be32_to_cpu(buffer->tr_number);
487 spin_lock_irq(&mdev->al_lock);
489 /* This loop runs backwards because in the cyclic
490 elements there might be an old version of the
491 updated element (in slot 0). So the element in slot 0
492 can overwrite old versions. */
493 for (j = AL_EXTENTS_PT; j >= 0; j--) {
494 pos = be32_to_cpu(buffer->updates[j].pos);
495 extent_nr = be32_to_cpu(buffer->updates[j].extent);
497 if (extent_nr == LC_FREE)
500 lc_set(mdev->act_log, extent_nr, pos);
503 spin_unlock_irq(&mdev->al_lock);
515 mdev->al_tr_number = to_tnr+1;
516 mdev->al_tr_pos = to;
517 if (++mdev->al_tr_pos >
518 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
521 /* ok, we are done with it */
522 mutex_unlock(&mdev->md_io_mutex);
524 dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
525 transactions, active_extents);
530 static void atodb_endio(struct bio *bio, int error)
532 struct drbd_atodb_wait *wc = bio->bi_private;
533 struct drbd_conf *mdev = wc->mdev;
535 int uptodate = bio_flagged(bio, BIO_UPTODATE);
537 /* strange behavior of some lower level drivers...
538 * fail the request by clearing the uptodate flag,
539 * but do not return any error?! */
540 if (!error && !uptodate)
543 drbd_chk_io_error(mdev, error, TRUE);
544 if (error && wc->error == 0)
547 if (atomic_dec_and_test(&wc->count))
548 complete(&wc->io_done);
550 page = bio->bi_io_vec[0].bv_page;
557 #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
558 /* activity log to on disk bitmap -- prepare bio unless that sector
559 * is already covered by previously prepared bios */
560 static int atodb_prepare_unless_covered(struct drbd_conf *mdev,
563 struct drbd_atodb_wait *wc) __must_hold(local)
567 sector_t on_disk_sector = enr + mdev->ldev->md.md_offset
568 + mdev->ldev->md.bm_offset;
569 unsigned int page_offset = PAGE_SIZE;
574 /* Check if that enr is already covered by an already created bio.
575 * Caution, bios[] is not NULL terminated,
576 * but only initialized to all NULL.
577 * For completely scattered activity log,
578 * the last invocation iterates over all bios,
579 * and finds the last NULL entry.
581 while ((bio = bios[i])) {
582 if (bio->bi_sector == on_disk_sector)
586 /* bios[i] == NULL, the next not yet used slot */
588 /* GFP_KERNEL, we are not in the write-out path */
589 bio = bio_alloc(GFP_KERNEL, 1);
594 const struct bio_vec *prev_bv = bios[i-1]->bi_io_vec;
595 page_offset = prev_bv->bv_offset + prev_bv->bv_len;
596 page = prev_bv->bv_page;
598 if (page_offset == PAGE_SIZE) {
599 page = alloc_page(__GFP_HIGHMEM);
608 drbd_bm_get_lel(mdev, offset,
609 min_t(size_t, S2W(1), drbd_bm_words(mdev) - offset),
610 kmap(page) + page_offset);
613 bio->bi_private = wc;
614 bio->bi_end_io = atodb_endio;
615 bio->bi_bdev = mdev->ldev->md_bdev;
616 bio->bi_sector = on_disk_sector;
618 if (bio_add_page(bio, page, MD_SECTOR_SIZE, page_offset) != MD_SECTOR_SIZE)
621 atomic_inc(&wc->count);
622 /* we already know that we may do this...
623 * get_ldev_if_state(mdev,D_ATTACHING);
624 * just get the extra reference, so that the local_cnt reflects
625 * the number of pending IO requests DRBD at its backing device.
627 atomic_inc(&mdev->local_cnt);
642 * drbd_al_to_on_disk_bm() - * Writes bitmap parts covered by active AL extents
643 * @mdev: DRBD device.
645 * Called when we detach (unconfigure) local storage,
646 * or when we go from R_PRIMARY to R_SECONDARY role.
648 void drbd_al_to_on_disk_bm(struct drbd_conf *mdev)
653 struct drbd_atodb_wait wc;
655 ERR_IF (!get_ldev_if_state(mdev, D_ATTACHING))
656 return; /* sorry, I don't have any act_log etc... */
658 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
660 nr_elements = mdev->act_log->nr_elements;
662 /* GFP_KERNEL, we are not in anyone's write-out path */
663 bios = kzalloc(sizeof(struct bio *) * nr_elements, GFP_KERNEL);
665 goto submit_one_by_one;
667 atomic_set(&wc.count, 0);
668 init_completion(&wc.io_done);
672 for (i = 0; i < nr_elements; i++) {
673 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
676 /* next statement also does atomic_inc wc.count and local_cnt */
677 if (atodb_prepare_unless_covered(mdev, bios,
678 enr/AL_EXT_PER_BM_SECT,
680 goto free_bios_submit_one_by_one;
683 /* unnecessary optimization? */
684 lc_unlock(mdev->act_log);
685 wake_up(&mdev->al_wait);
687 /* all prepared, submit them */
688 for (i = 0; i < nr_elements; i++) {
691 if (FAULT_ACTIVE(mdev, DRBD_FAULT_MD_WR)) {
692 bios[i]->bi_rw = WRITE;
693 bio_endio(bios[i], -EIO);
695 submit_bio(WRITE, bios[i]);
699 drbd_blk_run_queue(bdev_get_queue(mdev->ldev->md_bdev));
701 /* always (try to) flush bitmap to stable storage */
704 /* In case we did not submit a single IO do not wait for
705 * them to complete. ( Because we would wait forever here. )
707 * In case we had IOs and they are already complete, there
708 * is not point in waiting anyways.
709 * Therefore this if () ... */
710 if (atomic_read(&wc.count))
711 wait_for_completion(&wc.io_done);
718 free_bios_submit_one_by_one:
719 /* free everything by calling the endio callback directly. */
720 for (i = 0; i < nr_elements && bios[i]; i++)
721 bio_endio(bios[i], 0);
726 dev_warn(DEV, "Using the slow drbd_al_to_on_disk_bm()\n");
728 for (i = 0; i < mdev->act_log->nr_elements; i++) {
729 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
732 /* Really slow: if we have al-extents 16..19 active,
733 * sector 4 will be written four times! Synchronous! */
734 drbd_bm_write_sect(mdev, enr/AL_EXT_PER_BM_SECT);
737 lc_unlock(mdev->act_log);
738 wake_up(&mdev->al_wait);
743 * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
744 * @mdev: DRBD device.
746 void drbd_al_apply_to_bm(struct drbd_conf *mdev)
749 unsigned long add = 0;
753 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
755 for (i = 0; i < mdev->act_log->nr_elements; i++) {
756 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
759 add += drbd_bm_ALe_set_all(mdev, enr);
762 lc_unlock(mdev->act_log);
763 wake_up(&mdev->al_wait);
765 dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
766 ppsize(ppb, Bit2KB(add)));
769 static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
773 spin_lock_irq(&mdev->al_lock);
774 rv = (al_ext->refcnt == 0);
776 lc_del(mdev->act_log, al_ext);
777 spin_unlock_irq(&mdev->al_lock);
783 * drbd_al_shrink() - Removes all active extents form the activity log
784 * @mdev: DRBD device.
786 * Removes all active extents form the activity log, waiting until
787 * the reference count of each entry dropped to 0 first, of course.
789 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
791 void drbd_al_shrink(struct drbd_conf *mdev)
793 struct lc_element *al_ext;
796 D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
798 for (i = 0; i < mdev->act_log->nr_elements; i++) {
799 al_ext = lc_element_by_index(mdev->act_log, i);
800 if (al_ext->lc_number == LC_FREE)
802 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
805 wake_up(&mdev->al_wait);
808 static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
810 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
812 if (!get_ldev(mdev)) {
813 if (__ratelimit(&drbd_ratelimit_state))
814 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
819 drbd_bm_write_sect(mdev, udw->enr);
824 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
825 switch (mdev->state.conn) {
826 case C_SYNC_SOURCE: case C_SYNC_TARGET:
827 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
828 drbd_resync_finished(mdev);
834 drbd_bcast_sync_progress(mdev);
840 /* ATTENTION. The AL's extents are 4MB each, while the extents in the
841 * resync LRU-cache are 16MB each.
842 * The caller of this function has to hold an get_ldev() reference.
844 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
846 static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
847 int count, int success)
849 struct lc_element *e;
850 struct update_odbm_work *udw;
854 D_ASSERT(atomic_read(&mdev->local_cnt));
856 /* I simply assume that a sector/size pair never crosses
857 * a 16 MB extent border. (Currently this is true...) */
858 enr = BM_SECT_TO_EXT(sector);
860 e = lc_get(mdev->resync, enr);
862 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
863 if (ext->lce.lc_number == enr) {
865 ext->rs_left -= count;
867 ext->rs_failed += count;
868 if (ext->rs_left < ext->rs_failed) {
869 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
870 "rs_failed=%d count=%d\n",
871 (unsigned long long)sector,
872 ext->lce.lc_number, ext->rs_left,
873 ext->rs_failed, count);
876 lc_put(mdev->resync, &ext->lce);
877 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
881 /* Normally this element should be in the cache,
882 * since drbd_rs_begin_io() pulled it already in.
884 * But maybe an application write finished, and we set
885 * something outside the resync lru_cache in sync.
887 int rs_left = drbd_bm_e_weight(mdev, enr);
888 if (ext->flags != 0) {
889 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
891 ext->lce.lc_number, ext->rs_left,
892 ext->flags, enr, rs_left);
895 if (ext->rs_failed) {
896 dev_warn(DEV, "Kicking resync_lru element enr=%u "
897 "out with rs_failed=%d\n",
898 ext->lce.lc_number, ext->rs_failed);
899 set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
901 ext->rs_left = rs_left;
902 ext->rs_failed = success ? 0 : count;
903 lc_changed(mdev->resync, &ext->lce);
905 lc_put(mdev->resync, &ext->lce);
906 /* no race, we are within the al_lock! */
908 if (ext->rs_left == ext->rs_failed) {
911 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
913 udw->enr = ext->lce.lc_number;
914 udw->w.cb = w_update_odbm;
915 drbd_queue_work_front(&mdev->data.work, &udw->w);
917 dev_warn(DEV, "Could not kmalloc an udw\n");
918 set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
922 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
924 mdev->resync->nr_elements,
925 mdev->resync->flags);
929 /* clear the bit corresponding to the piece of storage in question:
930 * size byte of data starting from sector. Only clear a bits of the affected
931 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
933 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
936 void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
937 const char *file, const unsigned int line)
939 /* Is called from worker and receiver context _only_ */
940 unsigned long sbnr, ebnr, lbnr;
941 unsigned long count = 0;
942 sector_t esector, nr_sectors;
946 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
947 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
948 (unsigned long long)sector, size);
951 nr_sectors = drbd_get_capacity(mdev->this_bdev);
952 esector = sector + (size >> 9) - 1;
954 ERR_IF(sector >= nr_sectors) return;
955 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
957 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
959 /* we clear it (in sync).
960 * round up start sector, round down end sector. we make sure we only
961 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
962 if (unlikely(esector < BM_SECT_PER_BIT-1))
964 if (unlikely(esector == (nr_sectors-1)))
967 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
968 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
970 trace_drbd_resync(mdev, TRACE_LVL_METRICS,
971 "drbd_set_in_sync: sector=%llus size=%u sbnr=%lu ebnr=%lu\n",
972 (unsigned long long)sector, size, sbnr, ebnr);
978 * ok, (capacity & 7) != 0 sometimes, but who cares...
979 * we count rs_{total,left} in bits, not sectors.
981 spin_lock_irqsave(&mdev->al_lock, flags);
982 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
984 /* we need the lock for drbd_try_clear_on_disk_bm */
985 if (jiffies - mdev->rs_mark_time > HZ*10) {
986 /* should be rolling marks,
987 * but we estimate only anyways. */
988 if (mdev->rs_mark_left != drbd_bm_total_weight(mdev) &&
989 mdev->state.conn != C_PAUSED_SYNC_T &&
990 mdev->state.conn != C_PAUSED_SYNC_S) {
991 mdev->rs_mark_time = jiffies;
992 mdev->rs_mark_left = drbd_bm_total_weight(mdev);
995 if (get_ldev(mdev)) {
996 drbd_try_clear_on_disk_bm(mdev, sector, count, TRUE);
999 /* just wake_up unconditional now, various lc_chaged(),
1000 * lc_put() in drbd_try_clear_on_disk_bm(). */
1003 spin_unlock_irqrestore(&mdev->al_lock, flags);
1005 wake_up(&mdev->al_wait);
1009 * this is intended to set one request worth of data out of sync.
1010 * affects at least 1 bit,
1011 * and at most 1+DRBD_MAX_SEGMENT_SIZE/BM_BLOCK_SIZE bits.
1013 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
1014 * so this can be _any_ process.
1016 void __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
1017 const char *file, const unsigned int line)
1019 unsigned long sbnr, ebnr, lbnr, flags;
1020 sector_t esector, nr_sectors;
1021 unsigned int enr, count;
1022 struct lc_element *e;
1024 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
1025 dev_err(DEV, "sector: %llus, size: %d\n",
1026 (unsigned long long)sector, size);
1030 if (!get_ldev(mdev))
1031 return; /* no disk, no metadata, no bitmap to set bits in */
1033 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1034 esector = sector + (size >> 9) - 1;
1036 ERR_IF(sector >= nr_sectors)
1038 ERR_IF(esector >= nr_sectors)
1039 esector = (nr_sectors-1);
1041 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1043 /* we set it out of sync,
1044 * we do not need to round anything here */
1045 sbnr = BM_SECT_TO_BIT(sector);
1046 ebnr = BM_SECT_TO_BIT(esector);
1048 trace_drbd_resync(mdev, TRACE_LVL_METRICS,
1049 "drbd_set_out_of_sync: sector=%llus size=%u sbnr=%lu ebnr=%lu\n",
1050 (unsigned long long)sector, size, sbnr, ebnr);
1052 /* ok, (capacity & 7) != 0 sometimes, but who cares...
1053 * we count rs_{total,left} in bits, not sectors. */
1054 spin_lock_irqsave(&mdev->al_lock, flags);
1055 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
1057 enr = BM_SECT_TO_EXT(sector);
1058 e = lc_find(mdev->resync, enr);
1060 lc_entry(e, struct bm_extent, lce)->rs_left += count;
1061 spin_unlock_irqrestore(&mdev->al_lock, flags);
1068 struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
1070 struct lc_element *e;
1071 struct bm_extent *bm_ext;
1073 unsigned long rs_flags;
1075 spin_lock_irq(&mdev->al_lock);
1076 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
1077 spin_unlock_irq(&mdev->al_lock);
1080 e = lc_get(mdev->resync, enr);
1081 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1083 if (bm_ext->lce.lc_number != enr) {
1084 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1085 bm_ext->rs_failed = 0;
1086 lc_changed(mdev->resync, &bm_ext->lce);
1089 if (bm_ext->lce.refcnt == 1)
1090 mdev->resync_locked++;
1091 set_bit(BME_NO_WRITES, &bm_ext->flags);
1093 rs_flags = mdev->resync->flags;
1094 spin_unlock_irq(&mdev->al_lock);
1096 wake_up(&mdev->al_wait);
1099 if (rs_flags & LC_STARVING)
1100 dev_warn(DEV, "Have to wait for element"
1101 " (resync LRU too small?)\n");
1102 BUG_ON(rs_flags & LC_DIRTY);
1108 static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1110 struct lc_element *al_ext;
1113 spin_lock_irq(&mdev->al_lock);
1114 if (unlikely(enr == mdev->act_log->new_number))
1117 al_ext = lc_find(mdev->act_log, enr);
1123 spin_unlock_irq(&mdev->al_lock);
1127 dev_info(DEV, "Delaying sync read until app's write is done\n");
1134 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1135 * @mdev: DRBD device.
1136 * @sector: The sector number.
1138 * This functions sleeps on al_wait. Returns 1 on success, 0 if interrupted.
1140 int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1142 unsigned int enr = BM_SECT_TO_EXT(sector);
1143 struct bm_extent *bm_ext;
1146 trace_drbd_resync(mdev, TRACE_LVL_ALL,
1147 "drbd_rs_begin_io: sector=%llus (rs_end=%d)\n",
1148 (unsigned long long)sector, enr);
1150 sig = wait_event_interruptible(mdev->al_wait,
1151 (bm_ext = _bme_get(mdev, enr)));
1155 if (test_bit(BME_LOCKED, &bm_ext->flags))
1158 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1159 sig = wait_event_interruptible(mdev->al_wait,
1160 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i));
1162 spin_lock_irq(&mdev->al_lock);
1163 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1164 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1165 mdev->resync_locked--;
1166 wake_up(&mdev->al_wait);
1168 spin_unlock_irq(&mdev->al_lock);
1173 set_bit(BME_LOCKED, &bm_ext->flags);
1179 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1180 * @mdev: DRBD device.
1181 * @sector: The sector number.
1183 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1184 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1185 * if there is still application IO going on in this area.
1187 int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1189 unsigned int enr = BM_SECT_TO_EXT(sector);
1190 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1191 struct lc_element *e;
1192 struct bm_extent *bm_ext;
1195 trace_drbd_resync(mdev, TRACE_LVL_ALL, "drbd_try_rs_begin_io: sector=%llus\n",
1196 (unsigned long long)sector);
1198 spin_lock_irq(&mdev->al_lock);
1199 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1200 /* in case you have very heavy scattered io, it may
1201 * stall the syncer undefined if we give up the ref count
1202 * when we try again and requeue.
1204 * if we don't give up the refcount, but the next time
1205 * we are scheduled this extent has been "synced" by new
1206 * application writes, we'd miss the lc_put on the
1207 * extent we keep the refcount on.
1208 * so we remembered which extent we had to try again, and
1209 * if the next requested one is something else, we do
1210 * the lc_put here...
1211 * we also have to wake_up
1214 trace_drbd_resync(mdev, TRACE_LVL_ALL,
1215 "dropping %u, apparently got 'synced' by application io\n",
1218 e = lc_find(mdev->resync, mdev->resync_wenr);
1219 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1221 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1222 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1223 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1224 mdev->resync_wenr = LC_FREE;
1225 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1226 mdev->resync_locked--;
1227 wake_up(&mdev->al_wait);
1229 dev_alert(DEV, "LOGIC BUG\n");
1233 e = lc_try_get(mdev->resync, enr);
1234 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1236 if (test_bit(BME_LOCKED, &bm_ext->flags))
1238 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1239 mdev->resync_locked++;
1241 /* we did set the BME_NO_WRITES,
1242 * but then could not set BME_LOCKED,
1243 * so we tried again.
1244 * drop the extra reference. */
1245 trace_drbd_resync(mdev, TRACE_LVL_ALL,
1246 "dropping extra reference on %u\n", enr);
1248 bm_ext->lce.refcnt--;
1249 D_ASSERT(bm_ext->lce.refcnt > 0);
1253 /* do we rather want to try later? */
1254 if (mdev->resync_locked > mdev->resync->nr_elements-3) {
1255 trace_drbd_resync(mdev, TRACE_LVL_ALL,
1256 "resync_locked = %u!\n", mdev->resync_locked);
1260 /* Do or do not. There is no try. -- Yoda */
1261 e = lc_get(mdev->resync, enr);
1262 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1264 const unsigned long rs_flags = mdev->resync->flags;
1265 if (rs_flags & LC_STARVING)
1266 dev_warn(DEV, "Have to wait for element"
1267 " (resync LRU too small?)\n");
1268 BUG_ON(rs_flags & LC_DIRTY);
1271 if (bm_ext->lce.lc_number != enr) {
1272 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1273 bm_ext->rs_failed = 0;
1274 lc_changed(mdev->resync, &bm_ext->lce);
1275 wake_up(&mdev->al_wait);
1276 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1278 set_bit(BME_NO_WRITES, &bm_ext->flags);
1279 D_ASSERT(bm_ext->lce.refcnt == 1);
1280 mdev->resync_locked++;
1284 trace_drbd_resync(mdev, TRACE_LVL_ALL, "checking al for %u\n", enr);
1286 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1287 if (unlikely(al_enr+i == mdev->act_log->new_number))
1289 if (lc_is_used(mdev->act_log, al_enr+i))
1292 set_bit(BME_LOCKED, &bm_ext->flags);
1294 mdev->resync_wenr = LC_FREE;
1295 spin_unlock_irq(&mdev->al_lock);
1299 trace_drbd_resync(mdev, TRACE_LVL_ALL, "need to try again for %u\n", enr);
1301 mdev->resync_wenr = enr;
1302 spin_unlock_irq(&mdev->al_lock);
1306 void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1308 unsigned int enr = BM_SECT_TO_EXT(sector);
1309 struct lc_element *e;
1310 struct bm_extent *bm_ext;
1311 unsigned long flags;
1313 trace_drbd_resync(mdev, TRACE_LVL_ALL,
1314 "drbd_rs_complete_io: sector=%llus (rs_enr=%d)\n",
1315 (long long)sector, enr);
1317 spin_lock_irqsave(&mdev->al_lock, flags);
1318 e = lc_find(mdev->resync, enr);
1319 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1321 spin_unlock_irqrestore(&mdev->al_lock, flags);
1322 if (__ratelimit(&drbd_ratelimit_state))
1323 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1327 if (bm_ext->lce.refcnt == 0) {
1328 spin_unlock_irqrestore(&mdev->al_lock, flags);
1329 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1330 "but refcnt is 0!?\n",
1331 (unsigned long long)sector, enr);
1335 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1336 clear_bit(BME_LOCKED, &bm_ext->flags);
1337 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1338 mdev->resync_locked--;
1339 wake_up(&mdev->al_wait);
1342 spin_unlock_irqrestore(&mdev->al_lock, flags);
1346 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1347 * @mdev: DRBD device.
1349 void drbd_rs_cancel_all(struct drbd_conf *mdev)
1351 trace_drbd_resync(mdev, TRACE_LVL_METRICS, "drbd_rs_cancel_all\n");
1353 spin_lock_irq(&mdev->al_lock);
1355 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1356 lc_reset(mdev->resync);
1359 mdev->resync_locked = 0;
1360 mdev->resync_wenr = LC_FREE;
1361 spin_unlock_irq(&mdev->al_lock);
1362 wake_up(&mdev->al_wait);
1366 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1367 * @mdev: DRBD device.
1369 * Returns 0 upon success, -EAGAIN if at least one reference count was
1372 int drbd_rs_del_all(struct drbd_conf *mdev)
1374 struct lc_element *e;
1375 struct bm_extent *bm_ext;
1378 trace_drbd_resync(mdev, TRACE_LVL_METRICS, "drbd_rs_del_all\n");
1380 spin_lock_irq(&mdev->al_lock);
1382 if (get_ldev_if_state(mdev, D_FAILED)) {
1383 /* ok, ->resync is there. */
1384 for (i = 0; i < mdev->resync->nr_elements; i++) {
1385 e = lc_element_by_index(mdev->resync, i);
1386 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1387 if (bm_ext->lce.lc_number == LC_FREE)
1389 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1390 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1391 " got 'synced' by application io\n",
1393 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1394 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1395 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1396 mdev->resync_wenr = LC_FREE;
1397 lc_put(mdev->resync, &bm_ext->lce);
1399 if (bm_ext->lce.refcnt != 0) {
1400 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1401 "refcnt=%d\n", bm_ext->lce.refcnt);
1403 spin_unlock_irq(&mdev->al_lock);
1406 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1407 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1408 lc_del(mdev->resync, &bm_ext->lce);
1410 D_ASSERT(mdev->resync->used == 0);
1413 spin_unlock_irq(&mdev->al_lock);
1419 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1420 * @mdev: DRBD device.
1421 * @sector: The sector number.
1422 * @size: Size of failed IO operation, in byte.
1424 void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1426 /* Is called from worker and receiver context _only_ */
1427 unsigned long sbnr, ebnr, lbnr;
1428 unsigned long count;
1429 sector_t esector, nr_sectors;
1432 trace_drbd_resync(mdev, TRACE_LVL_SUMMARY,
1433 "drbd_rs_failed_io: sector=%llus, size=%u\n",
1434 (unsigned long long)sector, size);
1436 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
1437 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1438 (unsigned long long)sector, size);
1441 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1442 esector = sector + (size >> 9) - 1;
1444 ERR_IF(sector >= nr_sectors) return;
1445 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
1447 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1450 * round up start sector, round down end sector. we make sure we only
1451 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1452 if (unlikely(esector < BM_SECT_PER_BIT-1))
1454 if (unlikely(esector == (nr_sectors-1)))
1457 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1458 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1464 * ok, (capacity & 7) != 0 sometimes, but who cares...
1465 * we count rs_{total,left} in bits, not sectors.
1467 spin_lock_irq(&mdev->al_lock);
1468 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1470 mdev->rs_failed += count;
1472 if (get_ldev(mdev)) {
1473 drbd_try_clear_on_disk_bm(mdev, sector, count, FALSE);
1477 /* just wake_up unconditional now, various lc_chaged(),
1478 * lc_put() in drbd_try_clear_on_disk_bm(). */
1481 spin_unlock_irq(&mdev->al_lock);
1483 wake_up(&mdev->al_wait);