Commit | Line | Data |
---|---|---|
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> | |
27 | #include <linux/drbd.h> | |
28 | #include "drbd_int.h" | |
b411b363 PR |
29 | #include "drbd_wrappers.h" |
30 | ||
24c4830c | 31 | /* We maintain a trivial checksum in our on disk activity log. |
b411b363 | 32 | * With that we can ensure correct operation even when the storage |
25985edc | 33 | * device might do a partial (last) sector write while losing power. |
b411b363 PR |
34 | */ |
35 | struct __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 | ||
44 | struct update_odbm_work { | |
45 | struct drbd_work w; | |
46 | unsigned int enr; | |
47 | }; | |
48 | ||
49 | struct 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 | ||
58 | struct drbd_atodb_wait { | |
59 | atomic_t count; | |
60 | struct completion io_done; | |
61 | struct drbd_conf *mdev; | |
62 | int error; | |
63 | }; | |
64 | ||
65 | ||
66 | int w_al_write_transaction(struct drbd_conf *, struct drbd_work *, int); | |
67 | ||
b411b363 PR |
68 | static 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 | ||
a8a4e51e | 81 | if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags)) |
86e1e98e | 82 | rw |= REQ_FUA | REQ_FLUSH; |
721a9602 | 83 | rw |= REQ_SYNC; |
b411b363 | 84 | |
b411b363 PR |
85 | 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 | ||
0cf9d27e | 95 | if (drbd_insert_fault(mdev, (rw & WRITE) ? DRBD_FAULT_MD_WR : DRBD_FAULT_MD_RD)) |
b411b363 PR |
96 | 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 | ||
b411b363 PR |
102 | out: |
103 | bio_put(bio); | |
104 | return ok; | |
105 | } | |
106 | ||
107 | int 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 | ||
174 | static 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; | |
f91ab628 | 179 | int wake; |
b411b363 PR |
180 | |
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)) { | |
f91ab628 | 186 | wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags); |
b411b363 | 187 | spin_unlock_irq(&mdev->al_lock); |
f91ab628 PR |
188 | if (wake) |
189 | wake_up(&mdev->al_wait); | |
b411b363 PR |
190 | 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 | ||
209 | void 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 | ||
b411b363 PR |
217 | 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 | ||
243 | void 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 | ||
b411b363 PR |
249 | 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 | ||
19f843aa LE |
265 | #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 | ||
274 | static 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 | ||
283 | static 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 | ||
b411b363 PR |
292 | int |
293 | w_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)) { | |
6719fb03 LE |
306 | dev_err(DEV, |
307 | "disk is %s, cannot start al transaction (-%d +%d)\n", | |
308 | drbd_disk_str(mdev->state.disk), evicted, new_enr); | |
b411b363 PR |
309 | 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, | |
6719fb03 LE |
315 | * 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. */ | |
b411b363 | 318 | if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE) |
19f843aa | 319 | drbd_bm_write_page(mdev, al_extent_to_bm_page(evicted)); |
b411b363 | 320 | |
6719fb03 LE |
321 | /* 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, ... */ | |
b411b363 PR |
332 | 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)) | |
81e84650 | 368 | drbd_chk_io_error(mdev, 1, true); |
b411b363 PR |
369 | |
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 | */ | |
394 | static 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 | ||
e7fad8af | 410 | rv = (b->magic == cpu_to_be32(DRBD_MAGIC)); |
b411b363 PR |
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 | */ | |
426 | int 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); | |
841ce241 AG |
494 | if (!expect(rv != 0)) |
495 | goto cancel; | |
b411b363 PR |
496 | if (rv == -1) { |
497 | mutex_unlock(&mdev->md_io_mutex); | |
498 | return 0; | |
499 | } | |
500 | ||
501 | trn = be32_to_cpu(buffer->tr_number); | |
502 | ||
503 | spin_lock_irq(&mdev->al_lock); | |
504 | ||
505 | /* This loop runs backwards because in the cyclic | |
506 | elements there might be an old version of the | |
507 | updated element (in slot 0). So the element in slot 0 | |
508 | can overwrite old versions. */ | |
509 | for (j = AL_EXTENTS_PT; j >= 0; j--) { | |
510 | pos = be32_to_cpu(buffer->updates[j].pos); | |
511 | extent_nr = be32_to_cpu(buffer->updates[j].extent); | |
512 | ||
513 | if (extent_nr == LC_FREE) | |
514 | continue; | |
515 | ||
516 | lc_set(mdev->act_log, extent_nr, pos); | |
517 | active_extents++; | |
518 | } | |
519 | spin_unlock_irq(&mdev->al_lock); | |
520 | ||
521 | transactions++; | |
522 | ||
523 | cancel: | |
524 | if (i == to) | |
525 | break; | |
526 | i++; | |
527 | if (i > mx) | |
528 | i = 0; | |
529 | } | |
530 | ||
531 | mdev->al_tr_number = to_tnr+1; | |
532 | mdev->al_tr_pos = to; | |
533 | if (++mdev->al_tr_pos > | |
534 | div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT)) | |
535 | mdev->al_tr_pos = 0; | |
536 | ||
537 | /* ok, we are done with it */ | |
538 | mutex_unlock(&mdev->md_io_mutex); | |
539 | ||
540 | dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n", | |
541 | transactions, active_extents); | |
542 | ||
543 | return 1; | |
544 | } | |
545 | ||
b411b363 PR |
546 | /** |
547 | * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents | |
548 | * @mdev: DRBD device. | |
549 | */ | |
550 | void drbd_al_apply_to_bm(struct drbd_conf *mdev) | |
551 | { | |
552 | unsigned int enr; | |
553 | unsigned long add = 0; | |
554 | char ppb[10]; | |
6719fb03 | 555 | int i, tmp; |
b411b363 PR |
556 | |
557 | wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); | |
558 | ||
559 | for (i = 0; i < mdev->act_log->nr_elements; i++) { | |
560 | enr = lc_element_by_index(mdev->act_log, i)->lc_number; | |
561 | if (enr == LC_FREE) | |
562 | continue; | |
6719fb03 LE |
563 | tmp = drbd_bm_ALe_set_all(mdev, enr); |
564 | dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr); | |
565 | add += tmp; | |
b411b363 PR |
566 | } |
567 | ||
568 | lc_unlock(mdev->act_log); | |
569 | wake_up(&mdev->al_wait); | |
570 | ||
571 | dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n", | |
572 | ppsize(ppb, Bit2KB(add))); | |
573 | } | |
574 | ||
575 | static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext) | |
576 | { | |
577 | int rv; | |
578 | ||
579 | spin_lock_irq(&mdev->al_lock); | |
580 | rv = (al_ext->refcnt == 0); | |
581 | if (likely(rv)) | |
582 | lc_del(mdev->act_log, al_ext); | |
583 | spin_unlock_irq(&mdev->al_lock); | |
584 | ||
585 | return rv; | |
586 | } | |
587 | ||
588 | /** | |
589 | * drbd_al_shrink() - Removes all active extents form the activity log | |
590 | * @mdev: DRBD device. | |
591 | * | |
592 | * Removes all active extents form the activity log, waiting until | |
593 | * the reference count of each entry dropped to 0 first, of course. | |
594 | * | |
595 | * You need to lock mdev->act_log with lc_try_lock() / lc_unlock() | |
596 | */ | |
597 | void drbd_al_shrink(struct drbd_conf *mdev) | |
598 | { | |
599 | struct lc_element *al_ext; | |
600 | int i; | |
601 | ||
602 | D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags)); | |
603 | ||
604 | for (i = 0; i < mdev->act_log->nr_elements; i++) { | |
605 | al_ext = lc_element_by_index(mdev->act_log, i); | |
606 | if (al_ext->lc_number == LC_FREE) | |
607 | continue; | |
608 | wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext)); | |
609 | } | |
610 | ||
611 | wake_up(&mdev->al_wait); | |
612 | } | |
613 | ||
614 | static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused) | |
615 | { | |
616 | struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w); | |
617 | ||
618 | if (!get_ldev(mdev)) { | |
619 | if (__ratelimit(&drbd_ratelimit_state)) | |
620 | dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n"); | |
621 | kfree(udw); | |
622 | return 1; | |
623 | } | |
624 | ||
19f843aa | 625 | drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr)); |
b411b363 PR |
626 | put_ldev(mdev); |
627 | ||
628 | kfree(udw); | |
629 | ||
630 | if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) { | |
631 | switch (mdev->state.conn) { | |
632 | case C_SYNC_SOURCE: case C_SYNC_TARGET: | |
633 | case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T: | |
634 | drbd_resync_finished(mdev); | |
635 | default: | |
636 | /* nothing to do */ | |
637 | break; | |
638 | } | |
639 | } | |
640 | drbd_bcast_sync_progress(mdev); | |
641 | ||
642 | return 1; | |
643 | } | |
644 | ||
645 | ||
646 | /* ATTENTION. The AL's extents are 4MB each, while the extents in the | |
647 | * resync LRU-cache are 16MB each. | |
648 | * The caller of this function has to hold an get_ldev() reference. | |
649 | * | |
650 | * TODO will be obsoleted once we have a caching lru of the on disk bitmap | |
651 | */ | |
652 | static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector, | |
653 | int count, int success) | |
654 | { | |
655 | struct lc_element *e; | |
656 | struct update_odbm_work *udw; | |
657 | ||
658 | unsigned int enr; | |
659 | ||
660 | D_ASSERT(atomic_read(&mdev->local_cnt)); | |
661 | ||
662 | /* I simply assume that a sector/size pair never crosses | |
663 | * a 16 MB extent border. (Currently this is true...) */ | |
664 | enr = BM_SECT_TO_EXT(sector); | |
665 | ||
666 | e = lc_get(mdev->resync, enr); | |
667 | if (e) { | |
668 | struct bm_extent *ext = lc_entry(e, struct bm_extent, lce); | |
669 | if (ext->lce.lc_number == enr) { | |
670 | if (success) | |
671 | ext->rs_left -= count; | |
672 | else | |
673 | ext->rs_failed += count; | |
674 | if (ext->rs_left < ext->rs_failed) { | |
675 | dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d " | |
676 | "rs_failed=%d count=%d\n", | |
677 | (unsigned long long)sector, | |
678 | ext->lce.lc_number, ext->rs_left, | |
679 | ext->rs_failed, count); | |
680 | dump_stack(); | |
681 | ||
682 | lc_put(mdev->resync, &ext->lce); | |
683 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); | |
684 | return; | |
685 | } | |
686 | } else { | |
687 | /* Normally this element should be in the cache, | |
688 | * since drbd_rs_begin_io() pulled it already in. | |
689 | * | |
690 | * But maybe an application write finished, and we set | |
691 | * something outside the resync lru_cache in sync. | |
692 | */ | |
693 | int rs_left = drbd_bm_e_weight(mdev, enr); | |
694 | if (ext->flags != 0) { | |
695 | dev_warn(DEV, "changing resync lce: %d[%u;%02lx]" | |
696 | " -> %d[%u;00]\n", | |
697 | ext->lce.lc_number, ext->rs_left, | |
698 | ext->flags, enr, rs_left); | |
699 | ext->flags = 0; | |
700 | } | |
701 | if (ext->rs_failed) { | |
702 | dev_warn(DEV, "Kicking resync_lru element enr=%u " | |
703 | "out with rs_failed=%d\n", | |
704 | ext->lce.lc_number, ext->rs_failed); | |
b411b363 PR |
705 | } |
706 | ext->rs_left = rs_left; | |
707 | ext->rs_failed = success ? 0 : count; | |
708 | lc_changed(mdev->resync, &ext->lce); | |
709 | } | |
710 | lc_put(mdev->resync, &ext->lce); | |
711 | /* no race, we are within the al_lock! */ | |
712 | ||
713 | if (ext->rs_left == ext->rs_failed) { | |
714 | ext->rs_failed = 0; | |
715 | ||
716 | udw = kmalloc(sizeof(*udw), GFP_ATOMIC); | |
717 | if (udw) { | |
718 | udw->enr = ext->lce.lc_number; | |
719 | udw->w.cb = w_update_odbm; | |
720 | drbd_queue_work_front(&mdev->data.work, &udw->w); | |
721 | } else { | |
722 | dev_warn(DEV, "Could not kmalloc an udw\n"); | |
b411b363 PR |
723 | } |
724 | } | |
725 | } else { | |
726 | dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n", | |
727 | mdev->resync_locked, | |
728 | mdev->resync->nr_elements, | |
729 | mdev->resync->flags); | |
730 | } | |
731 | } | |
732 | ||
c6ea14df LE |
733 | void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go) |
734 | { | |
735 | unsigned long now = jiffies; | |
736 | unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark]; | |
737 | int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS; | |
738 | if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) { | |
739 | if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go && | |
740 | mdev->state.conn != C_PAUSED_SYNC_T && | |
741 | mdev->state.conn != C_PAUSED_SYNC_S) { | |
742 | mdev->rs_mark_time[next] = now; | |
743 | mdev->rs_mark_left[next] = still_to_go; | |
744 | mdev->rs_last_mark = next; | |
745 | } | |
746 | } | |
747 | } | |
748 | ||
b411b363 PR |
749 | /* clear the bit corresponding to the piece of storage in question: |
750 | * size byte of data starting from sector. Only clear a bits of the affected | |
751 | * one ore more _aligned_ BM_BLOCK_SIZE blocks. | |
752 | * | |
753 | * called by worker on C_SYNC_TARGET and receiver on SyncSource. | |
754 | * | |
755 | */ | |
756 | void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size, | |
757 | const char *file, const unsigned int line) | |
758 | { | |
759 | /* Is called from worker and receiver context _only_ */ | |
760 | unsigned long sbnr, ebnr, lbnr; | |
761 | unsigned long count = 0; | |
762 | sector_t esector, nr_sectors; | |
763 | int wake_up = 0; | |
764 | unsigned long flags; | |
765 | ||
1816a2b4 | 766 | if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
767 | dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n", |
768 | (unsigned long long)sector, size); | |
769 | return; | |
770 | } | |
771 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
772 | esector = sector + (size >> 9) - 1; | |
773 | ||
841ce241 AG |
774 | if (!expect(sector < nr_sectors)) |
775 | return; | |
776 | if (!expect(esector < nr_sectors)) | |
777 | esector = nr_sectors - 1; | |
b411b363 PR |
778 | |
779 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
780 | ||
781 | /* we clear it (in sync). | |
782 | * round up start sector, round down end sector. we make sure we only | |
783 | * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */ | |
784 | if (unlikely(esector < BM_SECT_PER_BIT-1)) | |
785 | return; | |
786 | if (unlikely(esector == (nr_sectors-1))) | |
787 | ebnr = lbnr; | |
788 | else | |
789 | ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); | |
790 | sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); | |
791 | ||
b411b363 PR |
792 | if (sbnr > ebnr) |
793 | return; | |
794 | ||
795 | /* | |
796 | * ok, (capacity & 7) != 0 sometimes, but who cares... | |
797 | * we count rs_{total,left} in bits, not sectors. | |
798 | */ | |
b411b363 | 799 | count = drbd_bm_clear_bits(mdev, sbnr, ebnr); |
1d7734a0 | 800 | if (count && get_ldev(mdev)) { |
c6ea14df | 801 | drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev)); |
1d7734a0 | 802 | spin_lock_irqsave(&mdev->al_lock, flags); |
81e84650 | 803 | drbd_try_clear_on_disk_bm(mdev, sector, count, true); |
1d7734a0 LE |
804 | spin_unlock_irqrestore(&mdev->al_lock, flags); |
805 | ||
b411b363 PR |
806 | /* just wake_up unconditional now, various lc_chaged(), |
807 | * lc_put() in drbd_try_clear_on_disk_bm(). */ | |
808 | wake_up = 1; | |
1d7734a0 | 809 | put_ldev(mdev); |
b411b363 | 810 | } |
b411b363 PR |
811 | if (wake_up) |
812 | wake_up(&mdev->al_wait); | |
813 | } | |
814 | ||
815 | /* | |
816 | * this is intended to set one request worth of data out of sync. | |
817 | * affects at least 1 bit, | |
1816a2b4 | 818 | * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits. |
b411b363 PR |
819 | * |
820 | * called by tl_clear and drbd_send_dblock (==drbd_make_request). | |
821 | * so this can be _any_ process. | |
822 | */ | |
73a01a18 | 823 | int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size, |
b411b363 PR |
824 | const char *file, const unsigned int line) |
825 | { | |
826 | unsigned long sbnr, ebnr, lbnr, flags; | |
827 | sector_t esector, nr_sectors; | |
73a01a18 | 828 | unsigned int enr, count = 0; |
b411b363 PR |
829 | struct lc_element *e; |
830 | ||
1816a2b4 | 831 | if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
832 | dev_err(DEV, "sector: %llus, size: %d\n", |
833 | (unsigned long long)sector, size); | |
73a01a18 | 834 | return 0; |
b411b363 PR |
835 | } |
836 | ||
837 | if (!get_ldev(mdev)) | |
73a01a18 | 838 | return 0; /* no disk, no metadata, no bitmap to set bits in */ |
b411b363 PR |
839 | |
840 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
841 | esector = sector + (size >> 9) - 1; | |
842 | ||
841ce241 | 843 | if (!expect(sector < nr_sectors)) |
b411b363 | 844 | goto out; |
841ce241 AG |
845 | if (!expect(esector < nr_sectors)) |
846 | esector = nr_sectors - 1; | |
b411b363 PR |
847 | |
848 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
849 | ||
850 | /* we set it out of sync, | |
851 | * we do not need to round anything here */ | |
852 | sbnr = BM_SECT_TO_BIT(sector); | |
853 | ebnr = BM_SECT_TO_BIT(esector); | |
854 | ||
b411b363 PR |
855 | /* ok, (capacity & 7) != 0 sometimes, but who cares... |
856 | * we count rs_{total,left} in bits, not sectors. */ | |
857 | spin_lock_irqsave(&mdev->al_lock, flags); | |
858 | count = drbd_bm_set_bits(mdev, sbnr, ebnr); | |
859 | ||
860 | enr = BM_SECT_TO_EXT(sector); | |
861 | e = lc_find(mdev->resync, enr); | |
862 | if (e) | |
863 | lc_entry(e, struct bm_extent, lce)->rs_left += count; | |
864 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
865 | ||
866 | out: | |
867 | put_ldev(mdev); | |
73a01a18 PR |
868 | |
869 | return count; | |
b411b363 PR |
870 | } |
871 | ||
872 | static | |
873 | struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr) | |
874 | { | |
875 | struct lc_element *e; | |
876 | struct bm_extent *bm_ext; | |
877 | int wakeup = 0; | |
878 | unsigned long rs_flags; | |
879 | ||
880 | spin_lock_irq(&mdev->al_lock); | |
881 | if (mdev->resync_locked > mdev->resync->nr_elements/2) { | |
882 | spin_unlock_irq(&mdev->al_lock); | |
883 | return NULL; | |
884 | } | |
885 | e = lc_get(mdev->resync, enr); | |
886 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
887 | if (bm_ext) { | |
888 | if (bm_ext->lce.lc_number != enr) { | |
889 | bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); | |
890 | bm_ext->rs_failed = 0; | |
891 | lc_changed(mdev->resync, &bm_ext->lce); | |
892 | wakeup = 1; | |
893 | } | |
894 | if (bm_ext->lce.refcnt == 1) | |
895 | mdev->resync_locked++; | |
896 | set_bit(BME_NO_WRITES, &bm_ext->flags); | |
897 | } | |
898 | rs_flags = mdev->resync->flags; | |
899 | spin_unlock_irq(&mdev->al_lock); | |
900 | if (wakeup) | |
901 | wake_up(&mdev->al_wait); | |
902 | ||
903 | if (!bm_ext) { | |
904 | if (rs_flags & LC_STARVING) | |
905 | dev_warn(DEV, "Have to wait for element" | |
906 | " (resync LRU too small?)\n"); | |
907 | BUG_ON(rs_flags & LC_DIRTY); | |
908 | } | |
909 | ||
910 | return bm_ext; | |
911 | } | |
912 | ||
913 | static int _is_in_al(struct drbd_conf *mdev, unsigned int enr) | |
914 | { | |
915 | struct lc_element *al_ext; | |
916 | int rv = 0; | |
917 | ||
918 | spin_lock_irq(&mdev->al_lock); | |
919 | if (unlikely(enr == mdev->act_log->new_number)) | |
920 | rv = 1; | |
921 | else { | |
922 | al_ext = lc_find(mdev->act_log, enr); | |
923 | if (al_ext) { | |
924 | if (al_ext->refcnt) | |
925 | rv = 1; | |
926 | } | |
927 | } | |
928 | spin_unlock_irq(&mdev->al_lock); | |
929 | ||
930 | /* | |
931 | if (unlikely(rv)) { | |
932 | dev_info(DEV, "Delaying sync read until app's write is done\n"); | |
933 | } | |
934 | */ | |
935 | return rv; | |
936 | } | |
937 | ||
938 | /** | |
939 | * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED | |
940 | * @mdev: DRBD device. | |
941 | * @sector: The sector number. | |
942 | * | |
80a40e43 | 943 | * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted. |
b411b363 PR |
944 | */ |
945 | int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector) | |
946 | { | |
947 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
948 | struct bm_extent *bm_ext; | |
949 | int i, sig; | |
f91ab628 PR |
950 | int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait. |
951 | 200 times -> 20 seconds. */ | |
b411b363 | 952 | |
f91ab628 | 953 | retry: |
b411b363 PR |
954 | sig = wait_event_interruptible(mdev->al_wait, |
955 | (bm_ext = _bme_get(mdev, enr))); | |
956 | if (sig) | |
80a40e43 | 957 | return -EINTR; |
b411b363 PR |
958 | |
959 | if (test_bit(BME_LOCKED, &bm_ext->flags)) | |
80a40e43 | 960 | return 0; |
b411b363 PR |
961 | |
962 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { | |
963 | sig = wait_event_interruptible(mdev->al_wait, | |
f91ab628 | 964 | !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) || |
c507f46f | 965 | test_bit(BME_PRIORITY, &bm_ext->flags)); |
f91ab628 PR |
966 | |
967 | if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) { | |
b411b363 PR |
968 | spin_lock_irq(&mdev->al_lock); |
969 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) { | |
f91ab628 | 970 | bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */ |
b411b363 PR |
971 | mdev->resync_locked--; |
972 | wake_up(&mdev->al_wait); | |
973 | } | |
974 | spin_unlock_irq(&mdev->al_lock); | |
f91ab628 PR |
975 | if (sig) |
976 | return -EINTR; | |
977 | if (schedule_timeout_interruptible(HZ/10)) | |
978 | return -EINTR; | |
c507f46f PR |
979 | if (sa && --sa == 0) |
980 | dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec." | |
981 | "Resync stalled?\n"); | |
f91ab628 | 982 | goto retry; |
b411b363 PR |
983 | } |
984 | } | |
b411b363 | 985 | set_bit(BME_LOCKED, &bm_ext->flags); |
80a40e43 | 986 | return 0; |
b411b363 PR |
987 | } |
988 | ||
989 | /** | |
990 | * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep | |
991 | * @mdev: DRBD device. | |
992 | * @sector: The sector number. | |
993 | * | |
994 | * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then | |
995 | * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN | |
996 | * if there is still application IO going on in this area. | |
997 | */ | |
998 | int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector) | |
999 | { | |
1000 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
1001 | const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT; | |
1002 | struct lc_element *e; | |
1003 | struct bm_extent *bm_ext; | |
1004 | int i; | |
1005 | ||
b411b363 PR |
1006 | spin_lock_irq(&mdev->al_lock); |
1007 | if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) { | |
1008 | /* in case you have very heavy scattered io, it may | |
1009 | * stall the syncer undefined if we give up the ref count | |
1010 | * when we try again and requeue. | |
1011 | * | |
1012 | * if we don't give up the refcount, but the next time | |
1013 | * we are scheduled this extent has been "synced" by new | |
1014 | * application writes, we'd miss the lc_put on the | |
1015 | * extent we keep the refcount on. | |
1016 | * so we remembered which extent we had to try again, and | |
1017 | * if the next requested one is something else, we do | |
1018 | * the lc_put here... | |
1019 | * we also have to wake_up | |
1020 | */ | |
b411b363 PR |
1021 | e = lc_find(mdev->resync, mdev->resync_wenr); |
1022 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1023 | if (bm_ext) { | |
1024 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
1025 | D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1026 | clear_bit(BME_NO_WRITES, &bm_ext->flags); | |
1027 | mdev->resync_wenr = LC_FREE; | |
1028 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) | |
1029 | mdev->resync_locked--; | |
1030 | wake_up(&mdev->al_wait); | |
1031 | } else { | |
1032 | dev_alert(DEV, "LOGIC BUG\n"); | |
1033 | } | |
1034 | } | |
1035 | /* TRY. */ | |
1036 | e = lc_try_get(mdev->resync, enr); | |
1037 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1038 | if (bm_ext) { | |
1039 | if (test_bit(BME_LOCKED, &bm_ext->flags)) | |
1040 | goto proceed; | |
1041 | if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) { | |
1042 | mdev->resync_locked++; | |
1043 | } else { | |
1044 | /* we did set the BME_NO_WRITES, | |
1045 | * but then could not set BME_LOCKED, | |
1046 | * so we tried again. | |
1047 | * drop the extra reference. */ | |
b411b363 PR |
1048 | bm_ext->lce.refcnt--; |
1049 | D_ASSERT(bm_ext->lce.refcnt > 0); | |
1050 | } | |
1051 | goto check_al; | |
1052 | } else { | |
1053 | /* do we rather want to try later? */ | |
6a0afdf5 | 1054 | if (mdev->resync_locked > mdev->resync->nr_elements-3) |
b411b363 | 1055 | goto try_again; |
b411b363 PR |
1056 | /* Do or do not. There is no try. -- Yoda */ |
1057 | e = lc_get(mdev->resync, enr); | |
1058 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1059 | if (!bm_ext) { | |
1060 | const unsigned long rs_flags = mdev->resync->flags; | |
1061 | if (rs_flags & LC_STARVING) | |
1062 | dev_warn(DEV, "Have to wait for element" | |
1063 | " (resync LRU too small?)\n"); | |
1064 | BUG_ON(rs_flags & LC_DIRTY); | |
1065 | goto try_again; | |
1066 | } | |
1067 | if (bm_ext->lce.lc_number != enr) { | |
1068 | bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); | |
1069 | bm_ext->rs_failed = 0; | |
1070 | lc_changed(mdev->resync, &bm_ext->lce); | |
1071 | wake_up(&mdev->al_wait); | |
1072 | D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0); | |
1073 | } | |
1074 | set_bit(BME_NO_WRITES, &bm_ext->flags); | |
1075 | D_ASSERT(bm_ext->lce.refcnt == 1); | |
1076 | mdev->resync_locked++; | |
1077 | goto check_al; | |
1078 | } | |
1079 | check_al: | |
b411b363 PR |
1080 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { |
1081 | if (unlikely(al_enr+i == mdev->act_log->new_number)) | |
1082 | goto try_again; | |
1083 | if (lc_is_used(mdev->act_log, al_enr+i)) | |
1084 | goto try_again; | |
1085 | } | |
1086 | set_bit(BME_LOCKED, &bm_ext->flags); | |
1087 | proceed: | |
1088 | mdev->resync_wenr = LC_FREE; | |
1089 | spin_unlock_irq(&mdev->al_lock); | |
1090 | return 0; | |
1091 | ||
1092 | try_again: | |
b411b363 PR |
1093 | if (bm_ext) |
1094 | mdev->resync_wenr = enr; | |
1095 | spin_unlock_irq(&mdev->al_lock); | |
1096 | return -EAGAIN; | |
1097 | } | |
1098 | ||
1099 | void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector) | |
1100 | { | |
1101 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
1102 | struct lc_element *e; | |
1103 | struct bm_extent *bm_ext; | |
1104 | unsigned long flags; | |
1105 | ||
b411b363 PR |
1106 | spin_lock_irqsave(&mdev->al_lock, flags); |
1107 | e = lc_find(mdev->resync, enr); | |
1108 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1109 | if (!bm_ext) { | |
1110 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1111 | if (__ratelimit(&drbd_ratelimit_state)) | |
1112 | dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n"); | |
1113 | return; | |
1114 | } | |
1115 | ||
1116 | if (bm_ext->lce.refcnt == 0) { | |
1117 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1118 | dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, " | |
1119 | "but refcnt is 0!?\n", | |
1120 | (unsigned long long)sector, enr); | |
1121 | return; | |
1122 | } | |
1123 | ||
1124 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) { | |
e3555d85 | 1125 | bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */ |
b411b363 PR |
1126 | mdev->resync_locked--; |
1127 | wake_up(&mdev->al_wait); | |
1128 | } | |
1129 | ||
1130 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1131 | } | |
1132 | ||
1133 | /** | |
1134 | * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED) | |
1135 | * @mdev: DRBD device. | |
1136 | */ | |
1137 | void drbd_rs_cancel_all(struct drbd_conf *mdev) | |
1138 | { | |
b411b363 PR |
1139 | spin_lock_irq(&mdev->al_lock); |
1140 | ||
1141 | if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */ | |
1142 | lc_reset(mdev->resync); | |
1143 | put_ldev(mdev); | |
1144 | } | |
1145 | mdev->resync_locked = 0; | |
1146 | mdev->resync_wenr = LC_FREE; | |
1147 | spin_unlock_irq(&mdev->al_lock); | |
1148 | wake_up(&mdev->al_wait); | |
1149 | } | |
1150 | ||
1151 | /** | |
1152 | * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU | |
1153 | * @mdev: DRBD device. | |
1154 | * | |
1155 | * Returns 0 upon success, -EAGAIN if at least one reference count was | |
1156 | * not zero. | |
1157 | */ | |
1158 | int drbd_rs_del_all(struct drbd_conf *mdev) | |
1159 | { | |
1160 | struct lc_element *e; | |
1161 | struct bm_extent *bm_ext; | |
1162 | int i; | |
1163 | ||
b411b363 PR |
1164 | spin_lock_irq(&mdev->al_lock); |
1165 | ||
1166 | if (get_ldev_if_state(mdev, D_FAILED)) { | |
1167 | /* ok, ->resync is there. */ | |
1168 | for (i = 0; i < mdev->resync->nr_elements; i++) { | |
1169 | e = lc_element_by_index(mdev->resync, i); | |
b2b163dd | 1170 | bm_ext = lc_entry(e, struct bm_extent, lce); |
b411b363 PR |
1171 | if (bm_ext->lce.lc_number == LC_FREE) |
1172 | continue; | |
1173 | if (bm_ext->lce.lc_number == mdev->resync_wenr) { | |
1174 | dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently" | |
1175 | " got 'synced' by application io\n", | |
1176 | mdev->resync_wenr); | |
1177 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
1178 | D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1179 | clear_bit(BME_NO_WRITES, &bm_ext->flags); | |
1180 | mdev->resync_wenr = LC_FREE; | |
1181 | lc_put(mdev->resync, &bm_ext->lce); | |
1182 | } | |
1183 | if (bm_ext->lce.refcnt != 0) { | |
1184 | dev_info(DEV, "Retrying drbd_rs_del_all() later. " | |
1185 | "refcnt=%d\n", bm_ext->lce.refcnt); | |
1186 | put_ldev(mdev); | |
1187 | spin_unlock_irq(&mdev->al_lock); | |
1188 | return -EAGAIN; | |
1189 | } | |
1190 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
1191 | D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1192 | lc_del(mdev->resync, &bm_ext->lce); | |
1193 | } | |
1194 | D_ASSERT(mdev->resync->used == 0); | |
1195 | put_ldev(mdev); | |
1196 | } | |
1197 | spin_unlock_irq(&mdev->al_lock); | |
1198 | ||
1199 | return 0; | |
1200 | } | |
1201 | ||
1202 | /** | |
1203 | * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks | |
1204 | * @mdev: DRBD device. | |
1205 | * @sector: The sector number. | |
1206 | * @size: Size of failed IO operation, in byte. | |
1207 | */ | |
1208 | void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size) | |
1209 | { | |
1210 | /* Is called from worker and receiver context _only_ */ | |
1211 | unsigned long sbnr, ebnr, lbnr; | |
1212 | unsigned long count; | |
1213 | sector_t esector, nr_sectors; | |
1214 | int wake_up = 0; | |
1215 | ||
1816a2b4 | 1216 | if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
1217 | dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n", |
1218 | (unsigned long long)sector, size); | |
1219 | return; | |
1220 | } | |
1221 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
1222 | esector = sector + (size >> 9) - 1; | |
1223 | ||
841ce241 AG |
1224 | if (!expect(sector < nr_sectors)) |
1225 | return; | |
1226 | if (!expect(esector < nr_sectors)) | |
1227 | esector = nr_sectors - 1; | |
b411b363 PR |
1228 | |
1229 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
1230 | ||
1231 | /* | |
1232 | * round up start sector, round down end sector. we make sure we only | |
1233 | * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */ | |
1234 | if (unlikely(esector < BM_SECT_PER_BIT-1)) | |
1235 | return; | |
1236 | if (unlikely(esector == (nr_sectors-1))) | |
1237 | ebnr = lbnr; | |
1238 | else | |
1239 | ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); | |
1240 | sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); | |
1241 | ||
1242 | if (sbnr > ebnr) | |
1243 | return; | |
1244 | ||
1245 | /* | |
1246 | * ok, (capacity & 7) != 0 sometimes, but who cares... | |
1247 | * we count rs_{total,left} in bits, not sectors. | |
1248 | */ | |
1249 | spin_lock_irq(&mdev->al_lock); | |
1250 | count = drbd_bm_count_bits(mdev, sbnr, ebnr); | |
1251 | if (count) { | |
1252 | mdev->rs_failed += count; | |
1253 | ||
1254 | if (get_ldev(mdev)) { | |
81e84650 | 1255 | drbd_try_clear_on_disk_bm(mdev, sector, count, false); |
b411b363 PR |
1256 | put_ldev(mdev); |
1257 | } | |
1258 | ||
1259 | /* just wake_up unconditional now, various lc_chaged(), | |
1260 | * lc_put() in drbd_try_clear_on_disk_bm(). */ | |
1261 | wake_up = 1; | |
1262 | } | |
1263 | spin_unlock_irq(&mdev->al_lock); | |
1264 | if (wake_up) | |
1265 | wake_up(&mdev->al_wait); | |
1266 | } |