blob: 090fc2ce0df48b9d32f3e0f99be179358c179e0e [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
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.
34 */
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 Reisnerb411b362009-09-25 16:07:19 -070068static 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)
72{
73 struct bio *bio;
74 struct drbd_md_io md_io;
75 int ok;
76
77 md_io.mdev = mdev;
78 init_completion(&md_io.event);
79 md_io.error = 0;
80
Philipp Reisnera8a4e512010-08-25 10:21:04 +020081 if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
82 rw |= REQ_FUA;
Jens Axboe721a9602011-03-09 11:56:30 +010083 rw |= REQ_SYNC;
Philipp Reisnerb411b362009-09-25 16:07:19 -070084
Philipp Reisnerb411b362009-09-25 16:07:19 -070085 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;
94
Andreas Gruenbacher0cf9d272010-12-07 10:43:29 +010095 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
Philipp Reisnerb411b362009-09-25 16:07:19 -070096 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;
101
Philipp Reisnerb411b362009-09-25 16:07:19 -0700102 out:
103 bio_put(bio);
104 return ok;
105}
106
107int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
108 sector_t sector, int rw)
109{
110 int logical_block_size, mask, ok;
111 int offset = 0;
112 struct page *iop = mdev->md_io_page;
113
114 D_ASSERT(mutex_is_locked(&mdev->md_io_mutex));
115
116 BUG_ON(!bdev->md_bdev);
117
118 logical_block_size = bdev_logical_block_size(bdev->md_bdev);
119 if (logical_block_size == 0)
120 logical_block_size = MD_SECTOR_SIZE;
121
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;
130
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);
136
137 ok = _drbd_md_sync_page_io(mdev, bdev, iop, sector,
138 READ, logical_block_size);
139
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;
145 }
146
147 memcpy(hp + offset*MD_SECTOR_SIZE, p, MD_SECTOR_SIZE);
148 }
149 }
150
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");
156
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;
162 }
163
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);
167
168 memcpy(p, hp + offset*MD_SECTOR_SIZE, MD_SECTOR_SIZE);
169 }
170
171 return ok;
172}
173
174static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
175{
176 struct lc_element *al_ext;
177 struct lc_element *tmp;
178 unsigned long al_flags = 0;
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100179 int wake;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700180
181 spin_lock_irq(&mdev->al_lock);
182 tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
183 if (unlikely(tmp != NULL)) {
184 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
185 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100186 wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700187 spin_unlock_irq(&mdev->al_lock);
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100188 if (wake)
189 wake_up(&mdev->al_wait);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700190 return NULL;
191 }
192 }
193 al_ext = lc_get(mdev->act_log, enr);
194 al_flags = mdev->act_log->flags;
195 spin_unlock_irq(&mdev->al_lock);
196
197 /*
198 if (!al_ext) {
199 if (al_flags & LC_STARVING)
200 dev_warn(DEV, "Have to wait for LRU element (AL too small?)\n");
201 if (al_flags & LC_DIRTY)
202 dev_warn(DEV, "Ongoing AL update (AL device too slow?)\n");
203 }
204 */
205
206 return al_ext;
207}
208
209void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector)
210{
211 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
212 struct lc_element *al_ext;
213 struct update_al_work al_work;
214
215 D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
216
Philipp Reisnerb411b362009-09-25 16:07:19 -0700217 wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr)));
218
219 if (al_ext->lc_number != enr) {
220 /* drbd_al_write_transaction(mdev,al_ext,enr);
221 * recurses into generic_make_request(), which
222 * disallows recursion, bios being serialized on the
223 * current->bio_tail list now.
224 * we have to delegate updates to the activity log
225 * to the worker thread. */
226 init_completion(&al_work.event);
227 al_work.al_ext = al_ext;
228 al_work.enr = enr;
229 al_work.old_enr = al_ext->lc_number;
230 al_work.w.cb = w_al_write_transaction;
231 drbd_queue_work_front(&mdev->data.work, &al_work.w);
232 wait_for_completion(&al_work.event);
233
234 mdev->al_writ_cnt++;
235
236 spin_lock_irq(&mdev->al_lock);
237 lc_changed(mdev->act_log, al_ext);
238 spin_unlock_irq(&mdev->al_lock);
239 wake_up(&mdev->al_wait);
240 }
241}
242
243void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
244{
245 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
246 struct lc_element *extent;
247 unsigned long flags;
248
Philipp Reisnerb411b362009-09-25 16:07:19 -0700249 spin_lock_irqsave(&mdev->al_lock, flags);
250
251 extent = lc_find(mdev->act_log, enr);
252
253 if (!extent) {
254 spin_unlock_irqrestore(&mdev->al_lock, flags);
255 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
256 return;
257 }
258
259 if (lc_put(mdev->act_log, extent) == 0)
260 wake_up(&mdev->al_wait);
261
262 spin_unlock_irqrestore(&mdev->al_lock, flags);
263}
264
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100265#if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
266/* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
267 * are still coupled, or assume too much about their relation.
268 * Code below will not work if this is violated.
269 * Will be cleaned up with some followup patch.
270 */
271# error FIXME
272#endif
273
274static unsigned int al_extent_to_bm_page(unsigned int al_enr)
275{
276 return al_enr >>
277 /* bit to page */
278 ((PAGE_SHIFT + 3) -
279 /* al extent number to bit */
280 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
281}
282
283static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
284{
285 return rs_enr >>
286 /* bit to page */
287 ((PAGE_SHIFT + 3) -
288 /* al extent number to bit */
289 (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
290}
291
Philipp Reisnerb411b362009-09-25 16:07:19 -0700292int
293w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused)
294{
295 struct update_al_work *aw = container_of(w, struct update_al_work, w);
296 struct lc_element *updated = aw->al_ext;
297 const unsigned int new_enr = aw->enr;
298 const unsigned int evicted = aw->old_enr;
299 struct al_transaction *buffer;
300 sector_t sector;
301 int i, n, mx;
302 unsigned int extent_nr;
303 u32 xor_sum = 0;
304
305 if (!get_ldev(mdev)) {
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200306 dev_err(DEV,
307 "disk is %s, cannot start al transaction (-%d +%d)\n",
308 drbd_disk_str(mdev->state.disk), evicted, new_enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700309 complete(&((struct update_al_work *)w)->event);
310 return 1;
311 }
312 /* do we have to do a bitmap write, first?
313 * TODO reduce maximum latency:
314 * submit both bios, then wait for both,
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200315 * instead of doing two synchronous sector writes.
316 * For now, we must not write the transaction,
317 * if we cannot write out the bitmap of the evicted extent. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700318 if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100319 drbd_bm_write_page(mdev, al_extent_to_bm_page(evicted));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700320
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200321 /* The bitmap write may have failed, causing a state change. */
322 if (mdev->state.disk < D_INCONSISTENT) {
323 dev_err(DEV,
324 "disk is %s, cannot write al transaction (-%d +%d)\n",
325 drbd_disk_str(mdev->state.disk), evicted, new_enr);
326 complete(&((struct update_al_work *)w)->event);
327 put_ldev(mdev);
328 return 1;
329 }
330
331 mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700332 buffer = (struct al_transaction *)page_address(mdev->md_io_page);
333
334 buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
335 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
336
337 n = lc_index_of(mdev->act_log, updated);
338
339 buffer->updates[0].pos = cpu_to_be32(n);
340 buffer->updates[0].extent = cpu_to_be32(new_enr);
341
342 xor_sum ^= new_enr;
343
344 mx = min_t(int, AL_EXTENTS_PT,
345 mdev->act_log->nr_elements - mdev->al_tr_cycle);
346 for (i = 0; i < mx; i++) {
347 unsigned idx = mdev->al_tr_cycle + i;
348 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
349 buffer->updates[i+1].pos = cpu_to_be32(idx);
350 buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
351 xor_sum ^= extent_nr;
352 }
353 for (; i < AL_EXTENTS_PT; i++) {
354 buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
355 buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
356 xor_sum ^= LC_FREE;
357 }
358 mdev->al_tr_cycle += AL_EXTENTS_PT;
359 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
360 mdev->al_tr_cycle = 0;
361
362 buffer->xor_sum = cpu_to_be32(xor_sum);
363
364 sector = mdev->ldev->md.md_offset
365 + mdev->ldev->md.al_offset + mdev->al_tr_pos;
366
367 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100368 drbd_chk_io_error(mdev, 1, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700369
370 if (++mdev->al_tr_pos >
371 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
372 mdev->al_tr_pos = 0;
373
374 D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
375 mdev->al_tr_number++;
376
377 mutex_unlock(&mdev->md_io_mutex);
378
379 complete(&((struct update_al_work *)w)->event);
380 put_ldev(mdev);
381
382 return 1;
383}
384
385/**
386 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
387 * @mdev: DRBD device.
388 * @bdev: Block device to read form.
389 * @b: pointer to an al_transaction.
390 * @index: On disk slot of the transaction to read.
391 *
392 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
393 */
394static int drbd_al_read_tr(struct drbd_conf *mdev,
395 struct drbd_backing_dev *bdev,
396 struct al_transaction *b,
397 int index)
398{
399 sector_t sector;
400 int rv, i;
401 u32 xor_sum = 0;
402
403 sector = bdev->md.md_offset + bdev->md.al_offset + index;
404
405 /* Dont process error normally,
406 * as this is done before disk is attached! */
407 if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
408 return -1;
409
410 rv = (be32_to_cpu(b->magic) == DRBD_MAGIC);
411
412 for (i = 0; i < AL_EXTENTS_PT + 1; i++)
413 xor_sum ^= be32_to_cpu(b->updates[i].extent);
414 rv &= (xor_sum == be32_to_cpu(b->xor_sum));
415
416 return rv;
417}
418
419/**
420 * drbd_al_read_log() - Restores the activity log from its on disk representation.
421 * @mdev: DRBD device.
422 * @bdev: Block device to read form.
423 *
424 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
425 */
426int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
427{
428 struct al_transaction *buffer;
429 int i;
430 int rv;
431 int mx;
432 int active_extents = 0;
433 int transactions = 0;
434 int found_valid = 0;
435 int from = 0;
436 int to = 0;
437 u32 from_tnr = 0;
438 u32 to_tnr = 0;
439 u32 cnr;
440
441 mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
442
443 /* lock out all other meta data io for now,
444 * and make sure the page is mapped.
445 */
446 mutex_lock(&mdev->md_io_mutex);
447 buffer = page_address(mdev->md_io_page);
448
449 /* Find the valid transaction in the log */
450 for (i = 0; i <= mx; i++) {
451 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
452 if (rv == 0)
453 continue;
454 if (rv == -1) {
455 mutex_unlock(&mdev->md_io_mutex);
456 return 0;
457 }
458 cnr = be32_to_cpu(buffer->tr_number);
459
460 if (++found_valid == 1) {
461 from = i;
462 to = i;
463 from_tnr = cnr;
464 to_tnr = cnr;
465 continue;
466 }
467 if ((int)cnr - (int)from_tnr < 0) {
468 D_ASSERT(from_tnr - cnr + i - from == mx+1);
469 from = i;
470 from_tnr = cnr;
471 }
472 if ((int)cnr - (int)to_tnr > 0) {
473 D_ASSERT(cnr - to_tnr == i - to);
474 to = i;
475 to_tnr = cnr;
476 }
477 }
478
479 if (!found_valid) {
480 dev_warn(DEV, "No usable activity log found.\n");
481 mutex_unlock(&mdev->md_io_mutex);
482 return 1;
483 }
484
485 /* Read the valid transactions.
486 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
487 i = from;
488 while (1) {
489 int j, pos;
490 unsigned int extent_nr;
491 unsigned int trn;
492
493 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
494 ERR_IF(rv == 0) goto cancel;
495 if (rv == -1) {
496 mutex_unlock(&mdev->md_io_mutex);
497 return 0;
498 }
499
500 trn = be32_to_cpu(buffer->tr_number);
501
502 spin_lock_irq(&mdev->al_lock);
503
504 /* This loop runs backwards because in the cyclic
505 elements there might be an old version of the
506 updated element (in slot 0). So the element in slot 0
507 can overwrite old versions. */
508 for (j = AL_EXTENTS_PT; j >= 0; j--) {
509 pos = be32_to_cpu(buffer->updates[j].pos);
510 extent_nr = be32_to_cpu(buffer->updates[j].extent);
511
512 if (extent_nr == LC_FREE)
513 continue;
514
515 lc_set(mdev->act_log, extent_nr, pos);
516 active_extents++;
517 }
518 spin_unlock_irq(&mdev->al_lock);
519
520 transactions++;
521
522cancel:
523 if (i == to)
524 break;
525 i++;
526 if (i > mx)
527 i = 0;
528 }
529
530 mdev->al_tr_number = to_tnr+1;
531 mdev->al_tr_pos = to;
532 if (++mdev->al_tr_pos >
533 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
534 mdev->al_tr_pos = 0;
535
536 /* ok, we are done with it */
537 mutex_unlock(&mdev->md_io_mutex);
538
539 dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
540 transactions, active_extents);
541
542 return 1;
543}
544
545static void atodb_endio(struct bio *bio, int error)
546{
547 struct drbd_atodb_wait *wc = bio->bi_private;
548 struct drbd_conf *mdev = wc->mdev;
549 struct page *page;
550 int uptodate = bio_flagged(bio, BIO_UPTODATE);
551
552 /* strange behavior of some lower level drivers...
553 * fail the request by clearing the uptodate flag,
554 * but do not return any error?! */
555 if (!error && !uptodate)
556 error = -EIO;
557
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100558 drbd_chk_io_error(mdev, error, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700559 if (error && wc->error == 0)
560 wc->error = error;
561
562 if (atomic_dec_and_test(&wc->count))
563 complete(&wc->io_done);
564
565 page = bio->bi_io_vec[0].bv_page;
566 put_page(page);
567 bio_put(bio);
568 mdev->bm_writ_cnt++;
569 put_ldev(mdev);
570}
571
Lars Ellenberg39ad2bb2010-03-04 15:52:30 +0100572/* sector to word */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700573#define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL))
Lars Ellenberg39ad2bb2010-03-04 15:52:30 +0100574
Philipp Reisnerb411b362009-09-25 16:07:19 -0700575/* activity log to on disk bitmap -- prepare bio unless that sector
576 * is already covered by previously prepared bios */
577static int atodb_prepare_unless_covered(struct drbd_conf *mdev,
578 struct bio **bios,
579 unsigned int enr,
580 struct drbd_atodb_wait *wc) __must_hold(local)
581{
582 struct bio *bio;
583 struct page *page;
Lars Ellenberg39ad2bb2010-03-04 15:52:30 +0100584 sector_t on_disk_sector;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700585 unsigned int page_offset = PAGE_SIZE;
586 int offset;
587 int i = 0;
588 int err = -ENOMEM;
589
Lars Ellenberg39ad2bb2010-03-04 15:52:30 +0100590 /* We always write aligned, full 4k blocks,
591 * so we can ignore the logical_block_size (for now) */
592 enr &= ~7U;
593 on_disk_sector = enr + mdev->ldev->md.md_offset
594 + mdev->ldev->md.bm_offset;
595
596 D_ASSERT(!(on_disk_sector & 7U));
597
Philipp Reisnerb411b362009-09-25 16:07:19 -0700598 /* Check if that enr is already covered by an already created bio.
599 * Caution, bios[] is not NULL terminated,
600 * but only initialized to all NULL.
601 * For completely scattered activity log,
602 * the last invocation iterates over all bios,
603 * and finds the last NULL entry.
604 */
605 while ((bio = bios[i])) {
606 if (bio->bi_sector == on_disk_sector)
607 return 0;
608 i++;
609 }
610 /* bios[i] == NULL, the next not yet used slot */
611
612 /* GFP_KERNEL, we are not in the write-out path */
613 bio = bio_alloc(GFP_KERNEL, 1);
614 if (bio == NULL)
615 return -ENOMEM;
616
617 if (i > 0) {
618 const struct bio_vec *prev_bv = bios[i-1]->bi_io_vec;
619 page_offset = prev_bv->bv_offset + prev_bv->bv_len;
620 page = prev_bv->bv_page;
621 }
622 if (page_offset == PAGE_SIZE) {
623 page = alloc_page(__GFP_HIGHMEM);
624 if (page == NULL)
625 goto out_bio_put;
626 page_offset = 0;
627 } else {
628 get_page(page);
629 }
630
631 offset = S2W(enr);
632 drbd_bm_get_lel(mdev, offset,
Lars Ellenberg39ad2bb2010-03-04 15:52:30 +0100633 min_t(size_t, S2W(8), drbd_bm_words(mdev) - offset),
Philipp Reisnerb411b362009-09-25 16:07:19 -0700634 kmap(page) + page_offset);
635 kunmap(page);
636
637 bio->bi_private = wc;
638 bio->bi_end_io = atodb_endio;
639 bio->bi_bdev = mdev->ldev->md_bdev;
640 bio->bi_sector = on_disk_sector;
641
Lars Ellenberg39ad2bb2010-03-04 15:52:30 +0100642 if (bio_add_page(bio, page, 4096, page_offset) != 4096)
Philipp Reisnerb411b362009-09-25 16:07:19 -0700643 goto out_put_page;
644
645 atomic_inc(&wc->count);
646 /* we already know that we may do this...
647 * get_ldev_if_state(mdev,D_ATTACHING);
648 * just get the extra reference, so that the local_cnt reflects
649 * the number of pending IO requests DRBD at its backing device.
650 */
651 atomic_inc(&mdev->local_cnt);
652
653 bios[i] = bio;
654
655 return 0;
656
657out_put_page:
658 err = -EINVAL;
659 put_page(page);
660out_bio_put:
661 bio_put(bio);
662 return err;
663}
664
665/**
Philipp Reisnerb411b362009-09-25 16:07:19 -0700666 * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
667 * @mdev: DRBD device.
668 */
669void drbd_al_apply_to_bm(struct drbd_conf *mdev)
670{
671 unsigned int enr;
672 unsigned long add = 0;
673 char ppb[10];
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200674 int i, tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700675
676 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
677
678 for (i = 0; i < mdev->act_log->nr_elements; i++) {
679 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
680 if (enr == LC_FREE)
681 continue;
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200682 tmp = drbd_bm_ALe_set_all(mdev, enr);
683 dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
684 add += tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700685 }
686
687 lc_unlock(mdev->act_log);
688 wake_up(&mdev->al_wait);
689
690 dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
691 ppsize(ppb, Bit2KB(add)));
692}
693
694static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
695{
696 int rv;
697
698 spin_lock_irq(&mdev->al_lock);
699 rv = (al_ext->refcnt == 0);
700 if (likely(rv))
701 lc_del(mdev->act_log, al_ext);
702 spin_unlock_irq(&mdev->al_lock);
703
704 return rv;
705}
706
707/**
708 * drbd_al_shrink() - Removes all active extents form the activity log
709 * @mdev: DRBD device.
710 *
711 * Removes all active extents form the activity log, waiting until
712 * the reference count of each entry dropped to 0 first, of course.
713 *
714 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
715 */
716void drbd_al_shrink(struct drbd_conf *mdev)
717{
718 struct lc_element *al_ext;
719 int i;
720
721 D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
722
723 for (i = 0; i < mdev->act_log->nr_elements; i++) {
724 al_ext = lc_element_by_index(mdev->act_log, i);
725 if (al_ext->lc_number == LC_FREE)
726 continue;
727 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
728 }
729
730 wake_up(&mdev->al_wait);
731}
732
733static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
734{
735 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
736
737 if (!get_ldev(mdev)) {
738 if (__ratelimit(&drbd_ratelimit_state))
739 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
740 kfree(udw);
741 return 1;
742 }
743
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100744 drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700745 put_ldev(mdev);
746
747 kfree(udw);
748
749 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
750 switch (mdev->state.conn) {
751 case C_SYNC_SOURCE: case C_SYNC_TARGET:
752 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
753 drbd_resync_finished(mdev);
754 default:
755 /* nothing to do */
756 break;
757 }
758 }
759 drbd_bcast_sync_progress(mdev);
760
761 return 1;
762}
763
764
765/* ATTENTION. The AL's extents are 4MB each, while the extents in the
766 * resync LRU-cache are 16MB each.
767 * The caller of this function has to hold an get_ldev() reference.
768 *
769 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
770 */
771static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
772 int count, int success)
773{
774 struct lc_element *e;
775 struct update_odbm_work *udw;
776
777 unsigned int enr;
778
779 D_ASSERT(atomic_read(&mdev->local_cnt));
780
781 /* I simply assume that a sector/size pair never crosses
782 * a 16 MB extent border. (Currently this is true...) */
783 enr = BM_SECT_TO_EXT(sector);
784
785 e = lc_get(mdev->resync, enr);
786 if (e) {
787 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
788 if (ext->lce.lc_number == enr) {
789 if (success)
790 ext->rs_left -= count;
791 else
792 ext->rs_failed += count;
793 if (ext->rs_left < ext->rs_failed) {
794 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
795 "rs_failed=%d count=%d\n",
796 (unsigned long long)sector,
797 ext->lce.lc_number, ext->rs_left,
798 ext->rs_failed, count);
799 dump_stack();
800
801 lc_put(mdev->resync, &ext->lce);
802 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
803 return;
804 }
805 } else {
806 /* Normally this element should be in the cache,
807 * since drbd_rs_begin_io() pulled it already in.
808 *
809 * But maybe an application write finished, and we set
810 * something outside the resync lru_cache in sync.
811 */
812 int rs_left = drbd_bm_e_weight(mdev, enr);
813 if (ext->flags != 0) {
814 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
815 " -> %d[%u;00]\n",
816 ext->lce.lc_number, ext->rs_left,
817 ext->flags, enr, rs_left);
818 ext->flags = 0;
819 }
820 if (ext->rs_failed) {
821 dev_warn(DEV, "Kicking resync_lru element enr=%u "
822 "out with rs_failed=%d\n",
823 ext->lce.lc_number, ext->rs_failed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700824 }
825 ext->rs_left = rs_left;
826 ext->rs_failed = success ? 0 : count;
827 lc_changed(mdev->resync, &ext->lce);
828 }
829 lc_put(mdev->resync, &ext->lce);
830 /* no race, we are within the al_lock! */
831
832 if (ext->rs_left == ext->rs_failed) {
833 ext->rs_failed = 0;
834
835 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
836 if (udw) {
837 udw->enr = ext->lce.lc_number;
838 udw->w.cb = w_update_odbm;
839 drbd_queue_work_front(&mdev->data.work, &udw->w);
840 } else {
841 dev_warn(DEV, "Could not kmalloc an udw\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700842 }
843 }
844 } else {
845 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
846 mdev->resync_locked,
847 mdev->resync->nr_elements,
848 mdev->resync->flags);
849 }
850}
851
Lars Ellenbergc6ea14d2010-11-05 09:23:37 +0100852void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
853{
854 unsigned long now = jiffies;
855 unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
856 int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
857 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
858 if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
859 mdev->state.conn != C_PAUSED_SYNC_T &&
860 mdev->state.conn != C_PAUSED_SYNC_S) {
861 mdev->rs_mark_time[next] = now;
862 mdev->rs_mark_left[next] = still_to_go;
863 mdev->rs_last_mark = next;
864 }
865 }
866}
867
Philipp Reisnerb411b362009-09-25 16:07:19 -0700868/* clear the bit corresponding to the piece of storage in question:
869 * size byte of data starting from sector. Only clear a bits of the affected
870 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
871 *
872 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
873 *
874 */
875void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
876 const char *file, const unsigned int line)
877{
878 /* Is called from worker and receiver context _only_ */
879 unsigned long sbnr, ebnr, lbnr;
880 unsigned long count = 0;
881 sector_t esector, nr_sectors;
882 int wake_up = 0;
883 unsigned long flags;
884
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100885 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700886 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
887 (unsigned long long)sector, size);
888 return;
889 }
890 nr_sectors = drbd_get_capacity(mdev->this_bdev);
891 esector = sector + (size >> 9) - 1;
892
893 ERR_IF(sector >= nr_sectors) return;
894 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
895
896 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
897
898 /* we clear it (in sync).
899 * round up start sector, round down end sector. we make sure we only
900 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
901 if (unlikely(esector < BM_SECT_PER_BIT-1))
902 return;
903 if (unlikely(esector == (nr_sectors-1)))
904 ebnr = lbnr;
905 else
906 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
907 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
908
Philipp Reisnerb411b362009-09-25 16:07:19 -0700909 if (sbnr > ebnr)
910 return;
911
912 /*
913 * ok, (capacity & 7) != 0 sometimes, but who cares...
914 * we count rs_{total,left} in bits, not sectors.
915 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700916 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200917 if (count && get_ldev(mdev)) {
Lars Ellenbergc6ea14d2010-11-05 09:23:37 +0100918 drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200919 spin_lock_irqsave(&mdev->al_lock, flags);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100920 drbd_try_clear_on_disk_bm(mdev, sector, count, true);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200921 spin_unlock_irqrestore(&mdev->al_lock, flags);
922
Philipp Reisnerb411b362009-09-25 16:07:19 -0700923 /* just wake_up unconditional now, various lc_chaged(),
924 * lc_put() in drbd_try_clear_on_disk_bm(). */
925 wake_up = 1;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200926 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700927 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700928 if (wake_up)
929 wake_up(&mdev->al_wait);
930}
931
932/*
933 * this is intended to set one request worth of data out of sync.
934 * affects at least 1 bit,
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100935 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700936 *
937 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
938 * so this can be _any_ process.
939 */
Philipp Reisner73a01a12010-10-27 14:33:00 +0200940int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700941 const char *file, const unsigned int line)
942{
943 unsigned long sbnr, ebnr, lbnr, flags;
944 sector_t esector, nr_sectors;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200945 unsigned int enr, count = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700946 struct lc_element *e;
947
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100948 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700949 dev_err(DEV, "sector: %llus, size: %d\n",
950 (unsigned long long)sector, size);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200951 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700952 }
953
954 if (!get_ldev(mdev))
Philipp Reisner73a01a12010-10-27 14:33:00 +0200955 return 0; /* no disk, no metadata, no bitmap to set bits in */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700956
957 nr_sectors = drbd_get_capacity(mdev->this_bdev);
958 esector = sector + (size >> 9) - 1;
959
960 ERR_IF(sector >= nr_sectors)
961 goto out;
962 ERR_IF(esector >= nr_sectors)
963 esector = (nr_sectors-1);
964
965 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
966
967 /* we set it out of sync,
968 * we do not need to round anything here */
969 sbnr = BM_SECT_TO_BIT(sector);
970 ebnr = BM_SECT_TO_BIT(esector);
971
Philipp Reisnerb411b362009-09-25 16:07:19 -0700972 /* ok, (capacity & 7) != 0 sometimes, but who cares...
973 * we count rs_{total,left} in bits, not sectors. */
974 spin_lock_irqsave(&mdev->al_lock, flags);
975 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
976
977 enr = BM_SECT_TO_EXT(sector);
978 e = lc_find(mdev->resync, enr);
979 if (e)
980 lc_entry(e, struct bm_extent, lce)->rs_left += count;
981 spin_unlock_irqrestore(&mdev->al_lock, flags);
982
983out:
984 put_ldev(mdev);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200985
986 return count;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700987}
988
989static
990struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
991{
992 struct lc_element *e;
993 struct bm_extent *bm_ext;
994 int wakeup = 0;
995 unsigned long rs_flags;
996
997 spin_lock_irq(&mdev->al_lock);
998 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
999 spin_unlock_irq(&mdev->al_lock);
1000 return NULL;
1001 }
1002 e = lc_get(mdev->resync, enr);
1003 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1004 if (bm_ext) {
1005 if (bm_ext->lce.lc_number != enr) {
1006 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1007 bm_ext->rs_failed = 0;
1008 lc_changed(mdev->resync, &bm_ext->lce);
1009 wakeup = 1;
1010 }
1011 if (bm_ext->lce.refcnt == 1)
1012 mdev->resync_locked++;
1013 set_bit(BME_NO_WRITES, &bm_ext->flags);
1014 }
1015 rs_flags = mdev->resync->flags;
1016 spin_unlock_irq(&mdev->al_lock);
1017 if (wakeup)
1018 wake_up(&mdev->al_wait);
1019
1020 if (!bm_ext) {
1021 if (rs_flags & LC_STARVING)
1022 dev_warn(DEV, "Have to wait for element"
1023 " (resync LRU too small?)\n");
1024 BUG_ON(rs_flags & LC_DIRTY);
1025 }
1026
1027 return bm_ext;
1028}
1029
1030static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
1031{
1032 struct lc_element *al_ext;
1033 int rv = 0;
1034
1035 spin_lock_irq(&mdev->al_lock);
1036 if (unlikely(enr == mdev->act_log->new_number))
1037 rv = 1;
1038 else {
1039 al_ext = lc_find(mdev->act_log, enr);
1040 if (al_ext) {
1041 if (al_ext->refcnt)
1042 rv = 1;
1043 }
1044 }
1045 spin_unlock_irq(&mdev->al_lock);
1046
1047 /*
1048 if (unlikely(rv)) {
1049 dev_info(DEV, "Delaying sync read until app's write is done\n");
1050 }
1051 */
1052 return rv;
1053}
1054
1055/**
1056 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
1057 * @mdev: DRBD device.
1058 * @sector: The sector number.
1059 *
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001060 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
Philipp Reisnerb411b362009-09-25 16:07:19 -07001061 */
1062int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1063{
1064 unsigned int enr = BM_SECT_TO_EXT(sector);
1065 struct bm_extent *bm_ext;
1066 int i, sig;
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001067 int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
1068 200 times -> 20 seconds. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001069
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001070retry:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001071 sig = wait_event_interruptible(mdev->al_wait,
1072 (bm_ext = _bme_get(mdev, enr)));
1073 if (sig)
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001074 return -EINTR;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001075
1076 if (test_bit(BME_LOCKED, &bm_ext->flags))
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001077 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001078
1079 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1080 sig = wait_event_interruptible(mdev->al_wait,
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001081 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
Philipp Reisnerc507f462010-11-22 15:49:17 +01001082 test_bit(BME_PRIORITY, &bm_ext->flags));
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001083
1084 if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001085 spin_lock_irq(&mdev->al_lock);
1086 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001087 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001088 mdev->resync_locked--;
1089 wake_up(&mdev->al_wait);
1090 }
1091 spin_unlock_irq(&mdev->al_lock);
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001092 if (sig)
1093 return -EINTR;
1094 if (schedule_timeout_interruptible(HZ/10))
1095 return -EINTR;
Philipp Reisnerc507f462010-11-22 15:49:17 +01001096 if (sa && --sa == 0)
1097 dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
1098 "Resync stalled?\n");
Philipp Reisnerf91ab622010-11-09 13:59:41 +01001099 goto retry;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001100 }
1101 }
Philipp Reisnerb411b362009-09-25 16:07:19 -07001102 set_bit(BME_LOCKED, &bm_ext->flags);
Lars Ellenberg80a40e42010-08-11 23:28:00 +02001103 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001104}
1105
1106/**
1107 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
1108 * @mdev: DRBD device.
1109 * @sector: The sector number.
1110 *
1111 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
1112 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
1113 * if there is still application IO going on in this area.
1114 */
1115int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1116{
1117 unsigned int enr = BM_SECT_TO_EXT(sector);
1118 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1119 struct lc_element *e;
1120 struct bm_extent *bm_ext;
1121 int i;
1122
Philipp Reisnerb411b362009-09-25 16:07:19 -07001123 spin_lock_irq(&mdev->al_lock);
1124 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1125 /* in case you have very heavy scattered io, it may
1126 * stall the syncer undefined if we give up the ref count
1127 * when we try again and requeue.
1128 *
1129 * if we don't give up the refcount, but the next time
1130 * we are scheduled this extent has been "synced" by new
1131 * application writes, we'd miss the lc_put on the
1132 * extent we keep the refcount on.
1133 * so we remembered which extent we had to try again, and
1134 * if the next requested one is something else, we do
1135 * the lc_put here...
1136 * we also have to wake_up
1137 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001138 e = lc_find(mdev->resync, mdev->resync_wenr);
1139 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1140 if (bm_ext) {
1141 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1142 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1143 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1144 mdev->resync_wenr = LC_FREE;
1145 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1146 mdev->resync_locked--;
1147 wake_up(&mdev->al_wait);
1148 } else {
1149 dev_alert(DEV, "LOGIC BUG\n");
1150 }
1151 }
1152 /* TRY. */
1153 e = lc_try_get(mdev->resync, enr);
1154 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1155 if (bm_ext) {
1156 if (test_bit(BME_LOCKED, &bm_ext->flags))
1157 goto proceed;
1158 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1159 mdev->resync_locked++;
1160 } else {
1161 /* we did set the BME_NO_WRITES,
1162 * but then could not set BME_LOCKED,
1163 * so we tried again.
1164 * drop the extra reference. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001165 bm_ext->lce.refcnt--;
1166 D_ASSERT(bm_ext->lce.refcnt > 0);
1167 }
1168 goto check_al;
1169 } else {
1170 /* do we rather want to try later? */
Jens Axboe6a0afdf2009-10-01 09:04:14 +02001171 if (mdev->resync_locked > mdev->resync->nr_elements-3)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001172 goto try_again;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001173 /* Do or do not. There is no try. -- Yoda */
1174 e = lc_get(mdev->resync, enr);
1175 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1176 if (!bm_ext) {
1177 const unsigned long rs_flags = mdev->resync->flags;
1178 if (rs_flags & LC_STARVING)
1179 dev_warn(DEV, "Have to wait for element"
1180 " (resync LRU too small?)\n");
1181 BUG_ON(rs_flags & LC_DIRTY);
1182 goto try_again;
1183 }
1184 if (bm_ext->lce.lc_number != enr) {
1185 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1186 bm_ext->rs_failed = 0;
1187 lc_changed(mdev->resync, &bm_ext->lce);
1188 wake_up(&mdev->al_wait);
1189 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1190 }
1191 set_bit(BME_NO_WRITES, &bm_ext->flags);
1192 D_ASSERT(bm_ext->lce.refcnt == 1);
1193 mdev->resync_locked++;
1194 goto check_al;
1195 }
1196check_al:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001197 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1198 if (unlikely(al_enr+i == mdev->act_log->new_number))
1199 goto try_again;
1200 if (lc_is_used(mdev->act_log, al_enr+i))
1201 goto try_again;
1202 }
1203 set_bit(BME_LOCKED, &bm_ext->flags);
1204proceed:
1205 mdev->resync_wenr = LC_FREE;
1206 spin_unlock_irq(&mdev->al_lock);
1207 return 0;
1208
1209try_again:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001210 if (bm_ext)
1211 mdev->resync_wenr = enr;
1212 spin_unlock_irq(&mdev->al_lock);
1213 return -EAGAIN;
1214}
1215
1216void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1217{
1218 unsigned int enr = BM_SECT_TO_EXT(sector);
1219 struct lc_element *e;
1220 struct bm_extent *bm_ext;
1221 unsigned long flags;
1222
Philipp Reisnerb411b362009-09-25 16:07:19 -07001223 spin_lock_irqsave(&mdev->al_lock, flags);
1224 e = lc_find(mdev->resync, enr);
1225 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1226 if (!bm_ext) {
1227 spin_unlock_irqrestore(&mdev->al_lock, flags);
1228 if (__ratelimit(&drbd_ratelimit_state))
1229 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1230 return;
1231 }
1232
1233 if (bm_ext->lce.refcnt == 0) {
1234 spin_unlock_irqrestore(&mdev->al_lock, flags);
1235 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1236 "but refcnt is 0!?\n",
1237 (unsigned long long)sector, enr);
1238 return;
1239 }
1240
1241 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
Philipp Reisnere3555d82010-11-07 15:56:29 +01001242 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001243 mdev->resync_locked--;
1244 wake_up(&mdev->al_wait);
1245 }
1246
1247 spin_unlock_irqrestore(&mdev->al_lock, flags);
1248}
1249
1250/**
1251 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1252 * @mdev: DRBD device.
1253 */
1254void drbd_rs_cancel_all(struct drbd_conf *mdev)
1255{
Philipp Reisnerb411b362009-09-25 16:07:19 -07001256 spin_lock_irq(&mdev->al_lock);
1257
1258 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1259 lc_reset(mdev->resync);
1260 put_ldev(mdev);
1261 }
1262 mdev->resync_locked = 0;
1263 mdev->resync_wenr = LC_FREE;
1264 spin_unlock_irq(&mdev->al_lock);
1265 wake_up(&mdev->al_wait);
1266}
1267
1268/**
1269 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1270 * @mdev: DRBD device.
1271 *
1272 * Returns 0 upon success, -EAGAIN if at least one reference count was
1273 * not zero.
1274 */
1275int drbd_rs_del_all(struct drbd_conf *mdev)
1276{
1277 struct lc_element *e;
1278 struct bm_extent *bm_ext;
1279 int i;
1280
Philipp Reisnerb411b362009-09-25 16:07:19 -07001281 spin_lock_irq(&mdev->al_lock);
1282
1283 if (get_ldev_if_state(mdev, D_FAILED)) {
1284 /* ok, ->resync is there. */
1285 for (i = 0; i < mdev->resync->nr_elements; i++) {
1286 e = lc_element_by_index(mdev->resync, i);
Philipp Reisnerb2b163d2010-04-02 08:40:33 +02001287 bm_ext = lc_entry(e, struct bm_extent, lce);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001288 if (bm_ext->lce.lc_number == LC_FREE)
1289 continue;
1290 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1291 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1292 " got 'synced' by application io\n",
1293 mdev->resync_wenr);
1294 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1295 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1296 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1297 mdev->resync_wenr = LC_FREE;
1298 lc_put(mdev->resync, &bm_ext->lce);
1299 }
1300 if (bm_ext->lce.refcnt != 0) {
1301 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1302 "refcnt=%d\n", bm_ext->lce.refcnt);
1303 put_ldev(mdev);
1304 spin_unlock_irq(&mdev->al_lock);
1305 return -EAGAIN;
1306 }
1307 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1308 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1309 lc_del(mdev->resync, &bm_ext->lce);
1310 }
1311 D_ASSERT(mdev->resync->used == 0);
1312 put_ldev(mdev);
1313 }
1314 spin_unlock_irq(&mdev->al_lock);
1315
1316 return 0;
1317}
1318
1319/**
1320 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1321 * @mdev: DRBD device.
1322 * @sector: The sector number.
1323 * @size: Size of failed IO operation, in byte.
1324 */
1325void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1326{
1327 /* Is called from worker and receiver context _only_ */
1328 unsigned long sbnr, ebnr, lbnr;
1329 unsigned long count;
1330 sector_t esector, nr_sectors;
1331 int wake_up = 0;
1332
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001333 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001334 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1335 (unsigned long long)sector, size);
1336 return;
1337 }
1338 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1339 esector = sector + (size >> 9) - 1;
1340
1341 ERR_IF(sector >= nr_sectors) return;
1342 ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1);
1343
1344 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1345
1346 /*
1347 * round up start sector, round down end sector. we make sure we only
1348 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1349 if (unlikely(esector < BM_SECT_PER_BIT-1))
1350 return;
1351 if (unlikely(esector == (nr_sectors-1)))
1352 ebnr = lbnr;
1353 else
1354 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1355 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1356
1357 if (sbnr > ebnr)
1358 return;
1359
1360 /*
1361 * ok, (capacity & 7) != 0 sometimes, but who cares...
1362 * we count rs_{total,left} in bits, not sectors.
1363 */
1364 spin_lock_irq(&mdev->al_lock);
1365 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1366 if (count) {
1367 mdev->rs_failed += count;
1368
1369 if (get_ldev(mdev)) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001370 drbd_try_clear_on_disk_bm(mdev, sector, count, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001371 put_ldev(mdev);
1372 }
1373
1374 /* just wake_up unconditional now, various lc_chaged(),
1375 * lc_put() in drbd_try_clear_on_disk_bm(). */
1376 wake_up = 1;
1377 }
1378 spin_unlock_irq(&mdev->al_lock);
1379 if (wake_up)
1380 wake_up(&mdev->al_wait);
1381}