]>
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 | ||
00d56944 | 66 | int w_al_write_transaction(struct drbd_work *, int); |
b411b363 | 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; | |
f91ab628 | 178 | int wake; |
b411b363 PR |
179 | |
180 | spin_lock_irq(&mdev->al_lock); | |
181 | tmp = lc_find(mdev->resync, enr/AL_EXT_PER_BM_SECT); | |
182 | if (unlikely(tmp != NULL)) { | |
183 | struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce); | |
184 | if (test_bit(BME_NO_WRITES, &bm_ext->flags)) { | |
f91ab628 | 185 | wake = !test_and_set_bit(BME_PRIORITY, &bm_ext->flags); |
b411b363 | 186 | spin_unlock_irq(&mdev->al_lock); |
f91ab628 PR |
187 | if (wake) |
188 | wake_up(&mdev->al_wait); | |
b411b363 PR |
189 | return NULL; |
190 | } | |
191 | } | |
46a15bc3 | 192 | al_ext = lc_get(mdev->act_log, enr); |
b411b363 | 193 | spin_unlock_irq(&mdev->al_lock); |
b411b363 PR |
194 | return al_ext; |
195 | } | |
196 | ||
197 | void drbd_al_begin_io(struct drbd_conf *mdev, sector_t sector) | |
198 | { | |
199 | unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9)); | |
200 | struct lc_element *al_ext; | |
201 | struct update_al_work al_work; | |
202 | ||
203 | D_ASSERT(atomic_read(&mdev->local_cnt) > 0); | |
204 | ||
b411b363 PR |
205 | wait_event(mdev->al_wait, (al_ext = _al_get(mdev, enr))); |
206 | ||
207 | if (al_ext->lc_number != enr) { | |
208 | /* drbd_al_write_transaction(mdev,al_ext,enr); | |
209 | * recurses into generic_make_request(), which | |
210 | * disallows recursion, bios being serialized on the | |
211 | * current->bio_tail list now. | |
212 | * we have to delegate updates to the activity log | |
213 | * to the worker thread. */ | |
214 | init_completion(&al_work.event); | |
215 | al_work.al_ext = al_ext; | |
216 | al_work.enr = enr; | |
217 | al_work.old_enr = al_ext->lc_number; | |
218 | al_work.w.cb = w_al_write_transaction; | |
a21e9298 | 219 | al_work.w.mdev = mdev; |
e42325a5 | 220 | drbd_queue_work_front(&mdev->tconn->data.work, &al_work.w); |
b411b363 PR |
221 | wait_for_completion(&al_work.event); |
222 | ||
223 | mdev->al_writ_cnt++; | |
224 | ||
225 | spin_lock_irq(&mdev->al_lock); | |
46a15bc3 | 226 | lc_committed(mdev->act_log); |
b411b363 PR |
227 | spin_unlock_irq(&mdev->al_lock); |
228 | wake_up(&mdev->al_wait); | |
229 | } | |
230 | } | |
231 | ||
232 | void drbd_al_complete_io(struct drbd_conf *mdev, sector_t sector) | |
233 | { | |
234 | unsigned int enr = (sector >> (AL_EXTENT_SHIFT-9)); | |
235 | struct lc_element *extent; | |
236 | unsigned long flags; | |
237 | ||
b411b363 PR |
238 | spin_lock_irqsave(&mdev->al_lock, flags); |
239 | ||
240 | extent = lc_find(mdev->act_log, enr); | |
241 | ||
242 | if (!extent) { | |
243 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
244 | dev_err(DEV, "al_complete_io() called on inactive extent %u\n", enr); | |
245 | return; | |
246 | } | |
247 | ||
248 | if (lc_put(mdev->act_log, extent) == 0) | |
249 | wake_up(&mdev->al_wait); | |
250 | ||
251 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
252 | } | |
253 | ||
19f843aa LE |
254 | #if (PAGE_SHIFT + 3) < (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT) |
255 | /* Currently BM_BLOCK_SHIFT, BM_EXT_SHIFT and AL_EXTENT_SHIFT | |
256 | * are still coupled, or assume too much about their relation. | |
257 | * Code below will not work if this is violated. | |
258 | * Will be cleaned up with some followup patch. | |
259 | */ | |
260 | # error FIXME | |
261 | #endif | |
262 | ||
263 | static unsigned int al_extent_to_bm_page(unsigned int al_enr) | |
264 | { | |
265 | return al_enr >> | |
266 | /* bit to page */ | |
267 | ((PAGE_SHIFT + 3) - | |
268 | /* al extent number to bit */ | |
269 | (AL_EXTENT_SHIFT - BM_BLOCK_SHIFT)); | |
270 | } | |
271 | ||
272 | static unsigned int rs_extent_to_bm_page(unsigned int rs_enr) | |
273 | { | |
274 | return rs_enr >> | |
275 | /* bit to page */ | |
276 | ((PAGE_SHIFT + 3) - | |
277 | /* al extent number to bit */ | |
278 | (BM_EXT_SHIFT - BM_BLOCK_SHIFT)); | |
279 | } | |
280 | ||
b411b363 | 281 | int |
00d56944 | 282 | w_al_write_transaction(struct drbd_work *w, int unused) |
b411b363 PR |
283 | { |
284 | struct update_al_work *aw = container_of(w, struct update_al_work, w); | |
00d56944 | 285 | struct drbd_conf *mdev = w->mdev; |
b411b363 PR |
286 | struct lc_element *updated = aw->al_ext; |
287 | const unsigned int new_enr = aw->enr; | |
288 | const unsigned int evicted = aw->old_enr; | |
289 | struct al_transaction *buffer; | |
290 | sector_t sector; | |
291 | int i, n, mx; | |
292 | unsigned int extent_nr; | |
293 | u32 xor_sum = 0; | |
294 | ||
295 | if (!get_ldev(mdev)) { | |
6719fb03 LE |
296 | dev_err(DEV, |
297 | "disk is %s, cannot start al transaction (-%d +%d)\n", | |
298 | drbd_disk_str(mdev->state.disk), evicted, new_enr); | |
b411b363 PR |
299 | complete(&((struct update_al_work *)w)->event); |
300 | return 1; | |
301 | } | |
302 | /* do we have to do a bitmap write, first? | |
303 | * TODO reduce maximum latency: | |
304 | * submit both bios, then wait for both, | |
6719fb03 LE |
305 | * instead of doing two synchronous sector writes. |
306 | * For now, we must not write the transaction, | |
307 | * if we cannot write out the bitmap of the evicted extent. */ | |
b411b363 | 308 | if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE) |
19f843aa | 309 | drbd_bm_write_page(mdev, al_extent_to_bm_page(evicted)); |
b411b363 | 310 | |
6719fb03 LE |
311 | /* The bitmap write may have failed, causing a state change. */ |
312 | if (mdev->state.disk < D_INCONSISTENT) { | |
313 | dev_err(DEV, | |
314 | "disk is %s, cannot write al transaction (-%d +%d)\n", | |
315 | drbd_disk_str(mdev->state.disk), evicted, new_enr); | |
316 | complete(&((struct update_al_work *)w)->event); | |
317 | put_ldev(mdev); | |
318 | return 1; | |
319 | } | |
320 | ||
321 | mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */ | |
b411b363 PR |
322 | buffer = (struct al_transaction *)page_address(mdev->md_io_page); |
323 | ||
324 | buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC); | |
325 | buffer->tr_number = cpu_to_be32(mdev->al_tr_number); | |
326 | ||
327 | n = lc_index_of(mdev->act_log, updated); | |
328 | ||
329 | buffer->updates[0].pos = cpu_to_be32(n); | |
330 | buffer->updates[0].extent = cpu_to_be32(new_enr); | |
331 | ||
332 | xor_sum ^= new_enr; | |
333 | ||
334 | mx = min_t(int, AL_EXTENTS_PT, | |
335 | mdev->act_log->nr_elements - mdev->al_tr_cycle); | |
336 | for (i = 0; i < mx; i++) { | |
337 | unsigned idx = mdev->al_tr_cycle + i; | |
338 | extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number; | |
339 | buffer->updates[i+1].pos = cpu_to_be32(idx); | |
340 | buffer->updates[i+1].extent = cpu_to_be32(extent_nr); | |
341 | xor_sum ^= extent_nr; | |
342 | } | |
343 | for (; i < AL_EXTENTS_PT; i++) { | |
344 | buffer->updates[i+1].pos = __constant_cpu_to_be32(-1); | |
345 | buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE); | |
346 | xor_sum ^= LC_FREE; | |
347 | } | |
348 | mdev->al_tr_cycle += AL_EXTENTS_PT; | |
349 | if (mdev->al_tr_cycle >= mdev->act_log->nr_elements) | |
350 | mdev->al_tr_cycle = 0; | |
351 | ||
352 | buffer->xor_sum = cpu_to_be32(xor_sum); | |
353 | ||
354 | sector = mdev->ldev->md.md_offset | |
355 | + mdev->ldev->md.al_offset + mdev->al_tr_pos; | |
356 | ||
357 | if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) | |
81e84650 | 358 | drbd_chk_io_error(mdev, 1, true); |
b411b363 PR |
359 | |
360 | if (++mdev->al_tr_pos > | |
361 | div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT)) | |
362 | mdev->al_tr_pos = 0; | |
363 | ||
364 | D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE); | |
365 | mdev->al_tr_number++; | |
366 | ||
367 | mutex_unlock(&mdev->md_io_mutex); | |
368 | ||
369 | complete(&((struct update_al_work *)w)->event); | |
370 | put_ldev(mdev); | |
371 | ||
372 | return 1; | |
373 | } | |
374 | ||
375 | /** | |
376 | * drbd_al_read_tr() - Read a single transaction from the on disk activity log | |
377 | * @mdev: DRBD device. | |
378 | * @bdev: Block device to read form. | |
379 | * @b: pointer to an al_transaction. | |
380 | * @index: On disk slot of the transaction to read. | |
381 | * | |
382 | * Returns -1 on IO error, 0 on checksum error and 1 upon success. | |
383 | */ | |
384 | static int drbd_al_read_tr(struct drbd_conf *mdev, | |
385 | struct drbd_backing_dev *bdev, | |
386 | struct al_transaction *b, | |
387 | int index) | |
388 | { | |
389 | sector_t sector; | |
390 | int rv, i; | |
391 | u32 xor_sum = 0; | |
392 | ||
393 | sector = bdev->md.md_offset + bdev->md.al_offset + index; | |
394 | ||
395 | /* Dont process error normally, | |
396 | * as this is done before disk is attached! */ | |
397 | if (!drbd_md_sync_page_io(mdev, bdev, sector, READ)) | |
398 | return -1; | |
399 | ||
e7fad8af | 400 | rv = (b->magic == cpu_to_be32(DRBD_MAGIC)); |
b411b363 PR |
401 | |
402 | for (i = 0; i < AL_EXTENTS_PT + 1; i++) | |
403 | xor_sum ^= be32_to_cpu(b->updates[i].extent); | |
404 | rv &= (xor_sum == be32_to_cpu(b->xor_sum)); | |
405 | ||
406 | return rv; | |
407 | } | |
408 | ||
409 | /** | |
410 | * drbd_al_read_log() - Restores the activity log from its on disk representation. | |
411 | * @mdev: DRBD device. | |
412 | * @bdev: Block device to read form. | |
413 | * | |
414 | * Returns 1 on success, returns 0 when reading the log failed due to IO errors. | |
415 | */ | |
416 | int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev) | |
417 | { | |
418 | struct al_transaction *buffer; | |
419 | int i; | |
420 | int rv; | |
421 | int mx; | |
422 | int active_extents = 0; | |
423 | int transactions = 0; | |
424 | int found_valid = 0; | |
425 | int from = 0; | |
426 | int to = 0; | |
427 | u32 from_tnr = 0; | |
428 | u32 to_tnr = 0; | |
429 | u32 cnr; | |
430 | ||
431 | mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT); | |
432 | ||
433 | /* lock out all other meta data io for now, | |
434 | * and make sure the page is mapped. | |
435 | */ | |
436 | mutex_lock(&mdev->md_io_mutex); | |
437 | buffer = page_address(mdev->md_io_page); | |
438 | ||
439 | /* Find the valid transaction in the log */ | |
440 | for (i = 0; i <= mx; i++) { | |
441 | rv = drbd_al_read_tr(mdev, bdev, buffer, i); | |
442 | if (rv == 0) | |
443 | continue; | |
444 | if (rv == -1) { | |
445 | mutex_unlock(&mdev->md_io_mutex); | |
446 | return 0; | |
447 | } | |
448 | cnr = be32_to_cpu(buffer->tr_number); | |
449 | ||
450 | if (++found_valid == 1) { | |
451 | from = i; | |
452 | to = i; | |
453 | from_tnr = cnr; | |
454 | to_tnr = cnr; | |
455 | continue; | |
456 | } | |
457 | if ((int)cnr - (int)from_tnr < 0) { | |
458 | D_ASSERT(from_tnr - cnr + i - from == mx+1); | |
459 | from = i; | |
460 | from_tnr = cnr; | |
461 | } | |
462 | if ((int)cnr - (int)to_tnr > 0) { | |
463 | D_ASSERT(cnr - to_tnr == i - to); | |
464 | to = i; | |
465 | to_tnr = cnr; | |
466 | } | |
467 | } | |
468 | ||
469 | if (!found_valid) { | |
470 | dev_warn(DEV, "No usable activity log found.\n"); | |
471 | mutex_unlock(&mdev->md_io_mutex); | |
472 | return 1; | |
473 | } | |
474 | ||
475 | /* Read the valid transactions. | |
476 | * dev_info(DEV, "Reading from %d to %d.\n",from,to); */ | |
477 | i = from; | |
478 | while (1) { | |
479 | int j, pos; | |
480 | unsigned int extent_nr; | |
481 | unsigned int trn; | |
482 | ||
483 | rv = drbd_al_read_tr(mdev, bdev, buffer, i); | |
841ce241 AG |
484 | if (!expect(rv != 0)) |
485 | goto cancel; | |
b411b363 PR |
486 | if (rv == -1) { |
487 | mutex_unlock(&mdev->md_io_mutex); | |
488 | return 0; | |
489 | } | |
490 | ||
491 | trn = be32_to_cpu(buffer->tr_number); | |
492 | ||
493 | spin_lock_irq(&mdev->al_lock); | |
494 | ||
495 | /* This loop runs backwards because in the cyclic | |
496 | elements there might be an old version of the | |
497 | updated element (in slot 0). So the element in slot 0 | |
498 | can overwrite old versions. */ | |
499 | for (j = AL_EXTENTS_PT; j >= 0; j--) { | |
500 | pos = be32_to_cpu(buffer->updates[j].pos); | |
501 | extent_nr = be32_to_cpu(buffer->updates[j].extent); | |
502 | ||
503 | if (extent_nr == LC_FREE) | |
504 | continue; | |
505 | ||
506 | lc_set(mdev->act_log, extent_nr, pos); | |
507 | active_extents++; | |
508 | } | |
509 | spin_unlock_irq(&mdev->al_lock); | |
510 | ||
511 | transactions++; | |
512 | ||
513 | cancel: | |
514 | if (i == to) | |
515 | break; | |
516 | i++; | |
517 | if (i > mx) | |
518 | i = 0; | |
519 | } | |
520 | ||
521 | mdev->al_tr_number = to_tnr+1; | |
522 | mdev->al_tr_pos = to; | |
523 | if (++mdev->al_tr_pos > | |
524 | div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT)) | |
525 | mdev->al_tr_pos = 0; | |
526 | ||
527 | /* ok, we are done with it */ | |
528 | mutex_unlock(&mdev->md_io_mutex); | |
529 | ||
530 | dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n", | |
531 | transactions, active_extents); | |
532 | ||
533 | return 1; | |
534 | } | |
535 | ||
b411b363 | 536 | /** |
867f5748 | 537 | * drbd_al_apply_to_bm() - Sets the bitmap to dirty(1) where covered by active AL extents |
b411b363 PR |
538 | * @mdev: DRBD device. |
539 | */ | |
540 | void drbd_al_apply_to_bm(struct drbd_conf *mdev) | |
541 | { | |
542 | unsigned int enr; | |
543 | unsigned long add = 0; | |
544 | char ppb[10]; | |
6719fb03 | 545 | int i, tmp; |
b411b363 PR |
546 | |
547 | wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); | |
548 | ||
549 | for (i = 0; i < mdev->act_log->nr_elements; i++) { | |
550 | enr = lc_element_by_index(mdev->act_log, i)->lc_number; | |
551 | if (enr == LC_FREE) | |
552 | continue; | |
6719fb03 LE |
553 | tmp = drbd_bm_ALe_set_all(mdev, enr); |
554 | dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr); | |
555 | add += tmp; | |
b411b363 PR |
556 | } |
557 | ||
558 | lc_unlock(mdev->act_log); | |
559 | wake_up(&mdev->al_wait); | |
560 | ||
561 | dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n", | |
562 | ppsize(ppb, Bit2KB(add))); | |
563 | } | |
564 | ||
565 | static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext) | |
566 | { | |
567 | int rv; | |
568 | ||
569 | spin_lock_irq(&mdev->al_lock); | |
570 | rv = (al_ext->refcnt == 0); | |
571 | if (likely(rv)) | |
572 | lc_del(mdev->act_log, al_ext); | |
573 | spin_unlock_irq(&mdev->al_lock); | |
574 | ||
575 | return rv; | |
576 | } | |
577 | ||
578 | /** | |
579 | * drbd_al_shrink() - Removes all active extents form the activity log | |
580 | * @mdev: DRBD device. | |
581 | * | |
582 | * Removes all active extents form the activity log, waiting until | |
583 | * the reference count of each entry dropped to 0 first, of course. | |
584 | * | |
585 | * You need to lock mdev->act_log with lc_try_lock() / lc_unlock() | |
586 | */ | |
587 | void drbd_al_shrink(struct drbd_conf *mdev) | |
588 | { | |
589 | struct lc_element *al_ext; | |
590 | int i; | |
591 | ||
46a15bc3 | 592 | D_ASSERT(test_bit(__LC_LOCKED, &mdev->act_log->flags)); |
b411b363 PR |
593 | |
594 | for (i = 0; i < mdev->act_log->nr_elements; i++) { | |
595 | al_ext = lc_element_by_index(mdev->act_log, i); | |
596 | if (al_ext->lc_number == LC_FREE) | |
597 | continue; | |
598 | wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext)); | |
599 | } | |
600 | ||
601 | wake_up(&mdev->al_wait); | |
602 | } | |
603 | ||
00d56944 | 604 | static int w_update_odbm(struct drbd_work *w, int unused) |
b411b363 PR |
605 | { |
606 | struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w); | |
00d56944 | 607 | struct drbd_conf *mdev = w->mdev; |
b411b363 PR |
608 | |
609 | if (!get_ldev(mdev)) { | |
610 | if (__ratelimit(&drbd_ratelimit_state)) | |
611 | dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n"); | |
612 | kfree(udw); | |
613 | return 1; | |
614 | } | |
615 | ||
19f843aa | 616 | drbd_bm_write_page(mdev, rs_extent_to_bm_page(udw->enr)); |
b411b363 PR |
617 | put_ldev(mdev); |
618 | ||
619 | kfree(udw); | |
620 | ||
621 | if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) { | |
622 | switch (mdev->state.conn) { | |
623 | case C_SYNC_SOURCE: case C_SYNC_TARGET: | |
624 | case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T: | |
625 | drbd_resync_finished(mdev); | |
626 | default: | |
627 | /* nothing to do */ | |
628 | break; | |
629 | } | |
630 | } | |
631 | drbd_bcast_sync_progress(mdev); | |
632 | ||
633 | return 1; | |
634 | } | |
635 | ||
636 | ||
637 | /* ATTENTION. The AL's extents are 4MB each, while the extents in the | |
638 | * resync LRU-cache are 16MB each. | |
639 | * The caller of this function has to hold an get_ldev() reference. | |
640 | * | |
641 | * TODO will be obsoleted once we have a caching lru of the on disk bitmap | |
642 | */ | |
643 | static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector, | |
644 | int count, int success) | |
645 | { | |
646 | struct lc_element *e; | |
647 | struct update_odbm_work *udw; | |
648 | ||
649 | unsigned int enr; | |
650 | ||
651 | D_ASSERT(atomic_read(&mdev->local_cnt)); | |
652 | ||
653 | /* I simply assume that a sector/size pair never crosses | |
654 | * a 16 MB extent border. (Currently this is true...) */ | |
655 | enr = BM_SECT_TO_EXT(sector); | |
656 | ||
657 | e = lc_get(mdev->resync, enr); | |
658 | if (e) { | |
659 | struct bm_extent *ext = lc_entry(e, struct bm_extent, lce); | |
660 | if (ext->lce.lc_number == enr) { | |
661 | if (success) | |
662 | ext->rs_left -= count; | |
663 | else | |
664 | ext->rs_failed += count; | |
665 | if (ext->rs_left < ext->rs_failed) { | |
666 | dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d " | |
667 | "rs_failed=%d count=%d\n", | |
668 | (unsigned long long)sector, | |
669 | ext->lce.lc_number, ext->rs_left, | |
670 | ext->rs_failed, count); | |
671 | dump_stack(); | |
672 | ||
673 | lc_put(mdev->resync, &ext->lce); | |
674 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); | |
675 | return; | |
676 | } | |
677 | } else { | |
678 | /* Normally this element should be in the cache, | |
679 | * since drbd_rs_begin_io() pulled it already in. | |
680 | * | |
681 | * But maybe an application write finished, and we set | |
682 | * something outside the resync lru_cache in sync. | |
683 | */ | |
684 | int rs_left = drbd_bm_e_weight(mdev, enr); | |
685 | if (ext->flags != 0) { | |
686 | dev_warn(DEV, "changing resync lce: %d[%u;%02lx]" | |
687 | " -> %d[%u;00]\n", | |
688 | ext->lce.lc_number, ext->rs_left, | |
689 | ext->flags, enr, rs_left); | |
690 | ext->flags = 0; | |
691 | } | |
692 | if (ext->rs_failed) { | |
693 | dev_warn(DEV, "Kicking resync_lru element enr=%u " | |
694 | "out with rs_failed=%d\n", | |
695 | ext->lce.lc_number, ext->rs_failed); | |
b411b363 PR |
696 | } |
697 | ext->rs_left = rs_left; | |
698 | ext->rs_failed = success ? 0 : count; | |
46a15bc3 LE |
699 | /* we don't keep a persistent log of the resync lru, |
700 | * we can commit any change right away. */ | |
701 | lc_committed(mdev->resync); | |
b411b363 PR |
702 | } |
703 | lc_put(mdev->resync, &ext->lce); | |
704 | /* no race, we are within the al_lock! */ | |
705 | ||
706 | if (ext->rs_left == ext->rs_failed) { | |
707 | ext->rs_failed = 0; | |
708 | ||
709 | udw = kmalloc(sizeof(*udw), GFP_ATOMIC); | |
710 | if (udw) { | |
711 | udw->enr = ext->lce.lc_number; | |
712 | udw->w.cb = w_update_odbm; | |
a21e9298 | 713 | udw->w.mdev = mdev; |
e42325a5 | 714 | drbd_queue_work_front(&mdev->tconn->data.work, &udw->w); |
b411b363 PR |
715 | } else { |
716 | dev_warn(DEV, "Could not kmalloc an udw\n"); | |
b411b363 PR |
717 | } |
718 | } | |
719 | } else { | |
720 | dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n", | |
721 | mdev->resync_locked, | |
722 | mdev->resync->nr_elements, | |
723 | mdev->resync->flags); | |
724 | } | |
725 | } | |
726 | ||
c6ea14df LE |
727 | void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go) |
728 | { | |
729 | unsigned long now = jiffies; | |
730 | unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark]; | |
731 | int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS; | |
732 | if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) { | |
733 | if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go && | |
734 | mdev->state.conn != C_PAUSED_SYNC_T && | |
735 | mdev->state.conn != C_PAUSED_SYNC_S) { | |
736 | mdev->rs_mark_time[next] = now; | |
737 | mdev->rs_mark_left[next] = still_to_go; | |
738 | mdev->rs_last_mark = next; | |
739 | } | |
740 | } | |
741 | } | |
742 | ||
b411b363 PR |
743 | /* clear the bit corresponding to the piece of storage in question: |
744 | * size byte of data starting from sector. Only clear a bits of the affected | |
745 | * one ore more _aligned_ BM_BLOCK_SIZE blocks. | |
746 | * | |
747 | * called by worker on C_SYNC_TARGET and receiver on SyncSource. | |
748 | * | |
749 | */ | |
750 | void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size, | |
751 | const char *file, const unsigned int line) | |
752 | { | |
753 | /* Is called from worker and receiver context _only_ */ | |
754 | unsigned long sbnr, ebnr, lbnr; | |
755 | unsigned long count = 0; | |
756 | sector_t esector, nr_sectors; | |
757 | int wake_up = 0; | |
758 | unsigned long flags; | |
759 | ||
c670a398 | 760 | if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
761 | dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n", |
762 | (unsigned long long)sector, size); | |
763 | return; | |
764 | } | |
765 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
766 | esector = sector + (size >> 9) - 1; | |
767 | ||
841ce241 AG |
768 | if (!expect(sector < nr_sectors)) |
769 | return; | |
770 | if (!expect(esector < nr_sectors)) | |
771 | esector = nr_sectors - 1; | |
b411b363 PR |
772 | |
773 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
774 | ||
775 | /* we clear it (in sync). | |
776 | * round up start sector, round down end sector. we make sure we only | |
777 | * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */ | |
778 | if (unlikely(esector < BM_SECT_PER_BIT-1)) | |
779 | return; | |
780 | if (unlikely(esector == (nr_sectors-1))) | |
781 | ebnr = lbnr; | |
782 | else | |
783 | ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); | |
784 | sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); | |
785 | ||
b411b363 PR |
786 | if (sbnr > ebnr) |
787 | return; | |
788 | ||
789 | /* | |
790 | * ok, (capacity & 7) != 0 sometimes, but who cares... | |
791 | * we count rs_{total,left} in bits, not sectors. | |
792 | */ | |
b411b363 | 793 | count = drbd_bm_clear_bits(mdev, sbnr, ebnr); |
1d7734a0 | 794 | if (count && get_ldev(mdev)) { |
c6ea14df | 795 | drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev)); |
1d7734a0 | 796 | spin_lock_irqsave(&mdev->al_lock, flags); |
81e84650 | 797 | drbd_try_clear_on_disk_bm(mdev, sector, count, true); |
1d7734a0 LE |
798 | spin_unlock_irqrestore(&mdev->al_lock, flags); |
799 | ||
b411b363 PR |
800 | /* just wake_up unconditional now, various lc_chaged(), |
801 | * lc_put() in drbd_try_clear_on_disk_bm(). */ | |
802 | wake_up = 1; | |
1d7734a0 | 803 | put_ldev(mdev); |
b411b363 | 804 | } |
b411b363 PR |
805 | if (wake_up) |
806 | wake_up(&mdev->al_wait); | |
807 | } | |
808 | ||
809 | /* | |
810 | * this is intended to set one request worth of data out of sync. | |
811 | * affects at least 1 bit, | |
1816a2b4 | 812 | * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits. |
b411b363 PR |
813 | * |
814 | * called by tl_clear and drbd_send_dblock (==drbd_make_request). | |
815 | * so this can be _any_ process. | |
816 | */ | |
73a01a18 | 817 | int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size, |
b411b363 PR |
818 | const char *file, const unsigned int line) |
819 | { | |
820 | unsigned long sbnr, ebnr, lbnr, flags; | |
821 | sector_t esector, nr_sectors; | |
73a01a18 | 822 | unsigned int enr, count = 0; |
b411b363 PR |
823 | struct lc_element *e; |
824 | ||
c670a398 | 825 | if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
826 | dev_err(DEV, "sector: %llus, size: %d\n", |
827 | (unsigned long long)sector, size); | |
73a01a18 | 828 | return 0; |
b411b363 PR |
829 | } |
830 | ||
831 | if (!get_ldev(mdev)) | |
73a01a18 | 832 | return 0; /* no disk, no metadata, no bitmap to set bits in */ |
b411b363 PR |
833 | |
834 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
835 | esector = sector + (size >> 9) - 1; | |
836 | ||
841ce241 | 837 | if (!expect(sector < nr_sectors)) |
b411b363 | 838 | goto out; |
841ce241 AG |
839 | if (!expect(esector < nr_sectors)) |
840 | esector = nr_sectors - 1; | |
b411b363 PR |
841 | |
842 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
843 | ||
844 | /* we set it out of sync, | |
845 | * we do not need to round anything here */ | |
846 | sbnr = BM_SECT_TO_BIT(sector); | |
847 | ebnr = BM_SECT_TO_BIT(esector); | |
848 | ||
b411b363 PR |
849 | /* ok, (capacity & 7) != 0 sometimes, but who cares... |
850 | * we count rs_{total,left} in bits, not sectors. */ | |
851 | spin_lock_irqsave(&mdev->al_lock, flags); | |
852 | count = drbd_bm_set_bits(mdev, sbnr, ebnr); | |
853 | ||
854 | enr = BM_SECT_TO_EXT(sector); | |
855 | e = lc_find(mdev->resync, enr); | |
856 | if (e) | |
857 | lc_entry(e, struct bm_extent, lce)->rs_left += count; | |
858 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
859 | ||
860 | out: | |
861 | put_ldev(mdev); | |
73a01a18 PR |
862 | |
863 | return count; | |
b411b363 PR |
864 | } |
865 | ||
866 | static | |
867 | struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr) | |
868 | { | |
869 | struct lc_element *e; | |
870 | struct bm_extent *bm_ext; | |
871 | int wakeup = 0; | |
872 | unsigned long rs_flags; | |
873 | ||
874 | spin_lock_irq(&mdev->al_lock); | |
875 | if (mdev->resync_locked > mdev->resync->nr_elements/2) { | |
876 | spin_unlock_irq(&mdev->al_lock); | |
877 | return NULL; | |
878 | } | |
879 | e = lc_get(mdev->resync, enr); | |
880 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
881 | if (bm_ext) { | |
882 | if (bm_ext->lce.lc_number != enr) { | |
883 | bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); | |
884 | bm_ext->rs_failed = 0; | |
46a15bc3 | 885 | lc_committed(mdev->resync); |
b411b363 PR |
886 | wakeup = 1; |
887 | } | |
888 | if (bm_ext->lce.refcnt == 1) | |
889 | mdev->resync_locked++; | |
890 | set_bit(BME_NO_WRITES, &bm_ext->flags); | |
891 | } | |
892 | rs_flags = mdev->resync->flags; | |
893 | spin_unlock_irq(&mdev->al_lock); | |
894 | if (wakeup) | |
895 | wake_up(&mdev->al_wait); | |
896 | ||
897 | if (!bm_ext) { | |
898 | if (rs_flags & LC_STARVING) | |
899 | dev_warn(DEV, "Have to wait for element" | |
900 | " (resync LRU too small?)\n"); | |
46a15bc3 | 901 | BUG_ON(rs_flags & LC_LOCKED); |
b411b363 PR |
902 | } |
903 | ||
904 | return bm_ext; | |
905 | } | |
906 | ||
907 | static int _is_in_al(struct drbd_conf *mdev, unsigned int enr) | |
908 | { | |
46a15bc3 | 909 | int rv; |
b411b363 PR |
910 | |
911 | spin_lock_irq(&mdev->al_lock); | |
46a15bc3 | 912 | rv = lc_is_used(mdev->act_log, enr); |
b411b363 PR |
913 | spin_unlock_irq(&mdev->al_lock); |
914 | ||
b411b363 PR |
915 | return rv; |
916 | } | |
917 | ||
918 | /** | |
919 | * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED | |
920 | * @mdev: DRBD device. | |
921 | * @sector: The sector number. | |
922 | * | |
80a40e43 | 923 | * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted. |
b411b363 PR |
924 | */ |
925 | int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector) | |
926 | { | |
927 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
928 | struct bm_extent *bm_ext; | |
929 | int i, sig; | |
f91ab628 PR |
930 | int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait. |
931 | 200 times -> 20 seconds. */ | |
b411b363 | 932 | |
f91ab628 | 933 | retry: |
b411b363 PR |
934 | sig = wait_event_interruptible(mdev->al_wait, |
935 | (bm_ext = _bme_get(mdev, enr))); | |
936 | if (sig) | |
80a40e43 | 937 | return -EINTR; |
b411b363 PR |
938 | |
939 | if (test_bit(BME_LOCKED, &bm_ext->flags)) | |
80a40e43 | 940 | return 0; |
b411b363 PR |
941 | |
942 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { | |
943 | sig = wait_event_interruptible(mdev->al_wait, | |
f91ab628 | 944 | !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) || |
c507f46f | 945 | test_bit(BME_PRIORITY, &bm_ext->flags)); |
f91ab628 PR |
946 | |
947 | if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) { | |
b411b363 PR |
948 | spin_lock_irq(&mdev->al_lock); |
949 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) { | |
f91ab628 | 950 | bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */ |
b411b363 PR |
951 | mdev->resync_locked--; |
952 | wake_up(&mdev->al_wait); | |
953 | } | |
954 | spin_unlock_irq(&mdev->al_lock); | |
f91ab628 PR |
955 | if (sig) |
956 | return -EINTR; | |
957 | if (schedule_timeout_interruptible(HZ/10)) | |
958 | return -EINTR; | |
c507f46f PR |
959 | if (sa && --sa == 0) |
960 | dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec." | |
961 | "Resync stalled?\n"); | |
f91ab628 | 962 | goto retry; |
b411b363 PR |
963 | } |
964 | } | |
b411b363 | 965 | set_bit(BME_LOCKED, &bm_ext->flags); |
80a40e43 | 966 | return 0; |
b411b363 PR |
967 | } |
968 | ||
969 | /** | |
970 | * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep | |
971 | * @mdev: DRBD device. | |
972 | * @sector: The sector number. | |
973 | * | |
974 | * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then | |
975 | * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN | |
976 | * if there is still application IO going on in this area. | |
977 | */ | |
978 | int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector) | |
979 | { | |
980 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
981 | const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT; | |
982 | struct lc_element *e; | |
983 | struct bm_extent *bm_ext; | |
984 | int i; | |
985 | ||
b411b363 PR |
986 | spin_lock_irq(&mdev->al_lock); |
987 | if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) { | |
988 | /* in case you have very heavy scattered io, it may | |
989 | * stall the syncer undefined if we give up the ref count | |
990 | * when we try again and requeue. | |
991 | * | |
992 | * if we don't give up the refcount, but the next time | |
993 | * we are scheduled this extent has been "synced" by new | |
994 | * application writes, we'd miss the lc_put on the | |
995 | * extent we keep the refcount on. | |
996 | * so we remembered which extent we had to try again, and | |
997 | * if the next requested one is something else, we do | |
998 | * the lc_put here... | |
999 | * we also have to wake_up | |
1000 | */ | |
b411b363 PR |
1001 | e = lc_find(mdev->resync, mdev->resync_wenr); |
1002 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1003 | if (bm_ext) { | |
1004 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
1005 | D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1006 | clear_bit(BME_NO_WRITES, &bm_ext->flags); | |
1007 | mdev->resync_wenr = LC_FREE; | |
1008 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) | |
1009 | mdev->resync_locked--; | |
1010 | wake_up(&mdev->al_wait); | |
1011 | } else { | |
1012 | dev_alert(DEV, "LOGIC BUG\n"); | |
1013 | } | |
1014 | } | |
1015 | /* TRY. */ | |
1016 | e = lc_try_get(mdev->resync, enr); | |
1017 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1018 | if (bm_ext) { | |
1019 | if (test_bit(BME_LOCKED, &bm_ext->flags)) | |
1020 | goto proceed; | |
1021 | if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) { | |
1022 | mdev->resync_locked++; | |
1023 | } else { | |
1024 | /* we did set the BME_NO_WRITES, | |
1025 | * but then could not set BME_LOCKED, | |
1026 | * so we tried again. | |
1027 | * drop the extra reference. */ | |
b411b363 PR |
1028 | bm_ext->lce.refcnt--; |
1029 | D_ASSERT(bm_ext->lce.refcnt > 0); | |
1030 | } | |
1031 | goto check_al; | |
1032 | } else { | |
1033 | /* do we rather want to try later? */ | |
6a0afdf5 | 1034 | if (mdev->resync_locked > mdev->resync->nr_elements-3) |
b411b363 | 1035 | goto try_again; |
b411b363 PR |
1036 | /* Do or do not. There is no try. -- Yoda */ |
1037 | e = lc_get(mdev->resync, enr); | |
1038 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1039 | if (!bm_ext) { | |
1040 | const unsigned long rs_flags = mdev->resync->flags; | |
1041 | if (rs_flags & LC_STARVING) | |
1042 | dev_warn(DEV, "Have to wait for element" | |
1043 | " (resync LRU too small?)\n"); | |
46a15bc3 | 1044 | BUG_ON(rs_flags & LC_LOCKED); |
b411b363 PR |
1045 | goto try_again; |
1046 | } | |
1047 | if (bm_ext->lce.lc_number != enr) { | |
1048 | bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); | |
1049 | bm_ext->rs_failed = 0; | |
46a15bc3 | 1050 | lc_committed(mdev->resync); |
b411b363 PR |
1051 | wake_up(&mdev->al_wait); |
1052 | D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0); | |
1053 | } | |
1054 | set_bit(BME_NO_WRITES, &bm_ext->flags); | |
1055 | D_ASSERT(bm_ext->lce.refcnt == 1); | |
1056 | mdev->resync_locked++; | |
1057 | goto check_al; | |
1058 | } | |
1059 | check_al: | |
b411b363 | 1060 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { |
b411b363 PR |
1061 | if (lc_is_used(mdev->act_log, al_enr+i)) |
1062 | goto try_again; | |
1063 | } | |
1064 | set_bit(BME_LOCKED, &bm_ext->flags); | |
1065 | proceed: | |
1066 | mdev->resync_wenr = LC_FREE; | |
1067 | spin_unlock_irq(&mdev->al_lock); | |
1068 | return 0; | |
1069 | ||
1070 | try_again: | |
b411b363 PR |
1071 | if (bm_ext) |
1072 | mdev->resync_wenr = enr; | |
1073 | spin_unlock_irq(&mdev->al_lock); | |
1074 | return -EAGAIN; | |
1075 | } | |
1076 | ||
1077 | void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector) | |
1078 | { | |
1079 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
1080 | struct lc_element *e; | |
1081 | struct bm_extent *bm_ext; | |
1082 | unsigned long flags; | |
1083 | ||
b411b363 PR |
1084 | spin_lock_irqsave(&mdev->al_lock, flags); |
1085 | e = lc_find(mdev->resync, enr); | |
1086 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1087 | if (!bm_ext) { | |
1088 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1089 | if (__ratelimit(&drbd_ratelimit_state)) | |
1090 | dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n"); | |
1091 | return; | |
1092 | } | |
1093 | ||
1094 | if (bm_ext->lce.refcnt == 0) { | |
1095 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1096 | dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, " | |
1097 | "but refcnt is 0!?\n", | |
1098 | (unsigned long long)sector, enr); | |
1099 | return; | |
1100 | } | |
1101 | ||
1102 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) { | |
e3555d85 | 1103 | bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */ |
b411b363 PR |
1104 | mdev->resync_locked--; |
1105 | wake_up(&mdev->al_wait); | |
1106 | } | |
1107 | ||
1108 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1109 | } | |
1110 | ||
1111 | /** | |
1112 | * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED) | |
1113 | * @mdev: DRBD device. | |
1114 | */ | |
1115 | void drbd_rs_cancel_all(struct drbd_conf *mdev) | |
1116 | { | |
b411b363 PR |
1117 | spin_lock_irq(&mdev->al_lock); |
1118 | ||
1119 | if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */ | |
1120 | lc_reset(mdev->resync); | |
1121 | put_ldev(mdev); | |
1122 | } | |
1123 | mdev->resync_locked = 0; | |
1124 | mdev->resync_wenr = LC_FREE; | |
1125 | spin_unlock_irq(&mdev->al_lock); | |
1126 | wake_up(&mdev->al_wait); | |
1127 | } | |
1128 | ||
1129 | /** | |
1130 | * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU | |
1131 | * @mdev: DRBD device. | |
1132 | * | |
1133 | * Returns 0 upon success, -EAGAIN if at least one reference count was | |
1134 | * not zero. | |
1135 | */ | |
1136 | int drbd_rs_del_all(struct drbd_conf *mdev) | |
1137 | { | |
1138 | struct lc_element *e; | |
1139 | struct bm_extent *bm_ext; | |
1140 | int i; | |
1141 | ||
b411b363 PR |
1142 | spin_lock_irq(&mdev->al_lock); |
1143 | ||
1144 | if (get_ldev_if_state(mdev, D_FAILED)) { | |
1145 | /* ok, ->resync is there. */ | |
1146 | for (i = 0; i < mdev->resync->nr_elements; i++) { | |
1147 | e = lc_element_by_index(mdev->resync, i); | |
b2b163dd | 1148 | bm_ext = lc_entry(e, struct bm_extent, lce); |
b411b363 PR |
1149 | if (bm_ext->lce.lc_number == LC_FREE) |
1150 | continue; | |
1151 | if (bm_ext->lce.lc_number == mdev->resync_wenr) { | |
1152 | dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently" | |
1153 | " got 'synced' by application io\n", | |
1154 | mdev->resync_wenr); | |
1155 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
1156 | D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1157 | clear_bit(BME_NO_WRITES, &bm_ext->flags); | |
1158 | mdev->resync_wenr = LC_FREE; | |
1159 | lc_put(mdev->resync, &bm_ext->lce); | |
1160 | } | |
1161 | if (bm_ext->lce.refcnt != 0) { | |
1162 | dev_info(DEV, "Retrying drbd_rs_del_all() later. " | |
1163 | "refcnt=%d\n", bm_ext->lce.refcnt); | |
1164 | put_ldev(mdev); | |
1165 | spin_unlock_irq(&mdev->al_lock); | |
1166 | return -EAGAIN; | |
1167 | } | |
1168 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
1169 | D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1170 | lc_del(mdev->resync, &bm_ext->lce); | |
1171 | } | |
1172 | D_ASSERT(mdev->resync->used == 0); | |
1173 | put_ldev(mdev); | |
1174 | } | |
1175 | spin_unlock_irq(&mdev->al_lock); | |
1176 | ||
1177 | return 0; | |
1178 | } | |
1179 | ||
1180 | /** | |
1181 | * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks | |
1182 | * @mdev: DRBD device. | |
1183 | * @sector: The sector number. | |
1184 | * @size: Size of failed IO operation, in byte. | |
1185 | */ | |
1186 | void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size) | |
1187 | { | |
1188 | /* Is called from worker and receiver context _only_ */ | |
1189 | unsigned long sbnr, ebnr, lbnr; | |
1190 | unsigned long count; | |
1191 | sector_t esector, nr_sectors; | |
1192 | int wake_up = 0; | |
1193 | ||
c670a398 | 1194 | if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
1195 | dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n", |
1196 | (unsigned long long)sector, size); | |
1197 | return; | |
1198 | } | |
1199 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
1200 | esector = sector + (size >> 9) - 1; | |
1201 | ||
841ce241 AG |
1202 | if (!expect(sector < nr_sectors)) |
1203 | return; | |
1204 | if (!expect(esector < nr_sectors)) | |
1205 | esector = nr_sectors - 1; | |
b411b363 PR |
1206 | |
1207 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
1208 | ||
1209 | /* | |
1210 | * round up start sector, round down end sector. we make sure we only | |
1211 | * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */ | |
1212 | if (unlikely(esector < BM_SECT_PER_BIT-1)) | |
1213 | return; | |
1214 | if (unlikely(esector == (nr_sectors-1))) | |
1215 | ebnr = lbnr; | |
1216 | else | |
1217 | ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); | |
1218 | sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); | |
1219 | ||
1220 | if (sbnr > ebnr) | |
1221 | return; | |
1222 | ||
1223 | /* | |
1224 | * ok, (capacity & 7) != 0 sometimes, but who cares... | |
1225 | * we count rs_{total,left} in bits, not sectors. | |
1226 | */ | |
1227 | spin_lock_irq(&mdev->al_lock); | |
1228 | count = drbd_bm_count_bits(mdev, sbnr, ebnr); | |
1229 | if (count) { | |
1230 | mdev->rs_failed += count; | |
1231 | ||
1232 | if (get_ldev(mdev)) { | |
81e84650 | 1233 | drbd_try_clear_on_disk_bm(mdev, sector, count, false); |
b411b363 PR |
1234 | put_ldev(mdev); |
1235 | } | |
1236 | ||
1237 | /* just wake_up unconditional now, various lc_chaged(), | |
1238 | * lc_put() in drbd_try_clear_on_disk_bm(). */ | |
1239 | wake_up = 1; | |
1240 | } | |
1241 | spin_unlock_irq(&mdev->al_lock); | |
1242 | if (wake_up) | |
1243 | wake_up(&mdev->al_wait); | |
1244 | } |