]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/block/drbd/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)
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 checksum 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 losing 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 void *drbd_md_get_buffer(struct drbd_conf
*mdev
)
72 wait_event(mdev
->misc_wait
,
73 (r
= atomic_cmpxchg(&mdev
->md_io_in_use
, 0, 1)) == 0 ||
74 mdev
->state
.disk
<= D_FAILED
);
76 return r
? NULL
: page_address(mdev
->md_io_page
);
79 void drbd_md_put_buffer(struct drbd_conf
*mdev
)
81 if (atomic_dec_and_test(&mdev
->md_io_in_use
))
82 wake_up(&mdev
->misc_wait
);
85 static bool md_io_allowed(struct drbd_conf
*mdev
)
87 enum drbd_disk_state ds
= mdev
->state
.disk
;
88 return ds
>= D_NEGOTIATING
|| ds
== D_ATTACHING
;
91 void wait_until_done_or_disk_failure(struct drbd_conf
*mdev
, struct drbd_backing_dev
*bdev
,
94 long dt
= bdev
->dc
.disk_timeout
* HZ
/ 10;
96 dt
= MAX_SCHEDULE_TIMEOUT
;
98 dt
= wait_event_timeout(mdev
->misc_wait
, *done
|| !md_io_allowed(mdev
), dt
);
100 dev_err(DEV
, "meta-data IO operation timed out\n");
103 static int _drbd_md_sync_page_io(struct drbd_conf
*mdev
,
104 struct drbd_backing_dev
*bdev
,
105 struct page
*page
, sector_t sector
,
111 mdev
->md_io
.done
= 0;
112 mdev
->md_io
.error
= -ENODEV
;
114 if ((rw
& WRITE
) && !test_bit(MD_NO_FUA
, &mdev
->flags
))
115 rw
|= REQ_FUA
| REQ_FLUSH
;
118 bio
= bio_alloc_drbd(GFP_NOIO
);
119 bio
->bi_bdev
= bdev
->md_bdev
;
120 bio
->bi_sector
= sector
;
121 ok
= (bio_add_page(bio
, page
, size
, 0) == size
);
124 bio
->bi_private
= &mdev
->md_io
;
125 bio
->bi_end_io
= drbd_md_io_complete
;
128 if (!get_ldev_if_state(mdev
, D_ATTACHING
)) { /* Corresponding put_ldev in drbd_md_io_complete() */
129 dev_err(DEV
, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n");
134 bio_get(bio
); /* one bio_put() is in the completion handler */
135 atomic_inc(&mdev
->md_io_in_use
); /* drbd_md_put_buffer() is in the completion handler */
136 if (drbd_insert_fault(mdev
, (rw
& WRITE
) ? DRBD_FAULT_MD_WR
: DRBD_FAULT_MD_RD
))
137 bio_endio(bio
, -EIO
);
140 wait_until_done_or_disk_failure(mdev
, bdev
, &mdev
->md_io
.done
);
141 ok
= bio_flagged(bio
, BIO_UPTODATE
) && mdev
->md_io
.error
== 0;
148 int drbd_md_sync_page_io(struct drbd_conf
*mdev
, struct drbd_backing_dev
*bdev
,
149 sector_t sector
, int rw
)
151 int logical_block_size
, mask
, ok
;
153 struct page
*iop
= mdev
->md_io_page
;
155 D_ASSERT(atomic_read(&mdev
->md_io_in_use
) == 1);
157 BUG_ON(!bdev
->md_bdev
);
159 logical_block_size
= bdev_logical_block_size(bdev
->md_bdev
);
160 if (logical_block_size
== 0)
161 logical_block_size
= MD_SECTOR_SIZE
;
163 /* in case logical_block_size != 512 [ s390 only? ] */
164 if (logical_block_size
!= MD_SECTOR_SIZE
) {
165 mask
= (logical_block_size
/ MD_SECTOR_SIZE
) - 1;
166 D_ASSERT(mask
== 1 || mask
== 3 || mask
== 7);
167 D_ASSERT(logical_block_size
== (mask
+1) * MD_SECTOR_SIZE
);
168 offset
= sector
& mask
;
169 sector
= sector
& ~mask
;
170 iop
= mdev
->md_io_tmpp
;
173 /* these are GFP_KERNEL pages, pre-allocated
174 * on device initialization */
175 void *p
= page_address(mdev
->md_io_page
);
176 void *hp
= page_address(mdev
->md_io_tmpp
);
178 ok
= _drbd_md_sync_page_io(mdev
, bdev
, iop
, sector
,
179 READ
, logical_block_size
);
182 dev_err(DEV
, "drbd_md_sync_page_io(,%llus,"
183 "READ [logical_block_size!=512]) failed!\n",
184 (unsigned long long)sector
);
188 memcpy(hp
+ offset
*MD_SECTOR_SIZE
, p
, MD_SECTOR_SIZE
);
192 if (sector
< drbd_md_first_sector(bdev
) ||
193 sector
> drbd_md_last_sector(bdev
))
194 dev_alert(DEV
, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
195 current
->comm
, current
->pid
, __func__
,
196 (unsigned long long)sector
, (rw
& WRITE
) ? "WRITE" : "READ");
198 ok
= _drbd_md_sync_page_io(mdev
, bdev
, iop
, sector
, rw
, logical_block_size
);
200 dev_err(DEV
, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
201 (unsigned long long)sector
, (rw
& WRITE
) ? "WRITE" : "READ");
205 if (logical_block_size
!= MD_SECTOR_SIZE
&& !(rw
& WRITE
)) {
206 void *p
= page_address(mdev
->md_io_page
);
207 void *hp
= page_address(mdev
->md_io_tmpp
);
209 memcpy(p
, hp
+ offset
*MD_SECTOR_SIZE
, MD_SECTOR_SIZE
);
215 static struct lc_element
*_al_get(struct drbd_conf
*mdev
, unsigned int enr
)
217 struct lc_element
*al_ext
;
218 struct lc_element
*tmp
;
219 unsigned long al_flags
= 0;
222 spin_lock_irq(&mdev
->al_lock
);
223 tmp
= lc_find(mdev
->resync
, enr
/AL_EXT_PER_BM_SECT
);
224 if (unlikely(tmp
!= NULL
)) {
225 struct bm_extent
*bm_ext
= lc_entry(tmp
, struct bm_extent
, lce
);
226 if (test_bit(BME_NO_WRITES
, &bm_ext
->flags
)) {
227 wake
= !test_and_set_bit(BME_PRIORITY
, &bm_ext
->flags
);
228 spin_unlock_irq(&mdev
->al_lock
);
230 wake_up(&mdev
->al_wait
);
234 al_ext
= lc_get(mdev
->act_log
, enr
);
235 al_flags
= mdev
->act_log
->flags
;
236 spin_unlock_irq(&mdev
->al_lock
);
240 if (al_flags & LC_STARVING)
241 dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n");
242 if (al_flags & LC_DIRTY)
243 dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n");
250 void drbd_al_begin_io(struct drbd_conf
*mdev
, sector_t sector
)
252 unsigned int enr
= (sector
>> (AL_EXTENT_SHIFT
-9));
253 struct lc_element
*al_ext
;
254 struct update_al_work al_work
;
256 D_ASSERT(atomic_read(&mdev
->local_cnt
) > 0);
258 wait_event(mdev
->al_wait
, (al_ext
= _al_get(mdev
, enr
)));
260 if (al_ext
->lc_number
!= enr
) {
261 /* drbd_al_write_transaction(mdev,al_ext,enr);
262 * recurses into generic_make_request(), which
263 * disallows recursion, bios being serialized on the
264 * current->bio_tail list now.
265 * we have to delegate updates to the activity log
266 * to the worker thread. */
267 init_completion(&al_work
.event
);
268 al_work
.al_ext
= al_ext
;
270 al_work
.old_enr
= al_ext
->lc_number
;
271 al_work
.w
.cb
= w_al_write_transaction
;
272 drbd_queue_work_front(&mdev
->data
.work
, &al_work
.w
);
273 wait_for_completion(&al_work
.event
);
277 spin_lock_irq(&mdev
->al_lock
);
278 lc_changed(mdev
->act_log
, al_ext
);
279 spin_unlock_irq(&mdev
->al_lock
);
280 wake_up(&mdev
->al_wait
);
284 void drbd_al_complete_io(struct drbd_conf
*mdev
, sector_t sector
)
286 unsigned int enr
= (sector
>> (AL_EXTENT_SHIFT
-9));
287 struct lc_element
*extent
;
290 spin_lock_irqsave(&mdev
->al_lock
, flags
);
292 extent
= lc_find(mdev
->act_log
, enr
);
295 spin_unlock_irqrestore(&mdev
->al_lock
, flags
);
296 dev_err(DEV
, "al_complete_io() called on inactive extent %u\n", enr
);
300 if (lc_put(mdev
->act_log
, extent
) == 0)
301 wake_up(&mdev
->al_wait
);
303 spin_unlock_irqrestore(&mdev
->al_lock
, flags
);
306 #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
307 /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
308 * are still coupled, or assume too much about their relation.
309 * Code below will not work if this is violated.
310 * Will be cleaned up with some followup patch.
315 static unsigned int al_extent_to_bm_page(unsigned int al_enr
)
320 /* al extent number to bit */
321 (AL_EXTENT_SHIFT
- BM_BLOCK_SHIFT
));
324 static unsigned int rs_extent_to_bm_page(unsigned int rs_enr
)
329 /* al extent number to bit */
330 (BM_EXT_SHIFT
- BM_BLOCK_SHIFT
));
334 w_al_write_transaction(struct drbd_conf
*mdev
, struct drbd_work
*w
, int unused
)
336 struct update_al_work
*aw
= container_of(w
, struct update_al_work
, w
);
337 struct lc_element
*updated
= aw
->al_ext
;
338 const unsigned int new_enr
= aw
->enr
;
339 const unsigned int evicted
= aw
->old_enr
;
340 struct al_transaction
*buffer
;
343 unsigned int extent_nr
;
346 if (!get_ldev(mdev
)) {
348 "disk is %s, cannot start al transaction (-%d +%d)\n",
349 drbd_disk_str(mdev
->state
.disk
), evicted
, new_enr
);
350 complete(&((struct update_al_work
*)w
)->event
);
353 /* do we have to do a bitmap write, first?
354 * TODO reduce maximum latency:
355 * submit both bios, then wait for both,
356 * instead of doing two synchronous sector writes.
357 * For now, we must not write the transaction,
358 * if we cannot write out the bitmap of the evicted extent. */
359 if (mdev
->state
.conn
< C_CONNECTED
&& evicted
!= LC_FREE
)
360 drbd_bm_write_page(mdev
, al_extent_to_bm_page(evicted
));
362 /* The bitmap write may have failed, causing a state change. */
363 if (mdev
->state
.disk
< D_INCONSISTENT
) {
365 "disk is %s, cannot write al transaction (-%d +%d)\n",
366 drbd_disk_str(mdev
->state
.disk
), evicted
, new_enr
);
367 complete(&((struct update_al_work
*)w
)->event
);
372 buffer
= drbd_md_get_buffer(mdev
); /* protects md_io_buffer, al_tr_cycle, ... */
374 dev_err(DEV
, "disk failed while waiting for md_io buffer\n");
375 complete(&((struct update_al_work
*)w
)->event
);
380 buffer
->magic
= __constant_cpu_to_be32(DRBD_MAGIC
);
381 buffer
->tr_number
= cpu_to_be32(mdev
->al_tr_number
);
383 n
= lc_index_of(mdev
->act_log
, updated
);
385 buffer
->updates
[0].pos
= cpu_to_be32(n
);
386 buffer
->updates
[0].extent
= cpu_to_be32(new_enr
);
390 mx
= min_t(int, AL_EXTENTS_PT
,
391 mdev
->act_log
->nr_elements
- mdev
->al_tr_cycle
);
392 for (i
= 0; i
< mx
; i
++) {
393 unsigned idx
= mdev
->al_tr_cycle
+ i
;
394 extent_nr
= lc_element_by_index(mdev
->act_log
, idx
)->lc_number
;
395 buffer
->updates
[i
+1].pos
= cpu_to_be32(idx
);
396 buffer
->updates
[i
+1].extent
= cpu_to_be32(extent_nr
);
397 xor_sum
^= extent_nr
;
399 for (; i
< AL_EXTENTS_PT
; i
++) {
400 buffer
->updates
[i
+1].pos
= __constant_cpu_to_be32(-1);
401 buffer
->updates
[i
+1].extent
= __constant_cpu_to_be32(LC_FREE
);
404 mdev
->al_tr_cycle
+= AL_EXTENTS_PT
;
405 if (mdev
->al_tr_cycle
>= mdev
->act_log
->nr_elements
)
406 mdev
->al_tr_cycle
= 0;
408 buffer
->xor_sum
= cpu_to_be32(xor_sum
);
410 sector
= mdev
->ldev
->md
.md_offset
411 + mdev
->ldev
->md
.al_offset
+ mdev
->al_tr_pos
;
413 if (!drbd_md_sync_page_io(mdev
, mdev
->ldev
, sector
, WRITE
))
414 drbd_chk_io_error(mdev
, 1, true);
416 if (++mdev
->al_tr_pos
>
417 div_ceil(mdev
->act_log
->nr_elements
, AL_EXTENTS_PT
))
420 D_ASSERT(mdev
->al_tr_pos
< MD_AL_MAX_SIZE
);
421 mdev
->al_tr_number
++;
423 drbd_md_put_buffer(mdev
);
425 complete(&((struct update_al_work
*)w
)->event
);
432 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
433 * @mdev: DRBD device.
434 * @bdev: Block device to read form.
435 * @b: pointer to an al_transaction.
436 * @index: On disk slot of the transaction to read.
438 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
440 static int drbd_al_read_tr(struct drbd_conf
*mdev
,
441 struct drbd_backing_dev
*bdev
,
442 struct al_transaction
*b
,
449 sector
= bdev
->md
.md_offset
+ bdev
->md
.al_offset
+ index
;
451 /* Dont process error normally,
452 * as this is done before disk is attached! */
453 if (!drbd_md_sync_page_io(mdev
, bdev
, sector
, READ
))
456 rv
= (be32_to_cpu(b
->magic
) == DRBD_MAGIC
);
458 for (i
= 0; i
< AL_EXTENTS_PT
+ 1; i
++)
459 xor_sum
^= be32_to_cpu(b
->updates
[i
].extent
);
460 rv
&= (xor_sum
== be32_to_cpu(b
->xor_sum
));
466 * drbd_al_read_log() - Restores the activity log from its on disk representation.
467 * @mdev: DRBD device.
468 * @bdev: Block device to read form.
470 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
472 int drbd_al_read_log(struct drbd_conf
*mdev
, struct drbd_backing_dev
*bdev
)
474 struct al_transaction
*buffer
;
478 int active_extents
= 0;
479 int transactions
= 0;
487 mx
= div_ceil(mdev
->act_log
->nr_elements
, AL_EXTENTS_PT
);
489 /* lock out all other meta data io for now,
490 * and make sure the page is mapped.
492 buffer
= drbd_md_get_buffer(mdev
);
496 /* Find the valid transaction in the log */
497 for (i
= 0; i
<= mx
; i
++) {
498 rv
= drbd_al_read_tr(mdev
, bdev
, buffer
, i
);
502 drbd_md_put_buffer(mdev
);
505 cnr
= be32_to_cpu(buffer
->tr_number
);
507 if (++found_valid
== 1) {
514 if ((int)cnr
- (int)from_tnr
< 0) {
515 D_ASSERT(from_tnr
- cnr
+ i
- from
== mx
+1);
519 if ((int)cnr
- (int)to_tnr
> 0) {
520 D_ASSERT(cnr
- to_tnr
== i
- to
);
527 dev_warn(DEV
, "No usable activity log found.\n");
528 drbd_md_put_buffer(mdev
);
532 /* Read the valid transactions.
533 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
537 unsigned int extent_nr
;
540 rv
= drbd_al_read_tr(mdev
, bdev
, buffer
, i
);
541 ERR_IF(rv
== 0) goto cancel
;
543 drbd_md_put_buffer(mdev
);
547 trn
= be32_to_cpu(buffer
->tr_number
);
549 spin_lock_irq(&mdev
->al_lock
);
551 /* This loop runs backwards because in the cyclic
552 elements there might be an old version of the
553 updated element (in slot 0). So the element in slot 0
554 can overwrite old versions. */
555 for (j
= AL_EXTENTS_PT
; j
>= 0; j
--) {
556 pos
= be32_to_cpu(buffer
->updates
[j
].pos
);
557 extent_nr
= be32_to_cpu(buffer
->updates
[j
].extent
);
559 if (extent_nr
== LC_FREE
)
562 lc_set(mdev
->act_log
, extent_nr
, pos
);
565 spin_unlock_irq(&mdev
->al_lock
);
577 mdev
->al_tr_number
= to_tnr
+1;
578 mdev
->al_tr_pos
= to
;
579 if (++mdev
->al_tr_pos
>
580 div_ceil(mdev
->act_log
->nr_elements
, AL_EXTENTS_PT
))
583 /* ok, we are done with it */
584 drbd_md_put_buffer(mdev
);
586 dev_info(DEV
, "Found %d transactions (%d active extents) in activity log.\n",
587 transactions
, active_extents
);
593 * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
594 * @mdev: DRBD device.
596 void drbd_al_apply_to_bm(struct drbd_conf
*mdev
)
599 unsigned long add
= 0;
603 wait_event(mdev
->al_wait
, lc_try_lock(mdev
->act_log
));
605 for (i
= 0; i
< mdev
->act_log
->nr_elements
; i
++) {
606 enr
= lc_element_by_index(mdev
->act_log
, i
)->lc_number
;
609 tmp
= drbd_bm_ALe_set_all(mdev
, enr
);
610 dynamic_dev_dbg(DEV
, "AL: set %d bits in extent %u\n", tmp
, enr
);
614 lc_unlock(mdev
->act_log
);
615 wake_up(&mdev
->al_wait
);
617 dev_info(DEV
, "Marked additional %s as out-of-sync based on AL.\n",
618 ppsize(ppb
, Bit2KB(add
)));
621 static int _try_lc_del(struct drbd_conf
*mdev
, struct lc_element
*al_ext
)
625 spin_lock_irq(&mdev
->al_lock
);
626 rv
= (al_ext
->refcnt
== 0);
628 lc_del(mdev
->act_log
, al_ext
);
629 spin_unlock_irq(&mdev
->al_lock
);
635 * drbd_al_shrink() - Removes all active extents form the activity log
636 * @mdev: DRBD device.
638 * Removes all active extents form the activity log, waiting until
639 * the reference count of each entry dropped to 0 first, of course.
641 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
643 void drbd_al_shrink(struct drbd_conf
*mdev
)
645 struct lc_element
*al_ext
;
648 D_ASSERT(test_bit(__LC_DIRTY
, &mdev
->act_log
->flags
));
650 for (i
= 0; i
< mdev
->act_log
->nr_elements
; i
++) {
651 al_ext
= lc_element_by_index(mdev
->act_log
, i
);
652 if (al_ext
->lc_number
== LC_FREE
)
654 wait_event(mdev
->al_wait
, _try_lc_del(mdev
, al_ext
));
657 wake_up(&mdev
->al_wait
);
660 static int w_update_odbm(struct drbd_conf
*mdev
, struct drbd_work
*w
, int unused
)
662 struct update_odbm_work
*udw
= container_of(w
, struct update_odbm_work
, w
);
664 if (!get_ldev(mdev
)) {
665 if (__ratelimit(&drbd_ratelimit_state
))
666 dev_warn(DEV
, "Can not update on disk bitmap, local IO disabled.\n");
671 drbd_bm_write_page(mdev
, rs_extent_to_bm_page(udw
->enr
));
676 if (drbd_bm_total_weight(mdev
) <= mdev
->rs_failed
) {
677 switch (mdev
->state
.conn
) {
678 case C_SYNC_SOURCE
: case C_SYNC_TARGET
:
679 case C_PAUSED_SYNC_S
: case C_PAUSED_SYNC_T
:
680 drbd_resync_finished(mdev
);
686 drbd_bcast_sync_progress(mdev
);
692 /* ATTENTION. The AL's extents are 4MB each, while the extents in the
693 * resync LRU-cache are 16MB each.
694 * The caller of this function has to hold an get_ldev() reference.
696 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
698 static void drbd_try_clear_on_disk_bm(struct drbd_conf
*mdev
, sector_t sector
,
699 int count
, int success
)
701 struct lc_element
*e
;
702 struct update_odbm_work
*udw
;
706 D_ASSERT(atomic_read(&mdev
->local_cnt
));
708 /* I simply assume that a sector/size pair never crosses
709 * a 16 MB extent border. (Currently this is true...) */
710 enr
= BM_SECT_TO_EXT(sector
);
712 e
= lc_get(mdev
->resync
, enr
);
714 struct bm_extent
*ext
= lc_entry(e
, struct bm_extent
, lce
);
715 if (ext
->lce
.lc_number
== enr
) {
717 ext
->rs_left
-= count
;
719 ext
->rs_failed
+= count
;
720 if (ext
->rs_left
< ext
->rs_failed
) {
721 dev_warn(DEV
, "BAD! sector=%llus enr=%u rs_left=%d "
722 "rs_failed=%d count=%d cstate=%s\n",
723 (unsigned long long)sector
,
724 ext
->lce
.lc_number
, ext
->rs_left
,
725 ext
->rs_failed
, count
,
726 drbd_conn_str(mdev
->state
.conn
));
728 /* We don't expect to be able to clear more bits
729 * than have been set when we originally counted
730 * the set bits to cache that value in ext->rs_left.
731 * Whatever the reason (disconnect during resync,
732 * delayed local completion of an application write),
733 * try to fix it up by recounting here. */
734 ext
->rs_left
= drbd_bm_e_weight(mdev
, enr
);
737 /* Normally this element should be in the cache,
738 * since drbd_rs_begin_io() pulled it already in.
740 * But maybe an application write finished, and we set
741 * something outside the resync lru_cache in sync.
743 int rs_left
= drbd_bm_e_weight(mdev
, enr
);
744 if (ext
->flags
!= 0) {
745 dev_warn(DEV
, "changing resync lce: %d[%u;%02lx]"
747 ext
->lce
.lc_number
, ext
->rs_left
,
748 ext
->flags
, enr
, rs_left
);
751 if (ext
->rs_failed
) {
752 dev_warn(DEV
, "Kicking resync_lru element enr=%u "
753 "out with rs_failed=%d\n",
754 ext
->lce
.lc_number
, ext
->rs_failed
);
756 ext
->rs_left
= rs_left
;
757 ext
->rs_failed
= success
? 0 : count
;
758 lc_changed(mdev
->resync
, &ext
->lce
);
760 lc_put(mdev
->resync
, &ext
->lce
);
761 /* no race, we are within the al_lock! */
763 if (ext
->rs_left
== ext
->rs_failed
) {
766 udw
= kmalloc(sizeof(*udw
), GFP_ATOMIC
);
768 udw
->enr
= ext
->lce
.lc_number
;
769 udw
->w
.cb
= w_update_odbm
;
770 drbd_queue_work_front(&mdev
->data
.work
, &udw
->w
);
772 dev_warn(DEV
, "Could not kmalloc an udw\n");
776 dev_err(DEV
, "lc_get() failed! locked=%d/%d flags=%lu\n",
778 mdev
->resync
->nr_elements
,
779 mdev
->resync
->flags
);
783 void drbd_advance_rs_marks(struct drbd_conf
*mdev
, unsigned long still_to_go
)
785 unsigned long now
= jiffies
;
786 unsigned long last
= mdev
->rs_mark_time
[mdev
->rs_last_mark
];
787 int next
= (mdev
->rs_last_mark
+ 1) % DRBD_SYNC_MARKS
;
788 if (time_after_eq(now
, last
+ DRBD_SYNC_MARK_STEP
)) {
789 if (mdev
->rs_mark_left
[mdev
->rs_last_mark
] != still_to_go
&&
790 mdev
->state
.conn
!= C_PAUSED_SYNC_T
&&
791 mdev
->state
.conn
!= C_PAUSED_SYNC_S
) {
792 mdev
->rs_mark_time
[next
] = now
;
793 mdev
->rs_mark_left
[next
] = still_to_go
;
794 mdev
->rs_last_mark
= next
;
799 /* clear the bit corresponding to the piece of storage in question:
800 * size byte of data starting from sector. Only clear a bits of the affected
801 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
803 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
806 void __drbd_set_in_sync(struct drbd_conf
*mdev
, sector_t sector
, int size
,
807 const char *file
, const unsigned int line
)
809 /* Is called from worker and receiver context _only_ */
810 unsigned long sbnr
, ebnr
, lbnr
;
811 unsigned long count
= 0;
812 sector_t esector
, nr_sectors
;
816 if (size
<= 0 || (size
& 0x1ff) != 0 || size
> DRBD_MAX_BIO_SIZE
) {
817 dev_err(DEV
, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
818 (unsigned long long)sector
, size
);
821 nr_sectors
= drbd_get_capacity(mdev
->this_bdev
);
822 esector
= sector
+ (size
>> 9) - 1;
824 ERR_IF(sector
>= nr_sectors
) return;
825 ERR_IF(esector
>= nr_sectors
) esector
= (nr_sectors
-1);
827 lbnr
= BM_SECT_TO_BIT(nr_sectors
-1);
829 /* we clear it (in sync).
830 * round up start sector, round down end sector. we make sure we only
831 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
832 if (unlikely(esector
< BM_SECT_PER_BIT
-1))
834 if (unlikely(esector
== (nr_sectors
-1)))
837 ebnr
= BM_SECT_TO_BIT(esector
- (BM_SECT_PER_BIT
-1));
838 sbnr
= BM_SECT_TO_BIT(sector
+ BM_SECT_PER_BIT
-1);
844 * ok, (capacity & 7) != 0 sometimes, but who cares...
845 * we count rs_{total,left} in bits, not sectors.
847 count
= drbd_bm_clear_bits(mdev
, sbnr
, ebnr
);
848 if (count
&& get_ldev(mdev
)) {
849 drbd_advance_rs_marks(mdev
, drbd_bm_total_weight(mdev
));
850 spin_lock_irqsave(&mdev
->al_lock
, flags
);
851 drbd_try_clear_on_disk_bm(mdev
, sector
, count
, true);
852 spin_unlock_irqrestore(&mdev
->al_lock
, flags
);
854 /* just wake_up unconditional now, various lc_chaged(),
855 * lc_put() in drbd_try_clear_on_disk_bm(). */
860 wake_up(&mdev
->al_wait
);
864 * this is intended to set one request worth of data out of sync.
865 * affects at least 1 bit,
866 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
868 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
869 * so this can be _any_ process.
871 int __drbd_set_out_of_sync(struct drbd_conf
*mdev
, sector_t sector
, int size
,
872 const char *file
, const unsigned int line
)
874 unsigned long sbnr
, ebnr
, lbnr
, flags
;
875 sector_t esector
, nr_sectors
;
876 unsigned int enr
, count
= 0;
877 struct lc_element
*e
;
879 if (size
<= 0 || (size
& 0x1ff) != 0 || size
> DRBD_MAX_BIO_SIZE
) {
880 dev_err(DEV
, "sector: %llus, size: %d\n",
881 (unsigned long long)sector
, size
);
886 return 0; /* no disk, no metadata, no bitmap to set bits in */
888 nr_sectors
= drbd_get_capacity(mdev
->this_bdev
);
889 esector
= sector
+ (size
>> 9) - 1;
891 ERR_IF(sector
>= nr_sectors
)
893 ERR_IF(esector
>= nr_sectors
)
894 esector
= (nr_sectors
-1);
896 lbnr
= BM_SECT_TO_BIT(nr_sectors
-1);
898 /* we set it out of sync,
899 * we do not need to round anything here */
900 sbnr
= BM_SECT_TO_BIT(sector
);
901 ebnr
= BM_SECT_TO_BIT(esector
);
903 /* ok, (capacity & 7) != 0 sometimes, but who cares...
904 * we count rs_{total,left} in bits, not sectors. */
905 spin_lock_irqsave(&mdev
->al_lock
, flags
);
906 count
= drbd_bm_set_bits(mdev
, sbnr
, ebnr
);
908 enr
= BM_SECT_TO_EXT(sector
);
909 e
= lc_find(mdev
->resync
, enr
);
911 lc_entry(e
, struct bm_extent
, lce
)->rs_left
+= count
;
912 spin_unlock_irqrestore(&mdev
->al_lock
, flags
);
921 struct bm_extent
*_bme_get(struct drbd_conf
*mdev
, unsigned int enr
)
923 struct lc_element
*e
;
924 struct bm_extent
*bm_ext
;
926 unsigned long rs_flags
;
928 spin_lock_irq(&mdev
->al_lock
);
929 if (mdev
->resync_locked
> mdev
->resync
->nr_elements
/2) {
930 spin_unlock_irq(&mdev
->al_lock
);
933 e
= lc_get(mdev
->resync
, enr
);
934 bm_ext
= e
? lc_entry(e
, struct bm_extent
, lce
) : NULL
;
936 if (bm_ext
->lce
.lc_number
!= enr
) {
937 bm_ext
->rs_left
= drbd_bm_e_weight(mdev
, enr
);
938 bm_ext
->rs_failed
= 0;
939 lc_changed(mdev
->resync
, &bm_ext
->lce
);
942 if (bm_ext
->lce
.refcnt
== 1)
943 mdev
->resync_locked
++;
944 set_bit(BME_NO_WRITES
, &bm_ext
->flags
);
946 rs_flags
= mdev
->resync
->flags
;
947 spin_unlock_irq(&mdev
->al_lock
);
949 wake_up(&mdev
->al_wait
);
952 if (rs_flags
& LC_STARVING
)
953 dev_warn(DEV
, "Have to wait for element"
954 " (resync LRU too small?)\n");
955 BUG_ON(rs_flags
& LC_DIRTY
);
961 static int _is_in_al(struct drbd_conf
*mdev
, unsigned int enr
)
963 struct lc_element
*al_ext
;
966 spin_lock_irq(&mdev
->al_lock
);
967 if (unlikely(enr
== mdev
->act_log
->new_number
))
970 al_ext
= lc_find(mdev
->act_log
, enr
);
976 spin_unlock_irq(&mdev
->al_lock
);
980 dev_info(DEV, "Delaying sync read until app's write is done\n");
987 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
988 * @mdev: DRBD device.
989 * @sector: The sector number.
991 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
993 int drbd_rs_begin_io(struct drbd_conf
*mdev
, sector_t sector
)
995 unsigned int enr
= BM_SECT_TO_EXT(sector
);
996 struct bm_extent
*bm_ext
;
998 int sa
= 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
999 200 times -> 20 seconds. */
1002 sig
= wait_event_interruptible(mdev
->al_wait
,
1003 (bm_ext
= _bme_get(mdev
, enr
)));
1007 if (test_bit(BME_LOCKED
, &bm_ext
->flags
))
1010 for (i
= 0; i
< AL_EXT_PER_BM_SECT
; i
++) {
1011 sig
= wait_event_interruptible(mdev
->al_wait
,
1012 !_is_in_al(mdev
, enr
* AL_EXT_PER_BM_SECT
+ i
) ||
1013 test_bit(BME_PRIORITY
, &bm_ext
->flags
));
1015 if (sig
|| (test_bit(BME_PRIORITY
, &bm_ext
->flags
) && sa
)) {
1016 spin_lock_irq(&mdev
->al_lock
);
1017 if (lc_put(mdev
->resync
, &bm_ext
->lce
) == 0) {
1018 bm_ext
->flags
= 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
1019 mdev
->resync_locked
--;
1020 wake_up(&mdev
->al_wait
);
1022 spin_unlock_irq(&mdev
->al_lock
);
1025 if (schedule_timeout_interruptible(HZ
/10))
1027 if (sa
&& --sa
== 0)
1028 dev_warn(DEV
,"drbd_rs_begin_io() stepped aside for 20sec."
1029 "Resync stalled?\n");
1033 set_bit(BME_LOCKED
, &bm_ext
->flags
);
1038 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1039 * @mdev: DRBD device.
1040 * @sector: The sector number.
1042 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1043 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1044 * if there is still application IO going on in this area.
1046 int drbd_try_rs_begin_io(struct drbd_conf
*mdev
, sector_t sector
)
1048 unsigned int enr
= BM_SECT_TO_EXT(sector
);
1049 const unsigned int al_enr
= enr
*AL_EXT_PER_BM_SECT
;
1050 struct lc_element
*e
;
1051 struct bm_extent
*bm_ext
;
1054 spin_lock_irq(&mdev
->al_lock
);
1055 if (mdev
->resync_wenr
!= LC_FREE
&& mdev
->resync_wenr
!= enr
) {
1056 /* in case you have very heavy scattered io, it may
1057 * stall the syncer undefined if we give up the ref count
1058 * when we try again and requeue.
1060 * if we don't give up the refcount, but the next time
1061 * we are scheduled this extent has been "synced" by new
1062 * application writes, we'd miss the lc_put on the
1063 * extent we keep the refcount on.
1064 * so we remembered which extent we had to try again, and
1065 * if the next requested one is something else, we do
1066 * the lc_put here...
1067 * we also have to wake_up
1069 e
= lc_find(mdev
->resync
, mdev
->resync_wenr
);
1070 bm_ext
= e
? lc_entry(e
, struct bm_extent
, lce
) : NULL
;
1072 D_ASSERT(!test_bit(BME_LOCKED
, &bm_ext
->flags
));
1073 D_ASSERT(test_bit(BME_NO_WRITES
, &bm_ext
->flags
));
1074 clear_bit(BME_NO_WRITES
, &bm_ext
->flags
);
1075 mdev
->resync_wenr
= LC_FREE
;
1076 if (lc_put(mdev
->resync
, &bm_ext
->lce
) == 0)
1077 mdev
->resync_locked
--;
1078 wake_up(&mdev
->al_wait
);
1080 dev_alert(DEV
, "LOGIC BUG\n");
1084 e
= lc_try_get(mdev
->resync
, enr
);
1085 bm_ext
= e
? lc_entry(e
, struct bm_extent
, lce
) : NULL
;
1087 if (test_bit(BME_LOCKED
, &bm_ext
->flags
))
1089 if (!test_and_set_bit(BME_NO_WRITES
, &bm_ext
->flags
)) {
1090 mdev
->resync_locked
++;
1092 /* we did set the BME_NO_WRITES,
1093 * but then could not set BME_LOCKED,
1094 * so we tried again.
1095 * drop the extra reference. */
1096 bm_ext
->lce
.refcnt
--;
1097 D_ASSERT(bm_ext
->lce
.refcnt
> 0);
1101 /* do we rather want to try later? */
1102 if (mdev
->resync_locked
> mdev
->resync
->nr_elements
-3)
1104 /* Do or do not. There is no try. -- Yoda */
1105 e
= lc_get(mdev
->resync
, enr
);
1106 bm_ext
= e
? lc_entry(e
, struct bm_extent
, lce
) : NULL
;
1108 const unsigned long rs_flags
= mdev
->resync
->flags
;
1109 if (rs_flags
& LC_STARVING
)
1110 dev_warn(DEV
, "Have to wait for element"
1111 " (resync LRU too small?)\n");
1112 BUG_ON(rs_flags
& LC_DIRTY
);
1115 if (bm_ext
->lce
.lc_number
!= enr
) {
1116 bm_ext
->rs_left
= drbd_bm_e_weight(mdev
, enr
);
1117 bm_ext
->rs_failed
= 0;
1118 lc_changed(mdev
->resync
, &bm_ext
->lce
);
1119 wake_up(&mdev
->al_wait
);
1120 D_ASSERT(test_bit(BME_LOCKED
, &bm_ext
->flags
) == 0);
1122 set_bit(BME_NO_WRITES
, &bm_ext
->flags
);
1123 D_ASSERT(bm_ext
->lce
.refcnt
== 1);
1124 mdev
->resync_locked
++;
1128 for (i
= 0; i
< AL_EXT_PER_BM_SECT
; i
++) {
1129 if (unlikely(al_enr
+i
== mdev
->act_log
->new_number
))
1131 if (lc_is_used(mdev
->act_log
, al_enr
+i
))
1134 set_bit(BME_LOCKED
, &bm_ext
->flags
);
1136 mdev
->resync_wenr
= LC_FREE
;
1137 spin_unlock_irq(&mdev
->al_lock
);
1142 mdev
->resync_wenr
= enr
;
1143 spin_unlock_irq(&mdev
->al_lock
);
1147 void drbd_rs_complete_io(struct drbd_conf
*mdev
, sector_t sector
)
1149 unsigned int enr
= BM_SECT_TO_EXT(sector
);
1150 struct lc_element
*e
;
1151 struct bm_extent
*bm_ext
;
1152 unsigned long flags
;
1154 spin_lock_irqsave(&mdev
->al_lock
, flags
);
1155 e
= lc_find(mdev
->resync
, enr
);
1156 bm_ext
= e
? lc_entry(e
, struct bm_extent
, lce
) : NULL
;
1158 spin_unlock_irqrestore(&mdev
->al_lock
, flags
);
1159 if (__ratelimit(&drbd_ratelimit_state
))
1160 dev_err(DEV
, "drbd_rs_complete_io() called, but extent not found\n");
1164 if (bm_ext
->lce
.refcnt
== 0) {
1165 spin_unlock_irqrestore(&mdev
->al_lock
, flags
);
1166 dev_err(DEV
, "drbd_rs_complete_io(,%llu [=%u]) called, "
1167 "but refcnt is 0!?\n",
1168 (unsigned long long)sector
, enr
);
1172 if (lc_put(mdev
->resync
, &bm_ext
->lce
) == 0) {
1173 bm_ext
->flags
= 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
1174 mdev
->resync_locked
--;
1175 wake_up(&mdev
->al_wait
);
1178 spin_unlock_irqrestore(&mdev
->al_lock
, flags
);
1182 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1183 * @mdev: DRBD device.
1185 void drbd_rs_cancel_all(struct drbd_conf
*mdev
)
1187 spin_lock_irq(&mdev
->al_lock
);
1189 if (get_ldev_if_state(mdev
, D_FAILED
)) { /* Makes sure ->resync is there. */
1190 lc_reset(mdev
->resync
);
1193 mdev
->resync_locked
= 0;
1194 mdev
->resync_wenr
= LC_FREE
;
1195 spin_unlock_irq(&mdev
->al_lock
);
1196 wake_up(&mdev
->al_wait
);
1200 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1201 * @mdev: DRBD device.
1203 * Returns 0 upon success, -EAGAIN if at least one reference count was
1206 int drbd_rs_del_all(struct drbd_conf
*mdev
)
1208 struct lc_element
*e
;
1209 struct bm_extent
*bm_ext
;
1212 spin_lock_irq(&mdev
->al_lock
);
1214 if (get_ldev_if_state(mdev
, D_FAILED
)) {
1215 /* ok, ->resync is there. */
1216 for (i
= 0; i
< mdev
->resync
->nr_elements
; i
++) {
1217 e
= lc_element_by_index(mdev
->resync
, i
);
1218 bm_ext
= lc_entry(e
, struct bm_extent
, lce
);
1219 if (bm_ext
->lce
.lc_number
== LC_FREE
)
1221 if (bm_ext
->lce
.lc_number
== mdev
->resync_wenr
) {
1222 dev_info(DEV
, "dropping %u in drbd_rs_del_all, apparently"
1223 " got 'synced' by application io\n",
1225 D_ASSERT(!test_bit(BME_LOCKED
, &bm_ext
->flags
));
1226 D_ASSERT(test_bit(BME_NO_WRITES
, &bm_ext
->flags
));
1227 clear_bit(BME_NO_WRITES
, &bm_ext
->flags
);
1228 mdev
->resync_wenr
= LC_FREE
;
1229 lc_put(mdev
->resync
, &bm_ext
->lce
);
1231 if (bm_ext
->lce
.refcnt
!= 0) {
1232 dev_info(DEV
, "Retrying drbd_rs_del_all() later. "
1233 "refcnt=%d\n", bm_ext
->lce
.refcnt
);
1235 spin_unlock_irq(&mdev
->al_lock
);
1238 D_ASSERT(!test_bit(BME_LOCKED
, &bm_ext
->flags
));
1239 D_ASSERT(!test_bit(BME_NO_WRITES
, &bm_ext
->flags
));
1240 lc_del(mdev
->resync
, &bm_ext
->lce
);
1242 D_ASSERT(mdev
->resync
->used
== 0);
1245 spin_unlock_irq(&mdev
->al_lock
);
1246 wake_up(&mdev
->al_wait
);
1252 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1253 * @mdev: DRBD device.
1254 * @sector: The sector number.
1255 * @size: Size of failed IO operation, in byte.
1257 void drbd_rs_failed_io(struct drbd_conf
*mdev
, sector_t sector
, int size
)
1259 /* Is called from worker and receiver context _only_ */
1260 unsigned long sbnr
, ebnr
, lbnr
;
1261 unsigned long count
;
1262 sector_t esector
, nr_sectors
;
1265 if (size
<= 0 || (size
& 0x1ff) != 0 || size
> DRBD_MAX_BIO_SIZE
) {
1266 dev_err(DEV
, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1267 (unsigned long long)sector
, size
);
1270 nr_sectors
= drbd_get_capacity(mdev
->this_bdev
);
1271 esector
= sector
+ (size
>> 9) - 1;
1273 ERR_IF(sector
>= nr_sectors
) return;
1274 ERR_IF(esector
>= nr_sectors
) esector
= (nr_sectors
-1);
1276 lbnr
= BM_SECT_TO_BIT(nr_sectors
-1);
1279 * round up start sector, round down end sector. we make sure we only
1280 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1281 if (unlikely(esector
< BM_SECT_PER_BIT
-1))
1283 if (unlikely(esector
== (nr_sectors
-1)))
1286 ebnr
= BM_SECT_TO_BIT(esector
- (BM_SECT_PER_BIT
-1));
1287 sbnr
= BM_SECT_TO_BIT(sector
+ BM_SECT_PER_BIT
-1);
1293 * ok, (capacity & 7) != 0 sometimes, but who cares...
1294 * we count rs_{total,left} in bits, not sectors.
1296 spin_lock_irq(&mdev
->al_lock
);
1297 count
= drbd_bm_count_bits(mdev
, sbnr
, ebnr
);
1299 mdev
->rs_failed
+= count
;
1301 if (get_ldev(mdev
)) {
1302 drbd_try_clear_on_disk_bm(mdev
, sector
, count
, false);
1306 /* just wake_up unconditional now, various lc_chaged(),
1307 * lc_put() in drbd_try_clear_on_disk_bm(). */
1310 spin_unlock_irq(&mdev
->al_lock
);
1312 wake_up(&mdev
->al_wait
);