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 | ||
31 | /* We maintain a trivial check sum in our on disk activity log. | |
32 | * With that we can ensure correct operation even when the storage | |
33 | * device might do a partial (last) sector write while loosing power. | |
34 | */ | |
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 PR |
81 | if ((rw & WRITE) && !test_bit(MD_NO_FUA, &mdev->flags)) |
82 | rw |= REQ_FUA; | |
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 | ||
265 | int | |
266 | w_al_write_transaction(struct drbd_conf *mdev, struct drbd_work *w, int unused) | |
267 | { | |
268 | struct update_al_work *aw = container_of(w, struct update_al_work, w); | |
269 | struct lc_element *updated = aw->al_ext; | |
270 | const unsigned int new_enr = aw->enr; | |
271 | const unsigned int evicted = aw->old_enr; | |
272 | struct al_transaction *buffer; | |
273 | sector_t sector; | |
274 | int i, n, mx; | |
275 | unsigned int extent_nr; | |
276 | u32 xor_sum = 0; | |
277 | ||
278 | if (!get_ldev(mdev)) { | |
6719fb03 LE |
279 | dev_err(DEV, |
280 | "disk is %s, cannot start al transaction (-%d +%d)\n", | |
281 | drbd_disk_str(mdev->state.disk), evicted, new_enr); | |
b411b363 PR |
282 | complete(&((struct update_al_work *)w)->event); |
283 | return 1; | |
284 | } | |
285 | /* do we have to do a bitmap write, first? | |
286 | * TODO reduce maximum latency: | |
287 | * submit both bios, then wait for both, | |
6719fb03 LE |
288 | * instead of doing two synchronous sector writes. |
289 | * For now, we must not write the transaction, | |
290 | * if we cannot write out the bitmap of the evicted extent. */ | |
b411b363 PR |
291 | if (mdev->state.conn < C_CONNECTED && evicted != LC_FREE) |
292 | drbd_bm_write_sect(mdev, evicted/AL_EXT_PER_BM_SECT); | |
293 | ||
6719fb03 LE |
294 | /* The bitmap write may have failed, causing a state change. */ |
295 | if (mdev->state.disk < D_INCONSISTENT) { | |
296 | dev_err(DEV, | |
297 | "disk is %s, cannot write al transaction (-%d +%d)\n", | |
298 | drbd_disk_str(mdev->state.disk), evicted, new_enr); | |
299 | complete(&((struct update_al_work *)w)->event); | |
300 | put_ldev(mdev); | |
301 | return 1; | |
302 | } | |
303 | ||
304 | mutex_lock(&mdev->md_io_mutex); /* protects md_io_buffer, al_tr_cycle, ... */ | |
b411b363 PR |
305 | buffer = (struct al_transaction *)page_address(mdev->md_io_page); |
306 | ||
307 | buffer->magic = __constant_cpu_to_be32(DRBD_MAGIC); | |
308 | buffer->tr_number = cpu_to_be32(mdev->al_tr_number); | |
309 | ||
310 | n = lc_index_of(mdev->act_log, updated); | |
311 | ||
312 | buffer->updates[0].pos = cpu_to_be32(n); | |
313 | buffer->updates[0].extent = cpu_to_be32(new_enr); | |
314 | ||
315 | xor_sum ^= new_enr; | |
316 | ||
317 | mx = min_t(int, AL_EXTENTS_PT, | |
318 | mdev->act_log->nr_elements - mdev->al_tr_cycle); | |
319 | for (i = 0; i < mx; i++) { | |
320 | unsigned idx = mdev->al_tr_cycle + i; | |
321 | extent_nr = lc_element_by_index(mdev->act_log, idx)->lc_number; | |
322 | buffer->updates[i+1].pos = cpu_to_be32(idx); | |
323 | buffer->updates[i+1].extent = cpu_to_be32(extent_nr); | |
324 | xor_sum ^= extent_nr; | |
325 | } | |
326 | for (; i < AL_EXTENTS_PT; i++) { | |
327 | buffer->updates[i+1].pos = __constant_cpu_to_be32(-1); | |
328 | buffer->updates[i+1].extent = __constant_cpu_to_be32(LC_FREE); | |
329 | xor_sum ^= LC_FREE; | |
330 | } | |
331 | mdev->al_tr_cycle += AL_EXTENTS_PT; | |
332 | if (mdev->al_tr_cycle >= mdev->act_log->nr_elements) | |
333 | mdev->al_tr_cycle = 0; | |
334 | ||
335 | buffer->xor_sum = cpu_to_be32(xor_sum); | |
336 | ||
337 | sector = mdev->ldev->md.md_offset | |
338 | + mdev->ldev->md.al_offset + mdev->al_tr_pos; | |
339 | ||
340 | if (!drbd_md_sync_page_io(mdev, mdev->ldev, sector, WRITE)) | |
341 | drbd_chk_io_error(mdev, 1, TRUE); | |
342 | ||
343 | if (++mdev->al_tr_pos > | |
344 | div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT)) | |
345 | mdev->al_tr_pos = 0; | |
346 | ||
347 | D_ASSERT(mdev->al_tr_pos < MD_AL_MAX_SIZE); | |
348 | mdev->al_tr_number++; | |
349 | ||
350 | mutex_unlock(&mdev->md_io_mutex); | |
351 | ||
352 | complete(&((struct update_al_work *)w)->event); | |
353 | put_ldev(mdev); | |
354 | ||
355 | return 1; | |
356 | } | |
357 | ||
358 | /** | |
359 | * drbd_al_read_tr() - Read a single transaction from the on disk activity log | |
360 | * @mdev: DRBD device. | |
361 | * @bdev: Block device to read form. | |
362 | * @b: pointer to an al_transaction. | |
363 | * @index: On disk slot of the transaction to read. | |
364 | * | |
365 | * Returns -1 on IO error, 0 on checksum error and 1 upon success. | |
366 | */ | |
367 | static int drbd_al_read_tr(struct drbd_conf *mdev, | |
368 | struct drbd_backing_dev *bdev, | |
369 | struct al_transaction *b, | |
370 | int index) | |
371 | { | |
372 | sector_t sector; | |
373 | int rv, i; | |
374 | u32 xor_sum = 0; | |
375 | ||
376 | sector = bdev->md.md_offset + bdev->md.al_offset + index; | |
377 | ||
378 | /* Dont process error normally, | |
379 | * as this is done before disk is attached! */ | |
380 | if (!drbd_md_sync_page_io(mdev, bdev, sector, READ)) | |
381 | return -1; | |
382 | ||
383 | rv = (be32_to_cpu(b->magic) == DRBD_MAGIC); | |
384 | ||
385 | for (i = 0; i < AL_EXTENTS_PT + 1; i++) | |
386 | xor_sum ^= be32_to_cpu(b->updates[i].extent); | |
387 | rv &= (xor_sum == be32_to_cpu(b->xor_sum)); | |
388 | ||
389 | return rv; | |
390 | } | |
391 | ||
392 | /** | |
393 | * drbd_al_read_log() - Restores the activity log from its on disk representation. | |
394 | * @mdev: DRBD device. | |
395 | * @bdev: Block device to read form. | |
396 | * | |
397 | * Returns 1 on success, returns 0 when reading the log failed due to IO errors. | |
398 | */ | |
399 | int drbd_al_read_log(struct drbd_conf *mdev, struct drbd_backing_dev *bdev) | |
400 | { | |
401 | struct al_transaction *buffer; | |
402 | int i; | |
403 | int rv; | |
404 | int mx; | |
405 | int active_extents = 0; | |
406 | int transactions = 0; | |
407 | int found_valid = 0; | |
408 | int from = 0; | |
409 | int to = 0; | |
410 | u32 from_tnr = 0; | |
411 | u32 to_tnr = 0; | |
412 | u32 cnr; | |
413 | ||
414 | mx = div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT); | |
415 | ||
416 | /* lock out all other meta data io for now, | |
417 | * and make sure the page is mapped. | |
418 | */ | |
419 | mutex_lock(&mdev->md_io_mutex); | |
420 | buffer = page_address(mdev->md_io_page); | |
421 | ||
422 | /* Find the valid transaction in the log */ | |
423 | for (i = 0; i <= mx; i++) { | |
424 | rv = drbd_al_read_tr(mdev, bdev, buffer, i); | |
425 | if (rv == 0) | |
426 | continue; | |
427 | if (rv == -1) { | |
428 | mutex_unlock(&mdev->md_io_mutex); | |
429 | return 0; | |
430 | } | |
431 | cnr = be32_to_cpu(buffer->tr_number); | |
432 | ||
433 | if (++found_valid == 1) { | |
434 | from = i; | |
435 | to = i; | |
436 | from_tnr = cnr; | |
437 | to_tnr = cnr; | |
438 | continue; | |
439 | } | |
440 | if ((int)cnr - (int)from_tnr < 0) { | |
441 | D_ASSERT(from_tnr - cnr + i - from == mx+1); | |
442 | from = i; | |
443 | from_tnr = cnr; | |
444 | } | |
445 | if ((int)cnr - (int)to_tnr > 0) { | |
446 | D_ASSERT(cnr - to_tnr == i - to); | |
447 | to = i; | |
448 | to_tnr = cnr; | |
449 | } | |
450 | } | |
451 | ||
452 | if (!found_valid) { | |
453 | dev_warn(DEV, "No usable activity log found.\n"); | |
454 | mutex_unlock(&mdev->md_io_mutex); | |
455 | return 1; | |
456 | } | |
457 | ||
458 | /* Read the valid transactions. | |
459 | * dev_info(DEV, "Reading from %d to %d.\n",from,to); */ | |
460 | i = from; | |
461 | while (1) { | |
462 | int j, pos; | |
463 | unsigned int extent_nr; | |
464 | unsigned int trn; | |
465 | ||
466 | rv = drbd_al_read_tr(mdev, bdev, buffer, i); | |
467 | ERR_IF(rv == 0) goto cancel; | |
468 | if (rv == -1) { | |
469 | mutex_unlock(&mdev->md_io_mutex); | |
470 | return 0; | |
471 | } | |
472 | ||
473 | trn = be32_to_cpu(buffer->tr_number); | |
474 | ||
475 | spin_lock_irq(&mdev->al_lock); | |
476 | ||
477 | /* This loop runs backwards because in the cyclic | |
478 | elements there might be an old version of the | |
479 | updated element (in slot 0). So the element in slot 0 | |
480 | can overwrite old versions. */ | |
481 | for (j = AL_EXTENTS_PT; j >= 0; j--) { | |
482 | pos = be32_to_cpu(buffer->updates[j].pos); | |
483 | extent_nr = be32_to_cpu(buffer->updates[j].extent); | |
484 | ||
485 | if (extent_nr == LC_FREE) | |
486 | continue; | |
487 | ||
488 | lc_set(mdev->act_log, extent_nr, pos); | |
489 | active_extents++; | |
490 | } | |
491 | spin_unlock_irq(&mdev->al_lock); | |
492 | ||
493 | transactions++; | |
494 | ||
495 | cancel: | |
496 | if (i == to) | |
497 | break; | |
498 | i++; | |
499 | if (i > mx) | |
500 | i = 0; | |
501 | } | |
502 | ||
503 | mdev->al_tr_number = to_tnr+1; | |
504 | mdev->al_tr_pos = to; | |
505 | if (++mdev->al_tr_pos > | |
506 | div_ceil(mdev->act_log->nr_elements, AL_EXTENTS_PT)) | |
507 | mdev->al_tr_pos = 0; | |
508 | ||
509 | /* ok, we are done with it */ | |
510 | mutex_unlock(&mdev->md_io_mutex); | |
511 | ||
512 | dev_info(DEV, "Found %d transactions (%d active extents) in activity log.\n", | |
513 | transactions, active_extents); | |
514 | ||
515 | return 1; | |
516 | } | |
517 | ||
518 | static void atodb_endio(struct bio *bio, int error) | |
519 | { | |
520 | struct drbd_atodb_wait *wc = bio->bi_private; | |
521 | struct drbd_conf *mdev = wc->mdev; | |
522 | struct page *page; | |
523 | int uptodate = bio_flagged(bio, BIO_UPTODATE); | |
524 | ||
525 | /* strange behavior of some lower level drivers... | |
526 | * fail the request by clearing the uptodate flag, | |
527 | * but do not return any error?! */ | |
528 | if (!error && !uptodate) | |
529 | error = -EIO; | |
530 | ||
531 | drbd_chk_io_error(mdev, error, TRUE); | |
532 | if (error && wc->error == 0) | |
533 | wc->error = error; | |
534 | ||
535 | if (atomic_dec_and_test(&wc->count)) | |
536 | complete(&wc->io_done); | |
537 | ||
538 | page = bio->bi_io_vec[0].bv_page; | |
539 | put_page(page); | |
540 | bio_put(bio); | |
541 | mdev->bm_writ_cnt++; | |
542 | put_ldev(mdev); | |
543 | } | |
544 | ||
39ad2bbb | 545 | /* sector to word */ |
b411b363 | 546 | #define S2W(s) ((s)<<(BM_EXT_SHIFT-BM_BLOCK_SHIFT-LN2_BPL)) |
39ad2bbb | 547 | |
b411b363 PR |
548 | /* activity log to on disk bitmap -- prepare bio unless that sector |
549 | * is already covered by previously prepared bios */ | |
550 | static int atodb_prepare_unless_covered(struct drbd_conf *mdev, | |
551 | struct bio **bios, | |
552 | unsigned int enr, | |
553 | struct drbd_atodb_wait *wc) __must_hold(local) | |
554 | { | |
555 | struct bio *bio; | |
556 | struct page *page; | |
39ad2bbb | 557 | sector_t on_disk_sector; |
b411b363 PR |
558 | unsigned int page_offset = PAGE_SIZE; |
559 | int offset; | |
560 | int i = 0; | |
561 | int err = -ENOMEM; | |
562 | ||
39ad2bbb LE |
563 | /* We always write aligned, full 4k blocks, |
564 | * so we can ignore the logical_block_size (for now) */ | |
565 | enr &= ~7U; | |
566 | on_disk_sector = enr + mdev->ldev->md.md_offset | |
567 | + mdev->ldev->md.bm_offset; | |
568 | ||
569 | D_ASSERT(!(on_disk_sector & 7U)); | |
570 | ||
b411b363 PR |
571 | /* Check if that enr is already covered by an already created bio. |
572 | * Caution, bios[] is not NULL terminated, | |
573 | * but only initialized to all NULL. | |
574 | * For completely scattered activity log, | |
575 | * the last invocation iterates over all bios, | |
576 | * and finds the last NULL entry. | |
577 | */ | |
578 | while ((bio = bios[i])) { | |
579 | if (bio->bi_sector == on_disk_sector) | |
580 | return 0; | |
581 | i++; | |
582 | } | |
583 | /* bios[i] == NULL, the next not yet used slot */ | |
584 | ||
585 | /* GFP_KERNEL, we are not in the write-out path */ | |
586 | bio = bio_alloc(GFP_KERNEL, 1); | |
587 | if (bio == NULL) | |
588 | return -ENOMEM; | |
589 | ||
590 | if (i > 0) { | |
591 | const struct bio_vec *prev_bv = bios[i-1]->bi_io_vec; | |
592 | page_offset = prev_bv->bv_offset + prev_bv->bv_len; | |
593 | page = prev_bv->bv_page; | |
594 | } | |
595 | if (page_offset == PAGE_SIZE) { | |
596 | page = alloc_page(__GFP_HIGHMEM); | |
597 | if (page == NULL) | |
598 | goto out_bio_put; | |
599 | page_offset = 0; | |
600 | } else { | |
601 | get_page(page); | |
602 | } | |
603 | ||
604 | offset = S2W(enr); | |
605 | drbd_bm_get_lel(mdev, offset, | |
39ad2bbb | 606 | min_t(size_t, S2W(8), drbd_bm_words(mdev) - offset), |
b411b363 PR |
607 | kmap(page) + page_offset); |
608 | kunmap(page); | |
609 | ||
610 | bio->bi_private = wc; | |
611 | bio->bi_end_io = atodb_endio; | |
612 | bio->bi_bdev = mdev->ldev->md_bdev; | |
613 | bio->bi_sector = on_disk_sector; | |
614 | ||
39ad2bbb | 615 | if (bio_add_page(bio, page, 4096, page_offset) != 4096) |
b411b363 PR |
616 | goto out_put_page; |
617 | ||
618 | atomic_inc(&wc->count); | |
619 | /* we already know that we may do this... | |
620 | * get_ldev_if_state(mdev,D_ATTACHING); | |
621 | * just get the extra reference, so that the local_cnt reflects | |
622 | * the number of pending IO requests DRBD at its backing device. | |
623 | */ | |
624 | atomic_inc(&mdev->local_cnt); | |
625 | ||
626 | bios[i] = bio; | |
627 | ||
628 | return 0; | |
629 | ||
630 | out_put_page: | |
631 | err = -EINVAL; | |
632 | put_page(page); | |
633 | out_bio_put: | |
634 | bio_put(bio); | |
635 | return err; | |
636 | } | |
637 | ||
638 | /** | |
639 | * drbd_al_to_on_disk_bm() - * Writes bitmap parts covered by active AL extents | |
640 | * @mdev: DRBD device. | |
641 | * | |
642 | * Called when we detach (unconfigure) local storage, | |
643 | * or when we go from R_PRIMARY to R_SECONDARY role. | |
644 | */ | |
645 | void drbd_al_to_on_disk_bm(struct drbd_conf *mdev) | |
646 | { | |
647 | int i, nr_elements; | |
648 | unsigned int enr; | |
649 | struct bio **bios; | |
650 | struct drbd_atodb_wait wc; | |
651 | ||
652 | ERR_IF (!get_ldev_if_state(mdev, D_ATTACHING)) | |
653 | return; /* sorry, I don't have any act_log etc... */ | |
654 | ||
655 | wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); | |
656 | ||
657 | nr_elements = mdev->act_log->nr_elements; | |
658 | ||
659 | /* GFP_KERNEL, we are not in anyone's write-out path */ | |
660 | bios = kzalloc(sizeof(struct bio *) * nr_elements, GFP_KERNEL); | |
661 | if (!bios) | |
662 | goto submit_one_by_one; | |
663 | ||
664 | atomic_set(&wc.count, 0); | |
665 | init_completion(&wc.io_done); | |
666 | wc.mdev = mdev; | |
667 | wc.error = 0; | |
668 | ||
669 | for (i = 0; i < nr_elements; i++) { | |
670 | enr = lc_element_by_index(mdev->act_log, i)->lc_number; | |
671 | if (enr == LC_FREE) | |
672 | continue; | |
673 | /* next statement also does atomic_inc wc.count and local_cnt */ | |
674 | if (atodb_prepare_unless_covered(mdev, bios, | |
675 | enr/AL_EXT_PER_BM_SECT, | |
676 | &wc)) | |
677 | goto free_bios_submit_one_by_one; | |
678 | } | |
679 | ||
680 | /* unnecessary optimization? */ | |
681 | lc_unlock(mdev->act_log); | |
682 | wake_up(&mdev->al_wait); | |
683 | ||
684 | /* all prepared, submit them */ | |
685 | for (i = 0; i < nr_elements; i++) { | |
686 | if (bios[i] == NULL) | |
687 | break; | |
0cf9d27e | 688 | if (drbd_insert_fault(mdev, DRBD_FAULT_MD_WR)) { |
b411b363 PR |
689 | bios[i]->bi_rw = WRITE; |
690 | bio_endio(bios[i], -EIO); | |
691 | } else { | |
692 | submit_bio(WRITE, bios[i]); | |
693 | } | |
694 | } | |
695 | ||
b411b363 PR |
696 | /* always (try to) flush bitmap to stable storage */ |
697 | drbd_md_flush(mdev); | |
698 | ||
699 | /* In case we did not submit a single IO do not wait for | |
700 | * them to complete. ( Because we would wait forever here. ) | |
701 | * | |
702 | * In case we had IOs and they are already complete, there | |
703 | * is not point in waiting anyways. | |
704 | * Therefore this if () ... */ | |
705 | if (atomic_read(&wc.count)) | |
706 | wait_for_completion(&wc.io_done); | |
707 | ||
708 | put_ldev(mdev); | |
709 | ||
710 | kfree(bios); | |
711 | return; | |
712 | ||
713 | free_bios_submit_one_by_one: | |
714 | /* free everything by calling the endio callback directly. */ | |
715 | for (i = 0; i < nr_elements && bios[i]; i++) | |
716 | bio_endio(bios[i], 0); | |
717 | ||
718 | kfree(bios); | |
719 | ||
720 | submit_one_by_one: | |
721 | dev_warn(DEV, "Using the slow drbd_al_to_on_disk_bm()\n"); | |
722 | ||
723 | for (i = 0; i < mdev->act_log->nr_elements; i++) { | |
724 | enr = lc_element_by_index(mdev->act_log, i)->lc_number; | |
725 | if (enr == LC_FREE) | |
726 | continue; | |
727 | /* Really slow: if we have al-extents 16..19 active, | |
728 | * sector 4 will be written four times! Synchronous! */ | |
729 | drbd_bm_write_sect(mdev, enr/AL_EXT_PER_BM_SECT); | |
730 | } | |
731 | ||
732 | lc_unlock(mdev->act_log); | |
733 | wake_up(&mdev->al_wait); | |
734 | put_ldev(mdev); | |
735 | } | |
736 | ||
737 | /** | |
738 | * drbd_al_apply_to_bm() - Sets the bitmap to diry(1) where covered ba active AL extents | |
739 | * @mdev: DRBD device. | |
740 | */ | |
741 | void drbd_al_apply_to_bm(struct drbd_conf *mdev) | |
742 | { | |
743 | unsigned int enr; | |
744 | unsigned long add = 0; | |
745 | char ppb[10]; | |
6719fb03 | 746 | int i, tmp; |
b411b363 PR |
747 | |
748 | wait_event(mdev->al_wait, lc_try_lock(mdev->act_log)); | |
749 | ||
750 | for (i = 0; i < mdev->act_log->nr_elements; i++) { | |
751 | enr = lc_element_by_index(mdev->act_log, i)->lc_number; | |
752 | if (enr == LC_FREE) | |
753 | continue; | |
6719fb03 LE |
754 | tmp = drbd_bm_ALe_set_all(mdev, enr); |
755 | dynamic_dev_dbg(DEV, "AL: set %d bits in extent %u\n", tmp, enr); | |
756 | add += tmp; | |
b411b363 PR |
757 | } |
758 | ||
759 | lc_unlock(mdev->act_log); | |
760 | wake_up(&mdev->al_wait); | |
761 | ||
762 | dev_info(DEV, "Marked additional %s as out-of-sync based on AL.\n", | |
763 | ppsize(ppb, Bit2KB(add))); | |
764 | } | |
765 | ||
766 | static int _try_lc_del(struct drbd_conf *mdev, struct lc_element *al_ext) | |
767 | { | |
768 | int rv; | |
769 | ||
770 | spin_lock_irq(&mdev->al_lock); | |
771 | rv = (al_ext->refcnt == 0); | |
772 | if (likely(rv)) | |
773 | lc_del(mdev->act_log, al_ext); | |
774 | spin_unlock_irq(&mdev->al_lock); | |
775 | ||
776 | return rv; | |
777 | } | |
778 | ||
779 | /** | |
780 | * drbd_al_shrink() - Removes all active extents form the activity log | |
781 | * @mdev: DRBD device. | |
782 | * | |
783 | * Removes all active extents form the activity log, waiting until | |
784 | * the reference count of each entry dropped to 0 first, of course. | |
785 | * | |
786 | * You need to lock mdev->act_log with lc_try_lock() / lc_unlock() | |
787 | */ | |
788 | void drbd_al_shrink(struct drbd_conf *mdev) | |
789 | { | |
790 | struct lc_element *al_ext; | |
791 | int i; | |
792 | ||
793 | D_ASSERT(test_bit(__LC_DIRTY, &mdev->act_log->flags)); | |
794 | ||
795 | for (i = 0; i < mdev->act_log->nr_elements; i++) { | |
796 | al_ext = lc_element_by_index(mdev->act_log, i); | |
797 | if (al_ext->lc_number == LC_FREE) | |
798 | continue; | |
799 | wait_event(mdev->al_wait, _try_lc_del(mdev, al_ext)); | |
800 | } | |
801 | ||
802 | wake_up(&mdev->al_wait); | |
803 | } | |
804 | ||
805 | static int w_update_odbm(struct drbd_conf *mdev, struct drbd_work *w, int unused) | |
806 | { | |
807 | struct update_odbm_work *udw = container_of(w, struct update_odbm_work, w); | |
808 | ||
809 | if (!get_ldev(mdev)) { | |
810 | if (__ratelimit(&drbd_ratelimit_state)) | |
811 | dev_warn(DEV, "Can not update on disk bitmap, local IO disabled.\n"); | |
812 | kfree(udw); | |
813 | return 1; | |
814 | } | |
815 | ||
816 | drbd_bm_write_sect(mdev, udw->enr); | |
817 | put_ldev(mdev); | |
818 | ||
819 | kfree(udw); | |
820 | ||
821 | if (drbd_bm_total_weight(mdev) <= mdev->rs_failed) { | |
822 | switch (mdev->state.conn) { | |
823 | case C_SYNC_SOURCE: case C_SYNC_TARGET: | |
824 | case C_PAUSED_SYNC_S: case C_PAUSED_SYNC_T: | |
825 | drbd_resync_finished(mdev); | |
826 | default: | |
827 | /* nothing to do */ | |
828 | break; | |
829 | } | |
830 | } | |
831 | drbd_bcast_sync_progress(mdev); | |
832 | ||
833 | return 1; | |
834 | } | |
835 | ||
836 | ||
837 | /* ATTENTION. The AL's extents are 4MB each, while the extents in the | |
838 | * resync LRU-cache are 16MB each. | |
839 | * The caller of this function has to hold an get_ldev() reference. | |
840 | * | |
841 | * TODO will be obsoleted once we have a caching lru of the on disk bitmap | |
842 | */ | |
843 | static void drbd_try_clear_on_disk_bm(struct drbd_conf *mdev, sector_t sector, | |
844 | int count, int success) | |
845 | { | |
846 | struct lc_element *e; | |
847 | struct update_odbm_work *udw; | |
848 | ||
849 | unsigned int enr; | |
850 | ||
851 | D_ASSERT(atomic_read(&mdev->local_cnt)); | |
852 | ||
853 | /* I simply assume that a sector/size pair never crosses | |
854 | * a 16 MB extent border. (Currently this is true...) */ | |
855 | enr = BM_SECT_TO_EXT(sector); | |
856 | ||
857 | e = lc_get(mdev->resync, enr); | |
858 | if (e) { | |
859 | struct bm_extent *ext = lc_entry(e, struct bm_extent, lce); | |
860 | if (ext->lce.lc_number == enr) { | |
861 | if (success) | |
862 | ext->rs_left -= count; | |
863 | else | |
864 | ext->rs_failed += count; | |
865 | if (ext->rs_left < ext->rs_failed) { | |
866 | dev_err(DEV, "BAD! sector=%llus enr=%u rs_left=%d " | |
867 | "rs_failed=%d count=%d\n", | |
868 | (unsigned long long)sector, | |
869 | ext->lce.lc_number, ext->rs_left, | |
870 | ext->rs_failed, count); | |
871 | dump_stack(); | |
872 | ||
873 | lc_put(mdev->resync, &ext->lce); | |
874 | drbd_force_state(mdev, NS(conn, C_DISCONNECTING)); | |
875 | return; | |
876 | } | |
877 | } else { | |
878 | /* Normally this element should be in the cache, | |
879 | * since drbd_rs_begin_io() pulled it already in. | |
880 | * | |
881 | * But maybe an application write finished, and we set | |
882 | * something outside the resync lru_cache in sync. | |
883 | */ | |
884 | int rs_left = drbd_bm_e_weight(mdev, enr); | |
885 | if (ext->flags != 0) { | |
886 | dev_warn(DEV, "changing resync lce: %d[%u;%02lx]" | |
887 | " -> %d[%u;00]\n", | |
888 | ext->lce.lc_number, ext->rs_left, | |
889 | ext->flags, enr, rs_left); | |
890 | ext->flags = 0; | |
891 | } | |
892 | if (ext->rs_failed) { | |
893 | dev_warn(DEV, "Kicking resync_lru element enr=%u " | |
894 | "out with rs_failed=%d\n", | |
895 | ext->lce.lc_number, ext->rs_failed); | |
896 | set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags); | |
897 | } | |
898 | ext->rs_left = rs_left; | |
899 | ext->rs_failed = success ? 0 : count; | |
900 | lc_changed(mdev->resync, &ext->lce); | |
901 | } | |
902 | lc_put(mdev->resync, &ext->lce); | |
903 | /* no race, we are within the al_lock! */ | |
904 | ||
905 | if (ext->rs_left == ext->rs_failed) { | |
906 | ext->rs_failed = 0; | |
907 | ||
908 | udw = kmalloc(sizeof(*udw), GFP_ATOMIC); | |
909 | if (udw) { | |
910 | udw->enr = ext->lce.lc_number; | |
911 | udw->w.cb = w_update_odbm; | |
912 | drbd_queue_work_front(&mdev->data.work, &udw->w); | |
913 | } else { | |
914 | dev_warn(DEV, "Could not kmalloc an udw\n"); | |
915 | set_bit(WRITE_BM_AFTER_RESYNC, &mdev->flags); | |
916 | } | |
917 | } | |
918 | } else { | |
919 | dev_err(DEV, "lc_get() failed! locked=%d/%d flags=%lu\n", | |
920 | mdev->resync_locked, | |
921 | mdev->resync->nr_elements, | |
922 | mdev->resync->flags); | |
923 | } | |
924 | } | |
925 | ||
c6ea14df LE |
926 | void drbd_advance_rs_marks(struct drbd_conf *mdev, unsigned long still_to_go) |
927 | { | |
928 | unsigned long now = jiffies; | |
929 | unsigned long last = mdev->rs_mark_time[mdev->rs_last_mark]; | |
930 | int next = (mdev->rs_last_mark + 1) % DRBD_SYNC_MARKS; | |
931 | if (time_after_eq(now, last + DRBD_SYNC_MARK_STEP)) { | |
932 | if (mdev->rs_mark_left[mdev->rs_last_mark] != still_to_go && | |
933 | mdev->state.conn != C_PAUSED_SYNC_T && | |
934 | mdev->state.conn != C_PAUSED_SYNC_S) { | |
935 | mdev->rs_mark_time[next] = now; | |
936 | mdev->rs_mark_left[next] = still_to_go; | |
937 | mdev->rs_last_mark = next; | |
938 | } | |
939 | } | |
940 | } | |
941 | ||
b411b363 PR |
942 | /* clear the bit corresponding to the piece of storage in question: |
943 | * size byte of data starting from sector. Only clear a bits of the affected | |
944 | * one ore more _aligned_ BM_BLOCK_SIZE blocks. | |
945 | * | |
946 | * called by worker on C_SYNC_TARGET and receiver on SyncSource. | |
947 | * | |
948 | */ | |
949 | void __drbd_set_in_sync(struct drbd_conf *mdev, sector_t sector, int size, | |
950 | const char *file, const unsigned int line) | |
951 | { | |
952 | /* Is called from worker and receiver context _only_ */ | |
953 | unsigned long sbnr, ebnr, lbnr; | |
954 | unsigned long count = 0; | |
955 | sector_t esector, nr_sectors; | |
956 | int wake_up = 0; | |
957 | unsigned long flags; | |
958 | ||
1816a2b4 | 959 | if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
960 | dev_err(DEV, "drbd_set_in_sync: sector=%llus size=%d nonsense!\n", |
961 | (unsigned long long)sector, size); | |
962 | return; | |
963 | } | |
964 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
965 | esector = sector + (size >> 9) - 1; | |
966 | ||
967 | ERR_IF(sector >= nr_sectors) return; | |
968 | ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1); | |
969 | ||
970 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
971 | ||
972 | /* we clear it (in sync). | |
973 | * round up start sector, round down end sector. we make sure we only | |
974 | * clear full, aligned, BM_BLOCK_SIZE (4K) blocks */ | |
975 | if (unlikely(esector < BM_SECT_PER_BIT-1)) | |
976 | return; | |
977 | if (unlikely(esector == (nr_sectors-1))) | |
978 | ebnr = lbnr; | |
979 | else | |
980 | ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); | |
981 | sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); | |
982 | ||
b411b363 PR |
983 | if (sbnr > ebnr) |
984 | return; | |
985 | ||
986 | /* | |
987 | * ok, (capacity & 7) != 0 sometimes, but who cares... | |
988 | * we count rs_{total,left} in bits, not sectors. | |
989 | */ | |
b411b363 | 990 | count = drbd_bm_clear_bits(mdev, sbnr, ebnr); |
1d7734a0 | 991 | if (count && get_ldev(mdev)) { |
c6ea14df | 992 | drbd_advance_rs_marks(mdev, drbd_bm_total_weight(mdev)); |
1d7734a0 LE |
993 | spin_lock_irqsave(&mdev->al_lock, flags); |
994 | drbd_try_clear_on_disk_bm(mdev, sector, count, TRUE); | |
995 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
996 | ||
b411b363 PR |
997 | /* just wake_up unconditional now, various lc_chaged(), |
998 | * lc_put() in drbd_try_clear_on_disk_bm(). */ | |
999 | wake_up = 1; | |
1d7734a0 | 1000 | put_ldev(mdev); |
b411b363 | 1001 | } |
b411b363 PR |
1002 | if (wake_up) |
1003 | wake_up(&mdev->al_wait); | |
1004 | } | |
1005 | ||
1006 | /* | |
1007 | * this is intended to set one request worth of data out of sync. | |
1008 | * affects at least 1 bit, | |
1816a2b4 | 1009 | * and at most 1+DRBD_MAX_BIO_SIZE/BM_BLOCK_SIZE bits. |
b411b363 PR |
1010 | * |
1011 | * called by tl_clear and drbd_send_dblock (==drbd_make_request). | |
1012 | * so this can be _any_ process. | |
1013 | */ | |
73a01a18 | 1014 | int __drbd_set_out_of_sync(struct drbd_conf *mdev, sector_t sector, int size, |
b411b363 PR |
1015 | const char *file, const unsigned int line) |
1016 | { | |
1017 | unsigned long sbnr, ebnr, lbnr, flags; | |
1018 | sector_t esector, nr_sectors; | |
73a01a18 | 1019 | unsigned int enr, count = 0; |
b411b363 PR |
1020 | struct lc_element *e; |
1021 | ||
1816a2b4 | 1022 | if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
1023 | dev_err(DEV, "sector: %llus, size: %d\n", |
1024 | (unsigned long long)sector, size); | |
73a01a18 | 1025 | return 0; |
b411b363 PR |
1026 | } |
1027 | ||
1028 | if (!get_ldev(mdev)) | |
73a01a18 | 1029 | return 0; /* no disk, no metadata, no bitmap to set bits in */ |
b411b363 PR |
1030 | |
1031 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
1032 | esector = sector + (size >> 9) - 1; | |
1033 | ||
1034 | ERR_IF(sector >= nr_sectors) | |
1035 | goto out; | |
1036 | ERR_IF(esector >= nr_sectors) | |
1037 | esector = (nr_sectors-1); | |
1038 | ||
1039 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
1040 | ||
1041 | /* we set it out of sync, | |
1042 | * we do not need to round anything here */ | |
1043 | sbnr = BM_SECT_TO_BIT(sector); | |
1044 | ebnr = BM_SECT_TO_BIT(esector); | |
1045 | ||
b411b363 PR |
1046 | /* ok, (capacity & 7) != 0 sometimes, but who cares... |
1047 | * we count rs_{total,left} in bits, not sectors. */ | |
1048 | spin_lock_irqsave(&mdev->al_lock, flags); | |
1049 | count = drbd_bm_set_bits(mdev, sbnr, ebnr); | |
1050 | ||
1051 | enr = BM_SECT_TO_EXT(sector); | |
1052 | e = lc_find(mdev->resync, enr); | |
1053 | if (e) | |
1054 | lc_entry(e, struct bm_extent, lce)->rs_left += count; | |
1055 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1056 | ||
1057 | out: | |
1058 | put_ldev(mdev); | |
73a01a18 PR |
1059 | |
1060 | return count; | |
b411b363 PR |
1061 | } |
1062 | ||
1063 | static | |
1064 | struct bm_extent *_bme_get(struct drbd_conf *mdev, unsigned int enr) | |
1065 | { | |
1066 | struct lc_element *e; | |
1067 | struct bm_extent *bm_ext; | |
1068 | int wakeup = 0; | |
1069 | unsigned long rs_flags; | |
1070 | ||
1071 | spin_lock_irq(&mdev->al_lock); | |
1072 | if (mdev->resync_locked > mdev->resync->nr_elements/2) { | |
1073 | spin_unlock_irq(&mdev->al_lock); | |
1074 | return NULL; | |
1075 | } | |
1076 | e = lc_get(mdev->resync, enr); | |
1077 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1078 | if (bm_ext) { | |
1079 | if (bm_ext->lce.lc_number != enr) { | |
1080 | bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); | |
1081 | bm_ext->rs_failed = 0; | |
1082 | lc_changed(mdev->resync, &bm_ext->lce); | |
1083 | wakeup = 1; | |
1084 | } | |
1085 | if (bm_ext->lce.refcnt == 1) | |
1086 | mdev->resync_locked++; | |
1087 | set_bit(BME_NO_WRITES, &bm_ext->flags); | |
1088 | } | |
1089 | rs_flags = mdev->resync->flags; | |
1090 | spin_unlock_irq(&mdev->al_lock); | |
1091 | if (wakeup) | |
1092 | wake_up(&mdev->al_wait); | |
1093 | ||
1094 | if (!bm_ext) { | |
1095 | if (rs_flags & LC_STARVING) | |
1096 | dev_warn(DEV, "Have to wait for element" | |
1097 | " (resync LRU too small?)\n"); | |
1098 | BUG_ON(rs_flags & LC_DIRTY); | |
1099 | } | |
1100 | ||
1101 | return bm_ext; | |
1102 | } | |
1103 | ||
1104 | static int _is_in_al(struct drbd_conf *mdev, unsigned int enr) | |
1105 | { | |
1106 | struct lc_element *al_ext; | |
1107 | int rv = 0; | |
1108 | ||
1109 | spin_lock_irq(&mdev->al_lock); | |
1110 | if (unlikely(enr == mdev->act_log->new_number)) | |
1111 | rv = 1; | |
1112 | else { | |
1113 | al_ext = lc_find(mdev->act_log, enr); | |
1114 | if (al_ext) { | |
1115 | if (al_ext->refcnt) | |
1116 | rv = 1; | |
1117 | } | |
1118 | } | |
1119 | spin_unlock_irq(&mdev->al_lock); | |
1120 | ||
1121 | /* | |
1122 | if (unlikely(rv)) { | |
1123 | dev_info(DEV, "Delaying sync read until app's write is done\n"); | |
1124 | } | |
1125 | */ | |
1126 | return rv; | |
1127 | } | |
1128 | ||
1129 | /** | |
1130 | * drbd_rs_begin_io() - Gets an extent in the resync LRU cache and sets it to BME_LOCKED | |
1131 | * @mdev: DRBD device. | |
1132 | * @sector: The sector number. | |
1133 | * | |
80a40e43 | 1134 | * This functions sleeps on al_wait. Returns 0 on success, -EINTR if interrupted. |
b411b363 PR |
1135 | */ |
1136 | int drbd_rs_begin_io(struct drbd_conf *mdev, sector_t sector) | |
1137 | { | |
1138 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
1139 | struct bm_extent *bm_ext; | |
1140 | int i, sig; | |
f91ab628 PR |
1141 | int sa = 200; /* Step aside 200 times, then grab the extent and let app-IO wait. |
1142 | 200 times -> 20 seconds. */ | |
b411b363 | 1143 | |
f91ab628 | 1144 | retry: |
b411b363 PR |
1145 | sig = wait_event_interruptible(mdev->al_wait, |
1146 | (bm_ext = _bme_get(mdev, enr))); | |
1147 | if (sig) | |
80a40e43 | 1148 | return -EINTR; |
b411b363 PR |
1149 | |
1150 | if (test_bit(BME_LOCKED, &bm_ext->flags)) | |
80a40e43 | 1151 | return 0; |
b411b363 PR |
1152 | |
1153 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { | |
1154 | sig = wait_event_interruptible(mdev->al_wait, | |
f91ab628 | 1155 | !_is_in_al(mdev, enr * AL_EXT_PER_BM_SECT + i) || |
c507f46f | 1156 | test_bit(BME_PRIORITY, &bm_ext->flags)); |
f91ab628 PR |
1157 | |
1158 | if (sig || (test_bit(BME_PRIORITY, &bm_ext->flags) && sa)) { | |
b411b363 PR |
1159 | spin_lock_irq(&mdev->al_lock); |
1160 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) { | |
f91ab628 | 1161 | bm_ext->flags = 0; /* clears BME_NO_WRITES and eventually BME_PRIORITY */ |
b411b363 PR |
1162 | mdev->resync_locked--; |
1163 | wake_up(&mdev->al_wait); | |
1164 | } | |
1165 | spin_unlock_irq(&mdev->al_lock); | |
f91ab628 PR |
1166 | if (sig) |
1167 | return -EINTR; | |
1168 | if (schedule_timeout_interruptible(HZ/10)) | |
1169 | return -EINTR; | |
c507f46f PR |
1170 | if (sa && --sa == 0) |
1171 | dev_warn(DEV,"drbd_rs_begin_io() stepped aside for 20sec." | |
1172 | "Resync stalled?\n"); | |
f91ab628 | 1173 | goto retry; |
b411b363 PR |
1174 | } |
1175 | } | |
b411b363 | 1176 | set_bit(BME_LOCKED, &bm_ext->flags); |
80a40e43 | 1177 | return 0; |
b411b363 PR |
1178 | } |
1179 | ||
1180 | /** | |
1181 | * drbd_try_rs_begin_io() - Gets an extent in the resync LRU cache, does not sleep | |
1182 | * @mdev: DRBD device. | |
1183 | * @sector: The sector number. | |
1184 | * | |
1185 | * Gets an extent in the resync LRU cache, sets it to BME_NO_WRITES, then | |
1186 | * tries to set it to BME_LOCKED. Returns 0 upon success, and -EAGAIN | |
1187 | * if there is still application IO going on in this area. | |
1188 | */ | |
1189 | int drbd_try_rs_begin_io(struct drbd_conf *mdev, sector_t sector) | |
1190 | { | |
1191 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
1192 | const unsigned int al_enr = enr*AL_EXT_PER_BM_SECT; | |
1193 | struct lc_element *e; | |
1194 | struct bm_extent *bm_ext; | |
1195 | int i; | |
1196 | ||
b411b363 PR |
1197 | spin_lock_irq(&mdev->al_lock); |
1198 | if (mdev->resync_wenr != LC_FREE && mdev->resync_wenr != enr) { | |
1199 | /* in case you have very heavy scattered io, it may | |
1200 | * stall the syncer undefined if we give up the ref count | |
1201 | * when we try again and requeue. | |
1202 | * | |
1203 | * if we don't give up the refcount, but the next time | |
1204 | * we are scheduled this extent has been "synced" by new | |
1205 | * application writes, we'd miss the lc_put on the | |
1206 | * extent we keep the refcount on. | |
1207 | * so we remembered which extent we had to try again, and | |
1208 | * if the next requested one is something else, we do | |
1209 | * the lc_put here... | |
1210 | * we also have to wake_up | |
1211 | */ | |
b411b363 PR |
1212 | e = lc_find(mdev->resync, mdev->resync_wenr); |
1213 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1214 | if (bm_ext) { | |
1215 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
1216 | D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1217 | clear_bit(BME_NO_WRITES, &bm_ext->flags); | |
1218 | mdev->resync_wenr = LC_FREE; | |
1219 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) | |
1220 | mdev->resync_locked--; | |
1221 | wake_up(&mdev->al_wait); | |
1222 | } else { | |
1223 | dev_alert(DEV, "LOGIC BUG\n"); | |
1224 | } | |
1225 | } | |
1226 | /* TRY. */ | |
1227 | e = lc_try_get(mdev->resync, enr); | |
1228 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1229 | if (bm_ext) { | |
1230 | if (test_bit(BME_LOCKED, &bm_ext->flags)) | |
1231 | goto proceed; | |
1232 | if (!test_and_set_bit(BME_NO_WRITES, &bm_ext->flags)) { | |
1233 | mdev->resync_locked++; | |
1234 | } else { | |
1235 | /* we did set the BME_NO_WRITES, | |
1236 | * but then could not set BME_LOCKED, | |
1237 | * so we tried again. | |
1238 | * drop the extra reference. */ | |
b411b363 PR |
1239 | bm_ext->lce.refcnt--; |
1240 | D_ASSERT(bm_ext->lce.refcnt > 0); | |
1241 | } | |
1242 | goto check_al; | |
1243 | } else { | |
1244 | /* do we rather want to try later? */ | |
6a0afdf5 | 1245 | if (mdev->resync_locked > mdev->resync->nr_elements-3) |
b411b363 | 1246 | goto try_again; |
b411b363 PR |
1247 | /* Do or do not. There is no try. -- Yoda */ |
1248 | e = lc_get(mdev->resync, enr); | |
1249 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1250 | if (!bm_ext) { | |
1251 | const unsigned long rs_flags = mdev->resync->flags; | |
1252 | if (rs_flags & LC_STARVING) | |
1253 | dev_warn(DEV, "Have to wait for element" | |
1254 | " (resync LRU too small?)\n"); | |
1255 | BUG_ON(rs_flags & LC_DIRTY); | |
1256 | goto try_again; | |
1257 | } | |
1258 | if (bm_ext->lce.lc_number != enr) { | |
1259 | bm_ext->rs_left = drbd_bm_e_weight(mdev, enr); | |
1260 | bm_ext->rs_failed = 0; | |
1261 | lc_changed(mdev->resync, &bm_ext->lce); | |
1262 | wake_up(&mdev->al_wait); | |
1263 | D_ASSERT(test_bit(BME_LOCKED, &bm_ext->flags) == 0); | |
1264 | } | |
1265 | set_bit(BME_NO_WRITES, &bm_ext->flags); | |
1266 | D_ASSERT(bm_ext->lce.refcnt == 1); | |
1267 | mdev->resync_locked++; | |
1268 | goto check_al; | |
1269 | } | |
1270 | check_al: | |
b411b363 PR |
1271 | for (i = 0; i < AL_EXT_PER_BM_SECT; i++) { |
1272 | if (unlikely(al_enr+i == mdev->act_log->new_number)) | |
1273 | goto try_again; | |
1274 | if (lc_is_used(mdev->act_log, al_enr+i)) | |
1275 | goto try_again; | |
1276 | } | |
1277 | set_bit(BME_LOCKED, &bm_ext->flags); | |
1278 | proceed: | |
1279 | mdev->resync_wenr = LC_FREE; | |
1280 | spin_unlock_irq(&mdev->al_lock); | |
1281 | return 0; | |
1282 | ||
1283 | try_again: | |
b411b363 PR |
1284 | if (bm_ext) |
1285 | mdev->resync_wenr = enr; | |
1286 | spin_unlock_irq(&mdev->al_lock); | |
1287 | return -EAGAIN; | |
1288 | } | |
1289 | ||
1290 | void drbd_rs_complete_io(struct drbd_conf *mdev, sector_t sector) | |
1291 | { | |
1292 | unsigned int enr = BM_SECT_TO_EXT(sector); | |
1293 | struct lc_element *e; | |
1294 | struct bm_extent *bm_ext; | |
1295 | unsigned long flags; | |
1296 | ||
b411b363 PR |
1297 | spin_lock_irqsave(&mdev->al_lock, flags); |
1298 | e = lc_find(mdev->resync, enr); | |
1299 | bm_ext = e ? lc_entry(e, struct bm_extent, lce) : NULL; | |
1300 | if (!bm_ext) { | |
1301 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1302 | if (__ratelimit(&drbd_ratelimit_state)) | |
1303 | dev_err(DEV, "drbd_rs_complete_io() called, but extent not found\n"); | |
1304 | return; | |
1305 | } | |
1306 | ||
1307 | if (bm_ext->lce.refcnt == 0) { | |
1308 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1309 | dev_err(DEV, "drbd_rs_complete_io(,%llu [=%u]) called, " | |
1310 | "but refcnt is 0!?\n", | |
1311 | (unsigned long long)sector, enr); | |
1312 | return; | |
1313 | } | |
1314 | ||
1315 | if (lc_put(mdev->resync, &bm_ext->lce) == 0) { | |
e3555d85 | 1316 | bm_ext->flags = 0; /* clear BME_LOCKED, BME_NO_WRITES and BME_PRIORITY */ |
b411b363 PR |
1317 | mdev->resync_locked--; |
1318 | wake_up(&mdev->al_wait); | |
1319 | } | |
1320 | ||
1321 | spin_unlock_irqrestore(&mdev->al_lock, flags); | |
1322 | } | |
1323 | ||
1324 | /** | |
1325 | * drbd_rs_cancel_all() - Removes all extents from the resync LRU (even BME_LOCKED) | |
1326 | * @mdev: DRBD device. | |
1327 | */ | |
1328 | void drbd_rs_cancel_all(struct drbd_conf *mdev) | |
1329 | { | |
b411b363 PR |
1330 | spin_lock_irq(&mdev->al_lock); |
1331 | ||
1332 | if (get_ldev_if_state(mdev, D_FAILED)) { /* Makes sure ->resync is there. */ | |
1333 | lc_reset(mdev->resync); | |
1334 | put_ldev(mdev); | |
1335 | } | |
1336 | mdev->resync_locked = 0; | |
1337 | mdev->resync_wenr = LC_FREE; | |
1338 | spin_unlock_irq(&mdev->al_lock); | |
1339 | wake_up(&mdev->al_wait); | |
1340 | } | |
1341 | ||
1342 | /** | |
1343 | * drbd_rs_del_all() - Gracefully remove all extents from the resync LRU | |
1344 | * @mdev: DRBD device. | |
1345 | * | |
1346 | * Returns 0 upon success, -EAGAIN if at least one reference count was | |
1347 | * not zero. | |
1348 | */ | |
1349 | int drbd_rs_del_all(struct drbd_conf *mdev) | |
1350 | { | |
1351 | struct lc_element *e; | |
1352 | struct bm_extent *bm_ext; | |
1353 | int i; | |
1354 | ||
b411b363 PR |
1355 | spin_lock_irq(&mdev->al_lock); |
1356 | ||
1357 | if (get_ldev_if_state(mdev, D_FAILED)) { | |
1358 | /* ok, ->resync is there. */ | |
1359 | for (i = 0; i < mdev->resync->nr_elements; i++) { | |
1360 | e = lc_element_by_index(mdev->resync, i); | |
b2b163dd | 1361 | bm_ext = lc_entry(e, struct bm_extent, lce); |
b411b363 PR |
1362 | if (bm_ext->lce.lc_number == LC_FREE) |
1363 | continue; | |
1364 | if (bm_ext->lce.lc_number == mdev->resync_wenr) { | |
1365 | dev_info(DEV, "dropping %u in drbd_rs_del_all, apparently" | |
1366 | " got 'synced' by application io\n", | |
1367 | mdev->resync_wenr); | |
1368 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
1369 | D_ASSERT(test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1370 | clear_bit(BME_NO_WRITES, &bm_ext->flags); | |
1371 | mdev->resync_wenr = LC_FREE; | |
1372 | lc_put(mdev->resync, &bm_ext->lce); | |
1373 | } | |
1374 | if (bm_ext->lce.refcnt != 0) { | |
1375 | dev_info(DEV, "Retrying drbd_rs_del_all() later. " | |
1376 | "refcnt=%d\n", bm_ext->lce.refcnt); | |
1377 | put_ldev(mdev); | |
1378 | spin_unlock_irq(&mdev->al_lock); | |
1379 | return -EAGAIN; | |
1380 | } | |
1381 | D_ASSERT(!test_bit(BME_LOCKED, &bm_ext->flags)); | |
1382 | D_ASSERT(!test_bit(BME_NO_WRITES, &bm_ext->flags)); | |
1383 | lc_del(mdev->resync, &bm_ext->lce); | |
1384 | } | |
1385 | D_ASSERT(mdev->resync->used == 0); | |
1386 | put_ldev(mdev); | |
1387 | } | |
1388 | spin_unlock_irq(&mdev->al_lock); | |
1389 | ||
1390 | return 0; | |
1391 | } | |
1392 | ||
1393 | /** | |
1394 | * drbd_rs_failed_io() - Record information on a failure to resync the specified blocks | |
1395 | * @mdev: DRBD device. | |
1396 | * @sector: The sector number. | |
1397 | * @size: Size of failed IO operation, in byte. | |
1398 | */ | |
1399 | void drbd_rs_failed_io(struct drbd_conf *mdev, sector_t sector, int size) | |
1400 | { | |
1401 | /* Is called from worker and receiver context _only_ */ | |
1402 | unsigned long sbnr, ebnr, lbnr; | |
1403 | unsigned long count; | |
1404 | sector_t esector, nr_sectors; | |
1405 | int wake_up = 0; | |
1406 | ||
1816a2b4 | 1407 | if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) { |
b411b363 PR |
1408 | dev_err(DEV, "drbd_rs_failed_io: sector=%llus size=%d nonsense!\n", |
1409 | (unsigned long long)sector, size); | |
1410 | return; | |
1411 | } | |
1412 | nr_sectors = drbd_get_capacity(mdev->this_bdev); | |
1413 | esector = sector + (size >> 9) - 1; | |
1414 | ||
1415 | ERR_IF(sector >= nr_sectors) return; | |
1416 | ERR_IF(esector >= nr_sectors) esector = (nr_sectors-1); | |
1417 | ||
1418 | lbnr = BM_SECT_TO_BIT(nr_sectors-1); | |
1419 | ||
1420 | /* | |
1421 | * round up start sector, round down end sector. we make sure we only | |
1422 | * handle full, aligned, BM_BLOCK_SIZE (4K) blocks */ | |
1423 | if (unlikely(esector < BM_SECT_PER_BIT-1)) | |
1424 | return; | |
1425 | if (unlikely(esector == (nr_sectors-1))) | |
1426 | ebnr = lbnr; | |
1427 | else | |
1428 | ebnr = BM_SECT_TO_BIT(esector - (BM_SECT_PER_BIT-1)); | |
1429 | sbnr = BM_SECT_TO_BIT(sector + BM_SECT_PER_BIT-1); | |
1430 | ||
1431 | if (sbnr > ebnr) | |
1432 | return; | |
1433 | ||
1434 | /* | |
1435 | * ok, (capacity & 7) != 0 sometimes, but who cares... | |
1436 | * we count rs_{total,left} in bits, not sectors. | |
1437 | */ | |
1438 | spin_lock_irq(&mdev->al_lock); | |
1439 | count = drbd_bm_count_bits(mdev, sbnr, ebnr); | |
1440 | if (count) { | |
1441 | mdev->rs_failed += count; | |
1442 | ||
1443 | if (get_ldev(mdev)) { | |
1444 | drbd_try_clear_on_disk_bm(mdev, sector, count, FALSE); | |
1445 | put_ldev(mdev); | |
1446 | } | |
1447 | ||
1448 | /* just wake_up unconditional now, various lc_chaged(), | |
1449 | * lc_put() in drbd_try_clear_on_disk_bm(). */ | |
1450 | wake_up = 1; | |
1451 | } | |
1452 | spin_unlock_irq(&mdev->al_lock); | |
1453 | if (wake_up) | |
1454 | wake_up(&mdev->al_wait); | |
1455 | } |