blob: 528342c82ba1a8dec903b4302820216abd6a2e31 [file] [log] [blame]
Philipp Reisnerb411b362009-09-25 16:07:19 -07001/*
2 drbd_actlog.c
3
4 This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
5
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>.
9
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.
14
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.
19
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.
23
24 */
25
26#include <linux/slab.h>
27#include <linux/drbd.h>
28#include "drbd_int.h"
Philipp Reisnerb411b362009-09-25 16:07:19 -070029#include "drbd_wrappers.h"
30
Bart Van Assche24c48302011-05-21 18:32:29 +020031/* We maintain a trivial checksum in our on disk activity log.
Philipp Reisnerb411b362009-09-25 16:07:19 -070032 * With that we can ensure correct operation even when the storage
Lucas De Marchi25985ed2011-03-30 22:57:33 -030033 * device might do a partial (last) sector write while losing power.
Philipp Reisnerb411b362009-09-25 16:07:19 -070034 */
35struct __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;
42};
43
44struct update_odbm_work {
45 struct drbd_work w;
46 unsigned int enr;
47};
48
49struct 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;
56};
57
58struct drbd_atodb_wait {
59 atomic_t count;
60 struct completion io_done;
61 struct drbd_conf *mdev;
62 int error;
63};
64
65
66int w_al_write_transaction(struct drbd_conf *, struct drbd_work *, int);
67
Philipp Reisnere1711732011-06-27 11:51:46 +020068void *drbd_md_get_buffer(struct drbd_conf *mdev)
69{
70 int r;
71
72 wait_event(mdev->misc_wait,
73 (r = atomic_cmpxchg(&mdev->md_io_in_use, 0, 1)) == 0 ||
74 mdev->state.disk <= D_FAILED);
75
76 return r ? NULL : page_address(mdev->md_io_page);
77}
78
79void drbd_md_put_buffer(struct drbd_conf *mdev)
80{
81 if (atomic_dec_and_test(&mdev->md_io_in_use))
82 wake_up(&mdev->misc_wait);
83}
84
Philipp Reisner0c464422011-06-26 22:26:31 +020085static bool md_io_allowed(struct drbd_conf *mdev)
86{
87 enum drbd_disk_state ds = mdev->state.disk;
88 return ds >= D_NEGOTIATING || ds == D_ATTACHING;
89}
90
91void wait_until_done_or_disk_failure(struct drbd_conf *mdev, unsigned int *done)
92{
93 wait_event(mdev->misc_wait, *done || !md_io_allowed(mdev));
94}
95
Philipp Reisnerb411b362009-09-25 16:07:19 -070096static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
97 struct drbd_backing_dev *bdev,
98 struct page *page, sector_t sector,
99 int rw, int size)
100{
101 struct bio *bio;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700102 int ok;
103
Philipp Reisner0c464422011-06-26 22:26:31 +0200104 mdev->md_io.done = 0;
105 mdev->md_io.error = -ENODEV;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700106
Philipp Reisnera8a4e512010-08-25 10:21:04 +0200107 if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
Lars Ellenberg86e1e982011-06-28 13:22:48 +0200108 rw |= REQ_FUA | REQ_FLUSH;
Jens Axboe721a9602011-03-09 11:56:30 +0100109 rw |= REQ_SYNC;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700110
Philipp Reisnerb411b362009-09-25 16:07:19 -0700111 bio = bio_alloc(GFP_NOIO, 1);
112 bio->bi_bdev = bdev->md_bdev;
113 bio->bi_sector = sector;
114 ok = (bio_add_page(bio, page, size, 0) == size);
115 if (!ok)
116 goto out;
Philipp Reisnercc94c652011-06-26 11:20:27 +0200117 bio->bi_private = &mdev->md_io;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700118 bio->bi_end_io = drbd_md_io_complete;
119 bio->bi_rw = rw;
120
Philipp Reisner4a2fe562011-07-04 11:14:31 +0200121 bio_get(bio); /* one bio_put() is in the completion handler */
Philipp Reisnere1711732011-06-27 11:51:46 +0200122 atomic_inc(&mdev->md_io_in_use); /* drbd_md_put_buffer() is in the completion handler */
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +0100123 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700124 bio_endio(bio, -EIO);
125 else
126 submit_bio(rw, bio);
Philipp Reisner0c464422011-06-26 22:26:31 +0200127 wait_until_done_or_disk_failure(mdev, &mdev->md_io.done);
Philipp Reisnercc94c652011-06-26 11:20:27 +0200128 ok = bio_flagged(bio, BIO_UPTODATE) && mdev->md_io.error == 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700129
Philipp Reisnerb411b362009-09-25 16:07:19 -0700130 out:
131 bio_put(bio);
132 return ok;
133}
134
135int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
136 sector_t sector, int rw)
137{
138 int logical_block_size, mask, ok;
139 int offset = 0;
140 struct page *iop = mdev->md_io_page;
141
Philipp Reisnere1711732011-06-27 11:51:46 +0200142 D_ASSERT(atomic_read(&mdev->md_io_in_use) == 1);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700143
144 BUG_ON(!bdev->md_bdev);
145
146 logical_block_size = bdev_logical_block_size(bdev->md_bdev);
147 if (logical_block_size == 0)
148 logical_block_size = MD_SECTOR_SIZE;
149
150 /* in case logical_block_size != 512 [ s390 only? ] */
151 if (logical_block_size != MD_SECTOR_SIZE) {
152 mask = (logical_block_size / MD_SECTOR_SIZE) - 1;
153 D_ASSERT(mask == 1 || mask == 3 || mask == 7);
154 D_ASSERT(logical_block_size == (mask+1) * MD_SECTOR_SIZE);
155 offset = sector & mask;
156 sector = sector & ~mask;
157 iop = mdev->md_io_tmpp;
158
159 if (rw & WRITE) {
160 /* these are GFP_KERNEL pages, pre-allocated
161 * on device initialization */
162 void *p = page_address(mdev->md_io_page);
163 void *hp = page_address(mdev->md_io_tmpp);
164
165 ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector,
166 READ, logical_block_size);
167
168 if (unlikely(!ok)) {
169 dev_err(DEV, "drbd_md_sync_page_io(,%llus,"
170 "READ [logical_block_size!=512]) failed!\n",
171 (unsigned long long)sector);
172 return 0;
173 }
174
175 memcpy(hp + offset*MD_SECTOR_SIZE, p, MD_SECTOR_SIZE);
176 }
177 }
178
179 if (sector < drbd_md_first_sector(bdev) ||
180 sector > drbd_md_last_sector(bdev))
181 dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
182 current->comm, current->pid, __func__,
183 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
184
185 ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, logical_block_size);
186 if (unlikely(!ok)) {
187 dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed!\n",
188 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
189 return 0;
190 }
191
192 if (logical_block_size != MD_SECTOR_SIZE && !(rw & WRITE)) {
193 void *p = page_address(mdev->md_io_page);
194 void *hp = page_address(mdev->md_io_tmpp);
195
196 memcpy(p, hp + offset*MD_SECTOR_SIZE, MD_SECTOR_SIZE);
197 }
198
199 return ok;
200}
201
202static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
203{
204 struct lc_element *al_ext;
205 struct lc_element *tmp;
206 unsigned long al_flags = 0;
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100207 int wake;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700208
209 spin_lock_irq(&mdev->al_lock);
210 tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
211 if (unlikely(tmp != NULL)) {
212 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
213 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100214 wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700215 spin_unlock_irq(&mdev->al_lock);
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100216 if (wake)
217 wake_up(&mdev->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700218 return NULL;
219 }
220 }
221 al_ext = lc_get(mdev->act_log, enr);
222 al_flags = mdev->act_log->flags;
223 spin_unlock_irq(&mdev->al_lock);
224
225 /*
226 if (!al_ext) {
227 if (al_flags & LC_STARVING)
228 dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n");
229 if (al_flags & LC_DIRTY)
230 dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n");
231 }
232 */
233
234 return al_ext;
235}
236
237void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
238{
239 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
240 struct lc_element *al_ext;
241 struct update_al_work al_work;
242
243 D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
244
Philipp Reisnerb411b362009-09-25 16:07:19 -0700245 wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
246
247 if (al_ext->lc_number != enr) {
248 /* drbd_al_write_transaction(mdev,al_ext,enr);
249 * recurses into generic_make_request(), which
250 * disallows recursion, bios being serialized on the
251 * current->bio_tail list now.
252 * we have to delegate updates to the activity log
253 * to the worker thread. */
254 init_completion(&al_work.event);
255 al_work.al_ext = al_ext;
256 al_work.enr = enr;
257 al_work.old_enr = al_ext->lc_number;
258 al_work.w.cb = w_al_write_transaction;
259 drbd_queue_work_front(&mdev->data.work, &al_work.w);
260 wait_for_completion(&al_work.event);
261
262 mdev->al_writ_cnt++;
263
264 spin_lock_irq(&mdev->al_lock);
265 lc_changed(mdev->act_log, al_ext);
266 spin_unlock_irq(&mdev->al_lock);
267 wake_up(&mdev->al_wait);
268 }
269}
270
271void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
272{
273 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
274 struct lc_element *extent;
275 unsigned long flags;
276
Philipp Reisnerb411b362009-09-25 16:07:19 -0700277 spin_lock_irqsave(&mdev->al_lock, flags);
278
279 extent = lc_find(mdev->act_log, enr);
280
281 if (!extent) {
282 spin_unlock_irqrestore(&mdev->al_lock, flags);
283 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
284 return;
285 }
286
287 if (lc_put(mdev->act_log, extent) == 0)
288 wake_up(&mdev->al_wait);
289
290 spin_unlock_irqrestore(&mdev->al_lock, flags);
291}
292
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100293#if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
294/* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
295 * are still coupled, or assume too much about their relation.
296 * Code below will not work if this is violated.
297 * Will be cleaned up with some followup patch.
298 */
299# error FIXME
300#endif
301
302static unsigned int al_extent_to_bm_page(unsigned int al_enr)
303{
304 return al_enr >>
305 /* bit to page */
306 ((PAGE_SHIFT + 3) -
307 /* al extent number to bit */
308 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
309}
310
311static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
312{
313 return rs_enr >>
314 /* bit to page */
315 ((PAGE_SHIFT + 3) -
316 /* al extent number to bit */
317 (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
318}
319
Philipp Reisnerb411b362009-09-25 16:07:19 -0700320int
321w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused)
322{
323 struct update_al_work *aw = container_of(w, struct update_al_work, w);
324 struct lc_element *updated = aw->al_ext;
325 const unsigned int new_enr = aw->enr;
326 const unsigned int evicted = aw->old_enr;
327 struct al_transaction *buffer;
328 sector_t sector;
329 int i, n, mx;
330 unsigned int extent_nr;
331 u32 xor_sum = 0;
332
333 if (!get_ldev(mdev)) {
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200334 dev_err(DEV,
335 "disk is %s, cannot start al transaction (-%d +%d)\n",
336 drbd_disk_str(mdev->state.disk), evicted, new_enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700337 complete(&((struct update_al_work *)w)->event);
338 return 1;
339 }
340 /* do we have to do a bitmap write, first?
341 * TODO reduce maximum latency:
342 * submit both bios, then wait for both,
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200343 * instead of doing two synchronous sector writes.
344 * For now, we must not write the transaction,
345 * if we cannot write out the bitmap of the evicted extent. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700346 if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100347 drbd_bm_write_page(mdev, al_extent_to_bm_page(evicted));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700348
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200349 /* The bitmap write may have failed, causing a state change. */
350 if (mdev->state.disk < D_INCONSISTENT) {
351 dev_err(DEV,
352 "disk is %s, cannot write al transaction (-%d +%d)\n",
353 drbd_disk_str(mdev->state.disk), evicted, new_enr);
354 complete(&((struct update_al_work *)w)->event);
355 put_ldev(mdev);
356 return 1;
357 }
358
Philipp Reisnere1711732011-06-27 11:51:46 +0200359 buffer = drbd_md_get_buffer(mdev); /* protects md_io_buffer, al_tr_cycle, ... */
360 if (!buffer) {
361 dev_err(DEV, "disk failed while waiting for md_io buffer\n");
362 complete(&((struct update_al_work *)w)->event);
363 put_ldev(mdev);
364 return 1;
365 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700366
367 buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
368 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
369
370 n = lc_index_of(mdev->act_log, updated);
371
372 buffer->updates[0].pos = cpu_to_be32(n);
373 buffer->updates[0].extent = cpu_to_be32(new_enr);
374
375 xor_sum ^= new_enr;
376
377 mx = min_t(int, AL_EXTENTS_PT,
378 mdev->act_log->nr_elements - mdev->al_tr_cycle);
379 for (i = 0; i < mx; i++) {
380 unsigned idx = mdev->al_tr_cycle + i;
381 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
382 buffer->updates[i+1].pos = cpu_to_be32(idx);
383 buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
384 xor_sum ^= extent_nr;
385 }
386 for (; i < AL_EXTENTS_PT; i++) {
387 buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
388 buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
389 xor_sum ^= LC_FREE;
390 }
391 mdev->al_tr_cycle += AL_EXTENTS_PT;
392 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
393 mdev->al_tr_cycle = 0;
394
395 buffer->xor_sum = cpu_to_be32(xor_sum);
396
397 sector = mdev->ldev->md.md_offset
398 + mdev->ldev->md.al_offset + mdev->al_tr_pos;
399
400 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100401 drbd_chk_io_error(mdev, 1, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700402
403 if (++mdev->al_tr_pos >
404 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
405 mdev->al_tr_pos = 0;
406
407 D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
408 mdev->al_tr_number++;
409
Philipp Reisnere1711732011-06-27 11:51:46 +0200410 drbd_md_put_buffer(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700411
412 complete(&((struct update_al_work *)w)->event);
413 put_ldev(mdev);
414
415 return 1;
416}
417
418/**
419 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
420 * @mdev: DRBD device.
421 * @bdev: Block device to read form.
422 * @b: pointer to an al_transaction.
423 * @index: On disk slot of the transaction to read.
424 *
425 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
426 */
427static int drbd_al_read_tr(struct drbd_conf *mdev,
428 struct drbd_backing_dev *bdev,
429 struct al_transaction *b,
430 int index)
431{
432 sector_t sector;
433 int rv, i;
434 u32 xor_sum = 0;
435
436 sector = bdev->md.md_offset + bdev->md.al_offset + index;
437
438 /* Dont process error normally,
439 * as this is done before disk is attached! */
440 if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
441 return -1;
442
443 rv = (be32_to_cpu(b->magic) == DRBD_MAGIC);
444
445 for (i = 0; i < AL_EXTENTS_PT + 1; i++)
446 xor_sum ^= be32_to_cpu(b->updates[i].extent);
447 rv &= (xor_sum == be32_to_cpu(b->xor_sum));
448
449 return rv;
450}
451
452/**
453 * drbd_al_read_log() - Restores the activity log from its on disk representation.
454 * @mdev: DRBD device.
455 * @bdev: Block device to read form.
456 *
457 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
458 */
459int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
460{
461 struct al_transaction *buffer;
462 int i;
463 int rv;
464 int mx;
465 int active_extents = 0;
466 int transactions = 0;
467 int found_valid = 0;
468 int from = 0;
469 int to = 0;
470 u32 from_tnr = 0;
471 u32 to_tnr = 0;
472 u32 cnr;
473
474 mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
475
476 /* lock out all other meta data io for now,
477 * and make sure the page is mapped.
478 */
Philipp Reisnere1711732011-06-27 11:51:46 +0200479 buffer = drbd_md_get_buffer(mdev);
480 if (!buffer)
481 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700482
483 /* Find the valid transaction in the log */
484 for (i = 0; i <= mx; i++) {
485 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
486 if (rv == 0)
487 continue;
488 if (rv == -1) {
Philipp Reisnere1711732011-06-27 11:51:46 +0200489 drbd_md_put_buffer(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700490 return 0;
491 }
492 cnr = be32_to_cpu(buffer->tr_number);
493
494 if (++found_valid == 1) {
495 from = i;
496 to = i;
497 from_tnr = cnr;
498 to_tnr = cnr;
499 continue;
500 }
501 if ((int)cnr - (int)from_tnr < 0) {
502 D_ASSERT(from_tnr - cnr + i - from == mx+1);
503 from = i;
504 from_tnr = cnr;
505 }
506 if ((int)cnr - (int)to_tnr > 0) {
507 D_ASSERT(cnr - to_tnr == i - to);
508 to = i;
509 to_tnr = cnr;
510 }
511 }
512
513 if (!found_valid) {
514 dev_warn(DEV, "No usable activity log found.\n");
Philipp Reisnere1711732011-06-27 11:51:46 +0200515 drbd_md_put_buffer(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700516 return 1;
517 }
518
519 /* Read the valid transactions.
520 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
521 i = from;
522 while (1) {
523 int j, pos;
524 unsigned int extent_nr;
525 unsigned int trn;
526
527 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
528 ERR_IF(rv == 0) goto cancel;
529 if (rv == -1) {
Philipp Reisnere1711732011-06-27 11:51:46 +0200530 drbd_md_put_buffer(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700531 return 0;
532 }
533
534 trn = be32_to_cpu(buffer->tr_number);
535
536 spin_lock_irq(&mdev->al_lock);
537
538 /* This loop runs backwards because in the cyclic
539 elements there might be an old version of the
540 updated element (in slot 0). So the element in slot 0
541 can overwrite old versions. */
542 for (j = AL_EXTENTS_PT; j >= 0; j--) {
543 pos = be32_to_cpu(buffer->updates[j].pos);
544 extent_nr = be32_to_cpu(buffer->updates[j].extent);
545
546 if (extent_nr == LC_FREE)
547 continue;
548
549 lc_set(mdev->act_log, extent_nr, pos);
550 active_extents++;
551 }
552 spin_unlock_irq(&mdev->al_lock);
553
554 transactions++;
555
556cancel:
557 if (i == to)
558 break;
559 i++;
560 if (i > mx)
561 i = 0;
562 }
563
564 mdev->al_tr_number = to_tnr+1;
565 mdev->al_tr_pos = to;
566 if (++mdev->al_tr_pos >
567 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
568 mdev->al_tr_pos = 0;
569
570 /* ok, we are done with it */
Philipp Reisnere1711732011-06-27 11:51:46 +0200571 drbd_md_put_buffer(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700572
573 dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
574 transactions, active_extents);
575
576 return 1;
577}
578
Philipp Reisnerb411b362009-09-25 16:07:19 -0700579/**
Philipp Reisnerb411b362009-09-25 16:07:19 -0700580 * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
581 * @mdev: DRBD device.
582 */
583void drbd_al_apply_to_bm(struct drbd_conf *mdev)
584{
585 unsigned int enr;
586 unsigned long add = 0;
587 char ppb[10];
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200588 int i, tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700589
590 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
591
592 for (i = 0; i < mdev->act_log->nr_elements; i++) {
593 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
594 if (enr == LC_FREE)
595 continue;
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200596 tmp = drbd_bm_ALe_set_all(mdev, enr);
597 dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
598 add += tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700599 }
600
601 lc_unlock(mdev->act_log);
602 wake_up(&mdev->al_wait);
603
604 dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
605 ppsize(ppb, Bit2KB(add)));
606}
607
608static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
609{
610 int rv;
611
612 spin_lock_irq(&mdev->al_lock);
613 rv = (al_ext->refcnt == 0);
614 if (likely(rv))
615 lc_del(mdev->act_log, al_ext);
616 spin_unlock_irq(&mdev->al_lock);
617
618 return rv;
619}
620
621/**
622 * drbd_al_shrink() - Removes all active extents form the activity log
623 * @mdev: DRBD device.
624 *
625 * Removes all active extents form the activity log, waiting until
626 * the reference count of each entry dropped to 0 first, of course.
627 *
628 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
629 */
630void drbd_al_shrink(struct drbd_conf *mdev)
631{
632 struct lc_element *al_ext;
633 int i;
634
635 D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
636
637 for (i = 0; i < mdev->act_log->nr_elements; i++) {
638 al_ext = lc_element_by_index(mdev->act_log, i);
639 if (al_ext->lc_number == LC_FREE)
640 continue;
641 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
642 }
643
644 wake_up(&mdev->al_wait);
645}
646
647static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
648{
649 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
650
651 if (!get_ldev(mdev)) {
652 if (__ratelimit(&drbd_ratelimit_state))
653 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
654 kfree(udw);
655 return 1;
656 }
657
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100658 drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700659 put_ldev(mdev);
660
661 kfree(udw);
662
663 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
664 switch (mdev->state.conn) {
665 case C_SYNC_SOURCE: case C_SYNC_TARGET:
666 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
667 drbd_resync_finished(mdev);
668 default:
669 /* nothing to do */
670 break;
671 }
672 }
673 drbd_bcast_sync_progress(mdev);
674
675 return 1;
676}
677
678
679/* ATTENTION. The AL's extents are 4MB each, while the extents in the
680 * resync LRU-cache are 16MB each.
681 * The caller of this function has to hold an get_ldev() reference.
682 *
683 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
684 */
685static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
686 int count, int success)
687{
688 struct lc_element *e;
689 struct update_odbm_work *udw;
690
691 unsigned int enr;
692
693 D_ASSERT(atomic_read(&mdev->local_cnt));
694
695 /* I simply assume that a sector/size pair never crosses
696 * a 16 MB extent border. (Currently this is true...) */
697 enr = BM_SECT_TO_EXT(sector);
698
699 e = lc_get(mdev->resync, enr);
700 if (e) {
701 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
702 if (ext->lce.lc_number == enr) {
703 if (success)
704 ext->rs_left -= count;
705 else
706 ext->rs_failed += count;
707 if (ext->rs_left < ext->rs_failed) {
708 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
709 "rs_failed=%d count=%d\n",
710 (unsigned long long)sector,
711 ext->lce.lc_number, ext->rs_left,
712 ext->rs_failed, count);
713 dump_stack();
714
715 lc_put(mdev->resync, &ext->lce);
716 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
717 return;
718 }
719 } else {
720 /* Normally this element should be in the cache,
721 * since drbd_rs_begin_io() pulled it already in.
722 *
723 * But maybe an application write finished, and we set
724 * something outside the resync lru_cache in sync.
725 */
726 int rs_left = drbd_bm_e_weight(mdev, enr);
727 if (ext->flags != 0) {
728 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
729 " -> %d[%u;00]\n",
730 ext->lce.lc_number, ext->rs_left,
731 ext->flags, enr, rs_left);
732 ext->flags = 0;
733 }
734 if (ext->rs_failed) {
735 dev_warn(DEV, "Kicking resync_lru element enr=%u "
736 "out with rs_failed=%d\n",
737 ext->lce.lc_number, ext->rs_failed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700738 }
739 ext->rs_left = rs_left;
740 ext->rs_failed = success ? 0 : count;
741 lc_changed(mdev->resync, &ext->lce);
742 }
743 lc_put(mdev->resync, &ext->lce);
744 /* no race, we are within the al_lock! */
745
746 if (ext->rs_left == ext->rs_failed) {
747 ext->rs_failed = 0;
748
749 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
750 if (udw) {
751 udw->enr = ext->lce.lc_number;
752 udw->w.cb = w_update_odbm;
753 drbd_queue_work_front(&mdev->data.work, &udw->w);
754 } else {
755 dev_warn(DEV, "Could not kmalloc an udw\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700756 }
757 }
758 } else {
759 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
760 mdev->resync_locked,
761 mdev->resync->nr_elements,
762 mdev->resync->flags);
763 }
764}
765
Lars Ellenbergc6ea14d2010-11-05 09:23:37 +0100766void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
767{
768 unsigned long now = jiffies;
769 unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
770 int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
771 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
772 if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
773 mdev->state.conn != C_PAUSED_SYNC_T &&
774 mdev->state.conn != C_PAUSED_SYNC_S) {
775 mdev->rs_mark_time[next] = now;
776 mdev->rs_mark_left[next] = still_to_go;
777 mdev->rs_last_mark = next;
778 }
779 }
780}
781
Philipp Reisnerb411b362009-09-25 16:07:19 -0700782/* clear the bit corresponding to the piece of storage in question:
783 * size byte of data starting from sector. Only clear a bits of the affected
784 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
785 *
786 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
787 *
788 */
789void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
790 const char *file, const unsigned int line)
791{
792 /* Is called from worker and receiver context _only_ */
793 unsigned long sbnr, ebnr, lbnr;
794 unsigned long count = 0;
795 sector_t esector, nr_sectors;
796 int wake_up = 0;
797 unsigned long flags;
798
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100799 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700800 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
801 (unsigned long long)sector, size);
802 return;
803 }
804 nr_sectors = drbd_get_capacity(mdev->this_bdev);
805 esector = sector + (size >> 9) - 1;
806
807 ERR_IF(sector >= nr_sectors) return;
808 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
809
810 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
811
812 /* we clear it (in sync).
813 * round up start sector, round down end sector. we make sure we only
814 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
815 if (unlikely(esector < BM_SECT_PER_BIT-1))
816 return;
817 if (unlikely(esector == (nr_sectors-1)))
818 ebnr = lbnr;
819 else
820 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
821 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
822
Philipp Reisnerb411b362009-09-25 16:07:19 -0700823 if (sbnr > ebnr)
824 return;
825
826 /*
827 * ok, (capacity & 7) != 0 sometimes, but who cares...
828 * we count rs_{total,left} in bits, not sectors.
829 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700830 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200831 if (count && get_ldev(mdev)) {
Lars Ellenbergc6ea14d2010-11-05 09:23:37 +0100832 drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200833 spin_lock_irqsave(&mdev->al_lock, flags);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100834 drbd_try_clear_on_disk_bm(mdev, sector, count, true);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200835 spin_unlock_irqrestore(&mdev->al_lock, flags);
836
Philipp Reisnerb411b362009-09-25 16:07:19 -0700837 /* just wake_up unconditional now, various lc_chaged(),
838 * lc_put() in drbd_try_clear_on_disk_bm(). */
839 wake_up = 1;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200840 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700841 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700842 if (wake_up)
843 wake_up(&mdev->al_wait);
844}
845
846/*
847 * this is intended to set one request worth of data out of sync.
848 * affects at least 1 bit,
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100849 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700850 *
851 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
852 * so this can be _any_ process.
853 */
Philipp Reisner73a01a12010-10-27 14:33:00 +0200854int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700855 const char *file, const unsigned int line)
856{
857 unsigned long sbnr, ebnr, lbnr, flags;
858 sector_t esector, nr_sectors;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200859 unsigned int enr, count = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700860 struct lc_element *e;
861
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100862 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700863 dev_err(DEV, "sector: %llus, size: %d\n",
864 (unsigned long long)sector, size);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200865 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700866 }
867
868 if (!get_ldev(mdev))
Philipp Reisner73a01a12010-10-27 14:33:00 +0200869 return 0; /* no disk, no metadata, no bitmap to set bits in */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700870
871 nr_sectors = drbd_get_capacity(mdev->this_bdev);
872 esector = sector + (size >> 9) - 1;
873
874 ERR_IF(sector >= nr_sectors)
875 goto out;
876 ERR_IF(esector >= nr_sectors)
877 esector = (nr_sectors-1);
878
879 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
880
881 /* we set it out of sync,
882 * we do not need to round anything here */
883 sbnr = BM_SECT_TO_BIT(sector);
884 ebnr = BM_SECT_TO_BIT(esector);
885
Philipp Reisnerb411b362009-09-25 16:07:19 -0700886 /* ok, (capacity & 7) != 0 sometimes, but who cares...
887 * we count rs_{total,left} in bits, not sectors. */
888 spin_lock_irqsave(&mdev->al_lock, flags);
889 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
890
891 enr = BM_SECT_TO_EXT(sector);
892 e = lc_find(mdev->resync, enr);
893 if (e)
894 lc_entry(e, struct bm_extent, lce)->rs_left += count;
895 spin_unlock_irqrestore(&mdev->al_lock, flags);
896
897out:
898 put_ldev(mdev);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200899
900 return count;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700901}
902
903static
904struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
905{
906 struct lc_element *e;
907 struct bm_extent *bm_ext;
908 int wakeup = 0;
909 unsigned long rs_flags;
910
911 spin_lock_irq(&mdev->al_lock);
912 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
913 spin_unlock_irq(&mdev->al_lock);
914 return NULL;
915 }
916 e = lc_get(mdev->resync, enr);
917 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
918 if (bm_ext) {
919 if (bm_ext->lce.lc_number != enr) {
920 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
921 bm_ext->rs_failed = 0;
922 lc_changed(mdev->resync, &bm_ext->lce);
923 wakeup = 1;
924 }
925 if (bm_ext->lce.refcnt == 1)
926 mdev->resync_locked++;
927 set_bit(BME_NO_WRITES, &bm_ext->flags);
928 }
929 rs_flags = mdev->resync->flags;
930 spin_unlock_irq(&mdev->al_lock);
931 if (wakeup)
932 wake_up(&mdev->al_wait);
933
934 if (!bm_ext) {
935 if (rs_flags & LC_STARVING)
936 dev_warn(DEV, "Have to wait for element"
937 " (resync LRU too small?)\n");
938 BUG_ON(rs_flags & LC_DIRTY);
939 }
940
941 return bm_ext;
942}
943
944static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
945{
946 struct lc_element *al_ext;
947 int rv = 0;
948
949 spin_lock_irq(&mdev->al_lock);
950 if (unlikely(enr == mdev->act_log->new_number))
951 rv = 1;
952 else {
953 al_ext = lc_find(mdev->act_log, enr);
954 if (al_ext) {
955 if (al_ext->refcnt)
956 rv = 1;
957 }
958 }
959 spin_unlock_irq(&mdev->al_lock);
960
961 /*
962 if (unlikely(rv)) {
963 dev_info(DEV, "Delaying sync read until app's write is done\n");
964 }
965 */
966 return rv;
967}
968
969/**
970 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
971 * @mdev: DRBD device.
972 * @sector: The sector number.
973 *
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200974 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700975 */
976int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
977{
978 unsigned int enr = BM_SECT_TO_EXT(sector);
979 struct bm_extent *bm_ext;
980 int i, sig;
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100981 int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
982 200 times -> 20 seconds. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700983
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100984retry:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985 sig = wait_event_interruptible(mdev->al_wait,
986 (bm_ext = _bme_get(mdev, enr)));
987 if (sig)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200988 return -EINTR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700989
990 if (test_bit(BME_LOCKED, &bm_ext->flags))
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200991 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700992
993 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
994 sig = wait_event_interruptible(mdev->al_wait,
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100995 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
Philipp Reisnerc507f462010-11-22 15:49:17 +0100996 test_bit(BME_PRIORITY, &bm_ext->flags));
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100997
998 if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700999 spin_lock_irq(&mdev->al_lock);
1000 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001001 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001002 mdev->resync_locked--;
1003 wake_up(&mdev->al_wait);
1004 }
1005 spin_unlock_irq(&mdev->al_lock);
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001006 if (sig)
1007 return -EINTR;
1008 if (schedule_timeout_interruptible(HZ/10))
1009 return -EINTR;
Philipp Reisnerc507f462010-11-22 15:49:17 +01001010 if (sa && --sa == 0)
1011 dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
1012 "Resync stalled?\n");
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001013 goto retry;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001014 }
1015 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001016 set_bit(BME_LOCKED, &bm_ext->flags);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001017 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001018}
1019
1020/**
1021 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1022 * @mdev: DRBD device.
1023 * @sector: The sector number.
1024 *
1025 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1026 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1027 * if there is still application IO going on in this area.
1028 */
1029int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1030{
1031 unsigned int enr = BM_SECT_TO_EXT(sector);
1032 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1033 struct lc_element *e;
1034 struct bm_extent *bm_ext;
1035 int i;
1036
Philipp Reisnerb411b362009-09-25 16:07:19 -07001037 spin_lock_irq(&mdev->al_lock);
1038 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1039 /* in case you have very heavy scattered io, it may
1040 * stall the syncer undefined if we give up the ref count
1041 * when we try again and requeue.
1042 *
1043 * if we don't give up the refcount, but the next time
1044 * we are scheduled this extent has been "synced" by new
1045 * application writes, we'd miss the lc_put on the
1046 * extent we keep the refcount on.
1047 * so we remembered which extent we had to try again, and
1048 * if the next requested one is something else, we do
1049 * the lc_put here...
1050 * we also have to wake_up
1051 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001052 e = lc_find(mdev->resync, mdev->resync_wenr);
1053 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1054 if (bm_ext) {
1055 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1056 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1057 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1058 mdev->resync_wenr = LC_FREE;
1059 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1060 mdev->resync_locked--;
1061 wake_up(&mdev->al_wait);
1062 } else {
1063 dev_alert(DEV, "LOGIC BUG\n");
1064 }
1065 }
1066 /* TRY. */
1067 e = lc_try_get(mdev->resync, enr);
1068 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1069 if (bm_ext) {
1070 if (test_bit(BME_LOCKED, &bm_ext->flags))
1071 goto proceed;
1072 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1073 mdev->resync_locked++;
1074 } else {
1075 /* we did set the BME_NO_WRITES,
1076 * but then could not set BME_LOCKED,
1077 * so we tried again.
1078 * drop the extra reference. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001079 bm_ext->lce.refcnt--;
1080 D_ASSERT(bm_ext->lce.refcnt > 0);
1081 }
1082 goto check_al;
1083 } else {
1084 /* do we rather want to try later? */
Jens Axboe6a0afdf2009-10-01 09:04:14 +02001085 if (mdev->resync_locked > mdev->resync->nr_elements-3)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001086 goto try_again;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001087 /* Do or do not. There is no try. -- Yoda */
1088 e = lc_get(mdev->resync, enr);
1089 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1090 if (!bm_ext) {
1091 const unsigned long rs_flags = mdev->resync->flags;
1092 if (rs_flags & LC_STARVING)
1093 dev_warn(DEV, "Have to wait for element"
1094 " (resync LRU too small?)\n");
1095 BUG_ON(rs_flags & LC_DIRTY);
1096 goto try_again;
1097 }
1098 if (bm_ext->lce.lc_number != enr) {
1099 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1100 bm_ext->rs_failed = 0;
1101 lc_changed(mdev->resync, &bm_ext->lce);
1102 wake_up(&mdev->al_wait);
1103 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1104 }
1105 set_bit(BME_NO_WRITES, &bm_ext->flags);
1106 D_ASSERT(bm_ext->lce.refcnt == 1);
1107 mdev->resync_locked++;
1108 goto check_al;
1109 }
1110check_al:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001111 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1112 if (unlikely(al_enr+i == mdev->act_log->new_number))
1113 goto try_again;
1114 if (lc_is_used(mdev->act_log, al_enr+i))
1115 goto try_again;
1116 }
1117 set_bit(BME_LOCKED, &bm_ext->flags);
1118proceed:
1119 mdev->resync_wenr = LC_FREE;
1120 spin_unlock_irq(&mdev->al_lock);
1121 return 0;
1122
1123try_again:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001124 if (bm_ext)
1125 mdev->resync_wenr = enr;
1126 spin_unlock_irq(&mdev->al_lock);
1127 return -EAGAIN;
1128}
1129
1130void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1131{
1132 unsigned int enr = BM_SECT_TO_EXT(sector);
1133 struct lc_element *e;
1134 struct bm_extent *bm_ext;
1135 unsigned long flags;
1136
Philipp Reisnerb411b362009-09-25 16:07:19 -07001137 spin_lock_irqsave(&mdev->al_lock, flags);
1138 e = lc_find(mdev->resync, enr);
1139 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1140 if (!bm_ext) {
1141 spin_unlock_irqrestore(&mdev->al_lock, flags);
1142 if (__ratelimit(&drbd_ratelimit_state))
1143 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1144 return;
1145 }
1146
1147 if (bm_ext->lce.refcnt == 0) {
1148 spin_unlock_irqrestore(&mdev->al_lock, flags);
1149 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1150 "but refcnt is 0!?\n",
1151 (unsigned long long)sector, enr);
1152 return;
1153 }
1154
1155 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
Philipp Reisnere3555d82010-11-07 15:56:29 +01001156 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001157 mdev->resync_locked--;
1158 wake_up(&mdev->al_wait);
1159 }
1160
1161 spin_unlock_irqrestore(&mdev->al_lock, flags);
1162}
1163
1164/**
1165 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1166 * @mdev: DRBD device.
1167 */
1168void drbd_rs_cancel_all(struct drbd_conf *mdev)
1169{
Philipp Reisnerb411b362009-09-25 16:07:19 -07001170 spin_lock_irq(&mdev->al_lock);
1171
1172 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1173 lc_reset(mdev->resync);
1174 put_ldev(mdev);
1175 }
1176 mdev->resync_locked = 0;
1177 mdev->resync_wenr = LC_FREE;
1178 spin_unlock_irq(&mdev->al_lock);
1179 wake_up(&mdev->al_wait);
1180}
1181
1182/**
1183 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1184 * @mdev: DRBD device.
1185 *
1186 * Returns 0 upon success, -EAGAIN if at least one reference count was
1187 * not zero.
1188 */
1189int drbd_rs_del_all(struct drbd_conf *mdev)
1190{
1191 struct lc_element *e;
1192 struct bm_extent *bm_ext;
1193 int i;
1194
Philipp Reisnerb411b362009-09-25 16:07:19 -07001195 spin_lock_irq(&mdev->al_lock);
1196
1197 if (get_ldev_if_state(mdev, D_FAILED)) {
1198 /* ok, ->resync is there. */
1199 for (i = 0; i < mdev->resync->nr_elements; i++) {
1200 e = lc_element_by_index(mdev->resync, i);
Philipp Reisnerb2b163d2010-04-02 08:40:33 +02001201 bm_ext = lc_entry(e, struct bm_extent, lce);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001202 if (bm_ext->lce.lc_number == LC_FREE)
1203 continue;
1204 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1205 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1206 " got 'synced' by application io\n",
1207 mdev->resync_wenr);
1208 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1209 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1210 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1211 mdev->resync_wenr = LC_FREE;
1212 lc_put(mdev->resync, &bm_ext->lce);
1213 }
1214 if (bm_ext->lce.refcnt != 0) {
1215 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1216 "refcnt=%d\n", bm_ext->lce.refcnt);
1217 put_ldev(mdev);
1218 spin_unlock_irq(&mdev->al_lock);
1219 return -EAGAIN;
1220 }
1221 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1222 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1223 lc_del(mdev->resync, &bm_ext->lce);
1224 }
1225 D_ASSERT(mdev->resync->used == 0);
1226 put_ldev(mdev);
1227 }
1228 spin_unlock_irq(&mdev->al_lock);
1229
1230 return 0;
1231}
1232
1233/**
1234 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1235 * @mdev: DRBD device.
1236 * @sector: The sector number.
1237 * @size: Size of failed IO operation, in byte.
1238 */
1239void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1240{
1241 /* Is called from worker and receiver context _only_ */
1242 unsigned long sbnr, ebnr, lbnr;
1243 unsigned long count;
1244 sector_t esector, nr_sectors;
1245 int wake_up = 0;
1246
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001247 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001248 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1249 (unsigned long long)sector, size);
1250 return;
1251 }
1252 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1253 esector = sector + (size >> 9) - 1;
1254
1255 ERR_IF(sector >= nr_sectors) return;
1256 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
1257
1258 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1259
1260 /*
1261 * round up start sector, round down end sector. we make sure we only
1262 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1263 if (unlikely(esector < BM_SECT_PER_BIT-1))
1264 return;
1265 if (unlikely(esector == (nr_sectors-1)))
1266 ebnr = lbnr;
1267 else
1268 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1269 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1270
1271 if (sbnr > ebnr)
1272 return;
1273
1274 /*
1275 * ok, (capacity & 7) != 0 sometimes, but who cares...
1276 * we count rs_{total,left} in bits, not sectors.
1277 */
1278 spin_lock_irq(&mdev->al_lock);
1279 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1280 if (count) {
1281 mdev->rs_failed += count;
1282
1283 if (get_ldev(mdev)) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001284 drbd_try_clear_on_disk_bm(mdev, sector, count, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001285 put_ldev(mdev);
1286 }
1287
1288 /* just wake_up unconditional now, various lc_chaged(),
1289 * lc_put() in drbd_try_clear_on_disk_bm(). */
1290 wake_up = 1;
1291 }
1292 spin_unlock_irq(&mdev->al_lock);
1293 if (wake_up)
1294 wake_up(&mdev->al_wait);
1295}