drbd: Clarify when activity log I/O is delegated to the worker thread
[deliverable/linux.git] / drivers / block / drbd / drbd_actlog.c
CommitLineData
b411b363
PR
1/*
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>
7ad651b5 27#include <linux/crc32c.h>
b411b363 28#include <linux/drbd.h>
7ad651b5
LE
29#include <linux/drbd_limits.h>
30#include <linux/dynamic_debug.h>
b411b363 31#include "drbd_int.h"
b411b363
PR
32#include "drbd_wrappers.h"
33
85f103d8
LE
34
35enum al_transaction_types {
36 AL_TR_UPDATE = 0,
37 AL_TR_INITIALIZED = 0xffff
38};
7ad651b5
LE
39/* all fields on disc in big endian */
40struct __packed al_transaction_on_disk {
41 /* don't we all like magic */
42 __be32 magic;
43
44 /* to identify the most recent transaction block
45 * in the on disk ring buffer */
46 __be32 tr_number;
47
48 /* checksum on the full 4k block, with this field set to 0. */
49 __be32 crc32c;
50
51 /* type of transaction, special transaction types like:
85f103d8
LE
52 * purge-all, set-all-idle, set-all-active, ... to-be-defined
53 * see also enum al_transaction_types */
7ad651b5
LE
54 __be16 transaction_type;
55
56 /* we currently allow only a few thousand extents,
57 * so 16bit will be enough for the slot number. */
58
59 /* how many updates in this transaction */
60 __be16 n_updates;
61
62 /* maximum slot number, "al-extents" in drbd.conf speak.
63 * Having this in each transaction should make reconfiguration
64 * of that parameter easier. */
65 __be16 context_size;
66
67 /* slot number the context starts with */
68 __be16 context_start_slot_nr;
69
70 /* Some reserved bytes. Expected usage is a 64bit counter of
71 * sectors-written since device creation, and other data generation tag
72 * supporting usage */
73 __be32 __reserved[4];
74
75 /* --- 36 byte used --- */
76
77 /* Reserve space for up to AL_UPDATES_PER_TRANSACTION changes
78 * in one transaction, then use the remaining byte in the 4k block for
79 * context information. "Flexible" number of updates per transaction
80 * does not help, as we have to account for the case when all update
81 * slots are used anyways, so it would only complicate code without
82 * additional benefit.
83 */
84 __be16 update_slot_nr[AL_UPDATES_PER_TRANSACTION];
85
86 /* but the extent number is 32bit, which at an extent size of 4 MiB
87 * allows to cover device sizes of up to 2**54 Byte (16 PiB) */
88 __be32 update_extent_nr[AL_UPDATES_PER_TRANSACTION];
89
90 /* --- 420 bytes used (36 + 64*6) --- */
91
92 /* 4096 - 420 = 3676 = 919 * 4 */
93 __be32 context[AL_CONTEXT_PER_TRANSACTION];
b411b363
PR
94};
95
96struct update_odbm_work {
97 struct drbd_work w;
98 unsigned int enr;
99};
100
101struct update_al_work {
102 struct drbd_work w;
b411b363 103 struct completion event;
7ad651b5 104 int err;
b411b363
PR
105};
106
56392d2f 107static int al_write_transaction(struct drbd_conf *mdev, bool delegate);
b411b363 108
cdfda633
PR
109void *drbd_md_get_buffer(struct drbd_conf *mdev)
110{
111 int r;
112
113 wait_event(mdev->misc_wait,
114 (r = atomic_cmpxchg(&mdev->md_io_in_use, 0, 1)) == 0 ||
115 mdev->state.disk <= D_FAILED);
116
117 return r ? NULL : page_address(mdev->md_io_page);
118}
119
120void drbd_md_put_buffer(struct drbd_conf *mdev)
121{
122 if (atomic_dec_and_test(&mdev->md_io_in_use))
123 wake_up(&mdev->misc_wait);
124}
125
e34b677d 126void wait_until_done_or_force_detached(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
32db80f6 127 unsigned int *done)
cdfda633 128{
32db80f6
PR
129 long dt;
130
131 rcu_read_lock();
132 dt = rcu_dereference(bdev->disk_conf)->disk_timeout;
133 rcu_read_unlock();
134 dt = dt * HZ / 10;
135 if (dt == 0)
136 dt = MAX_SCHEDULE_TIMEOUT;
137
e34b677d
LE
138 dt = wait_event_timeout(mdev->misc_wait,
139 *done || test_bit(FORCE_DETACH, &mdev->flags), dt);
140 if (dt == 0) {
32db80f6 141 dev_err(DEV, "meta-data IO operation timed out\n");
e34b677d
LE
142 drbd_chk_io_error(mdev, 1, DRBD_FORCE_DETACH);
143 }
cdfda633
PR
144}
145
b411b363
PR
146static int _drbd_md_sync_page_io(struct drbd_conf *mdev,
147 struct drbd_backing_dev *bdev,
148 struct page *page, sector_t sector,
149 int rw, int size)
150{
151 struct bio *bio;
ac29f403 152 int err;
b411b363 153
cdfda633
PR
154 mdev->md_io.done = 0;
155 mdev->md_io.error = -ENODEV;
b411b363 156
a8a4e51e 157 if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags))
86e1e98e 158 rw |= REQ_FUA | REQ_FLUSH;
721a9602 159 rw |= REQ_SYNC;
b411b363 160
da4a75d2 161 bio = bio_alloc_drbd(GFP_NOIO);
b411b363
PR
162 bio->bi_bdev = bdev->md_bdev;
163 bio->bi_sector = sector;
ac29f403
AG
164 err = -EIO;
165 if (bio_add_page(bio, page, size, 0) != size)
b411b363 166 goto out;
cdfda633 167 bio->bi_private = &mdev->md_io;
b411b363
PR
168 bio->bi_end_io = drbd_md_io_complete;
169 bio->bi_rw = rw;
170
c04ccaa6
LE
171 if (!(rw & WRITE) && mdev->state.disk == D_DISKLESS && mdev->ldev == NULL)
172 /* special case, drbd_md_read() during drbd_adm_attach(): no get_ldev */
173 ;
174 else if (!get_ldev_if_state(mdev, D_ATTACHING)) {
175 /* Corresponding put_ldev in drbd_md_io_complete() */
cdfda633
PR
176 dev_err(DEV, "ASSERT FAILED: get_ldev_if_state() == 1 in _drbd_md_sync_page_io()\n");
177 err = -ENODEV;
178 goto out;
179 }
180
181 bio_get(bio); /* one bio_put() is in the completion handler */
182 atomic_inc(&mdev->md_io_in_use); /* drbd_md_put_buffer() is in the completion handler */
0cf9d27e 183 if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD))
b411b363
PR
184 bio_endio(bio, -EIO);
185 else
186 submit_bio(rw, bio);
e34b677d 187 wait_until_done_or_force_detached(mdev, bdev, &mdev->md_io.done);
ac29f403 188 if (bio_flagged(bio, BIO_UPTODATE))
cdfda633 189 err = mdev->md_io.error;
b411b363 190
b411b363
PR
191 out:
192 bio_put(bio);
ac29f403 193 return err;
b411b363
PR
194}
195
196int drbd_md_sync_page_io(struct drbd_conf *mdev, struct drbd_backing_dev *bdev,
197 sector_t sector, int rw)
198{
3fbf4d21 199 int err;
b411b363
PR
200 struct page *iop = mdev->md_io_page;
201
cdfda633 202 D_ASSERT(atomic_read(&mdev->md_io_in_use) == 1);
b411b363
PR
203
204 BUG_ON(!bdev->md_bdev);
205
c04ccaa6 206 dev_dbg(DEV, "meta_data io: %s [%d]:%s(,%llus,%s) %pS\n",
7ad651b5 207 current->comm, current->pid, __func__,
c04ccaa6
LE
208 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ",
209 (void*)_RET_IP_ );
b411b363
PR
210
211 if (sector < drbd_md_first_sector(bdev) ||
7ad651b5 212 sector + 7 > drbd_md_last_sector(bdev))
b411b363
PR
213 dev_alert(DEV, "%s [%d]:%s(,%llus,%s) out of range md access!\n",
214 current->comm, current->pid, __func__,
215 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ");
216
ae8bf312
LE
217 /* we do all our meta data IO in aligned 4k blocks. */
218 err = _drbd_md_sync_page_io(mdev, bdev, iop, sector, rw, 4096);
3fbf4d21 219 if (err) {
935be260
AG
220 dev_err(DEV, "drbd_md_sync_page_io(,%llus,%s) failed with error %d\n",
221 (unsigned long long)sector, (rw & WRITE) ? "WRITE" : "READ", err);
b411b363 222 }
3fbf4d21 223 return err;
b411b363
PR
224}
225
226static struct lc_element *_al_get(struct drbd_conf *mdev, unsigned int enr)
227{
228 struct lc_element *al_ext;
229 struct lc_element *tmp;
f91ab628 230 int wake;
b411b363
PR
231
232 spin_lock_irq(&mdev->al_lock);
233 tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT);
234 if (unlikely(tmp != NULL)) {
235 struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
236 if (test_bit(BME_NO_WRITES, &bm_ext->flags)) {
f91ab628 237 wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags);
b411b363 238 spin_unlock_irq(&mdev->al_lock);
f91ab628
PR
239 if (wake)
240 wake_up(&mdev->al_wait);
b411b363
PR
241 return NULL;
242 }
243 }
46a15bc3 244 al_ext = lc_get(mdev->act_log, enr);
b411b363 245 spin_unlock_irq(&mdev->al_lock);
b411b363
PR
246 return al_ext;
247}
248
56392d2f
LE
249/*
250 * @delegate: delegate activity log I/O to the worker thread
251 */
252void drbd_al_begin_io(struct drbd_conf *mdev, struct drbd_interval *i, bool delegate)
b411b363 253{
7726547e
LE
254 /* for bios crossing activity log extent boundaries,
255 * we may need to activate two extents in one go */
e15766e9 256 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
81a3537a 257 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
e15766e9 258 unsigned enr;
7dc1d67f
LE
259 bool locked = false;
260
56392d2f
LE
261 /* When called through generic_make_request(), we must delegate
262 * activity log I/O to the worker thread: a further request
263 * submitted via generic_make_request() within the same task
264 * would be queued on current->bio_list, and would only start
265 * after this function returns (see generic_make_request()).
266 *
267 * However, if we *are* the worker, we must not delegate to ourselves.
268 */
269
270 if (delegate)
271 BUG_ON(current == mdev->tconn->worker.task);
b411b363 272
81a3537a 273 D_ASSERT(first <= last);
b411b363
PR
274 D_ASSERT(atomic_read(&mdev->local_cnt) > 0);
275
e15766e9
LE
276 for (enr = first; enr <= last; enr++)
277 wait_event(mdev->al_wait, _al_get(mdev, enr) != NULL);
b411b363 278
7dc1d67f
LE
279 /* Serialize multiple transactions.
280 * This uses test_and_set_bit, memory barrier is implicit.
281 */
282 wait_event(mdev->al_wait,
283 mdev->act_log->pending_changes == 0 ||
284 (locked = lc_try_lock_for_transaction(mdev->act_log)));
285
286 if (locked) {
7ad651b5
LE
287 /* Double check: it may have been committed by someone else,
288 * while we have been waiting for the lock. */
e15766e9 289 if (mdev->act_log->pending_changes) {
9a51ab1c
PR
290 bool write_al_updates;
291
292 rcu_read_lock();
293 write_al_updates = rcu_dereference(mdev->ldev->disk_conf)->al_updates;
294 rcu_read_unlock();
295
296 if (write_al_updates) {
56392d2f 297 al_write_transaction(mdev, delegate);
9a51ab1c
PR
298 mdev->al_writ_cnt++;
299 }
7ad651b5
LE
300
301 spin_lock_irq(&mdev->al_lock);
302 /* FIXME
1b7ab15b 303 if (err)
7ad651b5
LE
304 we need an "lc_cancel" here;
305 */
306 lc_committed(mdev->act_log);
307 spin_unlock_irq(&mdev->al_lock);
308 }
309 lc_unlock(mdev->act_log);
b411b363
PR
310 wake_up(&mdev->al_wait);
311 }
312}
313
181286ad 314void drbd_al_complete_io(struct drbd_conf *mdev, struct drbd_interval *i)
b411b363 315{
e15766e9
LE
316 /* for bios crossing activity log extent boundaries,
317 * we may need to activate two extents in one go */
318 unsigned first = i->sector >> (AL_EXTENT_SHIFT-9);
81a3537a 319 unsigned last = i->size == 0 ? first : (i->sector + (i->size >> 9) - 1) >> (AL_EXTENT_SHIFT-9);
e15766e9 320 unsigned enr;
b411b363
PR
321 struct lc_element *extent;
322 unsigned long flags;
323
81a3537a 324 D_ASSERT(first <= last);
b411b363
PR
325 spin_lock_irqsave(&mdev->al_lock, flags);
326
e15766e9
LE
327 for (enr = first; enr <= last; enr++) {
328 extent = lc_find(mdev->act_log, enr);
329 if (!extent) {
330 dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr);
331 continue;
332 }
376694a0 333 lc_put(mdev->act_log, extent);
b411b363 334 }
b411b363 335 spin_unlock_irqrestore(&mdev->al_lock, flags);
e15766e9 336 wake_up(&mdev->al_wait);
b411b363
PR
337}
338
19f843aa
LE
339#if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)
340/* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT
341 * are still coupled, or assume too much about their relation.
342 * Code below will not work if this is violated.
343 * Will be cleaned up with some followup patch.
344 */
345# error FIXME
346#endif
347
348static unsigned int al_extent_to_bm_page(unsigned int al_enr)
349{
350 return al_enr >>
351 /* bit to page */
352 ((PAGE_SHIFT + 3) -
353 /* al extent number to bit */
354 (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT));
355}
356
357static unsigned int rs_extent_to_bm_page(unsigned int rs_enr)
358{
359 return rs_enr >>
360 /* bit to page */
361 ((PAGE_SHIFT + 3) -
acb104c3 362 /* resync extent number to bit */
19f843aa
LE
363 (BM_EXT_SHIFT - BM_BLOCK_SHIFT));
364}
365
ae8bf312
LE
366static sector_t al_tr_number_to_on_disk_sector(struct drbd_conf *mdev)
367{
3a4d4eb3
LE
368 const unsigned int stripes = mdev->ldev->md.al_stripes;
369 const unsigned int stripe_size_4kB = mdev->ldev->md.al_stripe_size_4k;
ae8bf312
LE
370
371 /* transaction number, modulo on-disk ring buffer wrap around */
3a4d4eb3 372 unsigned int t = mdev->al_tr_number % (mdev->ldev->md.al_size_4k);
ae8bf312
LE
373
374 /* ... to aligned 4k on disk block */
375 t = ((t % stripes) * stripe_size_4kB) + t/stripes;
376
377 /* ... to 512 byte sector in activity log */
378 t *= 8;
379
380 /* ... plus offset to the on disk position */
381 return mdev->ldev->md.md_offset + mdev->ldev->md.al_offset + t;
382}
383
99920dc5 384static int
1b7ab15b 385_al_write_transaction(struct drbd_conf *mdev)
b411b363 386{
7ad651b5
LE
387 struct al_transaction_on_disk *buffer;
388 struct lc_element *e;
b411b363 389 sector_t sector;
7ad651b5
LE
390 int i, mx;
391 unsigned extent_nr;
392 unsigned crc = 0;
1b7ab15b 393 int err = 0;
b411b363
PR
394
395 if (!get_ldev(mdev)) {
7ad651b5
LE
396 dev_err(DEV, "disk is %s, cannot start al transaction\n",
397 drbd_disk_str(mdev->state.disk));
1b7ab15b 398 return -EIO;
b411b363 399 }
b411b363 400
6719fb03
LE
401 /* The bitmap write may have failed, causing a state change. */
402 if (mdev->state.disk < D_INCONSISTENT) {
403 dev_err(DEV,
7ad651b5
LE
404 "disk is %s, cannot write al transaction\n",
405 drbd_disk_str(mdev->state.disk));
6719fb03 406 put_ldev(mdev);
1b7ab15b 407 return -EIO;
6719fb03
LE
408 }
409
cdfda633
PR
410 buffer = drbd_md_get_buffer(mdev); /* protects md_io_buffer, al_tr_cycle, ... */
411 if (!buffer) {
412 dev_err(DEV, "disk failed while waiting for md_io buffer\n");
cdfda633 413 put_ldev(mdev);
1b7ab15b 414 return -ENODEV;
cdfda633 415 }
b411b363 416
7ad651b5
LE
417 memset(buffer, 0, sizeof(*buffer));
418 buffer->magic = cpu_to_be32(DRBD_AL_MAGIC);
b411b363
PR
419 buffer->tr_number = cpu_to_be32(mdev->al_tr_number);
420
7ad651b5
LE
421 i = 0;
422
423 /* Even though no one can start to change this list
424 * once we set the LC_LOCKED -- from drbd_al_begin_io(),
425 * lc_try_lock_for_transaction() --, someone may still
426 * be in the process of changing it. */
427 spin_lock_irq(&mdev->al_lock);
428 list_for_each_entry(e, &mdev->act_log->to_be_changed, list) {
429 if (i == AL_UPDATES_PER_TRANSACTION) {
430 i++;
431 break;
432 }
433 buffer->update_slot_nr[i] = cpu_to_be16(e->lc_index);
434 buffer->update_extent_nr[i] = cpu_to_be32(e->lc_new_number);
435 if (e->lc_number != LC_FREE)
436 drbd_bm_mark_for_writeout(mdev,
437 al_extent_to_bm_page(e->lc_number));
438 i++;
439 }
440 spin_unlock_irq(&mdev->al_lock);
441 BUG_ON(i > AL_UPDATES_PER_TRANSACTION);
b411b363 442
7ad651b5
LE
443 buffer->n_updates = cpu_to_be16(i);
444 for ( ; i < AL_UPDATES_PER_TRANSACTION; i++) {
445 buffer->update_slot_nr[i] = cpu_to_be16(-1);
446 buffer->update_extent_nr[i] = cpu_to_be32(LC_FREE);
447 }
b411b363 448
7ad651b5
LE
449 buffer->context_size = cpu_to_be16(mdev->act_log->nr_elements);
450 buffer->context_start_slot_nr = cpu_to_be16(mdev->al_tr_cycle);
b411b363 451
7ad651b5 452 mx = min_t(int, AL_CONTEXT_PER_TRANSACTION,
b411b363
PR
453 mdev->act_log->nr_elements - mdev->al_tr_cycle);
454 for (i = 0; i < mx; i++) {
455 unsigned idx = mdev->al_tr_cycle + i;
456 extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number;
7ad651b5 457 buffer->context[i] = cpu_to_be32(extent_nr);
b411b363 458 }
7ad651b5
LE
459 for (; i < AL_CONTEXT_PER_TRANSACTION; i++)
460 buffer->context[i] = cpu_to_be32(LC_FREE);
461
462 mdev->al_tr_cycle += AL_CONTEXT_PER_TRANSACTION;
b411b363
PR
463 if (mdev->al_tr_cycle >= mdev->act_log->nr_elements)
464 mdev->al_tr_cycle = 0;
465
ae8bf312 466 sector = al_tr_number_to_on_disk_sector(mdev);
b411b363 467
7ad651b5
LE
468 crc = crc32c(0, buffer, 4096);
469 buffer->crc32c = cpu_to_be32(crc);
b411b363 470
ae8bf312 471 /* normal execution path goes through all three branches */
7ad651b5 472 if (drbd_bm_write_hinted(mdev))
1b7ab15b 473 err = -EIO;
7ad651b5 474 /* drbd_chk_io_error done already */
3fbf4d21 475 else if (drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) {
1b7ab15b 476 err = -EIO;
0c849666 477 drbd_chk_io_error(mdev, 1, DRBD_META_IO_ERROR);
7ad651b5 478 } else {
7ad651b5
LE
479 mdev->al_tr_number++;
480 }
b411b363 481
cdfda633 482 drbd_md_put_buffer(mdev);
b411b363
PR
483 put_ldev(mdev);
484
1b7ab15b
PR
485 return err;
486}
487
488
489static int w_al_write_transaction(struct drbd_work *w, int unused)
490{
491 struct update_al_work *aw = container_of(w, struct update_al_work, w);
492 struct drbd_conf *mdev = w->mdev;
493 int err;
494
495 err = _al_write_transaction(mdev);
496 aw->err = err;
497 complete(&aw->event);
498
499 return err != -EIO ? err : 0;
500}
501
502/* Calls from worker context (see w_restart_disk_io()) need to write the
503 transaction directly. Others came through generic_make_request(),
504 those need to delegate it to the worker. */
56392d2f 505static int al_write_transaction(struct drbd_conf *mdev, bool delegate)
1b7ab15b 506{
56392d2f
LE
507 if (delegate) {
508 struct update_al_work al_work;
509 init_completion(&al_work.event);
510 al_work.w.cb = w_al_write_transaction;
511 al_work.w.mdev = mdev;
512 drbd_queue_work_front(&mdev->tconn->sender_work, &al_work.w);
513 wait_for_completion(&al_work.event);
514 return al_work.err;
515 } else
1b7ab15b 516 return _al_write_transaction(mdev);
b411b363
PR
517}
518
b411b363
PR
519static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext)
520{
521 int rv;
522
523 spin_lock_irq(&mdev->al_lock);
524 rv = (al_ext->refcnt == 0);
525 if (likely(rv))
526 lc_del(mdev->act_log, al_ext);
527 spin_unlock_irq(&mdev->al_lock);
528
529 return rv;
530}
531
532/**
533 * drbd_al_shrink() - Removes all active extents form the activity log
534 * @mdev: DRBD device.
535 *
536 * Removes all active extents form the activity log, waiting until
537 * the reference count of each entry dropped to 0 first, of course.
538 *
539 * You need to lock mdev->act_log with lc_try_lock() / lc_unlock()
540 */
541void drbd_al_shrink(struct drbd_conf *mdev)
542{
543 struct lc_element *al_ext;
544 int i;
545
46a15bc3 546 D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags));
b411b363
PR
547
548 for (i = 0; i < mdev->act_log->nr_elements; i++) {
549 al_ext = lc_element_by_index(mdev->act_log, i);
550 if (al_ext->lc_number == LC_FREE)
551 continue;
552 wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext));
553 }
554
555 wake_up(&mdev->al_wait);
556}
557
99920dc5 558static int w_update_odbm(struct drbd_work *w, int unused)
b411b363
PR
559{
560 struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w);
00d56944 561 struct drbd_conf *mdev = w->mdev;
3b98c0c2 562 struct sib_info sib = { .sib_reason = SIB_SYNC_PROGRESS, };
b411b363
PR
563
564 if (!get_ldev(mdev)) {
565 if (__ratelimit(&drbd_ratelimit_state))
566 dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n");
567 kfree(udw);
99920dc5 568 return 0;
b411b363
PR
569 }
570
19f843aa 571 drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr));
b411b363
PR
572 put_ldev(mdev);
573
574 kfree(udw);
575
576 if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) {
577 switch (mdev->state.conn) {
578 case C_SYNC_SOURCE: case C_SYNC_TARGET:
579 case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T:
580 drbd_resync_finished(mdev);
581 default:
582 /* nothing to do */
583 break;
584 }
585 }
3b98c0c2 586 drbd_bcast_event(mdev, &sib);
b411b363 587
99920dc5 588 return 0;
b411b363
PR
589}
590
591
592/* ATTENTION. The AL's extents are 4MB each, while the extents in the
593 * resync LRU-cache are 16MB each.
594 * The caller of this function has to hold an get_ldev() reference.
595 *
596 * TODO will be obsoleted once we have a caching lru of the on disk bitmap
597 */
598static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector,
599 int count, int success)
600{
601 struct lc_element *e;
602 struct update_odbm_work *udw;
603
604 unsigned int enr;
605
606 D_ASSERT(atomic_read(&mdev->local_cnt));
607
608 /* I simply assume that a sector/size pair never crosses
609 * a 16 MB extent border. (Currently this is true...) */
610 enr = BM_SECT_TO_EXT(sector);
611
612 e = lc_get(mdev->resync, enr);
613 if (e) {
614 struct bm_extent *ext = lc_entry(e, struct bm_extent, lce);
615 if (ext->lce.lc_number == enr) {
616 if (success)
617 ext->rs_left -= count;
618 else
619 ext->rs_failed += count;
620 if (ext->rs_left < ext->rs_failed) {
975b2979
PR
621 dev_warn(DEV, "BAD! sector=%llus enr=%u rs_left=%d "
622 "rs_failed=%d count=%d cstate=%s\n",
b411b363
PR
623 (unsigned long long)sector,
624 ext->lce.lc_number, ext->rs_left,
975b2979
PR
625 ext->rs_failed, count,
626 drbd_conn_str(mdev->state.conn));
627
628 /* We don't expect to be able to clear more bits
629 * than have been set when we originally counted
630 * the set bits to cache that value in ext->rs_left.
631 * Whatever the reason (disconnect during resync,
632 * delayed local completion of an application write),
633 * try to fix it up by recounting here. */
634 ext->rs_left = drbd_bm_e_weight(mdev, enr);
b411b363
PR
635 }
636 } else {
637 /* Normally this element should be in the cache,
638 * since drbd_rs_begin_io() pulled it already in.
639 *
640 * But maybe an application write finished, and we set
641 * something outside the resync lru_cache in sync.
642 */
643 int rs_left = drbd_bm_e_weight(mdev, enr);
644 if (ext->flags != 0) {
645 dev_warn(DEV, "changing resync lce: %d[%u;%02lx]"
646 " -> %d[%u;00]\n",
647 ext->lce.lc_number, ext->rs_left,
648 ext->flags, enr, rs_left);
649 ext->flags = 0;
650 }
651 if (ext->rs_failed) {
652 dev_warn(DEV, "Kicking resync_lru element enr=%u "
653 "out with rs_failed=%d\n",
654 ext->lce.lc_number, ext->rs_failed);
b411b363
PR
655 }
656 ext->rs_left = rs_left;
657 ext->rs_failed = success ? 0 : count;
46a15bc3
LE
658 /* we don't keep a persistent log of the resync lru,
659 * we can commit any change right away. */
660 lc_committed(mdev->resync);
b411b363
PR
661 }
662 lc_put(mdev->resync, &ext->lce);
663 /* no race, we are within the al_lock! */
664
665 if (ext->rs_left == ext->rs_failed) {
666 ext->rs_failed = 0;
667
668 udw = kmalloc(sizeof(*udw), GFP_ATOMIC);
669 if (udw) {
670 udw->enr = ext->lce.lc_number;
671 udw->w.cb = w_update_odbm;
a21e9298 672 udw->w.mdev = mdev;
d5b27b01 673 drbd_queue_work_front(&mdev->tconn->sender_work, &udw->w);
b411b363
PR
674 } else {
675 dev_warn(DEV, "Could not kmalloc an udw\n");
b411b363
PR
676 }
677 }
678 } else {
679 dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n",
680 mdev->resync_locked,
681 mdev->resync->nr_elements,
682 mdev->resync->flags);
683 }
684}
685
c6ea14df
LE
686void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go)
687{
688 unsigned long now = jiffies;
689 unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark];
690 int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS;
691 if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) {
692 if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go &&
693 mdev->state.conn != C_PAUSED_SYNC_T &&
694 mdev->state.conn != C_PAUSED_SYNC_S) {
695 mdev->rs_mark_time[next] = now;
696 mdev->rs_mark_left[next] = still_to_go;
697 mdev->rs_last_mark = next;
698 }
699 }
700}
701
b411b363
PR
702/* clear the bit corresponding to the piece of storage in question:
703 * size byte of data starting from sector. Only clear a bits of the affected
704 * one ore more _aligned_ BM_BLOCK_SIZE blocks.
705 *
706 * called by worker on C_SYNC_TARGET and receiver on SyncSource.
707 *
708 */
709void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size,
710 const char *file, const unsigned int line)
711{
712 /* Is called from worker and receiver context _only_ */
713 unsigned long sbnr, ebnr, lbnr;
714 unsigned long count = 0;
715 sector_t esector, nr_sectors;
716 int wake_up = 0;
717 unsigned long flags;
718
c670a398 719 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
b411b363
PR
720 dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n",
721 (unsigned long long)sector, size);
722 return;
723 }
518a4d53
PR
724
725 if (!get_ldev(mdev))
726 return; /* no disk, no metadata, no bitmap to clear bits in */
727
b411b363
PR
728 nr_sectors = drbd_get_capacity(mdev->this_bdev);
729 esector = sector + (size >> 9) - 1;
730
841ce241 731 if (!expect(sector < nr_sectors))
518a4d53 732 goto out;
841ce241
AG
733 if (!expect(esector < nr_sectors))
734 esector = nr_sectors - 1;
b411b363
PR
735
736 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
737
738 /* we clear it (in sync).
739 * round up start sector, round down end sector. we make sure we only
740 * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */
741 if (unlikely(esector < BM_SECT_PER_BIT-1))
518a4d53 742 goto out;
b411b363
PR
743 if (unlikely(esector == (nr_sectors-1)))
744 ebnr = lbnr;
745 else
746 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
747 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
748
b411b363 749 if (sbnr > ebnr)
518a4d53 750 goto out;
b411b363
PR
751
752 /*
753 * ok, (capacity & 7) != 0 sometimes, but who cares...
754 * we count rs_{total,left} in bits, not sectors.
755 */
b411b363 756 count = drbd_bm_clear_bits(mdev, sbnr, ebnr);
518a4d53 757 if (count) {
c6ea14df 758 drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev));
1d7734a0 759 spin_lock_irqsave(&mdev->al_lock, flags);
81e84650 760 drbd_try_clear_on_disk_bm(mdev, sector, count, true);
1d7734a0
LE
761 spin_unlock_irqrestore(&mdev->al_lock, flags);
762
b411b363
PR
763 /* just wake_up unconditional now, various lc_chaged(),
764 * lc_put() in drbd_try_clear_on_disk_bm(). */
765 wake_up = 1;
766 }
518a4d53
PR
767out:
768 put_ldev(mdev);
b411b363
PR
769 if (wake_up)
770 wake_up(&mdev->al_wait);
771}
772
773/*
774 * this is intended to set one request worth of data out of sync.
775 * affects at least 1 bit,
1816a2b4 776 * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits.
b411b363
PR
777 *
778 * called by tl_clear and drbd_send_dblock (==drbd_make_request).
779 * so this can be _any_ process.
780 */
73a01a18 781int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size,
b411b363
PR
782 const char *file, const unsigned int line)
783{
376694a0 784 unsigned long sbnr, ebnr, flags;
b411b363 785 sector_t esector, nr_sectors;
73a01a18 786 unsigned int enr, count = 0;
b411b363
PR
787 struct lc_element *e;
788
81a3537a
LE
789 /* this should be an empty REQ_FLUSH */
790 if (size == 0)
791 return 0;
792
793 if (size < 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
b411b363
PR
794 dev_err(DEV, "sector: %llus, size: %d\n",
795 (unsigned long long)sector, size);
73a01a18 796 return 0;
b411b363
PR
797 }
798
799 if (!get_ldev(mdev))
73a01a18 800 return 0; /* no disk, no metadata, no bitmap to set bits in */
b411b363
PR
801
802 nr_sectors = drbd_get_capacity(mdev->this_bdev);
803 esector = sector + (size >> 9) - 1;
804
841ce241 805 if (!expect(sector < nr_sectors))
b411b363 806 goto out;
841ce241
AG
807 if (!expect(esector < nr_sectors))
808 esector = nr_sectors - 1;
b411b363 809
b411b363
PR
810 /* we set it out of sync,
811 * we do not need to round anything here */
812 sbnr = BM_SECT_TO_BIT(sector);
813 ebnr = BM_SECT_TO_BIT(esector);
814
b411b363
PR
815 /* ok, (capacity & 7) != 0 sometimes, but who cares...
816 * we count rs_{total,left} in bits, not sectors. */
817 spin_lock_irqsave(&mdev->al_lock, flags);
818 count = drbd_bm_set_bits(mdev, sbnr, ebnr);
819
820 enr = BM_SECT_TO_EXT(sector);
821 e = lc_find(mdev->resync, enr);
822 if (e)
823 lc_entry(e, struct bm_extent, lce)->rs_left += count;
824 spin_unlock_irqrestore(&mdev->al_lock, flags);
825
826out:
827 put_ldev(mdev);
73a01a18
PR
828
829 return count;
b411b363
PR
830}
831
832static
833struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr)
834{
835 struct lc_element *e;
836 struct bm_extent *bm_ext;
837 int wakeup = 0;
838 unsigned long rs_flags;
839
840 spin_lock_irq(&mdev->al_lock);
841 if (mdev->resync_locked > mdev->resync->nr_elements/2) {
842 spin_unlock_irq(&mdev->al_lock);
843 return NULL;
844 }
845 e = lc_get(mdev->resync, enr);
846 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
847 if (bm_ext) {
848 if (bm_ext->lce.lc_number != enr) {
849 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
850 bm_ext->rs_failed = 0;
46a15bc3 851 lc_committed(mdev->resync);
b411b363
PR
852 wakeup = 1;
853 }
854 if (bm_ext->lce.refcnt == 1)
855 mdev->resync_locked++;
856 set_bit(BME_NO_WRITES, &bm_ext->flags);
857 }
858 rs_flags = mdev->resync->flags;
859 spin_unlock_irq(&mdev->al_lock);
860 if (wakeup)
861 wake_up(&mdev->al_wait);
862
863 if (!bm_ext) {
864 if (rs_flags & LC_STARVING)
865 dev_warn(DEV, "Have to wait for element"
866 " (resync LRU too small?)\n");
46a15bc3 867 BUG_ON(rs_flags & LC_LOCKED);
b411b363
PR
868 }
869
870 return bm_ext;
871}
872
873static int _is_in_al(struct drbd_conf *mdev, unsigned int enr)
874{
46a15bc3 875 int rv;
b411b363
PR
876
877 spin_lock_irq(&mdev->al_lock);
46a15bc3 878 rv = lc_is_used(mdev->act_log, enr);
b411b363
PR
879 spin_unlock_irq(&mdev->al_lock);
880
b411b363
PR
881 return rv;
882}
883
884/**
885 * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED
886 * @mdev: DRBD device.
887 * @sector: The sector number.
888 *
80a40e43 889 * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted.
b411b363
PR
890 */
891int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
892{
893 unsigned int enr = BM_SECT_TO_EXT(sector);
894 struct bm_extent *bm_ext;
895 int i, sig;
f91ab628
PR
896 int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait.
897 200 times -> 20 seconds. */
b411b363 898
f91ab628 899retry:
b411b363
PR
900 sig = wait_event_interruptible(mdev->al_wait,
901 (bm_ext = _bme_get(mdev, enr)));
902 if (sig)
80a40e43 903 return -EINTR;
b411b363
PR
904
905 if (test_bit(BME_LOCKED, &bm_ext->flags))
80a40e43 906 return 0;
b411b363
PR
907
908 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
909 sig = wait_event_interruptible(mdev->al_wait,
f91ab628 910 !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) ||
c507f46f 911 test_bit(BME_PRIORITY, &bm_ext->flags));
f91ab628
PR
912
913 if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) {
b411b363
PR
914 spin_lock_irq(&mdev->al_lock);
915 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
f91ab628 916 bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */
b411b363
PR
917 mdev->resync_locked--;
918 wake_up(&mdev->al_wait);
919 }
920 spin_unlock_irq(&mdev->al_lock);
f91ab628
PR
921 if (sig)
922 return -EINTR;
923 if (schedule_timeout_interruptible(HZ/10))
924 return -EINTR;
c507f46f
PR
925 if (sa && --sa == 0)
926 dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec."
927 "Resync stalled?\n");
f91ab628 928 goto retry;
b411b363
PR
929 }
930 }
b411b363 931 set_bit(BME_LOCKED, &bm_ext->flags);
80a40e43 932 return 0;
b411b363
PR
933}
934
935/**
936 * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep
937 * @mdev: DRBD device.
938 * @sector: The sector number.
939 *
940 * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then
941 * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN
942 * if there is still application IO going on in this area.
943 */
944int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector)
945{
946 unsigned int enr = BM_SECT_TO_EXT(sector);
947 const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT;
948 struct lc_element *e;
949 struct bm_extent *bm_ext;
950 int i;
951
b411b363
PR
952 spin_lock_irq(&mdev->al_lock);
953 if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) {
954 /* in case you have very heavy scattered io, it may
955 * stall the syncer undefined if we give up the ref count
956 * when we try again and requeue.
957 *
958 * if we don't give up the refcount, but the next time
959 * we are scheduled this extent has been "synced" by new
960 * application writes, we'd miss the lc_put on the
961 * extent we keep the refcount on.
962 * so we remembered which extent we had to try again, and
963 * if the next requested one is something else, we do
964 * the lc_put here...
965 * we also have to wake_up
966 */
b411b363
PR
967 e = lc_find(mdev->resync, mdev->resync_wenr);
968 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
969 if (bm_ext) {
970 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
971 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
972 clear_bit(BME_NO_WRITES, &bm_ext->flags);
973 mdev->resync_wenr = LC_FREE;
974 if (lc_put(mdev->resync, &bm_ext->lce) == 0)
975 mdev->resync_locked--;
976 wake_up(&mdev->al_wait);
977 } else {
978 dev_alert(DEV, "LOGIC BUG\n");
979 }
980 }
981 /* TRY. */
982 e = lc_try_get(mdev->resync, enr);
983 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
984 if (bm_ext) {
985 if (test_bit(BME_LOCKED, &bm_ext->flags))
986 goto proceed;
987 if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) {
988 mdev->resync_locked++;
989 } else {
990 /* we did set the BME_NO_WRITES,
991 * but then could not set BME_LOCKED,
992 * so we tried again.
993 * drop the extra reference. */
b411b363
PR
994 bm_ext->lce.refcnt--;
995 D_ASSERT(bm_ext->lce.refcnt > 0);
996 }
997 goto check_al;
998 } else {
999 /* do we rather want to try later? */
6a0afdf5 1000 if (mdev->resync_locked > mdev->resync->nr_elements-3)
b411b363 1001 goto try_again;
b411b363
PR
1002 /* Do or do not. There is no try. -- Yoda */
1003 e = lc_get(mdev->resync, enr);
1004 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1005 if (!bm_ext) {
1006 const unsigned long rs_flags = mdev->resync->flags;
1007 if (rs_flags & LC_STARVING)
1008 dev_warn(DEV, "Have to wait for element"
1009 " (resync LRU too small?)\n");
46a15bc3 1010 BUG_ON(rs_flags & LC_LOCKED);
b411b363
PR
1011 goto try_again;
1012 }
1013 if (bm_ext->lce.lc_number != enr) {
1014 bm_ext->rs_left = drbd_bm_e_weight(mdev, enr);
1015 bm_ext->rs_failed = 0;
46a15bc3 1016 lc_committed(mdev->resync);
b411b363
PR
1017 wake_up(&mdev->al_wait);
1018 D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0);
1019 }
1020 set_bit(BME_NO_WRITES, &bm_ext->flags);
1021 D_ASSERT(bm_ext->lce.refcnt == 1);
1022 mdev->resync_locked++;
1023 goto check_al;
1024 }
1025check_al:
b411b363 1026 for (i = 0; i < AL_EXT_PER_BM_SECT; i++) {
b411b363
PR
1027 if (lc_is_used(mdev->act_log, al_enr+i))
1028 goto try_again;
1029 }
1030 set_bit(BME_LOCKED, &bm_ext->flags);
1031proceed:
1032 mdev->resync_wenr = LC_FREE;
1033 spin_unlock_irq(&mdev->al_lock);
1034 return 0;
1035
1036try_again:
b411b363
PR
1037 if (bm_ext)
1038 mdev->resync_wenr = enr;
1039 spin_unlock_irq(&mdev->al_lock);
1040 return -EAGAIN;
1041}
1042
1043void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector)
1044{
1045 unsigned int enr = BM_SECT_TO_EXT(sector);
1046 struct lc_element *e;
1047 struct bm_extent *bm_ext;
1048 unsigned long flags;
1049
b411b363
PR
1050 spin_lock_irqsave(&mdev->al_lock, flags);
1051 e = lc_find(mdev->resync, enr);
1052 bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL;
1053 if (!bm_ext) {
1054 spin_unlock_irqrestore(&mdev->al_lock, flags);
1055 if (__ratelimit(&drbd_ratelimit_state))
1056 dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n");
1057 return;
1058 }
1059
1060 if (bm_ext->lce.refcnt == 0) {
1061 spin_unlock_irqrestore(&mdev->al_lock, flags);
1062 dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, "
1063 "but refcnt is 0!?\n",
1064 (unsigned long long)sector, enr);
1065 return;
1066 }
1067
1068 if (lc_put(mdev->resync, &bm_ext->lce) == 0) {
e3555d85 1069 bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */
b411b363
PR
1070 mdev->resync_locked--;
1071 wake_up(&mdev->al_wait);
1072 }
1073
1074 spin_unlock_irqrestore(&mdev->al_lock, flags);
1075}
1076
1077/**
1078 * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED)
1079 * @mdev: DRBD device.
1080 */
1081void drbd_rs_cancel_all(struct drbd_conf *mdev)
1082{
b411b363
PR
1083 spin_lock_irq(&mdev->al_lock);
1084
1085 if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */
1086 lc_reset(mdev->resync);
1087 put_ldev(mdev);
1088 }
1089 mdev->resync_locked = 0;
1090 mdev->resync_wenr = LC_FREE;
1091 spin_unlock_irq(&mdev->al_lock);
1092 wake_up(&mdev->al_wait);
1093}
1094
1095/**
1096 * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU
1097 * @mdev: DRBD device.
1098 *
1099 * Returns 0 upon success, -EAGAIN if at least one reference count was
1100 * not zero.
1101 */
1102int drbd_rs_del_all(struct drbd_conf *mdev)
1103{
1104 struct lc_element *e;
1105 struct bm_extent *bm_ext;
1106 int i;
1107
b411b363
PR
1108 spin_lock_irq(&mdev->al_lock);
1109
1110 if (get_ldev_if_state(mdev, D_FAILED)) {
1111 /* ok, ->resync is there. */
1112 for (i = 0; i < mdev->resync->nr_elements; i++) {
1113 e = lc_element_by_index(mdev->resync, i);
b2b163dd 1114 bm_ext = lc_entry(e, struct bm_extent, lce);
b411b363
PR
1115 if (bm_ext->lce.lc_number == LC_FREE)
1116 continue;
1117 if (bm_ext->lce.lc_number == mdev->resync_wenr) {
1118 dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently"
1119 " got 'synced' by application io\n",
1120 mdev->resync_wenr);
1121 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1122 D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags));
1123 clear_bit(BME_NO_WRITES, &bm_ext->flags);
1124 mdev->resync_wenr = LC_FREE;
1125 lc_put(mdev->resync, &bm_ext->lce);
1126 }
1127 if (bm_ext->lce.refcnt != 0) {
1128 dev_info(DEV, "Retrying drbd_rs_del_all() later. "
1129 "refcnt=%d\n", bm_ext->lce.refcnt);
1130 put_ldev(mdev);
1131 spin_unlock_irq(&mdev->al_lock);
1132 return -EAGAIN;
1133 }
1134 D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags));
1135 D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags));
1136 lc_del(mdev->resync, &bm_ext->lce);
1137 }
1138 D_ASSERT(mdev->resync->used == 0);
1139 put_ldev(mdev);
1140 }
1141 spin_unlock_irq(&mdev->al_lock);
a6a7d4f0 1142 wake_up(&mdev->al_wait);
b411b363
PR
1143
1144 return 0;
1145}
1146
1147/**
1148 * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks
1149 * @mdev: DRBD device.
1150 * @sector: The sector number.
1151 * @size: Size of failed IO operation, in byte.
1152 */
1153void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size)
1154{
1155 /* Is called from worker and receiver context _only_ */
1156 unsigned long sbnr, ebnr, lbnr;
1157 unsigned long count;
1158 sector_t esector, nr_sectors;
1159 int wake_up = 0;
1160
c670a398 1161 if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
b411b363
PR
1162 dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n",
1163 (unsigned long long)sector, size);
1164 return;
1165 }
1166 nr_sectors = drbd_get_capacity(mdev->this_bdev);
1167 esector = sector + (size >> 9) - 1;
1168
841ce241
AG
1169 if (!expect(sector < nr_sectors))
1170 return;
1171 if (!expect(esector < nr_sectors))
1172 esector = nr_sectors - 1;
b411b363
PR
1173
1174 lbnr = BM_SECT_TO_BIT(nr_sectors-1);
1175
1176 /*
1177 * round up start sector, round down end sector. we make sure we only
1178 * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */
1179 if (unlikely(esector < BM_SECT_PER_BIT-1))
1180 return;
1181 if (unlikely(esector == (nr_sectors-1)))
1182 ebnr = lbnr;
1183 else
1184 ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1));
1185 sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1);
1186
1187 if (sbnr > ebnr)
1188 return;
1189
1190 /*
1191 * ok, (capacity & 7) != 0 sometimes, but who cares...
1192 * we count rs_{total,left} in bits, not sectors.
1193 */
1194 spin_lock_irq(&mdev->al_lock);
1195 count = drbd_bm_count_bits(mdev, sbnr, ebnr);
1196 if (count) {
1197 mdev->rs_failed += count;
1198
1199 if (get_ldev(mdev)) {
81e84650 1200 drbd_try_clear_on_disk_bm(mdev, sector, count, false);
b411b363
PR
1201 put_ldev(mdev);
1202 }
1203
1204 /* just wake_up unconditional now, various lc_chaged(),
1205 * lc_put() in drbd_try_clear_on_disk_bm(). */
1206 wake_up = 1;
1207 }
1208 spin_unlock_irq(&mdev->al_lock);
1209 if (wake_up)
1210 wake_up(&mdev->al_wait);
1211}
This page took 0.191035 seconds and 5 git commands to generate.