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