FRV: Use generic show_interrupts()
[cris-mirror.git] / drivers / block / drbd / drbd_actlog.c
blobaca302492ff20a25e65ac645a601dbf51ada2f33
1 /*
2 drbd_actlog.c
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)
13 any later version.
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>
28 #include "drbd_int.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 {
36 u32 magic;
37 u32 tr_number;
38 struct __packed {
39 u32 pos;
40 u32 extent; } updates[1 + AL_EXTENTS_PT];
41 u32 xor_sum;
44 struct update_odbm_work {
45 struct drbd_work w;
46 unsigned int enr;
49 struct update_al_work {
50 struct drbd_work w;
51 struct lc_element *al_ext;
52 struct completion event;
53 unsigned int enr;
54 /* if old_enr != LC_FREE, write corresponding bitmap sector, too */
55 unsigned int old_enr;
58 struct drbd_atodb_wait {
59 atomic_t count;
60 struct completion io_done;
61 struct drbd_conf *mdev;
62 int error;
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,
71 int rw, int size)
73 struct bio *bio;
74 struct drbd_md_io md_io;
75 int ok;
77 md_io.mdev = mdev;
78 init_completion(&md_io.event);
79 md_io.error = 0;
81 if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
82 rw |= REQ_FUA;
83 rw |= REQ_SYNC;
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);
89 if (!ok)
90 goto out;
91 bio->bi_private = &md_io;
92 bio->bi_end_io = drbd_md_io_complete;
93 bio->bi_rw = rw;
95 if (FAULT_ACTIVE(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
96 bio_endio(bio, -EIO);
97 else
98 submit_bio(rw, bio);
99 wait_for_completion(&md_io.event);
100 ok = bio_flagged(bio, BIO_UPTODATE) && md_io.error == 0;
102 out:
103 bio_put(bio);
104 return ok;
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;
111 int offset = 0;
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;
131 if (rw & WRITE) {
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);
140 if (unlikely(!ok)) {
141 dev_err(DEV, "drbd_md_sync_page_io(,%llus,"
142 "READ [logical_block_size!=512]) failed!\n",
143 (unsigned long long)sector);
144 return 0;
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);
158 if (unlikely(!ok)) {
159 dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
160 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
161 return 0;
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);
171 return ok;
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 spin_unlock_irq(&mdev->al_lock);
186 return NULL;
189 al_ext = lc_get(mdev->act_log, enr);
190 al_flags = mdev->act_log->flags;
191 spin_unlock_irq(&mdev->al_lock);
194 if (!al_ext) {
195 if (al_flags & LC_STARVING)
196 dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n");
197 if (al_flags & LC_DIRTY)
198 dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n");
202 return al_ext;
205 void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
207 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
208 struct lc_element *al_ext;
209 struct update_al_work al_work;
211 D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
213 wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
215 if (al_ext->lc_number != enr) {
216 /* drbd_al_write_transaction(mdev,al_ext,enr);
217 * recurses into generic_make_request(), which
218 * disallows recursion, bios being serialized on the
219 * current->bio_tail list now.
220 * we have to delegate updates to the activity log
221 * to the worker thread. */
222 init_completion(&al_work.event);
223 al_work.al_ext = al_ext;
224 al_work.enr = enr;
225 al_work.old_enr = al_ext->lc_number;
226 al_work.w.cb = w_al_write_transaction;
227 drbd_queue_work_front(&mdev->data.work, &al_work.w);
228 wait_for_completion(&al_work.event);
230 mdev->al_writ_cnt++;
232 spin_lock_irq(&mdev->al_lock);
233 lc_changed(mdev->act_log, al_ext);
234 spin_unlock_irq(&mdev->al_lock);
235 wake_up(&mdev->al_wait);
239 void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
241 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
242 struct lc_element *extent;
243 unsigned long flags;
245 spin_lock_irqsave(&mdev->al_lock, flags);
247 extent = lc_find(mdev->act_log, enr);
249 if (!extent) {
250 spin_unlock_irqrestore(&mdev->al_lock, flags);
251 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
252 return;
255 if (lc_put(mdev->act_log, extent) == 0)
256 wake_up(&mdev->al_wait);
258 spin_unlock_irqrestore(&mdev->al_lock, flags);
262 w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused)
264 struct update_al_work *aw = container_of(w, struct update_al_work, w);
265 struct lc_element *updated = aw->al_ext;
266 const unsigned int new_enr = aw->enr;
267 const unsigned int evicted = aw->old_enr;
268 struct al_transaction *buffer;
269 sector_t sector;
270 int i, n, mx;
271 unsigned int extent_nr;
272 u32 xor_sum = 0;
274 if (!get_ldev(mdev)) {
275 dev_err(DEV,
276 "disk is %s, cannot start al transaction (-%d +%d)\n",
277 drbd_disk_str(mdev->state.disk), evicted, new_enr);
278 complete(&((struct update_al_work *)w)->event);
279 return 1;
281 /* do we have to do a bitmap write, first?
282 * TODO reduce maximum latency:
283 * submit both bios, then wait for both,
284 * instead of doing two synchronous sector writes.
285 * For now, we must not write the transaction,
286 * if we cannot write out the bitmap of the evicted extent. */
287 if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
288 drbd_bm_write_sect(mdev, evicted/AL_EXT_PER_BM_SECT);
290 /* The bitmap write may have failed, causing a state change. */
291 if (mdev->state.disk < D_INCONSISTENT) {
292 dev_err(DEV,
293 "disk is %s, cannot write al transaction (-%d +%d)\n",
294 drbd_disk_str(mdev->state.disk), evicted, new_enr);
295 complete(&((struct update_al_work *)w)->event);
296 put_ldev(mdev);
297 return 1;
300 mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
301 buffer = (struct al_transaction *)page_address(mdev->md_io_page);
303 buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
304 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
306 n = lc_index_of(mdev->act_log, updated);
308 buffer->updates[0].pos = cpu_to_be32(n);
309 buffer->updates[0].extent = cpu_to_be32(new_enr);
311 xor_sum ^= new_enr;
313 mx = min_t(int, AL_EXTENTS_PT,
314 mdev->act_log->nr_elements - mdev->al_tr_cycle);
315 for (i = 0; i < mx; i++) {
316 unsigned idx = mdev->al_tr_cycle + i;
317 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
318 buffer->updates[i+1].pos = cpu_to_be32(idx);
319 buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
320 xor_sum ^= extent_nr;
322 for (; i < AL_EXTENTS_PT; i++) {
323 buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
324 buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
325 xor_sum ^= LC_FREE;
327 mdev->al_tr_cycle += AL_EXTENTS_PT;
328 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
329 mdev->al_tr_cycle = 0;
331 buffer->xor_sum = cpu_to_be32(xor_sum);
333 sector = mdev->ldev->md.md_offset
334 + mdev->ldev->md.al_offset + mdev->al_tr_pos;
336 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
337 drbd_chk_io_error(mdev, 1, TRUE);
339 if (++mdev->al_tr_pos >
340 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
341 mdev->al_tr_pos = 0;
343 D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
344 mdev->al_tr_number++;
346 mutex_unlock(&mdev->md_io_mutex);
348 complete(&((struct update_al_work *)w)->event);
349 put_ldev(mdev);
351 return 1;
355 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
356 * @mdev: DRBD device.
357 * @bdev: Block device to read form.
358 * @b: pointer to an al_transaction.
359 * @index: On disk slot of the transaction to read.
361 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
363 static int drbd_al_read_tr(struct drbd_conf *mdev,
364 struct drbd_backing_dev *bdev,
365 struct al_transaction *b,
366 int index)
368 sector_t sector;
369 int rv, i;
370 u32 xor_sum = 0;
372 sector = bdev->md.md_offset + bdev->md.al_offset + index;
374 /* Dont process error normally,
375 * as this is done before disk is attached! */
376 if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
377 return -1;
379 rv = (be32_to_cpu(b->magic) == DRBD_MAGIC);
381 for (i = 0; i < AL_EXTENTS_PT + 1; i++)
382 xor_sum ^= be32_to_cpu(b->updates[i].extent);
383 rv &= (xor_sum == be32_to_cpu(b->xor_sum));
385 return rv;
389 * drbd_al_read_log() - Restores the activity log from its on disk representation.
390 * @mdev: DRBD device.
391 * @bdev: Block device to read form.
393 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
395 int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
397 struct al_transaction *buffer;
398 int i;
399 int rv;
400 int mx;
401 int active_extents = 0;
402 int transactions = 0;
403 int found_valid = 0;
404 int from = 0;
405 int to = 0;
406 u32 from_tnr = 0;
407 u32 to_tnr = 0;
408 u32 cnr;
410 mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
412 /* lock out all other meta data io for now,
413 * and make sure the page is mapped.
415 mutex_lock(&mdev->md_io_mutex);
416 buffer = page_address(mdev->md_io_page);
418 /* Find the valid transaction in the log */
419 for (i = 0; i <= mx; i++) {
420 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
421 if (rv == 0)
422 continue;
423 if (rv == -1) {
424 mutex_unlock(&mdev->md_io_mutex);
425 return 0;
427 cnr = be32_to_cpu(buffer->tr_number);
429 if (++found_valid == 1) {
430 from = i;
431 to = i;
432 from_tnr = cnr;
433 to_tnr = cnr;
434 continue;
436 if ((int)cnr - (int)from_tnr < 0) {
437 D_ASSERT(from_tnr - cnr + i - from == mx+1);
438 from = i;
439 from_tnr = cnr;
441 if ((int)cnr - (int)to_tnr > 0) {
442 D_ASSERT(cnr - to_tnr == i - to);
443 to = i;
444 to_tnr = cnr;
448 if (!found_valid) {
449 dev_warn(DEV, "No usable activity log found.\n");
450 mutex_unlock(&mdev->md_io_mutex);
451 return 1;
454 /* Read the valid transactions.
455 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
456 i = from;
457 while (1) {
458 int j, pos;
459 unsigned int extent_nr;
460 unsigned int trn;
462 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
463 ERR_IF(rv == 0) goto cancel;
464 if (rv == -1) {
465 mutex_unlock(&mdev->md_io_mutex);
466 return 0;
469 trn = be32_to_cpu(buffer->tr_number);
471 spin_lock_irq(&mdev->al_lock);
473 /* This loop runs backwards because in the cyclic
474 elements there might be an old version of the
475 updated element (in slot 0). So the element in slot 0
476 can overwrite old versions. */
477 for (j = AL_EXTENTS_PT; j >= 0; j--) {
478 pos = be32_to_cpu(buffer->updates[j].pos);
479 extent_nr = be32_to_cpu(buffer->updates[j].extent);
481 if (extent_nr == LC_FREE)
482 continue;
484 lc_set(mdev->act_log, extent_nr, pos);
485 active_extents++;
487 spin_unlock_irq(&mdev->al_lock);
489 transactions++;
491 cancel:
492 if (i == to)
493 break;
494 i++;
495 if (i > mx)
496 i = 0;
499 mdev->al_tr_number = to_tnr+1;
500 mdev->al_tr_pos = to;
501 if (++mdev->al_tr_pos >
502 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
503 mdev->al_tr_pos = 0;
505 /* ok, we are done with it */
506 mutex_unlock(&mdev->md_io_mutex);
508 dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
509 transactions, active_extents);
511 return 1;
514 static void atodb_endio(struct bio *bio, int error)
516 struct drbd_atodb_wait *wc = bio->bi_private;
517 struct drbd_conf *mdev = wc->mdev;
518 struct page *page;
519 int uptodate = bio_flagged(bio, BIO_UPTODATE);
521 /* strange behavior of some lower level drivers...
522 * fail the request by clearing the uptodate flag,
523 * but do not return any error?! */
524 if (!error && !uptodate)
525 error = -EIO;
527 drbd_chk_io_error(mdev, error, TRUE);
528 if (error && wc->error == 0)
529 wc->error = error;
531 if (atomic_dec_and_test(&wc->count))
532 complete(&wc->io_done);
534 page = bio->bi_io_vec[0].bv_page;
535 put_page(page);
536 bio_put(bio);
537 mdev->bm_writ_cnt++;
538 put_ldev(mdev);
541 /* sector to word */
542 #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
544 /* activity log to on disk bitmap -- prepare bio unless that sector
545 * is already covered by previously prepared bios */
546 static int atodb_prepare_unless_covered(struct drbd_conf *mdev,
547 struct bio **bios,
548 unsigned int enr,
549 struct drbd_atodb_wait *wc) __must_hold(local)
551 struct bio *bio;
552 struct page *page;
553 sector_t on_disk_sector;
554 unsigned int page_offset = PAGE_SIZE;
555 int offset;
556 int i = 0;
557 int err = -ENOMEM;
559 /* We always write aligned, full 4k blocks,
560 * so we can ignore the logical_block_size (for now) */
561 enr &= ~7U;
562 on_disk_sector = enr + mdev->ldev->md.md_offset
563 + mdev->ldev->md.bm_offset;
565 D_ASSERT(!(on_disk_sector & 7U));
567 /* Check if that enr is already covered by an already created bio.
568 * Caution, bios[] is not NULL terminated,
569 * but only initialized to all NULL.
570 * For completely scattered activity log,
571 * the last invocation iterates over all bios,
572 * and finds the last NULL entry.
574 while ((bio = bios[i])) {
575 if (bio->bi_sector == on_disk_sector)
576 return 0;
577 i++;
579 /* bios[i] == NULL, the next not yet used slot */
581 /* GFP_KERNEL, we are not in the write-out path */
582 bio = bio_alloc(GFP_KERNEL, 1);
583 if (bio == NULL)
584 return -ENOMEM;
586 if (i > 0) {
587 const struct bio_vec *prev_bv = bios[i-1]->bi_io_vec;
588 page_offset = prev_bv->bv_offset + prev_bv->bv_len;
589 page = prev_bv->bv_page;
591 if (page_offset == PAGE_SIZE) {
592 page = alloc_page(__GFP_HIGHMEM);
593 if (page == NULL)
594 goto out_bio_put;
595 page_offset = 0;
596 } else {
597 get_page(page);
600 offset = S2W(enr);
601 drbd_bm_get_lel(mdev, offset,
602 min_t(size_t, S2W(8), drbd_bm_words(mdev) - offset),
603 kmap(page) + page_offset);
604 kunmap(page);
606 bio->bi_private = wc;
607 bio->bi_end_io = atodb_endio;
608 bio->bi_bdev = mdev->ldev->md_bdev;
609 bio->bi_sector = on_disk_sector;
611 if (bio_add_page(bio, page, 4096, page_offset) != 4096)
612 goto out_put_page;
614 atomic_inc(&wc->count);
615 /* we already know that we may do this...
616 * get_ldev_if_state(mdev,D_ATTACHING);
617 * just get the extra reference, so that the local_cnt reflects
618 * the number of pending IO requests DRBD at its backing device.
620 atomic_inc(&mdev->local_cnt);
622 bios[i] = bio;
624 return 0;
626 out_put_page:
627 err = -EINVAL;
628 put_page(page);
629 out_bio_put:
630 bio_put(bio);
631 return err;
635 * drbd_al_to_on_disk_bm() - * Writes bitmap parts covered by active AL extents
636 * @mdev: DRBD device.
638 * Called when we detach (unconfigure) local storage,
639 * or when we go from R_PRIMARY to R_SECONDARY role.
641 void drbd_al_to_on_disk_bm(struct drbd_conf *mdev)
643 int i, nr_elements;
644 unsigned int enr;
645 struct bio **bios;
646 struct drbd_atodb_wait wc;
648 ERR_IF (!get_ldev_if_state(mdev, D_ATTACHING))
649 return; /* sorry, I don't have any act_log etc... */
651 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
653 nr_elements = mdev->act_log->nr_elements;
655 /* GFP_KERNEL, we are not in anyone's write-out path */
656 bios = kzalloc(sizeof(struct bio *) * nr_elements, GFP_KERNEL);
657 if (!bios)
658 goto submit_one_by_one;
660 atomic_set(&wc.count, 0);
661 init_completion(&wc.io_done);
662 wc.mdev = mdev;
663 wc.error = 0;
665 for (i = 0; i < nr_elements; i++) {
666 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
667 if (enr == LC_FREE)
668 continue;
669 /* next statement also does atomic_inc wc.count and local_cnt */
670 if (atodb_prepare_unless_covered(mdev, bios,
671 enr/AL_EXT_PER_BM_SECT,
672 &wc))
673 goto free_bios_submit_one_by_one;
676 /* unnecessary optimization? */
677 lc_unlock(mdev->act_log);
678 wake_up(&mdev->al_wait);
680 /* all prepared, submit them */
681 for (i = 0; i < nr_elements; i++) {
682 if (bios[i] == NULL)
683 break;
684 if (FAULT_ACTIVE(mdev, DRBD_FAULT_MD_WR)) {
685 bios[i]->bi_rw = WRITE;
686 bio_endio(bios[i], -EIO);
687 } else {
688 submit_bio(WRITE, bios[i]);
692 /* always (try to) flush bitmap to stable storage */
693 drbd_md_flush(mdev);
695 /* In case we did not submit a single IO do not wait for
696 * them to complete. ( Because we would wait forever here. )
698 * In case we had IOs and they are already complete, there
699 * is not point in waiting anyways.
700 * Therefore this if () ... */
701 if (atomic_read(&wc.count))
702 wait_for_completion(&wc.io_done);
704 put_ldev(mdev);
706 kfree(bios);
707 return;
709 free_bios_submit_one_by_one:
710 /* free everything by calling the endio callback directly. */
711 for (i = 0; i < nr_elements && bios[i]; i++)
712 bio_endio(bios[i], 0);
714 kfree(bios);
716 submit_one_by_one:
717 dev_warn(DEV, "Using the slow drbd_al_to_on_disk_bm()\n");
719 for (i = 0; i < mdev->act_log->nr_elements; i++) {
720 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
721 if (enr == LC_FREE)
722 continue;
723 /* Really slow: if we have al-extents 16..19 active,
724 * sector 4 will be written four times! Synchronous! */
725 drbd_bm_write_sect(mdev, enr/AL_EXT_PER_BM_SECT);
728 lc_unlock(mdev->act_log);
729 wake_up(&mdev->al_wait);
730 put_ldev(mdev);
734 * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
735 * @mdev: DRBD device.
737 void drbd_al_apply_to_bm(struct drbd_conf *mdev)
739 unsigned int enr;
740 unsigned long add = 0;
741 char ppb[10];
742 int i, tmp;
744 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
746 for (i = 0; i < mdev->act_log->nr_elements; i++) {
747 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
748 if (enr == LC_FREE)
749 continue;
750 tmp = drbd_bm_ALe_set_all(mdev, enr);
751 dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
752 add += tmp;
755 lc_unlock(mdev->act_log);
756 wake_up(&mdev->al_wait);
758 dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
759 ppsize(ppb, Bit2KB(add)));
762 static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
764 int rv;
766 spin_lock_irq(&mdev->al_lock);
767 rv = (al_ext->refcnt == 0);
768 if (likely(rv))
769 lc_del(mdev->act_log, al_ext);
770 spin_unlock_irq(&mdev->al_lock);
772 return rv;
776 * drbd_al_shrink() - Removes all active extents form the activity log
777 * @mdev: DRBD device.
779 * Removes all active extents form the activity log, waiting until
780 * the reference count of each entry dropped to 0 first, of course.
782 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
784 void drbd_al_shrink(struct drbd_conf *mdev)
786 struct lc_element *al_ext;
787 int i;
789 D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
791 for (i = 0; i < mdev->act_log->nr_elements; i++) {
792 al_ext = lc_element_by_index(mdev->act_log, i);
793 if (al_ext->lc_number == LC_FREE)
794 continue;
795 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
798 wake_up(&mdev->al_wait);
801 static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
803 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
805 if (!get_ldev(mdev)) {
806 if (__ratelimit(&drbd_ratelimit_state))
807 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
808 kfree(udw);
809 return 1;
812 drbd_bm_write_sect(mdev, udw->enr);
813 put_ldev(mdev);
815 kfree(udw);
817 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
818 switch (mdev->state.conn) {
819 case C_SYNC_SOURCE: case C_SYNC_TARGET:
820 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
821 drbd_resync_finished(mdev);
822 default:
823 /* nothing to do */
824 break;
827 drbd_bcast_sync_progress(mdev);
829 return 1;
833 /* ATTENTION. The AL's extents are 4MB each, while the extents in the
834 * resync LRU-cache are 16MB each.
835 * The caller of this function has to hold an get_ldev() reference.
837 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
839 static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
840 int count, int success)
842 struct lc_element *e;
843 struct update_odbm_work *udw;
845 unsigned int enr;
847 D_ASSERT(atomic_read(&mdev->local_cnt));
849 /* I simply assume that a sector/size pair never crosses
850 * a 16 MB extent border. (Currently this is true...) */
851 enr = BM_SECT_TO_EXT(sector);
853 e = lc_get(mdev->resync, enr);
854 if (e) {
855 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
856 if (ext->lce.lc_number == enr) {
857 if (success)
858 ext->rs_left -= count;
859 else
860 ext->rs_failed += count;
861 if (ext->rs_left < ext->rs_failed) {
862 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
863 "rs_failed=%d count=%d\n",
864 (unsigned long long)sector,
865 ext->lce.lc_number, ext->rs_left,
866 ext->rs_failed, count);
867 dump_stack();
869 lc_put(mdev->resync, &ext->lce);
870 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
871 return;
873 } else {
874 /* Normally this element should be in the cache,
875 * since drbd_rs_begin_io() pulled it already in.
877 * But maybe an application write finished, and we set
878 * something outside the resync lru_cache in sync.
880 int rs_left = drbd_bm_e_weight(mdev, enr);
881 if (ext->flags != 0) {
882 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
883 " -> %d[%u;00]\n",
884 ext->lce.lc_number, ext->rs_left,
885 ext->flags, enr, rs_left);
886 ext->flags = 0;
888 if (ext->rs_failed) {
889 dev_warn(DEV, "Kicking resync_lru element enr=%u "
890 "out with rs_failed=%d\n",
891 ext->lce.lc_number, ext->rs_failed);
892 set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
894 ext->rs_left = rs_left;
895 ext->rs_failed = success ? 0 : count;
896 lc_changed(mdev->resync, &ext->lce);
898 lc_put(mdev->resync, &ext->lce);
899 /* no race, we are within the al_lock! */
901 if (ext->rs_left == ext->rs_failed) {
902 ext->rs_failed = 0;
904 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
905 if (udw) {
906 udw->enr = ext->lce.lc_number;
907 udw->w.cb = w_update_odbm;
908 drbd_queue_work_front(&mdev->data.work, &udw->w);
909 } else {
910 dev_warn(DEV, "Could not kmalloc an udw\n");
911 set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags);
914 } else {
915 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
916 mdev->resync_locked,
917 mdev->resync->nr_elements,
918 mdev->resync->flags);
922 /* clear the bit corresponding to the piece of storage in question:
923 * size byte of data starting from sector. Only clear a bits of the affected
924 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
926 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
929 void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
930 const char *file, const unsigned int line)
932 /* Is called from worker and receiver context _only_ */
933 unsigned long sbnr, ebnr, lbnr;
934 unsigned long count = 0;
935 sector_t esector, nr_sectors;
936 int wake_up = 0;
937 unsigned long flags;
939 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
940 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
941 (unsigned long long)sector, size);
942 return;
944 nr_sectors = drbd_get_capacity(mdev->this_bdev);
945 esector = sector + (size >> 9) - 1;
947 ERR_IF(sector >= nr_sectors) return;
948 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
950 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
952 /* we clear it (in sync).
953 * round up start sector, round down end sector. we make sure we only
954 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
955 if (unlikely(esector < BM_SECT_PER_BIT-1))
956 return;
957 if (unlikely(esector == (nr_sectors-1)))
958 ebnr = lbnr;
959 else
960 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
961 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
963 if (sbnr > ebnr)
964 return;
967 * ok, (capacity & 7) != 0 sometimes, but who cares...
968 * we count rs_{total,left} in bits, not sectors.
970 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
971 if (count && get_ldev(mdev)) {
972 unsigned long now = jiffies;
973 unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
974 int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
975 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
976 unsigned long tw = drbd_bm_total_weight(mdev);
977 if (mdev->rs_mark_left[mdev->rs_last_mark] != tw &&
978 mdev->state.conn != C_PAUSED_SYNC_T &&
979 mdev->state.conn != C_PAUSED_SYNC_S) {
980 mdev->rs_mark_time[next] = now;
981 mdev->rs_mark_left[next] = tw;
982 mdev->rs_last_mark = next;
985 spin_lock_irqsave(&mdev->al_lock, flags);
986 drbd_try_clear_on_disk_bm(mdev, sector, count, TRUE);
987 spin_unlock_irqrestore(&mdev->al_lock, flags);
989 /* just wake_up unconditional now, various lc_chaged(),
990 * lc_put() in drbd_try_clear_on_disk_bm(). */
991 wake_up = 1;
992 put_ldev(mdev);
994 if (wake_up)
995 wake_up(&mdev->al_wait);
999 * this is intended to set one request worth of data out of sync.
1000 * affects at least 1 bit,
1001 * and at most 1+DRBD_MAX_SEGMENT_SIZE/BM_BLOCK_SIZE bits.
1003 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
1004 * so this can be _any_ process.
1006 void __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
1007 const char *file, const unsigned int line)
1009 unsigned long sbnr, ebnr, lbnr, flags;
1010 sector_t esector, nr_sectors;
1011 unsigned int enr, count;
1012 struct lc_element *e;
1014 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
1015 dev_err(DEV, "sector: %llus, size: %d\n",
1016 (unsigned long long)sector, size);
1017 return;
1020 if (!get_ldev(mdev))
1021 return; /* no disk, no metadata, no bitmap to set bits in */
1023 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1024 esector = sector + (size >> 9) - 1;
1026 ERR_IF(sector >= nr_sectors)
1027 goto out;
1028 ERR_IF(esector >= nr_sectors)
1029 esector = (nr_sectors-1);
1031 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1033 /* we set it out of sync,
1034 * we do not need to round anything here */
1035 sbnr = BM_SECT_TO_BIT(sector);
1036 ebnr = BM_SECT_TO_BIT(esector);
1038 /* ok, (capacity & 7) != 0 sometimes, but who cares...
1039 * we count rs_{total,left} in bits, not sectors. */
1040 spin_lock_irqsave(&mdev->al_lock, flags);
1041 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
1043 enr = BM_SECT_TO_EXT(sector);
1044 e = lc_find(mdev->resync, enr);
1045 if (e)
1046 lc_entry(e, struct bm_extent, lce)->rs_left += count;
1047 spin_unlock_irqrestore(&mdev->al_lock, flags);
1049 out:
1050 put_ldev(mdev);
1053 static
1054 struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
1056 struct lc_element *e;
1057 struct bm_extent *bm_ext;
1058 int wakeup = 0;
1059 unsigned long rs_flags;
1061 spin_lock_irq(&mdev->al_lock);
1062 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
1063 spin_unlock_irq(&mdev->al_lock);
1064 return NULL;
1066 e = lc_get(mdev->resync, enr);
1067 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1068 if (bm_ext) {
1069 if (bm_ext->lce.lc_number != enr) {
1070 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1071 bm_ext->rs_failed = 0;
1072 lc_changed(mdev->resync, &bm_ext->lce);
1073 wakeup = 1;
1075 if (bm_ext->lce.refcnt == 1)
1076 mdev->resync_locked++;
1077 set_bit(BME_NO_WRITES, &bm_ext->flags);
1079 rs_flags = mdev->resync->flags;
1080 spin_unlock_irq(&mdev->al_lock);
1081 if (wakeup)
1082 wake_up(&mdev->al_wait);
1084 if (!bm_ext) {
1085 if (rs_flags & LC_STARVING)
1086 dev_warn(DEV, "Have to wait for element"
1087 " (resync LRU too small?)\n");
1088 BUG_ON(rs_flags & LC_DIRTY);
1091 return bm_ext;
1094 static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1096 struct lc_element *al_ext;
1097 int rv = 0;
1099 spin_lock_irq(&mdev->al_lock);
1100 if (unlikely(enr == mdev->act_log->new_number))
1101 rv = 1;
1102 else {
1103 al_ext = lc_find(mdev->act_log, enr);
1104 if (al_ext) {
1105 if (al_ext->refcnt)
1106 rv = 1;
1109 spin_unlock_irq(&mdev->al_lock);
1112 if (unlikely(rv)) {
1113 dev_info(DEV, "Delaying sync read until app's write is done\n");
1116 return rv;
1120 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1121 * @mdev: DRBD device.
1122 * @sector: The sector number.
1124 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
1126 int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1128 unsigned int enr = BM_SECT_TO_EXT(sector);
1129 struct bm_extent *bm_ext;
1130 int i, sig;
1132 sig = wait_event_interruptible(mdev->al_wait,
1133 (bm_ext = _bme_get(mdev, enr)));
1134 if (sig)
1135 return -EINTR;
1137 if (test_bit(BME_LOCKED, &bm_ext->flags))
1138 return 0;
1140 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1141 sig = wait_event_interruptible(mdev->al_wait,
1142 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i));
1143 if (sig) {
1144 spin_lock_irq(&mdev->al_lock);
1145 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1146 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1147 mdev->resync_locked--;
1148 wake_up(&mdev->al_wait);
1150 spin_unlock_irq(&mdev->al_lock);
1151 return -EINTR;
1154 set_bit(BME_LOCKED, &bm_ext->flags);
1155 return 0;
1159 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1160 * @mdev: DRBD device.
1161 * @sector: The sector number.
1163 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1164 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1165 * if there is still application IO going on in this area.
1167 int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1169 unsigned int enr = BM_SECT_TO_EXT(sector);
1170 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1171 struct lc_element *e;
1172 struct bm_extent *bm_ext;
1173 int i;
1175 spin_lock_irq(&mdev->al_lock);
1176 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1177 /* in case you have very heavy scattered io, it may
1178 * stall the syncer undefined if we give up the ref count
1179 * when we try again and requeue.
1181 * if we don't give up the refcount, but the next time
1182 * we are scheduled this extent has been "synced" by new
1183 * application writes, we'd miss the lc_put on the
1184 * extent we keep the refcount on.
1185 * so we remembered which extent we had to try again, and
1186 * if the next requested one is something else, we do
1187 * the lc_put here...
1188 * we also have to wake_up
1190 e = lc_find(mdev->resync, mdev->resync_wenr);
1191 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1192 if (bm_ext) {
1193 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1194 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1195 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1196 mdev->resync_wenr = LC_FREE;
1197 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1198 mdev->resync_locked--;
1199 wake_up(&mdev->al_wait);
1200 } else {
1201 dev_alert(DEV, "LOGIC BUG\n");
1204 /* TRY. */
1205 e = lc_try_get(mdev->resync, enr);
1206 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1207 if (bm_ext) {
1208 if (test_bit(BME_LOCKED, &bm_ext->flags))
1209 goto proceed;
1210 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1211 mdev->resync_locked++;
1212 } else {
1213 /* we did set the BME_NO_WRITES,
1214 * but then could not set BME_LOCKED,
1215 * so we tried again.
1216 * drop the extra reference. */
1217 bm_ext->lce.refcnt--;
1218 D_ASSERT(bm_ext->lce.refcnt > 0);
1220 goto check_al;
1221 } else {
1222 /* do we rather want to try later? */
1223 if (mdev->resync_locked > mdev->resync->nr_elements-3)
1224 goto try_again;
1225 /* Do or do not. There is no try. -- Yoda */
1226 e = lc_get(mdev->resync, enr);
1227 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1228 if (!bm_ext) {
1229 const unsigned long rs_flags = mdev->resync->flags;
1230 if (rs_flags & LC_STARVING)
1231 dev_warn(DEV, "Have to wait for element"
1232 " (resync LRU too small?)\n");
1233 BUG_ON(rs_flags & LC_DIRTY);
1234 goto try_again;
1236 if (bm_ext->lce.lc_number != enr) {
1237 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1238 bm_ext->rs_failed = 0;
1239 lc_changed(mdev->resync, &bm_ext->lce);
1240 wake_up(&mdev->al_wait);
1241 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1243 set_bit(BME_NO_WRITES, &bm_ext->flags);
1244 D_ASSERT(bm_ext->lce.refcnt == 1);
1245 mdev->resync_locked++;
1246 goto check_al;
1248 check_al:
1249 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1250 if (unlikely(al_enr+i == mdev->act_log->new_number))
1251 goto try_again;
1252 if (lc_is_used(mdev->act_log, al_enr+i))
1253 goto try_again;
1255 set_bit(BME_LOCKED, &bm_ext->flags);
1256 proceed:
1257 mdev->resync_wenr = LC_FREE;
1258 spin_unlock_irq(&mdev->al_lock);
1259 return 0;
1261 try_again:
1262 if (bm_ext)
1263 mdev->resync_wenr = enr;
1264 spin_unlock_irq(&mdev->al_lock);
1265 return -EAGAIN;
1268 void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1270 unsigned int enr = BM_SECT_TO_EXT(sector);
1271 struct lc_element *e;
1272 struct bm_extent *bm_ext;
1273 unsigned long flags;
1275 spin_lock_irqsave(&mdev->al_lock, flags);
1276 e = lc_find(mdev->resync, enr);
1277 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1278 if (!bm_ext) {
1279 spin_unlock_irqrestore(&mdev->al_lock, flags);
1280 if (__ratelimit(&drbd_ratelimit_state))
1281 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1282 return;
1285 if (bm_ext->lce.refcnt == 0) {
1286 spin_unlock_irqrestore(&mdev->al_lock, flags);
1287 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1288 "but refcnt is 0!?\n",
1289 (unsigned long long)sector, enr);
1290 return;
1293 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
1294 clear_bit(BME_LOCKED, &bm_ext->flags);
1295 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1296 mdev->resync_locked--;
1297 wake_up(&mdev->al_wait);
1300 spin_unlock_irqrestore(&mdev->al_lock, flags);
1304 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1305 * @mdev: DRBD device.
1307 void drbd_rs_cancel_all(struct drbd_conf *mdev)
1309 spin_lock_irq(&mdev->al_lock);
1311 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1312 lc_reset(mdev->resync);
1313 put_ldev(mdev);
1315 mdev->resync_locked = 0;
1316 mdev->resync_wenr = LC_FREE;
1317 spin_unlock_irq(&mdev->al_lock);
1318 wake_up(&mdev->al_wait);
1322 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1323 * @mdev: DRBD device.
1325 * Returns 0 upon success, -EAGAIN if at least one reference count was
1326 * not zero.
1328 int drbd_rs_del_all(struct drbd_conf *mdev)
1330 struct lc_element *e;
1331 struct bm_extent *bm_ext;
1332 int i;
1334 spin_lock_irq(&mdev->al_lock);
1336 if (get_ldev_if_state(mdev, D_FAILED)) {
1337 /* ok, ->resync is there. */
1338 for (i = 0; i < mdev->resync->nr_elements; i++) {
1339 e = lc_element_by_index(mdev->resync, i);
1340 bm_ext = lc_entry(e, struct bm_extent, lce);
1341 if (bm_ext->lce.lc_number == LC_FREE)
1342 continue;
1343 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1344 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1345 " got 'synced' by application io\n",
1346 mdev->resync_wenr);
1347 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1348 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1349 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1350 mdev->resync_wenr = LC_FREE;
1351 lc_put(mdev->resync, &bm_ext->lce);
1353 if (bm_ext->lce.refcnt != 0) {
1354 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1355 "refcnt=%d\n", bm_ext->lce.refcnt);
1356 put_ldev(mdev);
1357 spin_unlock_irq(&mdev->al_lock);
1358 return -EAGAIN;
1360 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1361 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1362 lc_del(mdev->resync, &bm_ext->lce);
1364 D_ASSERT(mdev->resync->used == 0);
1365 put_ldev(mdev);
1367 spin_unlock_irq(&mdev->al_lock);
1369 return 0;
1373 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1374 * @mdev: DRBD device.
1375 * @sector: The sector number.
1376 * @size: Size of failed IO operation, in byte.
1378 void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1380 /* Is called from worker and receiver context _only_ */
1381 unsigned long sbnr, ebnr, lbnr;
1382 unsigned long count;
1383 sector_t esector, nr_sectors;
1384 int wake_up = 0;
1386 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_SEGMENT_SIZE) {
1387 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1388 (unsigned long long)sector, size);
1389 return;
1391 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1392 esector = sector + (size >> 9) - 1;
1394 ERR_IF(sector >= nr_sectors) return;
1395 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
1397 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1400 * round up start sector, round down end sector. we make sure we only
1401 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1402 if (unlikely(esector < BM_SECT_PER_BIT-1))
1403 return;
1404 if (unlikely(esector == (nr_sectors-1)))
1405 ebnr = lbnr;
1406 else
1407 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1408 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1410 if (sbnr > ebnr)
1411 return;
1414 * ok, (capacity & 7) != 0 sometimes, but who cares...
1415 * we count rs_{total,left} in bits, not sectors.
1417 spin_lock_irq(&mdev->al_lock);
1418 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1419 if (count) {
1420 mdev->rs_failed += count;
1422 if (get_ldev(mdev)) {
1423 drbd_try_clear_on_disk_bm(mdev, sector, count, FALSE);
1424 put_ldev(mdev);
1427 /* just wake_up unconditional now, various lc_chaged(),
1428 * lc_put() in drbd_try_clear_on_disk_bm(). */
1429 wake_up = 1;
1431 spin_unlock_irq(&mdev->al_lock);
1432 if (wake_up)
1433 wake_up(&mdev->al_wait);