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