blob: 637a9378567a585d2fd19ea8dc0158f9b8b59d46 [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 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))
Lars Ellenberg86e1e982011-06-28 13:22:48 +020082 rw |= REQ_FUA | REQ_FLUSH;
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;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100231 al_work.w.mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +0100232 drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700233 wait_for_completion(&al_work.event);
234
235 mdev->al_writ_cnt++;
236
237 spin_lock_irq(&mdev->al_lock);
238 lc_changed(mdev->act_log, al_ext);
239 spin_unlock_irq(&mdev->al_lock);
240 wake_up(&mdev->al_wait);
241 }
242}
243
244void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector)
245{
246 unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9));
247 struct lc_element *extent;
248 unsigned long flags;
249
Philipp Reisnerb411b362009-09-25 16:07:19 -0700250 spin_lock_irqsave(&mdev->al_lock, flags);
251
252 extent = lc_find(mdev->act_log, enr);
253
254 if (!extent) {
255 spin_unlock_irqrestore(&mdev->al_lock, flags);
256 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
257 return;
258 }
259
260 if (lc_put(mdev->act_log, extent) == 0)
261 wake_up(&mdev->al_wait);
262
263 spin_unlock_irqrestore(&mdev->al_lock, flags);
264}
265
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100266#if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
267/* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
268 * are still coupled, or assume too much about their relation.
269 * Code below will not work if this is violated.
270 * Will be cleaned up with some followup patch.
271 */
272# error FIXME
273#endif
274
275static unsigned int al_extent_to_bm_page(unsigned int al_enr)
276{
277 return al_enr >>
278 /* bit to page */
279 ((PAGE_SHIFT + 3) -
280 /* al extent number to bit */
281 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
282}
283
284static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
285{
286 return rs_enr >>
287 /* bit to page */
288 ((PAGE_SHIFT + 3) -
289 /* al extent number to bit */
290 (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
291}
292
Philipp Reisnerb411b362009-09-25 16:07:19 -0700293int
294w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused)
295{
296 struct update_al_work *aw = container_of(w, struct update_al_work, w);
297 struct lc_element *updated = aw->al_ext;
298 const unsigned int new_enr = aw->enr;
299 const unsigned int evicted = aw->old_enr;
300 struct al_transaction *buffer;
301 sector_t sector;
302 int i, n, mx;
303 unsigned int extent_nr;
304 u32 xor_sum = 0;
305
306 if (!get_ldev(mdev)) {
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200307 dev_err(DEV,
308 "disk is %s, cannot start al transaction (-%d +%d)\n",
309 drbd_disk_str(mdev->state.disk), evicted, new_enr);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700310 complete(&((struct update_al_work *)w)->event);
311 return 1;
312 }
313 /* do we have to do a bitmap write, first?
314 * TODO reduce maximum latency:
315 * submit both bios, then wait for both,
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200316 * instead of doing two synchronous sector writes.
317 * For now, we must not write the transaction,
318 * if we cannot write out the bitmap of the evicted extent. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700319 if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE)
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100320 drbd_bm_write_page(mdev, al_extent_to_bm_page(evicted));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700321
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200322 /* The bitmap write may have failed, causing a state change. */
323 if (mdev->state.disk < D_INCONSISTENT) {
324 dev_err(DEV,
325 "disk is %s, cannot write al transaction (-%d +%d)\n",
326 drbd_disk_str(mdev->state.disk), evicted, new_enr);
327 complete(&((struct update_al_work *)w)->event);
328 put_ldev(mdev);
329 return 1;
330 }
331
332 mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700333 buffer = (struct al_transaction *)page_address(mdev->md_io_page);
334
335 buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC);
336 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
337
338 n = lc_index_of(mdev->act_log, updated);
339
340 buffer->updates[0].pos = cpu_to_be32(n);
341 buffer->updates[0].extent = cpu_to_be32(new_enr);
342
343 xor_sum ^= new_enr;
344
345 mx = min_t(int, AL_EXTENTS_PT,
346 mdev->act_log->nr_elements - mdev->al_tr_cycle);
347 for (i = 0; i < mx; i++) {
348 unsigned idx = mdev->al_tr_cycle + i;
349 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
350 buffer->updates[i+1].pos = cpu_to_be32(idx);
351 buffer->updates[i+1].extent = cpu_to_be32(extent_nr);
352 xor_sum ^= extent_nr;
353 }
354 for (; i < AL_EXTENTS_PT; i++) {
355 buffer->updates[i+1].pos = __constant_cpu_to_be32(-1);
356 buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE);
357 xor_sum ^= LC_FREE;
358 }
359 mdev->al_tr_cycle += AL_EXTENTS_PT;
360 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
361 mdev->al_tr_cycle = 0;
362
363 buffer->xor_sum = cpu_to_be32(xor_sum);
364
365 sector = mdev->ldev->md.md_offset
366 + mdev->ldev->md.al_offset + mdev->al_tr_pos;
367
368 if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE))
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100369 drbd_chk_io_error(mdev, 1, true);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700370
371 if (++mdev->al_tr_pos >
372 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
373 mdev->al_tr_pos = 0;
374
375 D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE);
376 mdev->al_tr_number++;
377
378 mutex_unlock(&mdev->md_io_mutex);
379
380 complete(&((struct update_al_work *)w)->event);
381 put_ldev(mdev);
382
383 return 1;
384}
385
386/**
387 * drbd_al_read_tr() - Read a single transaction from the on disk activity log
388 * @mdev: DRBD device.
389 * @bdev: Block device to read form.
390 * @b: pointer to an al_transaction.
391 * @index: On disk slot of the transaction to read.
392 *
393 * Returns -1 on IO error, 0 on checksum error and 1 upon success.
394 */
395static int drbd_al_read_tr(struct drbd_conf *mdev,
396 struct drbd_backing_dev *bdev,
397 struct al_transaction *b,
398 int index)
399{
400 sector_t sector;
401 int rv, i;
402 u32 xor_sum = 0;
403
404 sector = bdev->md.md_offset + bdev->md.al_offset + index;
405
406 /* Dont process error normally,
407 * as this is done before disk is attached! */
408 if (!drbd_md_sync_page_io(mdev, bdev, sector, READ))
409 return -1;
410
Andreas Gruenbachere7fad8a2011-01-11 13:54:02 +0100411 rv = (b->magic == cpu_to_be32(DRBD_MAGIC));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700412
413 for (i = 0; i < AL_EXTENTS_PT + 1; i++)
414 xor_sum ^= be32_to_cpu(b->updates[i].extent);
415 rv &= (xor_sum == be32_to_cpu(b->xor_sum));
416
417 return rv;
418}
419
420/**
421 * drbd_al_read_log() - Restores the activity log from its on disk representation.
422 * @mdev: DRBD device.
423 * @bdev: Block device to read form.
424 *
425 * Returns 1 on success, returns 0 when reading the log failed due to IO errors.
426 */
427int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev)
428{
429 struct al_transaction *buffer;
430 int i;
431 int rv;
432 int mx;
433 int active_extents = 0;
434 int transactions = 0;
435 int found_valid = 0;
436 int from = 0;
437 int to = 0;
438 u32 from_tnr = 0;
439 u32 to_tnr = 0;
440 u32 cnr;
441
442 mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT);
443
444 /* lock out all other meta data io for now,
445 * and make sure the page is mapped.
446 */
447 mutex_lock(&mdev->md_io_mutex);
448 buffer = page_address(mdev->md_io_page);
449
450 /* Find the valid transaction in the log */
451 for (i = 0; i <= mx; i++) {
452 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
453 if (rv == 0)
454 continue;
455 if (rv == -1) {
456 mutex_unlock(&mdev->md_io_mutex);
457 return 0;
458 }
459 cnr = be32_to_cpu(buffer->tr_number);
460
461 if (++found_valid == 1) {
462 from = i;
463 to = i;
464 from_tnr = cnr;
465 to_tnr = cnr;
466 continue;
467 }
468 if ((int)cnr - (int)from_tnr < 0) {
469 D_ASSERT(from_tnr - cnr + i - from == mx+1);
470 from = i;
471 from_tnr = cnr;
472 }
473 if ((int)cnr - (int)to_tnr > 0) {
474 D_ASSERT(cnr - to_tnr == i - to);
475 to = i;
476 to_tnr = cnr;
477 }
478 }
479
480 if (!found_valid) {
481 dev_warn(DEV, "No usable activity log found.\n");
482 mutex_unlock(&mdev->md_io_mutex);
483 return 1;
484 }
485
486 /* Read the valid transactions.
487 * dev_info(DEV, "Reading from %d to %d.\n",from,to); */
488 i = from;
489 while (1) {
490 int j, pos;
491 unsigned int extent_nr;
492 unsigned int trn;
493
494 rv = drbd_al_read_tr(mdev, bdev, buffer, i);
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100495 if (!expect(rv != 0))
496 goto cancel;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700497 if (rv == -1) {
498 mutex_unlock(&mdev->md_io_mutex);
499 return 0;
500 }
501
502 trn = be32_to_cpu(buffer->tr_number);
503
504 spin_lock_irq(&mdev->al_lock);
505
506 /* This loop runs backwards because in the cyclic
507 elements there might be an old version of the
508 updated element (in slot 0). So the element in slot 0
509 can overwrite old versions. */
510 for (j = AL_EXTENTS_PT; j >= 0; j--) {
511 pos = be32_to_cpu(buffer->updates[j].pos);
512 extent_nr = be32_to_cpu(buffer->updates[j].extent);
513
514 if (extent_nr == LC_FREE)
515 continue;
516
517 lc_set(mdev->act_log, extent_nr, pos);
518 active_extents++;
519 }
520 spin_unlock_irq(&mdev->al_lock);
521
522 transactions++;
523
524cancel:
525 if (i == to)
526 break;
527 i++;
528 if (i > mx)
529 i = 0;
530 }
531
532 mdev->al_tr_number = to_tnr+1;
533 mdev->al_tr_pos = to;
534 if (++mdev->al_tr_pos >
535 div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT))
536 mdev->al_tr_pos = 0;
537
538 /* ok, we are done with it */
539 mutex_unlock(&mdev->md_io_mutex);
540
541 dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n",
542 transactions, active_extents);
543
544 return 1;
545}
546
Philipp Reisnerb411b362009-09-25 16:07:19 -0700547/**
Philipp Reisnerb411b362009-09-25 16:07:19 -0700548 * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents
549 * @mdev: DRBD device.
550 */
551void drbd_al_apply_to_bm(struct drbd_conf *mdev)
552{
553 unsigned int enr;
554 unsigned long add = 0;
555 char ppb[10];
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200556 int i, tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700557
558 wait_event(mdev->al_wait, lc_try_lock(mdev->act_log));
559
560 for (i = 0; i < mdev->act_log->nr_elements; i++) {
561 enr = lc_element_by_index(mdev->act_log, i)->lc_number;
562 if (enr == LC_FREE)
563 continue;
Lars Ellenberg6719fb02010-10-18 23:04:07 +0200564 tmp = drbd_bm_ALe_set_all(mdev, enr);
565 dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr);
566 add += tmp;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700567 }
568
569 lc_unlock(mdev->act_log);
570 wake_up(&mdev->al_wait);
571
572 dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n",
573 ppsize(ppb, Bit2KB(add)));
574}
575
576static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
577{
578 int rv;
579
580 spin_lock_irq(&mdev->al_lock);
581 rv = (al_ext->refcnt == 0);
582 if (likely(rv))
583 lc_del(mdev->act_log, al_ext);
584 spin_unlock_irq(&mdev->al_lock);
585
586 return rv;
587}
588
589/**
590 * drbd_al_shrink() - Removes all active extents form the activity log
591 * @mdev: DRBD device.
592 *
593 * Removes all active extents form the activity log, waiting until
594 * the reference count of each entry dropped to 0 first, of course.
595 *
596 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
597 */
598void drbd_al_shrink(struct drbd_conf *mdev)
599{
600 struct lc_element *al_ext;
601 int i;
602
603 D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags));
604
605 for (i = 0; i < mdev->act_log->nr_elements; i++) {
606 al_ext = lc_element_by_index(mdev->act_log, i);
607 if (al_ext->lc_number == LC_FREE)
608 continue;
609 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
610 }
611
612 wake_up(&mdev->al_wait);
613}
614
615static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused)
616{
617 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
618
619 if (!get_ldev(mdev)) {
620 if (__ratelimit(&drbd_ratelimit_state))
621 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
622 kfree(udw);
623 return 1;
624 }
625
Lars Ellenberg19f843a2010-12-15 08:59:11 +0100626 drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
Philipp Reisnerb411b362009-09-25 16:07:19 -0700627 put_ldev(mdev);
628
629 kfree(udw);
630
631 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
632 switch (mdev->state.conn) {
633 case C_SYNC_SOURCE: case C_SYNC_TARGET:
634 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
635 drbd_resync_finished(mdev);
636 default:
637 /* nothing to do */
638 break;
639 }
640 }
641 drbd_bcast_sync_progress(mdev);
642
643 return 1;
644}
645
646
647/* ATTENTION. The AL's extents are 4MB each, while the extents in the
648 * resync LRU-cache are 16MB each.
649 * The caller of this function has to hold an get_ldev() reference.
650 *
651 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
652 */
653static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
654 int count, int success)
655{
656 struct lc_element *e;
657 struct update_odbm_work *udw;
658
659 unsigned int enr;
660
661 D_ASSERT(atomic_read(&mdev->local_cnt));
662
663 /* I simply assume that a sector/size pair never crosses
664 * a 16 MB extent border. (Currently this is true...) */
665 enr = BM_SECT_TO_EXT(sector);
666
667 e = lc_get(mdev->resync, enr);
668 if (e) {
669 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
670 if (ext->lce.lc_number == enr) {
671 if (success)
672 ext->rs_left -= count;
673 else
674 ext->rs_failed += count;
675 if (ext->rs_left < ext->rs_failed) {
676 dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
677 "rs_failed=%d count=%d\n",
678 (unsigned long long)sector,
679 ext->lce.lc_number, ext->rs_left,
680 ext->rs_failed, count);
681 dump_stack();
682
683 lc_put(mdev->resync, &ext->lce);
684 drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
685 return;
686 }
687 } else {
688 /* Normally this element should be in the cache,
689 * since drbd_rs_begin_io() pulled it already in.
690 *
691 * But maybe an application write finished, and we set
692 * something outside the resync lru_cache in sync.
693 */
694 int rs_left = drbd_bm_e_weight(mdev, enr);
695 if (ext->flags != 0) {
696 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
697 " -> %d[%u;00]\n",
698 ext->lce.lc_number, ext->rs_left,
699 ext->flags, enr, rs_left);
700 ext->flags = 0;
701 }
702 if (ext->rs_failed) {
703 dev_warn(DEV, "Kicking resync_lru element enr=%u "
704 "out with rs_failed=%d\n",
705 ext->lce.lc_number, ext->rs_failed);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700706 }
707 ext->rs_left = rs_left;
708 ext->rs_failed = success ? 0 : count;
709 lc_changed(mdev->resync, &ext->lce);
710 }
711 lc_put(mdev->resync, &ext->lce);
712 /* no race, we are within the al_lock! */
713
714 if (ext->rs_left == ext->rs_failed) {
715 ext->rs_failed = 0;
716
717 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
718 if (udw) {
719 udw->enr = ext->lce.lc_number;
720 udw->w.cb = w_update_odbm;
Philipp Reisnera21e9292011-02-08 15:08:49 +0100721 udw->w.mdev = mdev;
Philipp Reisnere42325a2011-01-19 13:55:45 +0100722 drbd_queue_work_front(&mdev->tconn->data.work, &udw->w);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700723 } else {
724 dev_warn(DEV, "Could not kmalloc an udw\n");
Philipp Reisnerb411b362009-09-25 16:07:19 -0700725 }
726 }
727 } else {
728 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
729 mdev->resync_locked,
730 mdev->resync->nr_elements,
731 mdev->resync->flags);
732 }
733}
734
Lars Ellenbergc6ea14d2010-11-05 09:23:37 +0100735void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
736{
737 unsigned long now = jiffies;
738 unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
739 int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
740 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
741 if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
742 mdev->state.conn != C_PAUSED_SYNC_T &&
743 mdev->state.conn != C_PAUSED_SYNC_S) {
744 mdev->rs_mark_time[next] = now;
745 mdev->rs_mark_left[next] = still_to_go;
746 mdev->rs_last_mark = next;
747 }
748 }
749}
750
Philipp Reisnerb411b362009-09-25 16:07:19 -0700751/* clear the bit corresponding to the piece of storage in question:
752 * size byte of data starting from sector. Only clear a bits of the affected
753 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
754 *
755 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
756 *
757 */
758void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
759 const char *file, const unsigned int line)
760{
761 /* Is called from worker and receiver context _only_ */
762 unsigned long sbnr, ebnr, lbnr;
763 unsigned long count = 0;
764 sector_t esector, nr_sectors;
765 int wake_up = 0;
766 unsigned long flags;
767
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100768 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700769 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
770 (unsigned long long)sector, size);
771 return;
772 }
773 nr_sectors = drbd_get_capacity(mdev->this_bdev);
774 esector = sector + (size >> 9) - 1;
775
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100776 if (!expect(sector < nr_sectors))
777 return;
778 if (!expect(esector < nr_sectors))
779 esector = nr_sectors - 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700780
781 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
782
783 /* we clear it (in sync).
784 * round up start sector, round down end sector. we make sure we only
785 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
786 if (unlikely(esector < BM_SECT_PER_BIT-1))
787 return;
788 if (unlikely(esector == (nr_sectors-1)))
789 ebnr = lbnr;
790 else
791 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
792 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
793
Philipp Reisnerb411b362009-09-25 16:07:19 -0700794 if (sbnr > ebnr)
795 return;
796
797 /*
798 * ok, (capacity & 7) != 0 sometimes, but who cares...
799 * we count rs_{total,left} in bits, not sectors.
800 */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700801 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200802 if (count && get_ldev(mdev)) {
Lars Ellenbergc6ea14d2010-11-05 09:23:37 +0100803 drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200804 spin_lock_irqsave(&mdev->al_lock, flags);
Andreas Gruenbacher81e84652010-12-09 15:03:57 +0100805 drbd_try_clear_on_disk_bm(mdev, sector, count, true);
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200806 spin_unlock_irqrestore(&mdev->al_lock, flags);
807
Philipp Reisnerb411b362009-09-25 16:07:19 -0700808 /* just wake_up unconditional now, various lc_chaged(),
809 * lc_put() in drbd_try_clear_on_disk_bm(). */
810 wake_up = 1;
Lars Ellenberg1d7734a2010-08-11 21:21:50 +0200811 put_ldev(mdev);
Philipp Reisnerb411b362009-09-25 16:07:19 -0700812 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700813 if (wake_up)
814 wake_up(&mdev->al_wait);
815}
816
817/*
818 * this is intended to set one request worth of data out of sync.
819 * affects at least 1 bit,
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100820 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700821 *
822 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
823 * so this can be _any_ process.
824 */
Philipp Reisner73a01a12010-10-27 14:33:00 +0200825int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
Philipp Reisnerb411b362009-09-25 16:07:19 -0700826 const char *file, const unsigned int line)
827{
828 unsigned long sbnr, ebnr, lbnr, flags;
829 sector_t esector, nr_sectors;
Philipp Reisner73a01a12010-10-27 14:33:00 +0200830 unsigned int enr, count = 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700831 struct lc_element *e;
832
Lars Ellenberg1816a2b2010-11-11 15:19:07 +0100833 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700834 dev_err(DEV, "sector: %llus, size: %d\n",
835 (unsigned long long)sector, size);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200836 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700837 }
838
839 if (!get_ldev(mdev))
Philipp Reisner73a01a12010-10-27 14:33:00 +0200840 return 0; /* no disk, no metadata, no bitmap to set bits in */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700841
842 nr_sectors = drbd_get_capacity(mdev->this_bdev);
843 esector = sector + (size >> 9) - 1;
844
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100845 if (!expect(sector < nr_sectors))
Philipp Reisnerb411b362009-09-25 16:07:19 -0700846 goto out;
Andreas Gruenbacher841ce242010-12-15 19:31:20 +0100847 if (!expect(esector < nr_sectors))
848 esector = nr_sectors - 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700849
850 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
851
852 /* we set it out of sync,
853 * we do not need to round anything here */
854 sbnr = BM_SECT_TO_BIT(sector);
855 ebnr = BM_SECT_TO_BIT(esector);
856
Philipp Reisnerb411b362009-09-25 16:07:19 -0700857 /* ok, (capacity & 7) != 0 sometimes, but who cares...
858 * we count rs_{total,left} in bits, not sectors. */
859 spin_lock_irqsave(&mdev->al_lock, flags);
860 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
861
862 enr = BM_SECT_TO_EXT(sector);
863 e = lc_find(mdev->resync, enr);
864 if (e)
865 lc_entry(e, struct bm_extent, lce)->rs_left += count;
866 spin_unlock_irqrestore(&mdev->al_lock, flags);
867
868out:
869 put_ldev(mdev);
Philipp Reisner73a01a12010-10-27 14:33:00 +0200870
871 return count;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700872}
873
874static
875struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
876{
877 struct lc_element *e;
878 struct bm_extent *bm_ext;
879 int wakeup = 0;
880 unsigned long rs_flags;
881
882 spin_lock_irq(&mdev->al_lock);
883 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
884 spin_unlock_irq(&mdev->al_lock);
885 return NULL;
886 }
887 e = lc_get(mdev->resync, enr);
888 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
889 if (bm_ext) {
890 if (bm_ext->lce.lc_number != enr) {
891 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
892 bm_ext->rs_failed = 0;
893 lc_changed(mdev->resync, &bm_ext->lce);
894 wakeup = 1;
895 }
896 if (bm_ext->lce.refcnt == 1)
897 mdev->resync_locked++;
898 set_bit(BME_NO_WRITES, &bm_ext->flags);
899 }
900 rs_flags = mdev->resync->flags;
901 spin_unlock_irq(&mdev->al_lock);
902 if (wakeup)
903 wake_up(&mdev->al_wait);
904
905 if (!bm_ext) {
906 if (rs_flags & LC_STARVING)
907 dev_warn(DEV, "Have to wait for element"
908 " (resync LRU too small?)\n");
909 BUG_ON(rs_flags & LC_DIRTY);
910 }
911
912 return bm_ext;
913}
914
915static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
916{
917 struct lc_element *al_ext;
918 int rv = 0;
919
920 spin_lock_irq(&mdev->al_lock);
921 if (unlikely(enr == mdev->act_log->new_number))
922 rv = 1;
923 else {
924 al_ext = lc_find(mdev->act_log, enr);
925 if (al_ext) {
926 if (al_ext->refcnt)
927 rv = 1;
928 }
929 }
930 spin_unlock_irq(&mdev->al_lock);
931
932 /*
933 if (unlikely(rv)) {
934 dev_info(DEV, "Delaying sync read until app's write is done\n");
935 }
936 */
937 return rv;
938}
939
940/**
941 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
942 * @mdev: DRBD device.
943 * @sector: The sector number.
944 *
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200945 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
Philipp Reisnerb411b362009-09-25 16:07:19 -0700946 */
947int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
948{
949 unsigned int enr = BM_SECT_TO_EXT(sector);
950 struct bm_extent *bm_ext;
951 int i, sig;
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100952 int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
953 200 times -> 20 seconds. */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700954
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100955retry:
Philipp Reisnerb411b362009-09-25 16:07:19 -0700956 sig = wait_event_interruptible(mdev->al_wait,
957 (bm_ext = _bme_get(mdev, enr)));
958 if (sig)
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200959 return -EINTR;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700960
961 if (test_bit(BME_LOCKED, &bm_ext->flags))
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200962 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700963
964 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
965 sig = wait_event_interruptible(mdev->al_wait,
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100966 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
Philipp Reisnerc507f462010-11-22 15:49:17 +0100967 test_bit(BME_PRIORITY, &bm_ext->flags));
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100968
969 if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
Philipp Reisnerb411b362009-09-25 16:07:19 -0700970 spin_lock_irq(&mdev->al_lock);
971 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100972 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
Philipp Reisnerb411b362009-09-25 16:07:19 -0700973 mdev->resync_locked--;
974 wake_up(&mdev->al_wait);
975 }
976 spin_unlock_irq(&mdev->al_lock);
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100977 if (sig)
978 return -EINTR;
979 if (schedule_timeout_interruptible(HZ/10))
980 return -EINTR;
Philipp Reisnerc507f462010-11-22 15:49:17 +0100981 if (sa && --sa == 0)
982 dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
983 "Resync stalled?\n");
Philipp Reisnerf91ab622010-11-09 13:59:41 +0100984 goto retry;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700985 }
986 }
Philipp Reisnerb411b362009-09-25 16:07:19 -0700987 set_bit(BME_LOCKED, &bm_ext->flags);
Lars Ellenberg80a40e42010-08-11 23:28:00 +0200988 return 0;
Philipp Reisnerb411b362009-09-25 16:07:19 -0700989}
990
991/**
992 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
993 * @mdev: DRBD device.
994 * @sector: The sector number.
995 *
996 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
997 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
998 * if there is still application IO going on in this area.
999 */
1000int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
1001{
1002 unsigned int enr = BM_SECT_TO_EXT(sector);
1003 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
1004 struct lc_element *e;
1005 struct bm_extent *bm_ext;
1006 int i;
1007
Philipp Reisnerb411b362009-09-25 16:07:19 -07001008 spin_lock_irq(&mdev->al_lock);
1009 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
1010 /* in case you have very heavy scattered io, it may
1011 * stall the syncer undefined if we give up the ref count
1012 * when we try again and requeue.
1013 *
1014 * if we don't give up the refcount, but the next time
1015 * we are scheduled this extent has been "synced" by new
1016 * application writes, we'd miss the lc_put on the
1017 * extent we keep the refcount on.
1018 * so we remembered which extent we had to try again, and
1019 * if the next requested one is something else, we do
1020 * the lc_put here...
1021 * we also have to wake_up
1022 */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001023 e = lc_find(mdev->resync, mdev->resync_wenr);
1024 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1025 if (bm_ext) {
1026 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1027 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1028 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1029 mdev->resync_wenr = LC_FREE;
1030 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
1031 mdev->resync_locked--;
1032 wake_up(&mdev->al_wait);
1033 } else {
1034 dev_alert(DEV, "LOGIC BUG\n");
1035 }
1036 }
1037 /* TRY. */
1038 e = lc_try_get(mdev->resync, enr);
1039 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1040 if (bm_ext) {
1041 if (test_bit(BME_LOCKED, &bm_ext->flags))
1042 goto proceed;
1043 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
1044 mdev->resync_locked++;
1045 } else {
1046 /* we did set the BME_NO_WRITES,
1047 * but then could not set BME_LOCKED,
1048 * so we tried again.
1049 * drop the extra reference. */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001050 bm_ext->lce.refcnt--;
1051 D_ASSERT(bm_ext->lce.refcnt > 0);
1052 }
1053 goto check_al;
1054 } else {
1055 /* do we rather want to try later? */
Jens Axboe6a0afdf2009-10-01 09:04:14 +02001056 if (mdev->resync_locked > mdev->resync->nr_elements-3)
Philipp Reisnerb411b362009-09-25 16:07:19 -07001057 goto try_again;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001058 /* Do or do not. There is no try. -- Yoda */
1059 e = lc_get(mdev->resync, enr);
1060 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1061 if (!bm_ext) {
1062 const unsigned long rs_flags = mdev->resync->flags;
1063 if (rs_flags & LC_STARVING)
1064 dev_warn(DEV, "Have to wait for element"
1065 " (resync LRU too small?)\n");
1066 BUG_ON(rs_flags & LC_DIRTY);
1067 goto try_again;
1068 }
1069 if (bm_ext->lce.lc_number != enr) {
1070 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1071 bm_ext->rs_failed = 0;
1072 lc_changed(mdev->resync, &bm_ext->lce);
1073 wake_up(&mdev->al_wait);
1074 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1075 }
1076 set_bit(BME_NO_WRITES, &bm_ext->flags);
1077 D_ASSERT(bm_ext->lce.refcnt == 1);
1078 mdev->resync_locked++;
1079 goto check_al;
1080 }
1081check_al:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001082 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
1083 if (unlikely(al_enr+i == mdev->act_log->new_number))
1084 goto try_again;
1085 if (lc_is_used(mdev->act_log, al_enr+i))
1086 goto try_again;
1087 }
1088 set_bit(BME_LOCKED, &bm_ext->flags);
1089proceed:
1090 mdev->resync_wenr = LC_FREE;
1091 spin_unlock_irq(&mdev->al_lock);
1092 return 0;
1093
1094try_again:
Philipp Reisnerb411b362009-09-25 16:07:19 -07001095 if (bm_ext)
1096 mdev->resync_wenr = enr;
1097 spin_unlock_irq(&mdev->al_lock);
1098 return -EAGAIN;
1099}
1100
1101void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1102{
1103 unsigned int enr = BM_SECT_TO_EXT(sector);
1104 struct lc_element *e;
1105 struct bm_extent *bm_ext;
1106 unsigned long flags;
1107
Philipp Reisnerb411b362009-09-25 16:07:19 -07001108 spin_lock_irqsave(&mdev->al_lock, flags);
1109 e = lc_find(mdev->resync, enr);
1110 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1111 if (!bm_ext) {
1112 spin_unlock_irqrestore(&mdev->al_lock, flags);
1113 if (__ratelimit(&drbd_ratelimit_state))
1114 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1115 return;
1116 }
1117
1118 if (bm_ext->lce.refcnt == 0) {
1119 spin_unlock_irqrestore(&mdev->al_lock, flags);
1120 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1121 "but refcnt is 0!?\n",
1122 (unsigned long long)sector, enr);
1123 return;
1124 }
1125
1126 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
Philipp Reisnere3555d82010-11-07 15:56:29 +01001127 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
Philipp Reisnerb411b362009-09-25 16:07:19 -07001128 mdev->resync_locked--;
1129 wake_up(&mdev->al_wait);
1130 }
1131
1132 spin_unlock_irqrestore(&mdev->al_lock, flags);
1133}
1134
1135/**
1136 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1137 * @mdev: DRBD device.
1138 */
1139void drbd_rs_cancel_all(struct drbd_conf *mdev)
1140{
Philipp Reisnerb411b362009-09-25 16:07:19 -07001141 spin_lock_irq(&mdev->al_lock);
1142
1143 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1144 lc_reset(mdev->resync);
1145 put_ldev(mdev);
1146 }
1147 mdev->resync_locked = 0;
1148 mdev->resync_wenr = LC_FREE;
1149 spin_unlock_irq(&mdev->al_lock);
1150 wake_up(&mdev->al_wait);
1151}
1152
1153/**
1154 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1155 * @mdev: DRBD device.
1156 *
1157 * Returns 0 upon success, -EAGAIN if at least one reference count was
1158 * not zero.
1159 */
1160int drbd_rs_del_all(struct drbd_conf *mdev)
1161{
1162 struct lc_element *e;
1163 struct bm_extent *bm_ext;
1164 int i;
1165
Philipp Reisnerb411b362009-09-25 16:07:19 -07001166 spin_lock_irq(&mdev->al_lock);
1167
1168 if (get_ldev_if_state(mdev, D_FAILED)) {
1169 /* ok, ->resync is there. */
1170 for (i = 0; i < mdev->resync->nr_elements; i++) {
1171 e = lc_element_by_index(mdev->resync, i);
Philipp Reisnerb2b163d2010-04-02 08:40:33 +02001172 bm_ext = lc_entry(e, struct bm_extent, lce);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001173 if (bm_ext->lce.lc_number == LC_FREE)
1174 continue;
1175 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1176 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1177 " got 'synced' by application io\n",
1178 mdev->resync_wenr);
1179 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1180 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1181 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1182 mdev->resync_wenr = LC_FREE;
1183 lc_put(mdev->resync, &bm_ext->lce);
1184 }
1185 if (bm_ext->lce.refcnt != 0) {
1186 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1187 "refcnt=%d\n", bm_ext->lce.refcnt);
1188 put_ldev(mdev);
1189 spin_unlock_irq(&mdev->al_lock);
1190 return -EAGAIN;
1191 }
1192 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1193 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1194 lc_del(mdev->resync, &bm_ext->lce);
1195 }
1196 D_ASSERT(mdev->resync->used == 0);
1197 put_ldev(mdev);
1198 }
1199 spin_unlock_irq(&mdev->al_lock);
1200
1201 return 0;
1202}
1203
1204/**
1205 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1206 * @mdev: DRBD device.
1207 * @sector: The sector number.
1208 * @size: Size of failed IO operation, in byte.
1209 */
1210void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1211{
1212 /* Is called from worker and receiver context _only_ */
1213 unsigned long sbnr, ebnr, lbnr;
1214 unsigned long count;
1215 sector_t esector, nr_sectors;
1216 int wake_up = 0;
1217
Lars Ellenberg1816a2b2010-11-11 15:19:07 +01001218 if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
Philipp Reisnerb411b362009-09-25 16:07:19 -07001219 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1220 (unsigned long long)sector, size);
1221 return;
1222 }
1223 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1224 esector = sector + (size >> 9) - 1;
1225
Andreas Gruenbacher841ce242010-12-15 19:31:20 +01001226 if (!expect(sector < nr_sectors))
1227 return;
1228 if (!expect(esector < nr_sectors))
1229 esector = nr_sectors - 1;
Philipp Reisnerb411b362009-09-25 16:07:19 -07001230
1231 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1232
1233 /*
1234 * round up start sector, round down end sector. we make sure we only
1235 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1236 if (unlikely(esector < BM_SECT_PER_BIT-1))
1237 return;
1238 if (unlikely(esector == (nr_sectors-1)))
1239 ebnr = lbnr;
1240 else
1241 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1242 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1243
1244 if (sbnr > ebnr)
1245 return;
1246
1247 /*
1248 * ok, (capacity & 7) != 0 sometimes, but who cares...
1249 * we count rs_{total,left} in bits, not sectors.
1250 */
1251 spin_lock_irq(&mdev->al_lock);
1252 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1253 if (count) {
1254 mdev->rs_failed += count;
1255
1256 if (get_ldev(mdev)) {
Andreas Gruenbacher81e84652010-12-09 15:03:57 +01001257 drbd_try_clear_on_disk_bm(mdev, sector, count, false);
Philipp Reisnerb411b362009-09-25 16:07:19 -07001258 put_ldev(mdev);
1259 }
1260
1261 /* just wake_up unconditional now, various lc_chaged(),
1262 * lc_put() in drbd_try_clear_on_disk_bm(). */
1263 wake_up = 1;
1264 }
1265 spin_unlock_irq(&mdev->al_lock);
1266 if (wake_up)
1267 wake_up(&mdev->al_wait);
1268}